Skip to content

Commit

Permalink
Added copy/cut and paste options to edit context menus (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Oct 18, 2019
1 parent 37d4e73 commit ad4cefd
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,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 @@ -318,11 +320,12 @@ const createApplication = (core, proc, win, $content) => {

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);
};

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;
Expand All @@ -345,6 +348,42 @@ const createApplication = (core, proc, win, $content) => {
onclick: () => bus.emit('readFile', item, true)
}];

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

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

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

return (move
? vfs.move(file, dest)
: vfs.copy(file, dest))
.then(() => refresh());
});
}
}
});
}

const menu = [
...openMenu,
{
Expand All @@ -356,13 +395,14 @@ const createApplication = (core, proc, win, $content) => {
label: _('LBL_DELETE'),
disabled: !isValidFile,
onclick: () => dialog('delete', item, () => refresh())
}
},
...clipboardMenu
];

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

return menu;
Expand Down Expand Up @@ -397,10 +437,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

0 comments on commit ad4cefd

Please sign in to comment.