-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
mimerenderers.ts
176 lines (153 loc) · 5.05 KB
/
mimerenderers.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IWidgetTracker, WidgetTracker } from '@jupyterlab/apputils';
import {
MimeDocumentFactory,
DocumentRegistry,
MimeDocument
} from '@jupyterlab/docregistry';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { Token } from '@lumino/coreutils';
import { AttachedProperty } from '@lumino/properties';
import { JupyterFrontEnd, JupyterFrontEndPlugin } from './index';
import { ILayoutRestorer } from './layoutrestorer';
/**
* A class that tracks mime documents.
*/
export interface IMimeDocumentTracker extends IWidgetTracker<MimeDocument> {}
/* tslint:disable */
/**
* The mime document tracker token.
*/
export const IMimeDocumentTracker = new Token<IMimeDocumentTracker>(
'@jupyterlab/application:IMimeDocumentTracker'
);
/* tslint:enable */
/**
* Create rendermime plugins for rendermime extension modules.
*/
export function createRendermimePlugins(
extensions: IRenderMime.IExtensionModule[]
): JupyterFrontEndPlugin<void | IMimeDocumentTracker>[] {
const plugins: JupyterFrontEndPlugin<void | IMimeDocumentTracker>[] = [];
const namespace = 'application-mimedocuments';
const tracker = new WidgetTracker<MimeDocument>({ namespace });
extensions.forEach(mod => {
let data = mod.default;
// Handle CommonJS exports.
if (!mod.hasOwnProperty('__esModule')) {
data = mod as any;
}
if (!Array.isArray(data)) {
data = [data] as ReadonlyArray<IRenderMime.IExtension>;
}
(data as ReadonlyArray<IRenderMime.IExtension>).forEach(item => {
plugins.push(createRendermimePlugin(tracker, item));
});
});
// Also add a meta-plugin handling state restoration
// and exposing the mime document widget tracker.
plugins.push({
id: '@jupyterlab/application:mimedocument',
optional: [ILayoutRestorer],
provides: IMimeDocumentTracker,
autoStart: true,
activate: (app: JupyterFrontEnd, restorer: ILayoutRestorer | null) => {
if (restorer) {
void restorer.restore(tracker, {
command: 'docmanager:open',
args: widget => ({
path: widget.context.path,
factory: Private.factoryNameProperty.get(widget)
}),
name: widget =>
`${widget.context.path}:${Private.factoryNameProperty.get(widget)}`
});
}
return tracker;
}
});
return plugins;
}
/**
* Create rendermime plugins for rendermime extension modules.
*/
export function createRendermimePlugin(
tracker: WidgetTracker<MimeDocument>,
item: IRenderMime.IExtension
): JupyterFrontEndPlugin<void> {
return {
id: item.id,
requires: [IRenderMimeRegistry],
autoStart: true,
activate: (app: JupyterFrontEnd, rendermime: IRenderMimeRegistry) => {
// Add the mime renderer.
if (item.rank !== undefined) {
rendermime.addFactory(item.rendererFactory, item.rank);
} else {
rendermime.addFactory(item.rendererFactory);
}
// Handle the widget factory.
if (!item.documentWidgetFactoryOptions) {
return;
}
let registry = app.docRegistry;
let options: IRenderMime.IDocumentWidgetFactoryOptions[] = [];
if (Array.isArray(item.documentWidgetFactoryOptions)) {
options = item.documentWidgetFactoryOptions;
} else {
options = [
item.documentWidgetFactoryOptions as IRenderMime.IDocumentWidgetFactoryOptions
];
}
if (item.fileTypes) {
item.fileTypes.forEach(ft => {
app.docRegistry.addFileType(ft as DocumentRegistry.IFileType);
});
}
options.forEach(option => {
const toolbarFactory = option.toolbarFactory
? (w: MimeDocument) => option.toolbarFactory!(w.content.renderer)
: undefined;
let factory = new MimeDocumentFactory({
renderTimeout: item.renderTimeout,
dataType: item.dataType,
rendermime,
modelName: option.modelName,
name: option.name,
primaryFileType: registry.getFileType(option.primaryFileType),
fileTypes: option.fileTypes,
defaultFor: option.defaultFor,
defaultRendered: option.defaultRendered,
toolbarFactory
});
registry.addWidgetFactory(factory);
factory.widgetCreated.connect((sender, widget) => {
Private.factoryNameProperty.set(widget, factory.name);
// Notify the widget tracker if restore data needs to update.
widget.context.pathChanged.connect(() => {
void tracker.save(widget);
});
void tracker.add(widget);
});
});
}
};
}
/**
* Private namespace for the module.
*/
namespace Private {
/**
* An attached property for keeping the factory name
* that was used to create a mimedocument.
*/
export const factoryNameProperty = new AttachedProperty<
MimeDocument,
string | undefined
>({
name: 'factoryName',
create: () => undefined
});
}