forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
247 lines (224 loc) · 6.46 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/**
* @packageDocumentation
* @module toc-extension
*/
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 { ISettingRegistry } from '@jupyterlab/settingregistry';
import {
createLatexGenerator,
createMarkdownGenerator,
createNotebookGenerator,
createPythonGenerator,
createRenderedMarkdownGenerator,
ITableOfContentsRegistry,
TableOfContentsRegistry as Registry,
TableOfContents
} from '@jupyterlab/toc';
import { ITranslator } from '@jupyterlab/translation';
import { tocIcon } from '@jupyterlab/ui-components';
import { INotebookHeading } from '@jupyterlab/toc';
import { CodeCell, MarkdownCell } from '@jupyterlab/cells';
/**
* The command IDs used by TOC item.
*/
namespace CommandIDs {
export const runCells = 'toc:run-cells';
}
/**
* Activates the ToC extension.
*
* @private
* @param app - Jupyter application
* @param docmanager - document manager
* @param rendermime - rendered MIME registry
* @param translator - translator
* @param editorTracker - editor tracker
* @param restorer - application layout restorer
* @param labShell - Jupyter lab shell
* @param markdownViewerTracker - Markdown viewer tracker
* @param notebookTracker - notebook tracker
* @param settingRegistry - setting registry
* @returns table of contents registry
*/
async function activateTOC(
app: JupyterFrontEnd,
docmanager: IDocumentManager,
rendermime: IRenderMimeRegistry,
translator: ITranslator,
editorTracker?: IEditorTracker,
restorer?: ILayoutRestorer,
labShell?: ILabShell,
markdownViewerTracker?: IMarkdownViewerTracker,
notebookTracker?: INotebookTracker,
settingRegistry?: ISettingRegistry
): Promise<ITableOfContentsRegistry> {
const trans = translator.load('jupyterlab');
// Create the ToC widget:
const toc = new TableOfContents({
docmanager,
rendermime,
translator
});
// Create the ToC registry:
const registry = new Registry();
// Add the ToC to the left area:
toc.title.icon = tocIcon;
toc.title.caption = trans.__('Table of Contents');
toc.id = 'table-of-contents';
toc.node.setAttribute('role', 'region');
toc.node.setAttribute('aria-label', trans.__('Table of Contents section'));
app.shell.add(toc, 'left', { rank: 400 });
app.commands.addCommand(CommandIDs.runCells, {
execute: args => {
if (!notebookTracker) {
return null;
}
const panel = notebookTracker.currentWidget;
if (panel == null) {
return;
}
const cells = panel.content.widgets;
if (cells === undefined) {
return;
}
const activeCell = (toc.activeEntry as INotebookHeading).cellRef;
if (activeCell instanceof MarkdownCell) {
let level = activeCell.headingInfo.level;
for (let i = cells.indexOf(activeCell) + 1; i < cells.length; i++) {
const cell = cells[i];
if (
cell instanceof MarkdownCell &&
cell.headingInfo.level <= level &&
cell.headingInfo.level > -1
) {
break;
}
if (cell instanceof CodeCell) {
void CodeCell.execute(cell, panel.sessionContext);
}
}
} else {
if (activeCell instanceof CodeCell) {
void CodeCell.execute(activeCell, panel.sessionContext);
}
}
},
label: trans.__('Run Cell(s)')
});
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;
if (settingRegistry) {
try {
settings = await settingRegistry.load('@jupyterlab/toc-extension:plugin');
} catch (error) {
console.error(
`Failed to load settings for the Table of Contents extension.\n\n${error}`
);
}
}
// Create a notebook generator:
if (notebookTracker) {
const notebookGenerator = createNotebookGenerator(
notebookTracker,
toc,
rendermime.sanitizer,
translator,
settings
);
registry.add(notebookGenerator);
}
// Create a Markdown generator:
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:
if (markdownViewerTracker) {
const renderedMarkdownGenerator = createRenderedMarkdownGenerator(
markdownViewerTracker,
toc,
rendermime.sanitizer,
translator,
settings
);
registry.add(renderedMarkdownGenerator);
}
// Update the ToC when the active widget changes:
if (labShell) {
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:plugin',
autoStart: true,
provides: ITableOfContentsRegistry,
requires: [IDocumentManager, IRenderMimeRegistry, ITranslator],
optional: [
IEditorTracker,
ILayoutRestorer,
ILabShell,
IMarkdownViewerTracker,
INotebookTracker,
ISettingRegistry
],
activate: activateTOC
};
/**
* Exports.
*/
export default extension;