-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
index.ts
125 lines (105 loc) · 3.07 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/**
* @packageDocumentation
* @module vdom-extension
*/
import {
JupyterFrontEnd,
JupyterFrontEndPlugin,
ILayoutRestorer
} from '@jupyterlab/application';
import { WidgetTracker } from '@jupyterlab/apputils';
import { MimeDocumentFactory, MimeDocument } from '@jupyterlab/docregistry';
import { INotebookTracker } from '@jupyterlab/notebook';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { reactIcon } from '@jupyterlab/ui-components';
import { RenderedVDOM, IVDOMTracker } from '@jupyterlab/vdom';
/**
* The MIME type for VDOM.
*/
export const MIME_TYPE = 'application/vdom.v1+json';
/**
* The name of the factory that creates VDOM widgets.
*/
const FACTORY_NAME = 'VDOM';
const plugin: JupyterFrontEndPlugin<IVDOMTracker> = {
id: '@jupyterlab/vdom-extension:factory',
requires: [IRenderMimeRegistry],
optional: [INotebookTracker, ILayoutRestorer],
provides: IVDOMTracker,
autoStart: true,
activate: (
app: JupyterFrontEnd,
rendermime: IRenderMimeRegistry,
notebooks: INotebookTracker | null,
restorer: ILayoutRestorer | null
) => {
const tracker = new WidgetTracker<MimeDocument>({
namespace: 'vdom-widget'
});
// Add a renderer factory to application rendermime registry.
rendermime.addFactory(
{
safe: false,
mimeTypes: [MIME_TYPE],
createRenderer: options => new RenderedVDOM(options)
},
0
);
if (notebooks) {
notebooks.widgetAdded.connect((sender, panel) => {
// Get the notebook's context and rendermime;
const {
context,
content: { rendermime }
} = panel;
// Add the renderer factory to the notebook's rendermime registry;
rendermime.addFactory(
{
safe: false,
mimeTypes: [MIME_TYPE],
createRenderer: options => new RenderedVDOM(options, context)
},
0
);
});
}
app.docRegistry.addFileType({
name: 'vdom',
mimeTypes: [MIME_TYPE],
extensions: ['.vdom', '.vdom.json'],
icon: reactIcon
});
const factory = new MimeDocumentFactory({
renderTimeout: 1000,
dataType: 'json',
rendermime,
name: FACTORY_NAME,
primaryFileType: app.docRegistry.getFileType('vdom')!,
fileTypes: ['vdom', 'json'],
defaultFor: ['vdom']
});
factory.widgetCreated.connect((sender, widget) => {
widget.context.pathChanged.connect(() => {
void tracker.save(widget);
});
void tracker.add(widget);
});
// Add widget factory to document registry.
app.docRegistry.addWidgetFactory(factory);
if (restorer) {
// Handle state restoration.
void restorer.restore(tracker, {
command: 'docmanager:open',
args: widget => ({
path: widget.context.path,
factory: FACTORY_NAME
}),
name: widget => widget.context.path
});
}
return tracker;
}
};
export default plugin;