Kaynağa Gözat

Support reloading editor widgets.

Afshin Darian 8 yıl önce
ebeveyn
işleme
9520e6de58
1 değiştirilmiş dosya ile 29 ekleme ve 2 silme
  1. 29 2
      src/editorwidget/plugin.ts

+ 29 - 2
src/editorwidget/plugin.ts

@@ -33,6 +33,10 @@ import {
   IMainMenu
 } from '../mainmenu';
 
+import {
+  IStateDB
+} from '../statedb';
+
 import {
   IEditorTracker
 } from './index';
@@ -57,6 +61,11 @@ const PORTRAIT_ICON_CLASS = 'jp-MainAreaPortraitIcon';
  */
 const EDITOR_ICON_CLASS = 'jp-ImageTextEditor';
 
+/**
+ * The state database namespace for editor widgets.
+ */
+const NAMESPACE = 'editorwidgets';
+
 /**
  * The map of command ids used by the editor.
  */
@@ -83,7 +92,7 @@ const tracker = new InstanceTracker<EditorWidget>();
 export
 const editorHandlerProvider: JupyterLabPlugin<IEditorTracker> = {
   id: 'jupyter.services.editor-handler',
-  requires: [IDocumentRegistry, IMainMenu, ICommandPalette],
+  requires: [IDocumentRegistry, IMainMenu, ICommandPalette, IStateDB],
   provides: IEditorTracker,
   activate: activateEditorHandler,
   autoStart: true
@@ -93,7 +102,7 @@ const editorHandlerProvider: JupyterLabPlugin<IEditorTracker> = {
 /**
  * Sets up the editor widget
  */
-function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mainMenu: IMainMenu, palette: ICommandPalette): IEditorTracker {
+function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mainMenu: IMainMenu, palette: ICommandPalette, state: IStateDB): IEditorTracker {
   let widgetFactory = new EditorWidgetFactory({
     name: 'Editor',
     fileExtensions: ['*'],
@@ -107,6 +116,17 @@ function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mai
 
   widgetFactory.widgetCreated.connect((sender, widget) => {
     widget.title.icon = `${PORTRAIT_ICON_CLASS} ${EDITOR_ICON_CLASS}`;
+    // Add the file path to the state database.
+    let key = `${NAMESPACE}:${widget.context.path}`;
+    state.save(key, widget.context.path);
+    // Remove the file path from the state database on disposal.
+    widget.disposed.connect(() => { state.remove(key); });
+    // Keep track of path changes in the state database.
+    widget.context.pathChanged.connect((sender, path) => {
+      state.remove(key);
+      key = `${NAMESPACE}:${path}`;
+      state.save(key, path);
+    });
     tracker.add(widget);
   });
   registry.addWidgetFactory(widgetFactory);
@@ -190,6 +210,13 @@ function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mai
     cmdIds.runCode,
   ].forEach(command => palette.addItem({ command, category: 'Editor' }));
 
+  // Reload any editor widgets whose state has been stored.
+  Promise.all([state.fetchNamespace(NAMESPACE), app.started])
+    .then(([paths]) => {
+      let open = 'file-operations:open';
+      paths.forEach(path => { app.commands.execute(open, { path }); });
+    });
+
   return tracker;
 }