index.ts 11 KB

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