浏览代码

Merge pull request #9876 from jtpio/optional-deps

Make the markdown plugin more reusable
Steven Silvester 4 年之前
父节点
当前提交
d7b7d39047
共有 1 个文件被更改,包括 31 次插入34 次删除
  1. 31 34
      packages/markdownviewer-extension/src/index.ts

+ 31 - 34
packages/markdownviewer-extension/src/index.ts

@@ -50,12 +50,8 @@ const plugin: JupyterFrontEndPlugin<IMarkdownViewerTracker> = {
   activate,
   id: '@jupyterlab/markdownviewer-extension:plugin',
   provides: IMarkdownViewerTracker,
-  requires: [
-    ILayoutRestorer,
-    IRenderMimeRegistry,
-    ISettingRegistry,
-    ITranslator
-  ],
+  requires: [IRenderMimeRegistry, ITranslator],
+  optional: [ILayoutRestorer, ISettingRegistry],
   autoStart: true
 };
 
@@ -64,10 +60,10 @@ const plugin: JupyterFrontEndPlugin<IMarkdownViewerTracker> = {
  */
 function activate(
   app: JupyterFrontEnd,
-  restorer: ILayoutRestorer,
   rendermime: IRenderMimeRegistry,
-  settingRegistry: ISettingRegistry,
-  translator: ITranslator
+  translator: ITranslator,
+  restorer: ILayoutRestorer | null,
+  settingRegistry: ISettingRegistry | null
 ): IMarkdownViewerTracker {
   const trans = translator.load('jupyterlab');
   const { commands, docRegistry } = app;
@@ -93,28 +89,27 @@ function activate(
     });
   }
 
-  /**
-   * Update the setting values.
-   */
-  function updateSettings(settings: ISettingRegistry.ISettings) {
-    config = settings.composite as Partial<MarkdownViewer.IConfig>;
-    tracker.forEach(widget => {
-      updateWidget(widget.content);
-    });
-  }
-
-  // Fetch the initial state of the settings.
-  settingRegistry
-    .load(plugin.id)
-    .then((settings: ISettingRegistry.ISettings) => {
-      settings.changed.connect(() => {
+  if (settingRegistry) {
+    const updateSettings = (settings: ISettingRegistry.ISettings) => {
+      config = settings.composite as Partial<MarkdownViewer.IConfig>;
+      tracker.forEach(widget => {
+        updateWidget(widget.content);
+      });
+    };
+
+    // Fetch the initial state of the settings.
+    settingRegistry
+      .load(plugin.id)
+      .then((settings: ISettingRegistry.ISettings) => {
+        settings.changed.connect(() => {
+          updateSettings(settings);
+        });
         updateSettings(settings);
+      })
+      .catch((reason: Error) => {
+        console.error(reason.message);
       });
-      updateSettings(settings);
-    })
-    .catch((reason: Error) => {
-      console.error(reason.message);
-    });
+  }
 
   // Register the MarkdownViewer factory.
   const factory = new MarkdownViewerFactory({
@@ -136,11 +131,13 @@ function activate(
   docRegistry.addWidgetFactory(factory);
 
   // Handle state restoration.
-  void restorer.restore(tracker, {
-    command: 'docmanager:open',
-    args: widget => ({ path: widget.context.path, factory: FACTORY }),
-    name: widget => widget.context.path
-  });
+  if (restorer) {
+    void restorer.restore(tracker, {
+      command: 'docmanager:open',
+      args: widget => ({ path: widget.context.path, factory: FACTORY }),
+      name: widget => widget.context.path
+    });
+  }
 
   commands.addCommand(CommandIDs.markdownPreview, {
     label: trans.__('Markdown Preview'),