Skip to content

Commit d694e8b

Browse files
committed
add logic to copy files from drives browser to filebrowser
1 parent 5715a48 commit d694e8b

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/plugins/driveBrowserPlugin.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { Widget } from '@lumino/widgets';
3737

3838
import { driveBrowserIcon, removeIcon } from '../icons';
3939
import { Drive } from '../contents';
40-
import { setListingLimit } from '../requests';
40+
import { getContents, setListingLimit } from '../requests';
4141
import { CommandIDs } from '../token';
4242

4343
/**
@@ -697,5 +697,81 @@ namespace Private {
697697
'#drive-file-browser.jp-SidePanel .jp-DirListing-content .jp-DirListing-item[data-isdir]',
698698
rank: 110
699699
});
700+
701+
app.commands.addCommand(CommandIDs.copyToFilebrowser, {
702+
isVisible: () => {
703+
// So long as this command only handles one file at time, don't show it
704+
// if multiple files are selected.
705+
return (
706+
!!tracker.currentWidget &&
707+
Array.from(tracker.currentWidget.selectedItems()).length === 1 &&
708+
browser.model.path !== 's3:'
709+
);
710+
},
711+
isEnabled: () => {
712+
return (
713+
!!tracker.currentWidget &&
714+
tracker.currentWidget?.selectedItems().next()!.value &&
715+
tracker.currentWidget?.selectedItems().next()!.value.type !==
716+
'directory'
717+
);
718+
},
719+
execute: async () => {
720+
let currentPath: string = tracker.currentWidget?.selectedItems().next()
721+
.value.path;
722+
Clipboard.copyToSystem(currentPath);
723+
},
724+
label: 'Copy to Filebrowser',
725+
icon: driveBrowserIcon.bindprops({ stylesheet: 'menuItem' })
726+
});
727+
728+
app.contextMenu.addItem({
729+
command: CommandIDs.copyToFilebrowser,
730+
selector: '#drive-file-browser.jp-SidePanel .jp-DirListing-content',
731+
rank: 105
732+
});
733+
734+
app.commands.addCommand(CommandIDs.pasteToFilebrowser, {
735+
isVisible: () => {
736+
return (
737+
!!factory.tracker.currentWidget &&
738+
factory.tracker.currentWidget!.id === 'filebrowser'
739+
);
740+
},
741+
execute: async (args: any) => {
742+
// Get path from clipboard.
743+
let currentPath = await navigator.clipboard.readText();
744+
// Remove leading drive suffix.
745+
currentPath = currentPath.substring(currentPath.indexOf(':') + 1);
746+
const driveName = currentPath.substring(0, currentPath.indexOf('/'));
747+
// Remove drive name.
748+
currentPath = currentPath.substring(currentPath.indexOf('/') + 1);
749+
const fileName = PathExt.basename(currentPath);
750+
751+
const filebrowser = factory.tracker.find(fb => fb.id === 'filebrowser');
752+
753+
// Save new file.
754+
const toDir = factory.tracker.currentWidget!.model.path;
755+
app.serviceManager.contents.save(PathExt.join(toDir, fileName), {
756+
type: 'file',
757+
format: 'text',
758+
content: (
759+
await getContents(driveName, {
760+
path: currentPath,
761+
registeredFileTypes: drive.registeredFileTypes
762+
})
763+
).response.data.content
764+
});
765+
await filebrowser?.model.refresh();
766+
},
767+
label: 'Paste from Drive',
768+
icon: driveBrowserIcon.bindprops({ stylesheet: 'menuItem' })
769+
});
770+
771+
app.contextMenu.addItem({
772+
command: CommandIDs.pasteToFilebrowser,
773+
selector: '.jp-FileBrowser-listing',
774+
rank: 105
775+
});
700776
}
701777
}

src/token.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export namespace CommandIDs {
1919
export const copyPath = 'drives:copy-path';
2020
export const excludeDrive = 'drives:exclude-drive';
2121
export const includeDrive = 'drives:include-drive';
22+
export const copyToFilebrowser = 'drives:copy-to-filebrowser';
23+
export const pasteToFilebrowser = 'drives:paste-to-filebrowser';
2224
}
2325

2426
/**

0 commit comments

Comments
 (0)