1 |
|
2 |
|
3 |
|
4 | import { IEditorMimeTypeService } from '@jupyterlab/codeeditor';
|
5 | import { ABCWidgetFactory, DocumentRegistry } from '@jupyterlab/docregistry';
|
6 | import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
|
7 | import { ITranslator } from '@jupyterlab/translation';
|
8 | import { INotebookModel } from './model';
|
9 | import { NotebookPanel } from './panel';
|
10 | import { StaticNotebook } from './widget';
|
11 | import { NotebookHistory } from './history';
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export class NotebookWidgetFactory extends ABCWidgetFactory<
|
17 | NotebookPanel,
|
18 | INotebookModel
|
19 | > {
|
20 | |
21 |
|
22 |
|
23 |
|
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 |
|
38 |
|
39 | readonly rendermime: IRenderMimeRegistry;
|
40 |
|
41 | |
42 |
|
43 |
|
44 | readonly contentFactory: NotebookPanel.IContentFactory;
|
45 |
|
46 | |
47 |
|
48 |
|
49 | readonly mimeTypeService: IEditorMimeTypeService;
|
50 |
|
51 | |
52 |
|
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 |
|
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 |
|
73 |
|
74 |
|
75 |
|
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 |
|
110 |
|
111 | export namespace NotebookWidgetFactory {
|
112 | |
113 |
|
114 |
|
115 | export interface IOptions<T extends NotebookPanel>
|
116 | extends DocumentRegistry.IWidgetFactoryOptions<T> {
|
117 | |
118 |
|
119 |
|
120 | rendermime: IRenderMimeRegistry;
|
121 |
|
122 | |
123 |
|
124 |
|
125 | contentFactory: NotebookPanel.IContentFactory;
|
126 |
|
127 | |
128 |
|
129 |
|
130 | mimeTypeService: IEditorMimeTypeService;
|
131 |
|
132 | |
133 |
|
134 |
|
135 | editorConfig?: StaticNotebook.IEditorConfig;
|
136 |
|
137 | |
138 |
|
139 |
|
140 | notebookConfig?: StaticNotebook.INotebookConfig;
|
141 |
|
142 | |
143 |
|
144 |
|
145 | translator?: ITranslator;
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 | export interface IFactory
|
152 | extends DocumentRegistry.IWidgetFactory<NotebookPanel, INotebookModel> {
|
153 | |
154 |
|
155 |
|
156 | autoStartDefault: boolean;
|
157 |
|
158 | |
159 |
|
160 |
|
161 | editorConfig: StaticNotebook.IEditorConfig;
|
162 |
|
163 | |
164 |
|
165 |
|
166 | notebookConfig: StaticNotebook.INotebookConfig;
|
167 |
|
168 | |
169 |
|
170 |
|
171 | shutdownOnClose: boolean;
|
172 | }
|
173 | }
|