소스 검색

Refractor updating notebook settings for opened widgets

Frederic Collonval 6 년 전
부모
커밋
f881c5b109
2개의 변경된 파일58개의 추가작업 그리고 25개의 파일을 삭제
  1. 23 24
      packages/notebook-extension/src/index.ts
  2. 35 1
      packages/notebook/src/panel.ts

+ 23 - 24
packages/notebook-extension/src/index.ts

@@ -516,9 +516,7 @@ function activateNotebookHandler(
   settingRegistry: ISettingRegistry | null
 ): INotebookTracker {
   const services = app.serviceManager;
-  // An object for tracking the current notebook settings.
-  let editorConfig = StaticNotebook.defaultEditorConfig;
-  let notebookConfig = StaticNotebook.defaultNotebookConfig;
+
   const factory = new NotebookWidgetFactory({
     name: FACTORY,
     fileTypes: ['notebook'],
@@ -528,8 +526,8 @@ function activateNotebookHandler(
     canStartKernel: true,
     rendermime: rendermime,
     contentFactory,
-    editorConfig,
-    notebookConfig,
+    editorConfig: StaticNotebook.defaultEditorConfig,
+    notebookConfig: StaticNotebook.defaultNotebookConfig,
     mimeTypeService: editorServices.mimeTypeService
   });
   const { commands } = app;
@@ -582,6 +580,15 @@ function activateNotebookHandler(
     void tracker.add(widget);
   });
 
+  /**
+   * Update the settings of the current tracker.
+   */
+  function updateTracker(options: NotebookPanel.IConfig): void {
+    tracker.forEach(widget => {
+      widget.setConfig(options);
+    });
+  }
+
   /**
    * Update the setting values.
    */
@@ -616,27 +623,17 @@ function activateNotebookHandler(
           ? raw[key]
           : cached[key];
     });
-    factory.editorConfig = editorConfig = { code, markdown, raw };
-    factory.notebookConfig = notebookConfig = {
+    factory.editorConfig = { code, markdown, raw };
+    factory.notebookConfig = {
       scrollPastEnd: settings.get('scrollPastEnd').composite as boolean
     };
     factory.shutdownOnClose = settings.get('kernelShutdown')
       .composite as boolean;
-  }
 
-  /**
-   * Update the settings of the current tracker.
-   */
-  function updateTracker(settings: ISettingRegistry.ISettings | null): void {
-    tracker.forEach(widget => {
-      widget.content.editorConfig = editorConfig;
-      widget.content.notebookConfig = notebookConfig;
-      // Update kernel shutdown behavior
-      const kernelPreference = widget.context.session.kernelPreference;
-      widget.context.session.kernelPreference = {
-        ...kernelPreference,
-        shutdownOnClose: factory.shutdownOnClose
-      };
+    updateTracker({
+      editorConfig: factory.editorConfig,
+      notebookConfig: factory.notebookConfig,
+      kernelShutdown: factory.shutdownOnClose
     });
   }
 
@@ -648,15 +645,17 @@ function activateNotebookHandler(
     .then(() => fetchSettings)
     .then(settings => {
       updateConfig(settings);
-      updateTracker(settings);
       settings.changed.connect(() => {
         updateConfig(settings);
-        updateTracker(settings);
       });
     })
     .catch((reason: Error) => {
       console.warn(reason.message);
-      updateTracker(null);
+      updateTracker({
+        editorConfig: factory.editorConfig,
+        notebookConfig: factory.notebookConfig,
+        kernelShutdown: factory.shutdownOnClose
+      });
     });
 
   // Add main menu notebook menu.

+ 35 - 1
packages/notebook/src/panel.ts

@@ -17,7 +17,7 @@ import { RenderMimeRegistry } from '@jupyterlab/rendermime';
 
 import { INotebookModel } from './model';
 
-import { Notebook } from './widget';
+import { Notebook, StaticNotebook } from './widget';
 
 /**
  * The class name added to notebook panels.
@@ -108,6 +108,22 @@ export class NotebookPanel extends DocumentWidget<Notebook, INotebookModel> {
     return this.content ? this.content.model : null;
   }
 
+  /**
+   * Update the options for the current notebook panel.
+   *
+   * @param config new options to set
+   */
+  setConfig(config: NotebookPanel.IConfig): void {
+    this.content.editorConfig = config.editorConfig;
+    this.content.notebookConfig = config.notebookConfig;
+    // Update kernel shutdown behavior
+    const kernelPreference = this.context.session.kernelPreference;
+    this.context.session.kernelPreference = {
+      ...kernelPreference,
+      shutdownOnClose: config.kernelShutdown
+    };
+  }
+
   /**
    * Set URI fragment identifier.
    */
@@ -184,6 +200,24 @@ export class NotebookPanel extends DocumentWidget<Notebook, INotebookModel> {
  * A namespace for `NotebookPanel` statics.
  */
 export namespace NotebookPanel {
+  /**
+   * Notebook config interface for NotebookPanel
+   */
+  export interface IConfig {
+    /**
+     * A config object for cell editors
+     */
+    editorConfig: StaticNotebook.IEditorConfig;
+    /**
+     * A config object for notebook widget
+     */
+    notebookConfig: StaticNotebook.INotebookConfig;
+    /**
+     * Whether to shut down the kernel when closing the panel or not
+     */
+    kernelShutdown: boolean;
+  }
+
   /**
    * A content factory interface for NotebookPanel.
    */