1 |
|
2 |
|
3 | import { Dialog, showDialog } from '@jupyterlab/apputils';
|
4 | import * as nbformat from '@jupyterlab/nbformat';
|
5 | import { YNotebook } from '@jupyter/ydoc';
|
6 | import { nullTranslator } from '@jupyterlab/translation';
|
7 | import { JSONExt } from '@lumino/coreutils';
|
8 | import { Signal } from '@lumino/signaling';
|
9 | import { CellList } from './celllist';
|
10 |
|
11 |
|
12 |
|
13 | export class NotebookModel {
|
14 | |
15 |
|
16 |
|
17 | constructor(options = {}) {
|
18 | var _a, _b;
|
19 | |
20 |
|
21 |
|
22 | this.standaloneModel = false;
|
23 | this._dirty = false;
|
24 | this._readOnly = false;
|
25 | this._contentChanged = new Signal(this);
|
26 | this._stateChanged = new Signal(this);
|
27 | this._isDisposed = false;
|
28 | this._metadataChanged = new Signal(this);
|
29 | this.standaloneModel = typeof options.sharedModel === 'undefined';
|
30 | if (options.sharedModel) {
|
31 | this.sharedModel = options.sharedModel;
|
32 | }
|
33 | else {
|
34 | this.sharedModel = YNotebook.create({
|
35 | disableDocumentWideUndoRedo: (_a = options.disableDocumentWideUndoRedo) !== null && _a !== void 0 ? _a : true,
|
36 | data: {
|
37 | nbformat: nbformat.MAJOR_VERSION,
|
38 | nbformat_minor: nbformat.MINOR_VERSION,
|
39 | metadata: {
|
40 | kernelspec: { name: '', display_name: '' },
|
41 | language_info: { name: (_b = options.languagePreference) !== null && _b !== void 0 ? _b : '' }
|
42 | }
|
43 | }
|
44 | });
|
45 | }
|
46 | this._cells = new CellList(this.sharedModel);
|
47 | this._trans = (options.translator || nullTranslator).load('jupyterlab');
|
48 | this._deletedCells = [];
|
49 | this._collaborationEnabled = !!(options === null || options === void 0 ? void 0 : options.collaborationEnabled);
|
50 | this._cells.changed.connect(this._onCellsChanged, this);
|
51 | this.sharedModel.changed.connect(this._onStateChanged, this);
|
52 | this.sharedModel.metadataChanged.connect(this._onMetadataChanged, this);
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | get contentChanged() {
|
58 | return this._contentChanged;
|
59 | }
|
60 | |
61 |
|
62 |
|
63 | get metadataChanged() {
|
64 | return this._metadataChanged;
|
65 | }
|
66 | |
67 |
|
68 |
|
69 | get stateChanged() {
|
70 | return this._stateChanged;
|
71 | }
|
72 | |
73 |
|
74 |
|
75 | get cells() {
|
76 | return this._cells;
|
77 | }
|
78 | |
79 |
|
80 |
|
81 | get dirty() {
|
82 | return this._dirty;
|
83 | }
|
84 | set dirty(newValue) {
|
85 | const oldValue = this._dirty;
|
86 | if (newValue === oldValue) {
|
87 | return;
|
88 | }
|
89 | this._dirty = newValue;
|
90 | this.triggerStateChange({
|
91 | name: 'dirty',
|
92 | oldValue,
|
93 | newValue
|
94 | });
|
95 | }
|
96 | |
97 |
|
98 |
|
99 | get readOnly() {
|
100 | return this._readOnly;
|
101 | }
|
102 | set readOnly(newValue) {
|
103 | if (newValue === this._readOnly) {
|
104 | return;
|
105 | }
|
106 | const oldValue = this._readOnly;
|
107 | this._readOnly = newValue;
|
108 | this.triggerStateChange({ name: 'readOnly', oldValue, newValue });
|
109 | }
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | get metadata() {
|
120 | return this.sharedModel.metadata;
|
121 | }
|
122 | |
123 |
|
124 |
|
125 | get nbformat() {
|
126 | return this.sharedModel.nbformat;
|
127 | }
|
128 | |
129 |
|
130 |
|
131 | get nbformatMinor() {
|
132 | return this.sharedModel.nbformat_minor;
|
133 | }
|
134 | |
135 |
|
136 |
|
137 | get defaultKernelName() {
|
138 | var _a;
|
139 | const spec = this.getMetadata('kernelspec');
|
140 | return (_a = spec === null || spec === void 0 ? void 0 : spec.name) !== null && _a !== void 0 ? _a : '';
|
141 | }
|
142 | |
143 |
|
144 |
|
145 | get deletedCells() {
|
146 | return this._deletedCells;
|
147 | }
|
148 | |
149 |
|
150 |
|
151 | get defaultKernelLanguage() {
|
152 | var _a;
|
153 | const info = this.getMetadata('language_info');
|
154 | return (_a = info === null || info === void 0 ? void 0 : info.name) !== null && _a !== void 0 ? _a : '';
|
155 | }
|
156 | |
157 |
|
158 |
|
159 | get collaborative() {
|
160 | return this._collaborationEnabled;
|
161 | }
|
162 | |
163 |
|
164 |
|
165 | dispose() {
|
166 |
|
167 | if (this.isDisposed) {
|
168 | return;
|
169 | }
|
170 | this._isDisposed = true;
|
171 | const cells = this.cells;
|
172 | this._cells = null;
|
173 | cells.dispose();
|
174 | if (this.standaloneModel) {
|
175 | this.sharedModel.dispose();
|
176 | }
|
177 | Signal.clearData(this);
|
178 | }
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 | deleteMetadata(key) {
|
185 | return this.sharedModel.deleteMetadata(key);
|
186 | }
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | getMetadata(key) {
|
196 | return this.sharedModel.getMetadata(key);
|
197 | }
|
198 | |
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | setMetadata(key, value) {
|
205 | if (typeof value === 'undefined') {
|
206 | this.sharedModel.deleteMetadata(key);
|
207 | }
|
208 | else {
|
209 | this.sharedModel.setMetadata(key, value);
|
210 | }
|
211 | }
|
212 | |
213 |
|
214 |
|
215 | toString() {
|
216 | return JSON.stringify(this.toJSON());
|
217 | }
|
218 | |
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | fromString(value) {
|
225 | this.fromJSON(JSON.parse(value));
|
226 | }
|
227 | |
228 |
|
229 |
|
230 | toJSON() {
|
231 | this._ensureMetadata();
|
232 | return this.sharedModel.toJSON();
|
233 | }
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | fromJSON(value) {
|
241 | var _a, _b;
|
242 | const copy = JSONExt.deepCopy(value);
|
243 | const origNbformat = value.metadata.orig_nbformat;
|
244 |
|
245 | copy.nbformat = Math.max(value.nbformat, nbformat.MAJOR_VERSION);
|
246 | if (copy.nbformat !== value.nbformat ||
|
247 | copy.nbformat_minor < nbformat.MINOR_VERSION) {
|
248 | copy.nbformat_minor = nbformat.MINOR_VERSION;
|
249 | }
|
250 | if (origNbformat !== undefined && copy.nbformat !== origNbformat) {
|
251 | const newer = copy.nbformat > origNbformat;
|
252 | let msg;
|
253 | if (newer) {
|
254 | msg = this._trans.__(`This notebook has been converted from an older notebook format (v%1)
|
255 | to the current notebook format (v%2).
|
256 | The next time you save this notebook, the current notebook format (v%2) will be used.
|
257 | 'Older versions of Jupyter may not be able to read the new format.' To preserve the original format version,
|
258 | close the notebook without saving it.`, origNbformat, copy.nbformat);
|
259 | }
|
260 | else {
|
261 | msg = this._trans.__(`This notebook has been converted from an newer notebook format (v%1)
|
262 | to the current notebook format (v%2).
|
263 | The next time you save this notebook, the current notebook format (v%2) will be used.
|
264 | Some features of the original notebook may not be available.' To preserve the original format version,
|
265 | close the notebook without saving it.`, origNbformat, copy.nbformat);
|
266 | }
|
267 | void showDialog({
|
268 | title: this._trans.__('Notebook converted'),
|
269 | body: msg,
|
270 | buttons: [Dialog.okButton({ label: this._trans.__('Ok') })]
|
271 | });
|
272 | }
|
273 |
|
274 | if (((_b = (_a = copy.cells) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) === 0) {
|
275 | copy['cells'] = [
|
276 | { cell_type: 'code', source: '', metadata: { trusted: true } }
|
277 | ];
|
278 | }
|
279 | this.sharedModel.fromJSON(copy);
|
280 | this._ensureMetadata();
|
281 | this.dirty = true;
|
282 | }
|
283 | |
284 |
|
285 |
|
286 | _onCellsChanged(list, change) {
|
287 | switch (change.type) {
|
288 | case 'add':
|
289 | change.newValues.forEach(cell => {
|
290 | cell.contentChanged.connect(this.triggerContentChange, this);
|
291 | });
|
292 | break;
|
293 | case 'remove':
|
294 | break;
|
295 | case 'set':
|
296 | change.newValues.forEach(cell => {
|
297 | cell.contentChanged.connect(this.triggerContentChange, this);
|
298 | });
|
299 | break;
|
300 | default:
|
301 | break;
|
302 | }
|
303 | this.triggerContentChange();
|
304 | }
|
305 | _onMetadataChanged(sender, changes) {
|
306 | this._metadataChanged.emit(changes);
|
307 | this.triggerContentChange();
|
308 | }
|
309 | _onStateChanged(sender, changes) {
|
310 | if (changes.stateChange) {
|
311 | changes.stateChange.forEach(value => {
|
312 | if (value.name === 'dirty') {
|
313 |
|
314 |
|
315 |
|
316 | this.dirty = value.newValue;
|
317 | }
|
318 | else if (value.oldValue !== value.newValue) {
|
319 | this.triggerStateChange({
|
320 | newValue: undefined,
|
321 | oldValue: undefined,
|
322 | ...value
|
323 | });
|
324 | }
|
325 | });
|
326 | }
|
327 | }
|
328 | |
329 |
|
330 |
|
331 | _ensureMetadata(languageName = '') {
|
332 | if (!this.getMetadata('language_info')) {
|
333 | this.sharedModel.setMetadata('language_info', { name: languageName });
|
334 | }
|
335 | if (!this.getMetadata('kernelspec')) {
|
336 | this.sharedModel.setMetadata('kernelspec', {
|
337 | name: '',
|
338 | display_name: ''
|
339 | });
|
340 | }
|
341 | }
|
342 | |
343 |
|
344 |
|
345 | triggerStateChange(args) {
|
346 | this._stateChanged.emit(args);
|
347 | }
|
348 | |
349 |
|
350 |
|
351 | triggerContentChange() {
|
352 | this._contentChanged.emit(void 0);
|
353 | this.dirty = true;
|
354 | }
|
355 | |
356 |
|
357 |
|
358 | get isDisposed() {
|
359 | return this._isDisposed;
|
360 | }
|
361 | }
|
362 |
|
\ | No newline at end of file |