UNPKG

5.39 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { WidgetTracker } from '@jupyterlab/apputils';
4import { MimeDocumentFactory } from '@jupyterlab/docregistry';
5import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
6import { ITranslator } from '@jupyterlab/translation';
7import { LabIcon } from '@jupyterlab/ui-components';
8import { Token } from '@lumino/coreutils';
9import { AttachedProperty } from '@lumino/properties';
10import { ILayoutRestorer } from './layoutrestorer';
11/**
12 * The mime document tracker token.
13 */
14export const IMimeDocumentTracker = new Token('@jupyterlab/application:IMimeDocumentTracker');
15/**
16 * Create rendermime plugins for rendermime extension modules.
17 */
18export 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 // Handle CommonJS exports.
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 // Also add a meta-plugin handling state restoration
36 // and exposing the mime document widget tracker.
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 * Create rendermime plugins for rendermime extension modules.
60 */
61export function createRendermimePlugin(tracker, item) {
62 return {
63 id: item.id,
64 requires: [IRenderMimeRegistry, ITranslator],
65 autoStart: true,
66 activate: (app, rendermime, translator) => {
67 // Add the mime renderer.
68 if (item.rank !== undefined) {
69 rendermime.addFactory(item.rendererFactory, item.rank);
70 }
71 else {
72 rendermime.addFactory(item.rendererFactory);
73 }
74 // Handle the widget factory.
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 // upconvert the contents of the icon field to a proper LabIcon
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 // Notify the widget tracker if restore data needs to update.
119 widget.context.pathChanged.connect(() => {
120 void tracker.save(widget);
121 });
122 void tracker.add(widget);
123 });
124 });
125 }
126 };
127}
128/**
129 * Private namespace for the module.
130 */
131var Private;
132(function (Private) {
133 /**
134 * An attached property for keeping the factory name
135 * that was used to create a mimedocument.
136 */
137 Private.factoryNameProperty = new AttachedProperty({
138 name: 'factoryName',
139 create: () => undefined
140 });
141})(Private || (Private = {}));
142//# sourceMappingURL=mimerenderers.js.map
\No newline at end of file