Parcourir la source

Change setting JSON structure and way of loading settings

Makes the JSON for schema/plugin.json to be simpler. Changes the
way that settings are loaded to handle errors in loading.
Martha Cryan il y a 5 ans
Parent
commit
bbae5c0ecb

+ 7 - 11
packages/toc/schema/plugin.json

@@ -2,22 +2,18 @@
   "title": "Table of Contents",
   "description": "Table of contents settings.",
   "definitions": {
-    "tocConfig": {
-      "properties": {
-        "collapsibleNotebooks": {
-          "type": "boolean"
-        }
+    "properties": {
+      "collapsibleNotebooks": {
+        "type": "boolean"
       }
     }
   },
   "properties": {
-    "tocConfig": {
+    "collapsibleNotebooks": {
       "title": "ToC Configuration",
-      "description": "The configuration for the table of contents.",
-      "$ref": "#/definitions/tocConfig",
-      "default": {
-        "collapsibleNotebooks": true
-      }
+      "description": "A setting for specifying whether to allow collapsing notebook cells.",
+      "$ref": "#/definitions",
+      "default": true
     }
   },
   "additionalProperties": false,

+ 18 - 16
packages/toc/src/extension.ts

@@ -14,8 +14,6 @@ import { INotebookTracker } from '@jupyterlab/notebook';
 import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
 import { ISettingRegistry } from '@jupyterlab/settingregistry';
 
-import { JSONObject } from '@lumino/coreutils';
-
 import { TableOfContents } from './toc';
 import {
   createLatexGenerator,
@@ -44,7 +42,7 @@ import '../style/index.css';
  * @param rendermime - rendered MIME registry
  * @returns table of contents registry
  */
-function activateTOC(
+async function activateTOC(
   app: JupyterFrontEnd,
   docmanager: IDocumentManager,
   editorTracker: IEditorTracker,
@@ -54,7 +52,7 @@ function activateTOC(
   notebookTracker: INotebookTracker,
   rendermime: IRenderMimeRegistry,
   settingRegistry: ISettingRegistry
-): ITableOfContentsRegistry {
+): Promise<ITableOfContentsRegistry> {
   // Create the ToC widget:
   const toc = new TableOfContents({ docmanager, rendermime });
 
@@ -71,19 +69,23 @@ function activateTOC(
   restorer.add(toc, 'juputerlab-toc');
 
   // Attempt to load plugin settings:
-  settingRegistry.load('@jupyterlab/toc:plugin').then(settings => {
-    const config = settings.get('tocConfig').composite as JSONObject;
-    const collapsibleNotebooks = config.collapsibleNotebooks as boolean;
-
-    // Create a notebook generator:
-    const notebookGenerator = createNotebookGenerator(
-      notebookTracker,
-      toc,
-      collapsibleNotebooks,
-      rendermime.sanitizer
+  let settings: ISettingRegistry.ISettings | undefined;
+  try {
+    settings = await settingRegistry.load('@jupyterlab/toc:plugin');
+  } catch (error) {
+    console.error(
+      'Failed to load settings for the Table of Contents extension.'
     );
-    registry.add(notebookGenerator);
-  });
+  }
+
+  // Create a notebook generator:
+  const notebookGenerator = createNotebookGenerator(
+    notebookTracker,
+    toc,
+    rendermime.sanitizer,
+    settings
+  );
+  registry.add(notebookGenerator);
 
   // Create a Markdown generator:
   const markdownGenerator = createMarkdownGenerator(

+ 7 - 2
packages/toc/src/generators/notebook/index.ts

@@ -4,6 +4,7 @@
 import { ISanitizer } from '@jupyterlab/apputils';
 import { CodeCell, CodeCellModel, MarkdownCell, Cell } from '@jupyterlab/cells';
 import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
+import { ISettingRegistry } from '@jupyterlab/settingregistry';
 import { TableOfContentsRegistry as Registry } from '../../registry';
 import { TableOfContents } from '../../toc';
 import { isMarkdown } from '../../utils/is_markdown';
@@ -31,9 +32,13 @@ import { toolbar } from './toolbar_generator';
 function createNotebookGenerator(
   tracker: INotebookTracker,
   widget: TableOfContents,
-  collapsibleNotebooks: boolean,
-  sanitizer: ISanitizer
+  sanitizer: ISanitizer,
+  settings?: ISettingRegistry.ISettings
 ): Registry.IGenerator<NotebookPanel> {
+  let collapsibleNotebooks = true;
+  if (settings) {
+    collapsibleNotebooks = settings.composite.collapsibleNotebooks as boolean;
+  }
   const options = new OptionsManager(widget, tracker, {
     numbering: false,
     collapsibleNotebooks: collapsibleNotebooks,