Skip to content

Commit 47397e0

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

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

index.js

+45-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,41 @@ 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+
});
381+
}
382+
}
383+
});
384+
}
385+
348386
const menu = [
349387
...openMenu,
350388
{
@@ -356,13 +394,14 @@ const createApplication = (core, proc, win, $content) => {
356394
label: _('LBL_DELETE'),
357395
disabled: !isValidFile,
358396
onclick: () => dialog('delete', item, () => refresh())
359-
}
397+
},
398+
...clipboardMenu
360399
];
361400

362401
menu.push({
363402
label: _('LBL_DOWNLOAD'),
364403
disabled: !item || isDirectory || !isValidFile,
365-
onclick: () => core.make('osjs/vfs').download(item)
404+
onclick: () => vfs.download(item)
366405
});
367406

368407
return menu;
@@ -397,10 +436,9 @@ const createApplication = (core, proc, win, $content) => {
397436
let files;
398437

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

0 commit comments

Comments
 (0)