Skip to content

Commit

Permalink
Change setting JSON structure and way of loading settings
Browse files Browse the repository at this point in the history
Makes the JSON for schema/plugin.json to be simpler. Changes the
way that settings are loaded to handle errors in loading.
  • Loading branch information
Martha Cryan committed May 6, 2020
1 parent 5b7e351 commit ecf1b10
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
18 changes: 7 additions & 11 deletions schema/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 18 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 });

Expand All @@ -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(
Expand Down
9 changes: 7 additions & 2 deletions src/generators/notebook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ecf1b10

Please sign in to comment.