forked from jupyterlab/jupyterlab-toc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.ts
152 lines (137 loc) · 3.98 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ILabShell,
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { IEditorTracker } from '@jupyterlab/fileeditor';
import { IMarkdownViewerTracker } from '@jupyterlab/markdownviewer';
import { INotebookTracker } from '@jupyterlab/notebook';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { TableOfContents } from './toc';
import {
createLatexGenerator,
createNotebookGenerator,
createMarkdownGenerator,
createPythonGenerator,
createRenderedMarkdownGenerator
} from './generators';
import {
ITableOfContentsRegistry,
TableOfContentsRegistry as Registry
} from './registry';
import '../style/index.css';
/**
* Activates the ToC extension.
*
* @private
* @param app - Jupyter application
* @param docmanager - document manager
* @param editorTracker - editor tracker
* @param labShell - Jupyter lab shell
* @param restorer - application layout restorer
* @param markdownViewerTracker - Markdown viewer tracker
* @param notebookTracker - notebook tracker
* @param rendermime - rendered MIME registry
* @returns table of contents registry
*/
function activateTOC(
app: JupyterFrontEnd,
docmanager: IDocumentManager,
editorTracker: IEditorTracker,
labShell: ILabShell,
restorer: ILayoutRestorer,
markdownViewerTracker: IMarkdownViewerTracker,
notebookTracker: INotebookTracker,
rendermime: IRenderMimeRegistry
): ITableOfContentsRegistry {
// Create the ToC widget:
const toc = new TableOfContents({ docmanager, rendermime });
// Create the ToC registry:
const registry = new Registry();
// Add the ToC to the left area:
toc.title.iconClass = 'jp-TableOfContents-icon jp-SideBar-tabIcon';
toc.title.caption = 'Table of Contents';
toc.id = 'table-of-contents';
labShell.add(toc, 'left', { rank: 700 });
// Add the ToC widget to the application restorer:
restorer.add(toc, 'juputerlab-toc');
// Create a notebook generator:
const notebookGenerator = createNotebookGenerator(
notebookTracker,
toc,
rendermime.sanitizer
);
registry.add(notebookGenerator);
// Create a Markdown generator:
const markdownGenerator = createMarkdownGenerator(
editorTracker,
toc,
rendermime.sanitizer
);
registry.add(markdownGenerator);
// Create a rendered Markdown generator:
const renderedMarkdownGenerator = createRenderedMarkdownGenerator(
markdownViewerTracker,
toc,
rendermime.sanitizer
);
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);
// Update the ToC when the active widget changes:
labShell.currentChanged.connect(onConnect);
return registry;
/**
* Callback invoked when the active widget changes.
*
* @private
*/
function onConnect() {
let widget = app.shell.currentWidget;
if (!widget) {
return;
}
let generator = registry.find(widget);
if (!generator) {
// If the previously used widget is still available, stick with it.
// Otherwise, set the current ToC widget to null.
if (toc.current && toc.current.widget.isDisposed) {
toc.current = null;
}
return;
}
toc.current = { widget, generator };
}
}
/**
* Initialization data for the ToC extension.
*
* @private
*/
const extension: JupyterFrontEndPlugin<ITableOfContentsRegistry> = {
id: 'jupyterlab-toc',
autoStart: true,
provides: ITableOfContentsRegistry,
requires: [
IDocumentManager,
IEditorTracker,
ILabShell,
ILayoutRestorer,
IMarkdownViewerTracker,
INotebookTracker,
IRenderMimeRegistry
],
activate: activateTOC
};
/**
* Exports.
*/
export default extension;