index.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. let menu = createMenu(app);
  159. mainMenu.addMenu(menu, { rank: 1 });
  160. }
  161. /**
  162. * Add the main file browser commands to the application's command registry.
  163. */
  164. function addCommands(app: JupyterLab, tracker: InstanceTracker<FileBrowser>, mainBrowser: FileBrowser): void {
  165. const { commands } = app;
  166. commands.addCommand(CommandIDs.del, {
  167. execute: () => {
  168. const widget = tracker.currentWidget;
  169. if (!widget) {
  170. return;
  171. }
  172. return widget.delete();
  173. },
  174. iconClass: 'jp-MaterialIcon jp-CloseIcon',
  175. label: 'Delete',
  176. mnemonic: 0
  177. });
  178. commands.addCommand(CommandIDs.copy, {
  179. execute: () => {
  180. const widget = tracker.currentWidget;
  181. if (!widget) {
  182. return;
  183. }
  184. return widget.copy();
  185. },
  186. iconClass: 'jp-MaterialIcon jp-CopyIcon',
  187. label: 'Copy',
  188. mnemonic: 0
  189. });
  190. commands.addCommand(CommandIDs.cut, {
  191. execute: () => {
  192. const widget = tracker.currentWidget;
  193. if (!widget) {
  194. return;
  195. }
  196. return widget.cut();
  197. },
  198. iconClass: 'jp-MaterialIcon jp-CutIcon',
  199. label: 'Cut'
  200. });
  201. commands.addCommand(CommandIDs.download, {
  202. execute: () => {
  203. const widget = tracker.currentWidget;
  204. if (!widget) {
  205. return;
  206. }
  207. return widget.download();
  208. },
  209. iconClass: 'jp-MaterialIcon jp-DownloadIcon',
  210. label: 'Download'
  211. });
  212. commands.addCommand(CommandIDs.duplicate, {
  213. execute: () => {
  214. const widget = tracker.currentWidget;
  215. if (!widget) {
  216. return;
  217. }
  218. return widget.duplicate();
  219. },
  220. iconClass: 'jp-MaterialIcon jp-CopyIcon',
  221. label: 'Duplicate'
  222. });
  223. commands.addCommand(CommandIDs.hideBrowser, {
  224. execute: () => {
  225. if (!mainBrowser.isHidden) {
  226. app.shell.collapseLeft();
  227. }
  228. }
  229. });
  230. commands.addCommand(CommandIDs.open, {
  231. execute: () => {
  232. const widget = tracker.currentWidget;
  233. if (!widget) {
  234. return;
  235. }
  236. each(widget.selectedItems(), item => {
  237. if (item.type === 'directory') {
  238. widget.model.cd(item.path);
  239. } else {
  240. commands.execute('docmanager:open', { path: item.path });
  241. }
  242. });
  243. },
  244. iconClass: 'jp-MaterialIcon jp-OpenFolderIcon',
  245. label: 'Open',
  246. mnemonic: 0,
  247. });
  248. commands.addCommand(CommandIDs.paste, {
  249. execute: () => {
  250. const widget = tracker.currentWidget;
  251. if (!widget) {
  252. return;
  253. }
  254. return widget.paste();
  255. },
  256. iconClass: 'jp-MaterialIcon jp-PasteIcon',
  257. label: 'Paste',
  258. mnemonic: 0
  259. });
  260. commands.addCommand(CommandIDs.rename, {
  261. execute: (args) => {
  262. const widget = tracker.currentWidget;
  263. if (!widget) {
  264. return;
  265. }
  266. return widget.rename();
  267. },
  268. iconClass: 'jp-MaterialIcon jp-EditIcon',
  269. label: 'Rename',
  270. mnemonic: 0
  271. });
  272. commands.addCommand(CommandIDs.showBrowser, {
  273. execute: () => { app.shell.activateById(mainBrowser.id); }
  274. });
  275. commands.addCommand(CommandIDs.shutdown, {
  276. execute: () => {
  277. const widget = tracker.currentWidget;
  278. if (!widget) {
  279. return;
  280. }
  281. return widget.shutdownKernels();
  282. },
  283. iconClass: 'jp-MaterialIcon jp-StopIcon',
  284. label: 'Shutdown Kernel'
  285. });
  286. commands.addCommand(CommandIDs.toggleBrowser, {
  287. execute: () => {
  288. if (mainBrowser.isHidden) {
  289. return commands.execute(CommandIDs.showBrowser, void 0);
  290. } else {
  291. return commands.execute(CommandIDs.hideBrowser, void 0);
  292. }
  293. }
  294. });
  295. commands.addCommand(CommandIDs.createLauncher, {
  296. label: 'New...',
  297. execute: () => {
  298. return commands.execute('launcher:create', {
  299. cwd: mainBrowser.model.path,
  300. });
  301. }
  302. });
  303. // Create a launcher with a banner if there are no open items.
  304. app.restored.then(() => {
  305. if (app.shell.isEmpty('main')) {
  306. commands.execute('launcher:create', {
  307. cwd: mainBrowser.model.path
  308. });
  309. }
  310. });
  311. }
  312. /**
  313. * Create a top level menu for the file browser.
  314. */
  315. function createMenu(app: JupyterLab): Menu {
  316. const { commands } = app;
  317. const menu = new Menu({ commands });
  318. menu.title.label = 'File';
  319. [
  320. CommandIDs.createLauncher,
  321. 'docmanager:save',
  322. 'docmanager:save-as',
  323. 'docmanager:rename',
  324. 'docmanager:restore-checkpoint',
  325. 'docmanager:clone',
  326. 'docmanager:close',
  327. 'docmanager:close-all-files'
  328. ].forEach(command => { menu.addItem({ command }); });
  329. menu.addItem({ type: 'separator' });
  330. menu.addItem({ command: 'settingeditor:open' });
  331. return menu;
  332. }
  333. /**
  334. * Create a context menu for the file browser listing.
  335. *
  336. * #### Notes
  337. * This function generates temporary commands with an incremented name. These
  338. * commands are disposed when the menu itself is disposed.
  339. */
  340. function createContextMenu(path: string, commands: CommandRegistry, registry: DocumentRegistry): Menu {
  341. const menu = new Menu({ commands });
  342. menu.addItem({ command: CommandIDs.open });
  343. const factories = registry.preferredWidgetFactories(path).map(f => f.name);
  344. if (path && factories.length > 1) {
  345. const command = 'docmanager:open';
  346. const openWith = new Menu({ commands });
  347. openWith.title.label = 'Open With...';
  348. factories.forEach(factory => {
  349. openWith.addItem({ args: { factory, path }, command });
  350. });
  351. menu.addItem({ type: 'submenu', submenu: openWith });
  352. }
  353. menu.addItem({ command: CommandIDs.rename });
  354. menu.addItem({ command: CommandIDs.del });
  355. menu.addItem({ command: CommandIDs.duplicate });
  356. menu.addItem({ command: CommandIDs.cut });
  357. menu.addItem({ command: CommandIDs.copy });
  358. menu.addItem({ command: CommandIDs.paste });
  359. menu.addItem({ command: CommandIDs.download });
  360. menu.addItem({ command: CommandIDs.shutdown });
  361. return menu;
  362. }