Browse Source

Backport PR #11445: Makes restorer parameter optional in toc-extension (#11460)

Frédéric Collonval 3 năm trước cách đây
mục cha
commit
88c8ad58cc
1 tập tin đã thay đổi với 57 bổ sung46 xóa
  1. 57 46
      packages/toc-extension/src/index.ts

+ 57 - 46
packages/toc-extension/src/index.ts

@@ -45,26 +45,26 @@ namespace CommandIDs {
  * @private
  * @param app - Jupyter application
  * @param docmanager - document manager
+ * @param rendermime - rendered MIME registry
+ * @param translator - translator
  * @param editorTracker - editor tracker
- * @param labShell - Jupyter lab shell
  * @param restorer - application layout restorer
+ * @param labShell - Jupyter lab shell
  * @param markdownViewerTracker - Markdown viewer tracker
  * @param notebookTracker - notebook tracker
- * @param rendermime - rendered MIME registry
- * @param translator - translator
  * @param settingRegistry - setting registry
  * @returns table of contents registry
  */
 async function activateTOC(
   app: JupyterFrontEnd,
   docmanager: IDocumentManager,
-  editorTracker: IEditorTracker,
-  restorer: ILayoutRestorer,
-  markdownViewerTracker: IMarkdownViewerTracker,
-  notebookTracker: INotebookTracker,
   rendermime: IRenderMimeRegistry,
   translator: ITranslator,
+  editorTracker?: IEditorTracker,
+  restorer?: ILayoutRestorer,
   labShell?: ILabShell,
+  markdownViewerTracker?: IMarkdownViewerTracker,
+  notebookTracker?: INotebookTracker,
   settingRegistry?: ISettingRegistry
 ): Promise<ITableOfContentsRegistry> {
   const trans = translator.load('jupyterlab');
@@ -89,6 +89,10 @@ async function activateTOC(
 
   app.commands.addCommand(CommandIDs.runCells, {
     execute: args => {
+      if (!notebookTracker) {
+        return null;
+      }
+
       const panel = notebookTracker.currentWidget;
       if (panel == null) {
         return;
@@ -121,8 +125,10 @@ async function activateTOC(
     label: trans.__('Run Cell(s)')
   });
 
-  // Add the ToC widget to the application restorer:
-  restorer.add(toc, '@jupyterlab/toc:plugin');
+  if (restorer) {
+    // Add the ToC widget to the application restorer:
+    restorer.add(toc, '@jupyterlab/toc:plugin');
+  }
 
   // Attempt to load plugin settings:
   let settings: ISettingRegistry.ISettings | undefined;
@@ -137,42 +143,48 @@ async function activateTOC(
   }
 
   // Create a notebook generator:
-  const notebookGenerator = createNotebookGenerator(
-    notebookTracker,
-    toc,
-    rendermime.sanitizer,
-    translator,
-    settings
-  );
-  registry.add(notebookGenerator);
+  if (notebookTracker) {
+    const notebookGenerator = createNotebookGenerator(
+      notebookTracker,
+      toc,
+      rendermime.sanitizer,
+      translator,
+      settings
+    );
+    registry.add(notebookGenerator);
+  }
 
   // Create a Markdown generator:
-  const markdownGenerator = createMarkdownGenerator(
-    editorTracker,
-    toc,
-    rendermime.sanitizer,
-    translator,
-    settings
-  );
-  registry.add(markdownGenerator);
+  if (editorTracker) {
+    const markdownGenerator = createMarkdownGenerator(
+      editorTracker,
+      toc,
+      rendermime.sanitizer,
+      translator,
+      settings
+    );
+    registry.add(markdownGenerator);
+
+    // Create a LaTeX generator:
+    const latexGenerator = createLatexGenerator(editorTracker);
+    registry.add(latexGenerator);
+
+    // Create a Python generator:
+    const pythonGenerator = createPythonGenerator(editorTracker);
+    registry.add(pythonGenerator);
+  }
 
   // Create a rendered Markdown generator:
-  const renderedMarkdownGenerator = createRenderedMarkdownGenerator(
-    markdownViewerTracker,
-    toc,
-    rendermime.sanitizer,
-    translator,
-    settings
-  );
-  registry.add(renderedMarkdownGenerator);
-
-  // Create a LaTeX generator:
-  const latexGenerator = createLatexGenerator(editorTracker);
-  registry.add(latexGenerator);
-
-  // Create a Python generator:
-  const pythonGenerator = createPythonGenerator(editorTracker);
-  registry.add(pythonGenerator);
+  if (markdownViewerTracker) {
+    const renderedMarkdownGenerator = createRenderedMarkdownGenerator(
+      markdownViewerTracker,
+      toc,
+      rendermime.sanitizer,
+      translator,
+      settings
+    );
+    registry.add(renderedMarkdownGenerator);
+  }
 
   // Update the ToC when the active widget changes:
   if (labShell) {
@@ -213,16 +225,15 @@ const extension: JupyterFrontEndPlugin<ITableOfContentsRegistry> = {
   id: '@jupyterlab/toc:plugin',
   autoStart: true,
   provides: ITableOfContentsRegistry,
-  requires: [
-    IDocumentManager,
+  requires: [IDocumentManager, IRenderMimeRegistry, ITranslator],
+  optional: [
     IEditorTracker,
     ILayoutRestorer,
+    ILabShell,
     IMarkdownViewerTracker,
     INotebookTracker,
-    IRenderMimeRegistry,
-    ITranslator
+    ISettingRegistry
   ],
-  optional: [ILabShell, ISettingRegistry],
   activate: activateTOC
 };