index.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ILayoutRestorer, JupyterLab, JupyterLabPlugin
  5. } from '@jupyterlab/application';
  6. import {
  7. ICommandPalette, IMainMenu, InstanceTracker, ToolbarButton
  8. } from '@jupyterlab/apputils';
  9. import {
  10. IStateDB
  11. } from '@jupyterlab/coreutils';
  12. import {
  13. IDocumentManager
  14. } from '@jupyterlab/docmanager';
  15. import {
  16. DocumentRegistry
  17. } from '@jupyterlab/docregistry';
  18. import {
  19. FileBrowserModel, FileBrowser, IFileBrowserFactory
  20. } from '@jupyterlab/filebrowser';
  21. import {
  22. each
  23. } from '@phosphor/algorithm';
  24. import {
  25. CommandRegistry
  26. } from '@phosphor/commands';
  27. import {
  28. Menu
  29. } from '@phosphor/widgets';
  30. /**
  31. * The command IDs used by the file browser plugin.
  32. */
  33. namespace CommandIDs {
  34. export
  35. const copy = 'filebrowser:copy';
  36. export
  37. const cut = 'filebrowser:cut';
  38. export
  39. const del = 'filebrowser:delete';
  40. export
  41. const download = 'filebrowser:download';
  42. export
  43. const duplicate = 'filebrowser:duplicate';
  44. export
  45. const hideBrowser = 'filebrowser:hide-main'; // For main browser only.
  46. export
  47. const open = 'filebrowser:open';
  48. export
  49. const paste = 'filebrowser:paste';
  50. export
  51. const rename = 'filebrowser:rename';
  52. export
  53. const showBrowser = 'filebrowser:activate-main'; // For main browser only.
  54. export
  55. const shutdown = 'filebrowser:shutdown';
  56. export
  57. const toggleBrowser = 'filebrowser:toggle-main'; // For main browser only.
  58. export
  59. const createLauncher = 'filebrowser:create-main-launcher'; // For main browser only.
  60. };
  61. /**
  62. * The default file browser extension.
  63. */
  64. const fileBrowserPlugin: JupyterLabPlugin<void> = {
  65. activate: activateFileBrowser,
  66. id: 'jupyter.extensions.filebrowser',
  67. requires: [
  68. IFileBrowserFactory,
  69. IDocumentManager,
  70. IMainMenu,
  71. ICommandPalette,
  72. ILayoutRestorer
  73. ],
  74. autoStart: true
  75. };
  76. /**
  77. * The default file browser factory provider.
  78. */
  79. const factoryPlugin: JupyterLabPlugin<IFileBrowserFactory> = {
  80. activate: activateFactory,
  81. id: 'jupyter.services.filebrowser',
  82. provides: IFileBrowserFactory,
  83. requires: [IDocumentManager, IStateDB],
  84. autoStart: true
  85. };
  86. /**
  87. * The file browser namespace token.
  88. */
  89. const namespace = 'filebrowser';
  90. /**
  91. * Export the plugins as default.
  92. */
  93. const plugins: JupyterLabPlugin<any>[] = [factoryPlugin, fileBrowserPlugin];
  94. export default plugins;
  95. /**
  96. * Activate the file browser factory provider.
  97. */
  98. function activateFactory(app: JupyterLab, docManager: IDocumentManager, state: IStateDB): IFileBrowserFactory {
  99. const { commands } = app;
  100. const tracker = new InstanceTracker<FileBrowser>({ namespace });
  101. const createFileBrowser = (id: string, options: IFileBrowserFactory.IOptions = {}) => {
  102. const model = new FileBrowserModel({
  103. manager: docManager,
  104. driveName: options.driveName || '',
  105. state: options.state === null ? null : options.state || state
  106. });
  107. const widget = new FileBrowser({
  108. id, model, commands: options.commands || commands
  109. });
  110. const { registry } = docManager;
  111. // Add a launcher toolbar item.
  112. let launcher = new ToolbarButton({
  113. className: 'jp-AddIcon',
  114. onClick: () => {
  115. return commands.execute('launcher:create', {
  116. cwd: widget.model.path
  117. });
  118. }
  119. });
  120. launcher.addClass('jp-MaterialIcon');
  121. widget.toolbar.insertItem(0, 'launch', launcher);
  122. // Add a context menu handler to the file browser's directory listing.
  123. let node = widget.node.getElementsByClassName('jp-DirListing-content')[0];
  124. node.addEventListener('contextmenu', (event: MouseEvent) => {
  125. event.preventDefault();
  126. const path = widget.pathForClick(event) || '';
  127. const menu = createContextMenu(path, commands, registry);
  128. menu.open(event.clientX, event.clientY);
  129. });
  130. // Track the newly created file browser.
  131. tracker.add(widget);
  132. return widget;
  133. };
  134. let defaultBrowser = createFileBrowser('filebrowser');
  135. return { createFileBrowser, defaultBrowser, tracker };
  136. }
  137. /**
  138. * Activate the default file browser in the sidebar.
  139. */
  140. function activateFileBrowser(app: JupyterLab, factory: IFileBrowserFactory, docManager: IDocumentManager, mainMenu: IMainMenu, palette: ICommandPalette, restorer: ILayoutRestorer): void {
  141. const fbWidget = factory.defaultBrowser;
  142. // Let the application restorer track the primary file browser (that is
  143. // automatically created) for restoration of application state (e.g. setting
  144. // the file browser as the current side bar widget).
  145. //
  146. // All other file browsers created by using the factory function are
  147. // responsible for their own restoration behavior, if any.
  148. restorer.add(fbWidget, namespace);
  149. addCommands(app, factory.tracker, fbWidget);
  150. fbWidget.title.label = 'Files';
  151. app.shell.addToLeftArea(fbWidget, { rank: 100 });
  152. // If the layout is a fresh session without saved data, open file browser.
  153. app.restored.then(layout => {
  154. if (layout.fresh) {
  155. app.commands.execute(CommandIDs.showBrowser, void 0);
  156. }
  157. });
  158. // Create a launcher if there are no open items.
  159. app.restored.then(() => {
  160. if (app.shell.isEmpty('main')) {
  161. // Make sure the model is restored.
  162. fbWidget.model.restored.then(() => {
  163. app.commands.execute('launcher:create', {
  164. cwd: fbWidget.model.path
  165. });
  166. });
  167. }
  168. });
  169. let menu = createMenu(app);
  170. mainMenu.addMenu(menu, { rank: 1 });
  171. }
  172. /**
  173. * Add the main file browser commands to the application's command registry.
  174. */
  175. function addCommands(app: JupyterLab, tracker: InstanceTracker<FileBrowser>, mainBrowser: FileBrowser): void {
  176. const { commands } = app;
  177. commands.addCommand(CommandIDs.del, {
  178. execute: () => {
  179. const widget = tracker.currentWidget;
  180. if (!widget) {
  181. return;
  182. }
  183. return widget.delete();
  184. },
  185. iconClass: 'jp-MaterialIcon jp-CloseIcon',
  186. label: 'Delete',
  187. mnemonic: 0
  188. });
  189. commands.addCommand(CommandIDs.copy, {
  190. execute: () => {
  191. const widget = tracker.currentWidget;
  192. if (!widget) {
  193. return;
  194. }
  195. return widget.copy();
  196. },
  197. iconClass: 'jp-MaterialIcon jp-CopyIcon',
  198. label: 'Copy',
  199. mnemonic: 0
  200. });
  201. commands.addCommand(CommandIDs.cut, {
  202. execute: () => {
  203. const widget = tracker.currentWidget;
  204. if (!widget) {
  205. return;
  206. }
  207. return widget.cut();
  208. },
  209. iconClass: 'jp-MaterialIcon jp-CutIcon',
  210. label: 'Cut'
  211. });
  212. commands.addCommand(CommandIDs.download, {
  213. execute: () => {
  214. const widget = tracker.currentWidget;
  215. if (!widget) {
  216. return;
  217. }
  218. return widget.download();
  219. },
  220. iconClass: 'jp-MaterialIcon jp-DownloadIcon',
  221. label: 'Download'
  222. });
  223. commands.addCommand(CommandIDs.duplicate, {
  224. execute: () => {
  225. const widget = tracker.currentWidget;
  226. if (!widget) {
  227. return;
  228. }
  229. return widget.duplicate();
  230. },
  231. iconClass: 'jp-MaterialIcon jp-CopyIcon',
  232. label: 'Duplicate'
  233. });
  234. commands.addCommand(CommandIDs.hideBrowser, {
  235. execute: () => {
  236. if (!mainBrowser.isHidden) {
  237. app.shell.collapseLeft();
  238. }
  239. }
  240. });
  241. commands.addCommand(CommandIDs.open, {
  242. execute: () => {
  243. const widget = tracker.currentWidget;
  244. if (!widget) {
  245. return;
  246. }
  247. each(widget.selectedItems(), item => {
  248. if (item.type === 'directory') {
  249. widget.model.cd(item.path);
  250. } else {
  251. commands.execute('docmanager:open', { path: item.path });
  252. }
  253. });
  254. },
  255. iconClass: 'jp-MaterialIcon jp-OpenFolderIcon',
  256. label: 'Open',
  257. mnemonic: 0,
  258. });
  259. commands.addCommand(CommandIDs.paste, {
  260. execute: () => {
  261. const widget = tracker.currentWidget;
  262. if (!widget) {
  263. return;
  264. }
  265. return widget.paste();
  266. },
  267. iconClass: 'jp-MaterialIcon jp-PasteIcon',
  268. label: 'Paste',
  269. mnemonic: 0
  270. });
  271. commands.addCommand(CommandIDs.rename, {
  272. execute: (args) => {
  273. const widget = tracker.currentWidget;
  274. if (!widget) {
  275. return;
  276. }
  277. return widget.rename();
  278. },
  279. iconClass: 'jp-MaterialIcon jp-EditIcon',
  280. label: 'Rename',
  281. mnemonic: 0
  282. });
  283. commands.addCommand(CommandIDs.showBrowser, {
  284. execute: () => { app.shell.activateById(mainBrowser.id); }
  285. });
  286. commands.addCommand(CommandIDs.shutdown, {
  287. execute: () => {
  288. const widget = tracker.currentWidget;
  289. if (!widget) {
  290. return;
  291. }
  292. return widget.shutdownKernels();
  293. },
  294. iconClass: 'jp-MaterialIcon jp-StopIcon',
  295. label: 'Shutdown Kernel'
  296. });
  297. commands.addCommand(CommandIDs.toggleBrowser, {
  298. execute: () => {
  299. if (mainBrowser.isHidden) {
  300. return commands.execute(CommandIDs.showBrowser, void 0);
  301. } else {
  302. return commands.execute(CommandIDs.hideBrowser, void 0);
  303. }
  304. }
  305. });
  306. commands.addCommand(CommandIDs.createLauncher, {
  307. label: 'New...',
  308. execute: () => {
  309. return commands.execute('launcher:create', {
  310. cwd: mainBrowser.model.path,
  311. });
  312. }
  313. });
  314. }
  315. /**
  316. * Create a top level menu for the file browser.
  317. */
  318. function createMenu(app: JupyterLab): Menu {
  319. const { commands } = app;
  320. const menu = new Menu({ commands });
  321. menu.title.label = 'File';
  322. [
  323. CommandIDs.createLauncher,
  324. 'docmanager:save',
  325. 'docmanager:save-as',
  326. 'docmanager:rename',
  327. 'docmanager:restore-checkpoint',
  328. 'docmanager:clone',
  329. 'docmanager:close',
  330. 'docmanager:close-all-files'
  331. ].forEach(command => { menu.addItem({ command }); });
  332. menu.addItem({ type: 'separator' });
  333. menu.addItem({ command: 'settingeditor:open' });
  334. return menu;
  335. }
  336. /**
  337. * Create a context menu for the file browser listing.
  338. *
  339. * #### Notes
  340. * This function generates temporary commands with an incremented name. These
  341. * commands are disposed when the menu itself is disposed.
  342. */
  343. function createContextMenu(path: string, commands: CommandRegistry, registry: DocumentRegistry): Menu {
  344. const menu = new Menu({ commands });
  345. menu.addItem({ command: CommandIDs.open });
  346. const factories = registry.preferredWidgetFactories(path).map(f => f.name);
  347. if (path && factories.length > 1) {
  348. const command = 'docmanager:open';
  349. const openWith = new Menu({ commands });
  350. openWith.title.label = 'Open With...';
  351. factories.forEach(factory => {
  352. openWith.addItem({ args: { factory, path }, command });
  353. });
  354. menu.addItem({ type: 'submenu', submenu: openWith });
  355. }
  356. menu.addItem({ command: CommandIDs.rename });
  357. menu.addItem({ command: CommandIDs.del });
  358. menu.addItem({ command: CommandIDs.duplicate });
  359. menu.addItem({ command: CommandIDs.cut });
  360. menu.addItem({ command: CommandIDs.copy });
  361. menu.addItem({ command: CommandIDs.paste });
  362. menu.addItem({ command: CommandIDs.download });
  363. menu.addItem({ command: CommandIDs.shutdown });
  364. return menu;
  365. }