1 |
|
2 |
|
3 | import { Dialog, Printing, showDialog } from '@jupyterlab/apputils';
|
4 | import { isMarkdownCellModel } from '@jupyterlab/cells';
|
5 | import { PageConfig } from '@jupyterlab/coreutils';
|
6 | import { DocumentWidget } from '@jupyterlab/docregistry';
|
7 | import { Token } from '@lumino/coreutils';
|
8 | import { Notebook } from './widget';
|
9 |
|
10 |
|
11 |
|
12 | const NOTEBOOK_PANEL_CLASS = 'jp-NotebookPanel';
|
13 | const NOTEBOOK_PANEL_TOOLBAR_CLASS = 'jp-NotebookPanel-toolbar';
|
14 | const NOTEBOOK_PANEL_NOTEBOOK_CLASS = 'jp-NotebookPanel-notebook';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export class NotebookPanel extends DocumentWidget {
|
23 | |
24 |
|
25 |
|
26 | constructor(options) {
|
27 | super(options);
|
28 | |
29 |
|
30 |
|
31 |
|
32 | this._autorestarting = false;
|
33 |
|
34 | this.addClass(NOTEBOOK_PANEL_CLASS);
|
35 | this.toolbar.addClass(NOTEBOOK_PANEL_TOOLBAR_CLASS);
|
36 | this.content.addClass(NOTEBOOK_PANEL_NOTEBOOK_CLASS);
|
37 |
|
38 | this.content.model = this.context.model;
|
39 | this.context.sessionContext.kernelChanged.connect(this._onKernelChanged, this);
|
40 | this.context.sessionContext.statusChanged.connect(this._onSessionStatusChanged, this);
|
41 |
|
42 | this.context.saveState.connect(this._onSave, this);
|
43 | void this.revealed.then(() => {
|
44 | if (this.isDisposed) {
|
45 |
|
46 | return;
|
47 | }
|
48 |
|
49 | if (this.content.widgets.length === 1) {
|
50 | const cellModel = this.content.widgets[0].model;
|
51 | if (cellModel.type === 'code' &&
|
52 | cellModel.sharedModel.getSource() === '') {
|
53 | this.content.mode = 'edit';
|
54 | }
|
55 | }
|
56 | });
|
57 | }
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | _onSave(sender, state) {
|
65 | if (state === 'started' && this.model) {
|
66 |
|
67 | for (const cell of this.model.cells) {
|
68 | if (isMarkdownCellModel(cell)) {
|
69 | for (const key of cell.attachments.keys) {
|
70 | if (!cell.sharedModel.getSource().includes(key)) {
|
71 | cell.attachments.remove(key);
|
72 | }
|
73 | }
|
74 | }
|
75 | }
|
76 | }
|
77 | }
|
78 | |
79 |
|
80 |
|
81 | get sessionContext() {
|
82 | return this.context.sessionContext;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | get model() {
|
88 | return this.content.model;
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 | setConfig(config) {
|
96 | this.content.editorConfig = config.editorConfig;
|
97 | this.content.notebookConfig = config.notebookConfig;
|
98 |
|
99 | const kernelPreference = this.context.sessionContext.kernelPreference;
|
100 | this.context.sessionContext.kernelPreference = {
|
101 | ...kernelPreference,
|
102 | shutdownOnDispose: config.kernelShutdown,
|
103 | autoStartDefault: config.autoStartDefault
|
104 | };
|
105 | }
|
106 | |
107 |
|
108 |
|
109 | setFragment(fragment) {
|
110 | void this.context.ready.then(() => {
|
111 | void this.content.setFragment(fragment);
|
112 | });
|
113 | }
|
114 | |
115 |
|
116 |
|
117 | dispose() {
|
118 | this.content.dispose();
|
119 | super.dispose();
|
120 | }
|
121 | |
122 |
|
123 |
|
124 | [Printing.symbol]() {
|
125 | return async () => {
|
126 |
|
127 | if (this.context.model.dirty && !this.context.model.readOnly) {
|
128 | await this.context.save();
|
129 | }
|
130 | await Printing.printURL(PageConfig.getNBConvertURL({
|
131 | format: 'html',
|
132 | download: false,
|
133 | path: this.context.path
|
134 | }));
|
135 | };
|
136 | }
|
137 | |
138 |
|
139 |
|
140 | onBeforeHide(msg) {
|
141 | super.onBeforeHide(msg);
|
142 |
|
143 | this.content.isParentHidden = true;
|
144 | }
|
145 | |
146 |
|
147 |
|
148 | onBeforeShow(msg) {
|
149 |
|
150 |
|
151 |
|
152 | this.content.isParentHidden = false;
|
153 | super.onBeforeShow(msg);
|
154 | }
|
155 | |
156 |
|
157 |
|
158 | _onKernelChanged(sender, args) {
|
159 | if (!this.model || !args.newValue) {
|
160 | return;
|
161 | }
|
162 | const { newValue } = args;
|
163 | void newValue.info.then(info => {
|
164 | var _a;
|
165 | if (this.model &&
|
166 | ((_a = this.context.sessionContext.session) === null || _a === void 0 ? void 0 : _a.kernel) === newValue) {
|
167 | this._updateLanguage(info.language_info);
|
168 | }
|
169 | });
|
170 | void this._updateSpec(newValue);
|
171 | }
|
172 | _onSessionStatusChanged(sender, status) {
|
173 | var _a;
|
174 |
|
175 |
|
176 | if (status === 'autorestarting' && !this._autorestarting) {
|
177 |
|
178 |
|
179 | void showDialog({
|
180 | title: this._trans.__('Kernel Restarting'),
|
181 | body: this._trans.__('The kernel for %1 appears to have died. It will restart automatically.', (_a = this.sessionContext.session) === null || _a === void 0 ? void 0 : _a.path),
|
182 | buttons: [Dialog.okButton({ label: this._trans.__('Ok') })]
|
183 | });
|
184 | this._autorestarting = true;
|
185 | }
|
186 | else if (status === 'restarting') {
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | }
|
192 | else {
|
193 | this._autorestarting = false;
|
194 | }
|
195 | }
|
196 | |
197 |
|
198 |
|
199 | _updateLanguage(language) {
|
200 | this.model.setMetadata('language_info', language);
|
201 | }
|
202 | |
203 |
|
204 |
|
205 | async _updateSpec(kernel) {
|
206 | const spec = await kernel.spec;
|
207 | if (this.isDisposed) {
|
208 | return;
|
209 | }
|
210 | this.model.setMetadata('kernelspec', {
|
211 | name: kernel.name,
|
212 | display_name: spec === null || spec === void 0 ? void 0 : spec.display_name,
|
213 | language: spec === null || spec === void 0 ? void 0 : spec.language
|
214 | });
|
215 | }
|
216 | }
|
217 |
|
218 |
|
219 |
|
220 | (function (NotebookPanel) {
|
221 | |
222 |
|
223 |
|
224 | class ContentFactory extends Notebook.ContentFactory {
|
225 | |
226 |
|
227 |
|
228 | createNotebook(options) {
|
229 | return new Notebook(options);
|
230 | }
|
231 | }
|
232 | NotebookPanel.ContentFactory = ContentFactory;
|
233 | |
234 |
|
235 |
|
236 | NotebookPanel.IContentFactory = new Token('@jupyterlab/notebook:IContentFactory', `A factory object that creates new notebooks.
|
237 | Use this if you want to create and host notebooks in your own UI elements.`);
|
238 | })(NotebookPanel || (NotebookPanel = {}));
|
239 |
|
\ | No newline at end of file |