index.ts 12 KB

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