forked from jupyterlab/jupyterlab
/
index.ts
executable file
·153 lines (124 loc) · 4.16 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
// @ts-ignore
__webpack_public_path__ = URLExt.join(PageConfig.getBaseUrl(), 'example/');
import '@jupyterlab/application/style/index.css';
import '@jupyterlab/codemirror/style/index.css';
import '@jupyterlab/notebook/style/index.css';
import '@jupyterlab/theme-light-extension/style/index.css';
import '../index.css';
import { CommandRegistry } from '@lumino/commands';
import { CommandPalette, SplitPanel, Widget } from '@lumino/widgets';
import { ServiceManager } from '@jupyterlab/services';
import { MathJaxTypesetter } from '@jupyterlab/mathjax2';
import {
NotebookPanel,
NotebookWidgetFactory,
NotebookModelFactory
} from '@jupyterlab/notebook';
import {
CompleterModel,
Completer,
CompletionHandler,
KernelConnector
} from '@jupyterlab/completer';
import { editorServices } from '@jupyterlab/codemirror';
import { DocumentManager } from '@jupyterlab/docmanager';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import {
RenderMimeRegistry,
standardRendererFactories as initialFactories
} from '@jupyterlab/rendermime';
import { SetupCommands } from './commands';
function main(): void {
let manager = new ServiceManager();
void manager.ready.then(() => {
createApp(manager);
});
}
function createApp(manager: ServiceManager.IManager): void {
// Initialize the command registry with the bindings.
let commands = new CommandRegistry();
let useCapture = true;
// Setup the keydown listener for the document.
document.addEventListener(
'keydown',
event => {
commands.processKeydownEvent(event);
},
useCapture
);
let rendermime = new RenderMimeRegistry({
initialFactories: initialFactories,
latexTypesetter: new MathJaxTypesetter({
url: PageConfig.getOption('mathjaxUrl'),
config: PageConfig.getOption('mathjaxConfig')
})
});
let opener = {
open: (widget: Widget) => {
// Do nothing for sibling widgets for now.
}
};
let docRegistry = new DocumentRegistry();
let docManager = new DocumentManager({
registry: docRegistry,
manager,
opener
});
let mFactory = new NotebookModelFactory({});
let editorFactory = editorServices.factoryService.newInlineEditor;
let contentFactory = new NotebookPanel.ContentFactory({ editorFactory });
let wFactory = new NotebookWidgetFactory({
name: 'Notebook',
modelName: 'notebook',
fileTypes: ['notebook'],
defaultFor: ['notebook'],
preferKernel: true,
canStartKernel: true,
rendermime,
contentFactory,
mimeTypeService: editorServices.mimeTypeService
});
docRegistry.addModelFactory(mFactory);
docRegistry.addWidgetFactory(wFactory);
let notebookPath = PageConfig.getOption('notebookPath');
let nbWidget = docManager.open(notebookPath) as NotebookPanel;
let palette = new CommandPalette({ commands });
palette.addClass('notebookCommandPalette');
const editor =
nbWidget.content.activeCell && nbWidget.content.activeCell.editor;
const model = new CompleterModel();
const completer = new Completer({ editor, model });
const connector = new KernelConnector({
session: nbWidget.sessionContext.session
});
const handler = new CompletionHandler({ completer, connector });
// Set the handler's editor.
handler.editor = editor;
// Listen for active cell changes.
nbWidget.content.activeCellChanged.connect((sender, cell) => {
handler.editor = cell && cell.editor;
});
// Hide the widget when it first loads.
completer.hide();
let panel = new SplitPanel();
panel.id = 'main';
panel.orientation = 'horizontal';
panel.spacing = 0;
SplitPanel.setStretch(palette, 0);
SplitPanel.setStretch(nbWidget, 1);
panel.addWidget(palette);
panel.addWidget(nbWidget);
// Attach the panel to the DOM.
Widget.attach(panel, document.body);
Widget.attach(completer, document.body);
// Handle resize events.
window.addEventListener('resize', () => {
panel.update();
});
SetupCommands(commands, palette, nbWidget, handler);
console.log('Example started!');
}
window.addEventListener('load', main);