Skip to content

Commit

Permalink
Copy/Move functionality from menus (#8)
Browse files Browse the repository at this point in the history
* Added state aware 'Edit' menu to toolbar
* Added copy/cut and paste options to edit context menus (#7)
  • Loading branch information
andersevenrud authored Dec 7, 2019
1 parent 98febe9 commit 57e6f9b
Showing 1 changed file with 122 additions and 46 deletions.
168 changes: 122 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ const view = (bus, core, proc, win) => (state, actions) => {
h(MenubarItem, {
onclick: ev => bus.emit('openMenu', ev, state, actions, {name: 'file'})
}, _('LBL_FILE')),
h(MenubarItem, {
onclick: ev => bus.emit('openMenu', ev, state, actions, {name: 'edit'})
}, _('LBL_EDIT')),
h(MenubarItem, {
onclick: ev => bus.emit('openMenu', ev, state, actions, {name: 'view'})
}, _('LBL_VIEW')),
Expand Down Expand Up @@ -222,6 +225,10 @@ const actions = (bus, core, proc, win) => ({
const index = Math.min(state.history.length - 1, state.historyIndex + 1);
bus.emit('openDirectory', state.history[index], true);
return {historyIndex: index};
},

getSelectedIndex: () => state => {
return state.fileview.selectedIndex;
}
});

Expand Down Expand Up @@ -289,6 +296,7 @@ const createDialog = (bus, core, proc, win) => (type, item, cb) => {
const createApplication = (core, proc, win, $content) => {
const homePath = {path: 'home:/'}; // FIXME
let currentPath = proc.args.path ? Object.assign({}, homePath, proc.args.path) : homePath;
let currentFile = undefined;

// FIXME
const settings = {
Expand All @@ -298,6 +306,8 @@ const createApplication = (core, proc, win, $content) => {
const title = core.make('osjs/locale')
.translatableFlat(proc.metadata.title);

const {pathJoin} = core.make('osjs/fs');
const vfs = core.make('osjs/vfs');
const bus = core.make('osjs/event-handler', 'FileManager');
const dialog = createDialog(bus, core, proc, win);
const a = app(state(bus, core, proc, win),
Expand All @@ -306,14 +316,115 @@ const createApplication = (core, proc, win, $content) => {
$content);

const getFileIcon = file => file.icon || core.make('osjs/fs').icon(file);
const refresh = (currentFile) => bus.emit('openDirectory', currentPath, null, currentFile);
const refresh = (fileOrWatch) => {
// FIXME This should be implemented a bit better
if (fileOrWatch === true && core.config('vfs.watch')) {
return;
}

bus.emit('openDirectory', currentPath, null, fileOrWatch);
};

const upload = f => {
const uploadpath = currentPath.path.replace(/\/?$/, '/') + f.name;
return core.make('osjs/vfs').writefile({path: uploadpath}, f);
return vfs.writefile({path: uploadpath}, f);
};

bus.on('selectFile', file => a.setStatus(getFileStatus(file)));
const _ = core.make('osjs/locale').translate;
const __ = core.make('osjs/locale').translatable(translations);
const clipboard = core.make('osjs/clipboard');

const createEditMenuItems = (item, fromContext) => {
const isDirectory = item && item.isDirectory;
// FIXME: Check read-only ?
const isValidFile = item && ['..', '.'].indexOf(item.filename) === -1;

const openMenu = isDirectory
? [{
label: _('LBL_GO'),
disabled: !item,
onclick: () => bus.emit('readFile', item)
}]
: [{
label: _('LBL_OPEN'),
disabled: !item,
onclick: () => bus.emit('readFile', item)
}, {
label: __('LBL_OPEN_WITH'),
disabled: !item,
onclick: () => bus.emit('readFile', item, true)
}];

const clipboardMenu = [
{
label: _('LBL_COPY'),
disabled: !isValidFile,
onclick: () => clipboard.set(({item}), 'filemanager:copy')
},
{
label: _('LBL_CUT'),
disabled: !isValidFile,
onclick: () => clipboard.set(({item, callback: () => refresh(true)}), 'filemanager:move')
}
];

if (!fromContext) {
clipboardMenu.push({
label: _('LBL_PASTE'),
disabled: !clipboard.has(/^filemanager:/),
onclick: () => {
if (clipboard.has(/^filemanager:/)) {
const move = clipboard.has('filemanager:move');

// TODO: Error handling
clipboard.get(move)
.then(({item, callback}) => {
const dest = {path: pathJoin(currentPath.path, item.filename)};

return (move
? vfs.move(item, dest)
: vfs.copy(item, dest))
.then(() => {
refresh(true);

if (typeof callback === 'function') {
callback();
}
});
});
}
}
});
}

const menu = [
...openMenu,
{
label: _('LBL_RENAME'),
disabled: !isValidFile,
onclick: () => dialog('rename', item, () => refresh(true))
},
{
label: _('LBL_DELETE'),
disabled: !isValidFile,
onclick: () => dialog('delete', item, () => refresh(true))
},
...clipboardMenu
];

menu.push({
label: _('LBL_DOWNLOAD'),
disabled: !item || isDirectory || !isValidFile,
onclick: () => vfs.download(item)
});

return menu;
};

bus.on('selectFile', file => {
currentFile = file;
a.setStatus(getFileStatus(file));
});
bus.on('selectMountpoint', mount => bus.emit('openDirectory', {path: mount.root}));

bus.on('readFile', (file, forceDialog) => {
Expand All @@ -339,10 +450,9 @@ const createApplication = (core, proc, win, $content) => {
let files;

try {
files = await core.make('osjs/vfs')
.readdir(file, {
showHiddenFiles: settings.showHiddenFiles
});
files = await vfs.readdir(file, {
showHiddenFiles: settings.showHiddenFiles
});
} catch (e) {
console.warn(e);
a.setPath(typeof currentPath === 'string' ? currentPath : currentPath.path);
Expand Down Expand Up @@ -376,14 +486,12 @@ const createApplication = (core, proc, win, $content) => {

win.setTitle(`${title} - ${path}`);

currentFile = undefined;
currentPath = file;
proc.args.path = file;
});

bus.on('openMenu', (ev, state, actions, item) => {
const _ = core.make('osjs/locale').translate;
const __ = core.make('osjs/locale').translatable(translations);

const menus = {
file: [
{label: _('LBL_UPLOAD'), onclick: () => {
Expand All @@ -398,10 +506,12 @@ const createApplication = (core, proc, win, $content) => {
};
field.click();
}},
{label: _('LBL_MKDIR'), onclick: () => dialog('mkdir', {path: currentPath.path}, () => refresh())},
{label: _('LBL_MKDIR'), onclick: () => dialog('mkdir', {path: currentPath.path}, () => refresh(true))},
{label: _('LBL_QUIT'), onclick: () => proc.destroy()}
],

edit: createEditMenuItems(currentFile, false),

view: [
{label: _('LBL_REFRESH'), onclick: () => refresh()},
{label: __('LBL_MINIMALISTIC'), checked: state.minimalistic, onclick: () => {
Expand All @@ -427,41 +537,7 @@ const createApplication = (core, proc, win, $content) => {
return;
}

const _ = core.make('osjs/locale').translate;
const __ = core.make('osjs/locale').translatable(translations);

const openMenu = item.isDirectory
? [{
label: _('LBL_GO'),
onclick: () => bus.emit('readFile', item)
}]
: [{
label: _('LBL_OPEN'),
onclick: () => bus.emit('readFile', item)
}, {
label: __('LBL_OPEN_WITH'),
onclick: () => bus.emit('readFile', item, true)
}];

const menu = [
...openMenu,
{
label: _('LBL_RENAME'),
onclick: () => dialog('rename', item, () => refresh())
},
{
label: _('LBL_DELETE'),
onclick: () => dialog('delete', item, () => refresh())
}
];

if (!item.isDirectory) {
menu.push({
label: _('LBL_DOWNLOAD'),
onclick: () => core.make('osjs/vfs').download(item)
});
}

const menu = createEditMenuItems(item, true);
core.make('osjs/contextmenu').show({
position: ev,
menu
Expand Down

0 comments on commit 57e6f9b

Please sign in to comment.