UNPKG

4.75 kBPlain TextView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3
4import { IEditorMimeTypeService } from '@jupyterlab/codeeditor';
5import { ABCWidgetFactory, DocumentRegistry } from '@jupyterlab/docregistry';
6import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
7import { ITranslator } from '@jupyterlab/translation';
8import { INotebookModel } from './model';
9import { NotebookPanel } from './panel';
10import { StaticNotebook } from './widget';
11import { NotebookHistory } from './history';
12
13/**
14 * A widget factory for notebook panels.
15 */
16export class NotebookWidgetFactory extends ABCWidgetFactory<
17 NotebookPanel,
18 INotebookModel
19> {
20 /**
21 * Construct a new notebook widget factory.
22 *
23 * @param options - The options used to construct the factory.
24 */
25 constructor(options: NotebookWidgetFactory.IOptions<NotebookPanel>) {
26 super(options);
27 this.rendermime = options.rendermime;
28 this.contentFactory = options.contentFactory;
29 this.mimeTypeService = options.mimeTypeService;
30 this._editorConfig =
31 options.editorConfig || StaticNotebook.defaultEditorConfig;
32 this._notebookConfig =
33 options.notebookConfig || StaticNotebook.defaultNotebookConfig;
34 }
35
36 /*
37 * The rendermime instance.
38 */
39 readonly rendermime: IRenderMimeRegistry;
40
41 /**
42 * The content factory used by the widget factory.
43 */
44 readonly contentFactory: NotebookPanel.IContentFactory;
45
46 /**
47 * The service used to look up mime types.
48 */
49 readonly mimeTypeService: IEditorMimeTypeService;
50
51 /**
52 * A configuration object for cell editor settings.
53 */
54 get editorConfig(): StaticNotebook.IEditorConfig {
55 return this._editorConfig;
56 }
57 set editorConfig(value: StaticNotebook.IEditorConfig) {
58 this._editorConfig = value;
59 }
60
61 /**
62 * A configuration object for notebook settings.
63 */
64 get notebookConfig(): StaticNotebook.INotebookConfig {
65 return this._notebookConfig;
66 }
67 set notebookConfig(value: StaticNotebook.INotebookConfig) {
68 this._notebookConfig = value;
69 }
70
71 /**
72 * Create a new widget.
73 *
74 * #### Notes
75 * The factory will start the appropriate kernel.
76 */
77 protected createNewWidget(
78 context: DocumentRegistry.IContext<INotebookModel>,
79 source?: NotebookPanel
80 ): NotebookPanel {
81 const translator = (context as any).translator;
82 const kernelHistory = new NotebookHistory({
83 sessionContext: context.sessionContext,
84 translator: translator
85 });
86 const nbOptions = {
87 rendermime: source
88 ? source.content.rendermime
89 : this.rendermime.clone({ resolver: context.urlResolver }),
90 contentFactory: this.contentFactory,
91 mimeTypeService: this.mimeTypeService,
92 editorConfig: source ? source.content.editorConfig : this._editorConfig,
93 notebookConfig: source
94 ? source.content.notebookConfig
95 : this._notebookConfig,
96 translator,
97 kernelHistory
98 };
99 const content = this.contentFactory.createNotebook(nbOptions);
100
101 return new NotebookPanel({ context, content });
102 }
103
104 private _editorConfig: StaticNotebook.IEditorConfig;
105 private _notebookConfig: StaticNotebook.INotebookConfig;
106}
107
108/**
109 * The namespace for `NotebookWidgetFactory` statics.
110 */
111export namespace NotebookWidgetFactory {
112 /**
113 * The options used to construct a `NotebookWidgetFactory`.
114 */
115 export interface IOptions<T extends NotebookPanel>
116 extends DocumentRegistry.IWidgetFactoryOptions<T> {
117 /*
118 * A rendermime instance.
119 */
120 rendermime: IRenderMimeRegistry;
121
122 /**
123 * A notebook panel content factory.
124 */
125 contentFactory: NotebookPanel.IContentFactory;
126
127 /**
128 * The service used to look up mime types.
129 */
130 mimeTypeService: IEditorMimeTypeService;
131
132 /**
133 * The notebook cell editor configuration.
134 */
135 editorConfig?: StaticNotebook.IEditorConfig;
136
137 /**
138 * The notebook configuration.
139 */
140 notebookConfig?: StaticNotebook.INotebookConfig;
141
142 /**
143 * The application language translator.
144 */
145 translator?: ITranslator;
146 }
147
148 /**
149 * The interface for a notebook widget factory.
150 */
151 export interface IFactory
152 extends DocumentRegistry.IWidgetFactory<NotebookPanel, INotebookModel> {
153 /**
154 * Whether to automatically start the preferred kernel.
155 */
156 autoStartDefault: boolean;
157
158 /**
159 * A configuration object for cell editor settings.
160 */
161 editorConfig: StaticNotebook.IEditorConfig;
162
163 /**
164 * A configuration object for notebook settings.
165 */
166 notebookConfig: StaticNotebook.INotebookConfig;
167
168 /**
169 * Whether the kernel should be shutdown when the widget is closed.
170 */
171 shutdownOnClose: boolean;
172 }
173}