Skip to content

Commit ad4cefd

Browse files
committed
Added copy/cut and paste options to edit context menus (#7)
1 parent 37d4e73 commit ad4cefd

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

Diff for: index.js

+46-7
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ const createApplication = (core, proc, win, $content) => {
306306
const title = core.make('osjs/locale')
307307
.translatableFlat(proc.metadata.title);
308308

309+
const {pathJoin} = core.make('osjs/fs');
310+
const vfs = core.make('osjs/vfs');
309311
const bus = core.make('osjs/event-handler', 'FileManager');
310312
const dialog = createDialog(bus, core, proc, win);
311313
const a = app(state(bus, core, proc, win),
@@ -318,11 +320,12 @@ const createApplication = (core, proc, win, $content) => {
318320

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

324326
const _ = core.make('osjs/locale').translate;
325327
const __ = core.make('osjs/locale').translatable(translations);
328+
const clipboard = core.make('osjs/clipboard');
326329

327330
const createEditMenuItems = (item, fromContext) => {
328331
const isDirectory = item && item.isDirectory;
@@ -345,6 +348,42 @@ const createApplication = (core, proc, win, $content) => {
345348
onclick: () => bus.emit('readFile', item, true)
346349
}];
347350

351+
const clipboardMenu = [
352+
{
353+
label: _('LBL_COPY'),
354+
disabled: !isValidFile,
355+
onclick: () => clipboard.set(item, 'vfs:copy')
356+
},
357+
{
358+
label: _('LBL_CUT'),
359+
disabled: !isValidFile,
360+
onclick: () => clipboard.set(item, 'vfs:move')
361+
}
362+
];
363+
364+
if (!fromContext) {
365+
clipboardMenu.push({
366+
label: _('LBL_PASTE'),
367+
disabled: !clipboard.has(/^vfs:/),
368+
onclick: () => {
369+
if (clipboard.has(/^vfs:/)) {
370+
const move = clipboard.has('vfs:move');
371+
372+
// TODO: Error handling
373+
clipboard.get(move)
374+
.then(file => {
375+
const dest = {path: pathJoin(currentPath.path, file.filename)};
376+
377+
return (move
378+
? vfs.move(file, dest)
379+
: vfs.copy(file, dest))
380+
.then(() => refresh());
381+
});
382+
}
383+
}
384+
});
385+
}
386+
348387
const menu = [
349388
...openMenu,
350389
{
@@ -356,13 +395,14 @@ const createApplication = (core, proc, win, $content) => {
356395
label: _('LBL_DELETE'),
357396
disabled: !isValidFile,
358397
onclick: () => dialog('delete', item, () => refresh())
359-
}
398+
},
399+
...clipboardMenu
360400
];
361401

362402
menu.push({
363403
label: _('LBL_DOWNLOAD'),
364404
disabled: !item || isDirectory || !isValidFile,
365-
onclick: () => core.make('osjs/vfs').download(item)
405+
onclick: () => vfs.download(item)
366406
});
367407

368408
return menu;
@@ -397,10 +437,9 @@ const createApplication = (core, proc, win, $content) => {
397437
let files;
398438

399439
try {
400-
files = await core.make('osjs/vfs')
401-
.readdir(file, {
402-
showHiddenFiles: settings.showHiddenFiles
403-
});
440+
files = await vfs.readdir(file, {
441+
showHiddenFiles: settings.showHiddenFiles
442+
});
404443
} catch (e) {
405444
console.warn(e);
406445
a.setPath(typeof currentPath === 'string' ? currentPath : currentPath.path);

0 commit comments

Comments
 (0)