index.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. JupyterLab, JupyterLabPlugin
  5. } from '@jupyterlab/application';
  6. import {
  7. showDialog, showErrorMessage, Spinner, Dialog, ICommandPalette
  8. } from '@jupyterlab/apputils';
  9. import {
  10. IChangedArgs, ISettingRegistry
  11. } from '@jupyterlab/coreutils';
  12. import {
  13. renameDialog, getOpenPath, DocumentManager, IDocumentManager
  14. } from '@jupyterlab/docmanager';
  15. import {
  16. DocumentRegistry
  17. } from '@jupyterlab/docregistry';
  18. import {
  19. IMainMenu
  20. } from '@jupyterlab/mainmenu';
  21. import {
  22. Contents, Kernel
  23. } from '@jupyterlab/services';
  24. import {
  25. IDisposable
  26. } from '@phosphor/disposable';
  27. /**
  28. * The command IDs used by the document manager plugin.
  29. */
  30. namespace CommandIDs {
  31. export
  32. const clone = 'docmanager:clone';
  33. export
  34. const close = 'docmanager:close';
  35. export
  36. const closeAllFiles = 'docmanager:close-all-files';
  37. export
  38. const createFrom = 'docmanager:create-from';
  39. export
  40. const deleteFile = 'docmanager:delete-file';
  41. export
  42. const newUntitled = 'docmanager:new-untitled';
  43. export
  44. const open = 'docmanager:open';
  45. export
  46. const openBrowserTab = 'docmanager:open-browser-tab';
  47. export
  48. const openDirect = 'docmanager:open-direct';
  49. export
  50. const rename = 'docmanager:rename';
  51. export
  52. const restoreCheckpoint = 'docmanager:restore-checkpoint';
  53. export
  54. const save = 'docmanager:save';
  55. export
  56. const saveAll = 'docmanager:save-all';
  57. export
  58. const saveAs = 'docmanager:save-as';
  59. export
  60. const toggleAutosave = 'docmanager:toggle-autosave';
  61. }
  62. const pluginId = '@jupyterlab/docmanager-extension:plugin';
  63. /**
  64. * The default document manager provider.
  65. */
  66. const plugin: JupyterLabPlugin<IDocumentManager> = {
  67. id: pluginId,
  68. provides: IDocumentManager,
  69. requires: [ICommandPalette, IMainMenu, ISettingRegistry],
  70. activate: (app: JupyterLab, palette: ICommandPalette, menu: IMainMenu, settingRegistry: ISettingRegistry): IDocumentManager => {
  71. const manager = app.serviceManager;
  72. const contexts = new WeakSet<DocumentRegistry.Context>();
  73. const opener: DocumentManager.IWidgetOpener = {
  74. open: (widget, options) => {
  75. const shell = app.shell;
  76. if (!widget.id) {
  77. widget.id = `document-manager-${++Private.id}`;
  78. }
  79. widget.title.dataset = {
  80. 'type': 'document-title',
  81. ...widget.title.dataset
  82. };
  83. if (!widget.isAttached) {
  84. app.shell.addToMainArea(widget, options || {});
  85. // Add a loading spinner, and remove it when the widget is ready.
  86. if (widget.ready !== undefined) {
  87. const spinner = new Spinner();
  88. widget.node.appendChild(spinner.node);
  89. widget.ready.then(() => {
  90. widget.node.removeChild(spinner.node);
  91. spinner.dispose();
  92. });
  93. }
  94. }
  95. shell.activateById(widget.id);
  96. // Handle dirty state for open documents.
  97. let context = docManager.contextForWidget(widget);
  98. if (!contexts.has(context)) {
  99. handleContext(app, context);
  100. contexts.add(context);
  101. }
  102. }
  103. };
  104. const registry = app.docRegistry;
  105. const when = app.restored.then(() => void 0);
  106. const docManager = new DocumentManager({ registry, manager, opener, when });
  107. // Register the file operations commands.
  108. addCommands(app, docManager, palette, opener, settingRegistry);
  109. const onSettingsUpdated = (settings: ISettingRegistry.ISettings) => {
  110. const autosave = settings.get('autosave').composite as boolean | null;
  111. docManager.autosave = (autosave === true || autosave === false)
  112. ? autosave
  113. : true;
  114. };
  115. // Fetch the initial state of the settings.
  116. Promise.all([settingRegistry.load(pluginId), app.restored])
  117. .then(([settings]) => {
  118. settings.changed.connect(onSettingsUpdated);
  119. onSettingsUpdated(settings);
  120. }).catch((reason: Error) => {
  121. console.error(reason.message);
  122. });
  123. menu.settingsMenu.addGroup([{ command: CommandIDs.toggleAutosave }], 5);
  124. return docManager;
  125. }
  126. };
  127. /**
  128. * Export the plugin as default.
  129. */
  130. export default plugin;
  131. /**
  132. * Add the file operations commands to the application's command registry.
  133. */
  134. function addCommands(app: JupyterLab, docManager: IDocumentManager, palette: ICommandPalette, opener: DocumentManager.IWidgetOpener, settingRegistry: ISettingRegistry): void {
  135. const { commands, docRegistry } = app;
  136. const category = 'File Operations';
  137. const isEnabled = () => {
  138. const { currentWidget } = app.shell;
  139. return !!(currentWidget && docManager.contextForWidget(currentWidget));
  140. };
  141. const fileType = () => {
  142. const { currentWidget } = app.shell;
  143. if (!currentWidget) {
  144. return 'File';
  145. }
  146. const context = docManager.contextForWidget(currentWidget);
  147. if (!context) {
  148. return '';
  149. }
  150. const fts = docRegistry.getFileTypesForPath(context.path);
  151. return (fts.length && fts[0].displayName) ? fts[0].displayName : 'File';
  152. };
  153. commands.addCommand(CommandIDs.close, {
  154. label: () => {
  155. const widget = app.shell.currentWidget;
  156. let name = 'File';
  157. if (widget) {
  158. const typeName = fileType();
  159. name = typeName || widget.title.label;
  160. }
  161. return `Close ${name}`;
  162. },
  163. isEnabled: () => !!app.shell.currentWidget &&
  164. !!app.shell.currentWidget.title.closable,
  165. execute: () => {
  166. if (app.shell.currentWidget) {
  167. app.shell.currentWidget.close();
  168. }
  169. }
  170. });
  171. commands.addCommand(CommandIDs.closeAllFiles, {
  172. label: 'Close All',
  173. execute: () => { app.shell.closeAll(); }
  174. });
  175. commands.addCommand(CommandIDs.deleteFile, {
  176. label: () => `Delete ${fileType()}`,
  177. execute: args => {
  178. const path = typeof args['path'] === 'undefined' ? ''
  179. : args['path'] as string;
  180. if (!path) {
  181. const command = CommandIDs.deleteFile;
  182. throw new Error(`A non-empty path is required for ${command}.`);
  183. }
  184. return docManager.deleteFile(path);
  185. }
  186. });
  187. commands.addCommand(CommandIDs.newUntitled, {
  188. execute: args => {
  189. const errorTitle = args['error'] as string || 'Error';
  190. const path = typeof args['path'] === 'undefined' ? ''
  191. : args['path'] as string;
  192. let options: Partial<Contents.ICreateOptions> = {
  193. type: args['type'] as Contents.ContentType,
  194. path
  195. };
  196. if (args['type'] === 'file') {
  197. options.ext = args['ext'] as string || '.txt';
  198. }
  199. return docManager.services.contents.newUntitled(options)
  200. .catch(error => showErrorMessage(errorTitle, error));
  201. },
  202. label: args => args['label'] as string || `New ${args['type'] as string}`
  203. });
  204. commands.addCommand(CommandIDs.open, {
  205. execute: args => {
  206. const path = typeof args['path'] === 'undefined' ? ''
  207. : args['path'] as string;
  208. const factory = args['factory'] as string || void 0;
  209. const kernel = args['kernel'] as Kernel.IModel || void 0;
  210. const options = args['options'] as DocumentRegistry.IOpenOptions || void 0;
  211. return docManager.services.contents.get(path, { content: false })
  212. .then(() => docManager.openOrReveal(path, factory, kernel, options));
  213. },
  214. icon: args => args['icon'] as string || '',
  215. label: args => (args['label'] || args['factory']) as string,
  216. mnemonic: args => args['mnemonic'] as number || -1
  217. });
  218. commands.addCommand(CommandIDs.openBrowserTab, {
  219. execute: args => {
  220. const path = typeof args['path'] === 'undefined' ? ''
  221. : args['path'] as string;
  222. if (!path) {
  223. return;
  224. }
  225. return docManager.services.contents.getDownloadUrl(path).then(url => {
  226. window.open(url, '_blank');
  227. });
  228. },
  229. icon: args => args['icon'] as string || '',
  230. label: () => 'Open in New Browser Tab'
  231. });
  232. commands.addCommand(CommandIDs.openDirect, {
  233. label: () => 'Open from Path',
  234. caption: 'Open from path',
  235. isEnabled: () => true,
  236. execute: () => {
  237. return getOpenPath(docManager.services.contents).then(path => {
  238. if (!path) {
  239. return;
  240. }
  241. docManager.services.contents.get(path, { content: false }).then( (args) => {
  242. // exists
  243. return commands.execute(CommandIDs.open, {path: path});
  244. }, () => {
  245. // does not exist
  246. return showDialog({
  247. title: 'Cannot open',
  248. body: 'File not found',
  249. buttons: [Dialog.okButton()]
  250. });
  251. });
  252. return;
  253. });
  254. },
  255. });
  256. commands.addCommand(CommandIDs.restoreCheckpoint, {
  257. label: () => `Revert ${fileType()} to Saved`,
  258. caption: 'Revert contents to previous checkpoint',
  259. isEnabled,
  260. execute: () => {
  261. if (isEnabled()) {
  262. let context = docManager.contextForWidget(app.shell.currentWidget);
  263. if (context.model.readOnly) {
  264. return context.revert();
  265. }
  266. return context.restoreCheckpoint().then(() => context.revert());
  267. }
  268. }
  269. });
  270. commands.addCommand(CommandIDs.save, {
  271. label: () => `Save ${fileType()}`,
  272. caption: 'Save and create checkpoint',
  273. isEnabled,
  274. execute: () => {
  275. if (isEnabled()) {
  276. let context = docManager.contextForWidget(app.shell.currentWidget);
  277. if (context.model.readOnly) {
  278. return showDialog({
  279. title: 'Cannot Save',
  280. body: 'Document is read-only',
  281. buttons: [Dialog.okButton()]
  282. });
  283. }
  284. return context.save()
  285. .then(() => context.createCheckpoint())
  286. .catch(err => {
  287. // If the save was canceled by user-action, do nothing.
  288. if (err.message === 'Cancel') {
  289. return;
  290. }
  291. throw err;
  292. });
  293. }
  294. }
  295. });
  296. commands.addCommand(CommandIDs.saveAll, {
  297. label: () => 'Save All',
  298. caption: 'Save all open documents',
  299. isEnabled: () => {
  300. const iterator = app.shell.widgets('main');
  301. let widget = iterator.next();
  302. while (widget) {
  303. if (docManager.contextForWidget(widget)) {
  304. return true;
  305. }
  306. widget = iterator.next();
  307. }
  308. return false;
  309. },
  310. execute: () => {
  311. const iterator = app.shell.widgets('main');
  312. const promises: Promise<void>[] = [];
  313. const paths = new Set<string>(); // Cache so we don't double save files.
  314. let widget = iterator.next();
  315. while (widget) {
  316. const context = docManager.contextForWidget(widget);
  317. if (context && !context.model.readOnly && !paths.has(context.path)) {
  318. paths.add(context.path);
  319. promises.push(context.save());
  320. }
  321. widget = iterator.next();
  322. }
  323. return Promise.all(promises);
  324. }
  325. });
  326. commands.addCommand(CommandIDs.saveAs, {
  327. label: () => `Save ${fileType()} As…`,
  328. caption: 'Save with new path',
  329. isEnabled,
  330. execute: () => {
  331. if (isEnabled()) {
  332. let context = docManager.contextForWidget(app.shell.currentWidget);
  333. return context.saveAs();
  334. }
  335. }
  336. });
  337. commands.addCommand(CommandIDs.rename, {
  338. label: () => `Rename ${fileType()}…`,
  339. isEnabled,
  340. execute: () => {
  341. if (isEnabled()) {
  342. let context = docManager.contextForWidget(app.shell.currentWidget);
  343. return renameDialog(docManager, context!.path);
  344. }
  345. }
  346. });
  347. commands.addCommand(CommandIDs.clone, {
  348. label: () => `New View for ${fileType()}`,
  349. isEnabled,
  350. execute: (args) => {
  351. const widget = app.shell.currentWidget;
  352. const options = args['options'] as DocumentRegistry.IOpenOptions || void 0;
  353. if (!widget) {
  354. return;
  355. }
  356. // Clone the widget.
  357. let child = docManager.cloneWidget(widget);
  358. if (child) {
  359. opener.open(child, options);
  360. }
  361. },
  362. });
  363. commands.addCommand(CommandIDs.toggleAutosave, {
  364. label: args =>
  365. args['isPalette'] ? 'Toggle Document Autosave' : 'Autosave Documents',
  366. isToggled: () => docManager.autosave,
  367. execute: () => {
  368. const value = !docManager.autosave;
  369. const key = 'autosave';
  370. return settingRegistry.set(pluginId, key, value)
  371. .catch((reason: Error) => {
  372. console.error(`Failed to set ${pluginId}:${key} - ${reason.message}`);
  373. });
  374. }
  375. });
  376. app.contextMenu.addItem({
  377. command: CommandIDs.rename,
  378. selector: '[data-type="document-title"]',
  379. rank: 1
  380. });
  381. app.contextMenu.addItem({
  382. command: CommandIDs.clone,
  383. selector: '[data-type="document-title"]',
  384. rank: 2
  385. });
  386. [
  387. CommandIDs.openDirect,
  388. CommandIDs.save,
  389. CommandIDs.restoreCheckpoint,
  390. CommandIDs.saveAs,
  391. CommandIDs.clone,
  392. CommandIDs.close,
  393. CommandIDs.closeAllFiles
  394. ].forEach(command => { palette.addItem({ command, category }); });
  395. palette.addItem({
  396. command: CommandIDs.toggleAutosave,
  397. category,
  398. args: { isPalette: true }
  399. });
  400. }
  401. /**
  402. * Handle dirty state for a context.
  403. */
  404. function handleContext(app: JupyterLab, context: DocumentRegistry.Context): void {
  405. let disposable: IDisposable | null = null;
  406. let onStateChanged = (sender: any, args: IChangedArgs<any>) => {
  407. if (args.name === 'dirty') {
  408. if (args.newValue === true) {
  409. if (!disposable) {
  410. disposable = app.setDirty();
  411. }
  412. } else if (disposable) {
  413. disposable.dispose();
  414. disposable = null;
  415. }
  416. }
  417. };
  418. context.ready.then(() => {
  419. context.model.stateChanged.connect(onStateChanged);
  420. if (context.model.dirty) {
  421. disposable = app.setDirty();
  422. }
  423. });
  424. context.disposed.connect(() => {
  425. if (disposable) {
  426. disposable.dispose();
  427. }
  428. });
  429. }
  430. /**
  431. * A namespace for private module data.
  432. */
  433. namespace Private {
  434. /**
  435. * A counter for unique IDs.
  436. */
  437. export
  438. let id = 0;
  439. }