Parcourir la source

Merge pull request #6996 from vidartf/rendermime-opt-req

Make dependencies of rendermime extension optional
Steven Silvester il y a 5 ans
Parent
commit
6625c6493f
1 fichiers modifiés avec 40 ajouts et 36 suppressions
  1. 40 36
      packages/rendermime-extension/src/index.ts

+ 40 - 36
packages/rendermime-extension/src/index.ts

@@ -26,8 +26,8 @@ namespace CommandIDs {
  */
 const plugin: JupyterFrontEndPlugin<IRenderMimeRegistry> = {
   id: '@jupyterlab/rendermime-extension:plugin',
-  requires: [IDocumentManager],
-  optional: [ILatexTypesetter],
+  requires: [],
+  optional: [IDocumentManager, ILatexTypesetter],
   provides: IRenderMimeRegistry,
   activate: activate,
   autoStart: true
@@ -43,45 +43,49 @@ export default plugin;
  */
 function activate(
   app: JupyterFrontEnd,
-  docManager: IDocumentManager,
+  docManager: IDocumentManager | null,
   latexTypesetter: ILatexTypesetter | null
 ) {
-  app.commands.addCommand(CommandIDs.handleLink, {
-    label: 'Handle Local Link',
-    execute: args => {
-      const path = args['path'] as string | undefined | null;
-      const id = args['id'] as string | undefined | null;
-      if (!path) {
-        return;
-      }
-      // First check if the path exists on the server.
-      return docManager.services.contents
-        .get(path, { content: false })
-        .then(() => {
-          // Open the link with the default rendered widget factory,
-          // if applicable.
-          const factory = docManager.registry.defaultRenderedWidgetFactory(
-            path
-          );
-          const widget = docManager.openOrReveal(path, factory.name);
+  if (docManager) {
+    app.commands.addCommand(CommandIDs.handleLink, {
+      label: 'Handle Local Link',
+      execute: args => {
+        const path = args['path'] as string | undefined | null;
+        const id = args['id'] as string | undefined | null;
+        if (!path) {
+          return;
+        }
+        // First check if the path exists on the server.
+        return docManager.services.contents
+          .get(path, { content: false })
+          .then(() => {
+            // Open the link with the default rendered widget factory,
+            // if applicable.
+            const factory = docManager.registry.defaultRenderedWidgetFactory(
+              path
+            );
+            const widget = docManager.openOrReveal(path, factory.name);
 
-          // Handle the hash if one has been provided.
-          if (widget && id) {
-            widget.setFragment(id);
-          }
-        });
-    }
-  });
+            // Handle the hash if one has been provided.
+            if (widget && id) {
+              widget.setFragment(id);
+            }
+          });
+      }
+    });
+  }
   return new RenderMimeRegistry({
     initialFactories: standardRendererFactories,
-    linkHandler: {
-      handleLink: (node: HTMLElement, path: string, id?: string) => {
-        app.commandLinker.connectNode(node, CommandIDs.handleLink, {
-          path,
-          id
-        });
-      }
-    },
+    linkHandler: !docManager
+      ? null
+      : {
+          handleLink: (node: HTMLElement, path: string, id?: string) => {
+            app.commandLinker.connectNode(node, CommandIDs.handleLink, {
+              path,
+              id
+            });
+          }
+        },
     latexTypesetter
   });
 }