1 |
|
2 |
|
3 | import { WidgetTracker } from '@jupyterlab/apputils';
|
4 | import { MimeDocumentFactory } from '@jupyterlab/docregistry';
|
5 | import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
|
6 | import { ITranslator } from '@jupyterlab/translation';
|
7 | import { LabIcon } from '@jupyterlab/ui-components';
|
8 | import { Token } from '@lumino/coreutils';
|
9 | import { AttachedProperty } from '@lumino/properties';
|
10 | import { ILayoutRestorer } from './layoutrestorer';
|
11 |
|
12 |
|
13 |
|
14 | export const IMimeDocumentTracker = new Token('@jupyterlab/application:IMimeDocumentTracker');
|
15 |
|
16 |
|
17 |
|
18 | export function createRendermimePlugins(extensions) {
|
19 | const plugins = [];
|
20 | const namespace = 'application-mimedocuments';
|
21 | const tracker = new WidgetTracker({ namespace });
|
22 | extensions.forEach(mod => {
|
23 | let data = mod.default;
|
24 |
|
25 | if (!mod.hasOwnProperty('__esModule')) {
|
26 | data = mod;
|
27 | }
|
28 | if (!Array.isArray(data)) {
|
29 | data = [data];
|
30 | }
|
31 | data.forEach(item => {
|
32 | plugins.push(createRendermimePlugin(tracker, item));
|
33 | });
|
34 | });
|
35 |
|
36 |
|
37 | plugins.push({
|
38 | id: '@jupyterlab/application:mimedocument',
|
39 | optional: [ILayoutRestorer],
|
40 | provides: IMimeDocumentTracker,
|
41 | autoStart: true,
|
42 | activate: (app, restorer) => {
|
43 | if (restorer) {
|
44 | void restorer.restore(tracker, {
|
45 | command: 'docmanager:open',
|
46 | args: widget => ({
|
47 | path: widget.context.path,
|
48 | factory: Private.factoryNameProperty.get(widget)
|
49 | }),
|
50 | name: widget => `${widget.context.path}:${Private.factoryNameProperty.get(widget)}`
|
51 | });
|
52 | }
|
53 | return tracker;
|
54 | }
|
55 | });
|
56 | return plugins;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | export function createRendermimePlugin(tracker, item) {
|
62 | return {
|
63 | id: item.id,
|
64 | requires: [IRenderMimeRegistry, ITranslator],
|
65 | autoStart: true,
|
66 | activate: (app, rendermime, translator) => {
|
67 |
|
68 | if (item.rank !== undefined) {
|
69 | rendermime.addFactory(item.rendererFactory, item.rank);
|
70 | }
|
71 | else {
|
72 | rendermime.addFactory(item.rendererFactory);
|
73 | }
|
74 |
|
75 | if (!item.documentWidgetFactoryOptions) {
|
76 | return;
|
77 | }
|
78 | const registry = app.docRegistry;
|
79 | let options = [];
|
80 | if (Array.isArray(item.documentWidgetFactoryOptions)) {
|
81 | options = item.documentWidgetFactoryOptions;
|
82 | }
|
83 | else {
|
84 | options = [
|
85 | item.documentWidgetFactoryOptions
|
86 | ];
|
87 | }
|
88 | if (item.fileTypes) {
|
89 | item.fileTypes.forEach(ft => {
|
90 | if (ft.icon) {
|
91 |
|
92 | ft = Object.assign(Object.assign({}, ft), { icon: LabIcon.resolve({ icon: ft.icon }) });
|
93 | }
|
94 | app.docRegistry.addFileType(ft);
|
95 | });
|
96 | }
|
97 | options.forEach(option => {
|
98 | const toolbarFactory = option.toolbarFactory
|
99 | ? (w) => option.toolbarFactory(w.content.renderer)
|
100 | : undefined;
|
101 | const factory = new MimeDocumentFactory({
|
102 | renderTimeout: item.renderTimeout,
|
103 | dataType: item.dataType,
|
104 | rendermime,
|
105 | modelName: option.modelName,
|
106 | name: option.name,
|
107 | primaryFileType: registry.getFileType(option.primaryFileType),
|
108 | fileTypes: option.fileTypes,
|
109 | defaultFor: option.defaultFor,
|
110 | defaultRendered: option.defaultRendered,
|
111 | toolbarFactory,
|
112 | translator,
|
113 | factory: item.rendererFactory
|
114 | });
|
115 | registry.addWidgetFactory(factory);
|
116 | factory.widgetCreated.connect((sender, widget) => {
|
117 | Private.factoryNameProperty.set(widget, factory.name);
|
118 |
|
119 | widget.context.pathChanged.connect(() => {
|
120 | void tracker.save(widget);
|
121 | });
|
122 | void tracker.add(widget);
|
123 | });
|
124 | });
|
125 | }
|
126 | };
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 | var Private;
|
132 | (function (Private) {
|
133 | |
134 |
|
135 |
|
136 |
|
137 | Private.factoryNameProperty = new AttachedProperty({
|
138 | name: 'factoryName',
|
139 | create: () => undefined
|
140 | });
|
141 | })(Private || (Private = {}));
|
142 |
|
\ | No newline at end of file |