@jupyterlab/cells 4.0.0-alpha.8 → 4.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/model.js CHANGED
@@ -2,25 +2,12 @@
2
2
  | Copyright (c) Jupyter Development Team.
3
3
  | Distributed under the terms of the Modified BSD License.
4
4
  |----------------------------------------------------------------------------*/
5
- var __rest = (this && this.__rest) || function (s, e) {
6
- var t = {};
7
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
8
- t[p] = s[p];
9
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
10
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
11
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
12
- t[p[i]] = s[p[i]];
13
- }
14
- return t;
15
- };
16
- import { JSONExt } from '@lumino/coreutils';
17
5
  import { Signal } from '@lumino/signaling';
18
6
  import { AttachmentsModel } from '@jupyterlab/attachments';
19
7
  import { CodeEditor } from '@jupyterlab/codeeditor';
20
- import * as models from '@jupyterlab/shared-models';
21
- import { UUID } from '@lumino/coreutils';
22
8
  import { OutputAreaModel } from '@jupyterlab/outputarea';
23
- const globalModelDBMutex = models.createMutex();
9
+ import { createMutex, createStandaloneCell } from '@jupyter/ydoc';
10
+ const globalModelDBMutex = createMutex();
24
11
  export function isCodeCellModel(model) {
25
12
  return model.type === 'code';
26
13
  }
@@ -34,14 +21,14 @@ export function isRawCellModel(model) {
34
21
  * An implementation of the cell model.
35
22
  */
36
23
  export class CellModel extends CodeEditor.Model {
37
- /**
38
- * Construct a cell model from optional cell content.
39
- */
40
- constructor(options) {
41
- var _a;
24
+ constructor(options = {}) {
25
+ const { cell_type, sharedModel, ...others } = options;
42
26
  super({
43
- modelDB: options.modelDB,
44
- id: options.id || ((_a = options.cell) === null || _a === void 0 ? void 0 : _a.id) || UUID.uuid4()
27
+ sharedModel: sharedModel !== null && sharedModel !== void 0 ? sharedModel : createStandaloneCell({
28
+ cell_type: cell_type !== null && cell_type !== void 0 ? cell_type : 'raw',
29
+ id: options.id
30
+ }),
31
+ ...others
45
32
  });
46
33
  /**
47
34
  * A signal emitted when the state of the model changes.
@@ -51,49 +38,18 @@ export class CellModel extends CodeEditor.Model {
51
38
  * A signal emitted when a model state changes.
52
39
  */
53
40
  this.stateChanged = new Signal(this);
54
- this.value.changed.connect(this.onGenericChange, this);
55
- const cellType = this.modelDB.createValue('type');
56
- cellType.set(this.type);
57
- const observableMetadata = this.modelDB.createMap('metadata');
58
- observableMetadata.changed.connect(this.onModelDBMetadataChange, this);
59
- observableMetadata.changed.connect(this.onGenericChange, this);
60
- const cell = options.cell;
61
- const trusted = this.modelDB.createValue('trusted');
62
- trusted.changed.connect(this.onTrustedChanged, this);
63
- if (!cell) {
64
- trusted.set(false);
65
- return;
66
- }
67
- trusted.set(!!cell.metadata['trusted']);
68
- delete cell.metadata['trusted'];
69
- // Set the text value, normalizing line endings to \n
70
- if (Array.isArray(cell.source)) {
71
- this.value.text = cell.source
72
- .map(s => s.replace(/\r\n/g, '\n').replace(/\r/g, '\n'))
73
- .join('');
74
- }
75
- else {
76
- this.value.text = cell.source.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
77
- }
78
- const metadata = JSONExt.deepCopy(cell.metadata);
79
- if (this.type !== 'raw') {
80
- delete metadata['format'];
81
- }
82
- if (this.type !== 'code') {
83
- delete metadata['collapsed'];
84
- delete metadata['scrolled'];
85
- }
86
- for (const key in metadata) {
87
- observableMetadata.set(key, metadata[key]);
88
- }
41
+ this._metadataChanged = new Signal(this);
42
+ this._trusted = false;
43
+ this.standaloneModel = typeof options.sharedModel === 'undefined';
44
+ this.trusted = !!this.getMetadata('trusted') || !!options.trusted;
45
+ this.sharedModel.changed.connect(this.onGenericChange, this);
46
+ this.sharedModel.metadataChanged.connect(this._onMetadataChanged, this);
89
47
  }
90
48
  /**
91
- * The type of cell.
49
+ * Signal emitted when cell metadata changes.
92
50
  */
93
- get type() {
94
- // This getter really should be abstract, but our current constructor
95
- // depends on .type working
96
- return 'raw';
51
+ get metadataChanged() {
52
+ return this._metadataChanged;
97
53
  }
98
54
  /**
99
55
  * The id for the cell.
@@ -105,41 +61,31 @@ export class CellModel extends CodeEditor.Model {
105
61
  * The metadata associated with the cell.
106
62
  */
107
63
  get metadata() {
108
- return this.modelDB.get('metadata');
64
+ return this.sharedModel.metadata;
109
65
  }
110
66
  /**
111
- * Get the trusted state of the model.
67
+ * The trusted state of the model.
112
68
  */
113
69
  get trusted() {
114
- return this.modelDB.getValue('trusted');
70
+ return this._trusted;
115
71
  }
116
- /**
117
- * Set the trusted state of the model.
118
- */
119
72
  set trusted(newValue) {
120
73
  const oldValue = this.trusted;
121
- if (oldValue === newValue) {
122
- return;
74
+ if (oldValue !== newValue) {
75
+ this._trusted = newValue;
76
+ this.onTrustedChanged(this, { newValue, oldValue });
123
77
  }
124
- this.modelDB.setValue('trusted', newValue);
125
78
  }
126
79
  /**
127
- * Serialize the model to JSON.
80
+ * Dispose of the resources held by the model.
128
81
  */
129
- toJSON() {
130
- const metadata = Object.create(null);
131
- for (const key of this.metadata.keys()) {
132
- const value = JSON.parse(JSON.stringify(this.metadata.get(key)));
133
- metadata[key] = value;
134
- }
135
- if (this.trusted) {
136
- metadata['trusted'] = true;
82
+ dispose() {
83
+ if (this.isDisposed) {
84
+ return;
137
85
  }
138
- return {
139
- cell_type: this.type,
140
- source: this.value.text,
141
- metadata
142
- };
86
+ this.sharedModel.changed.disconnect(this.onGenericChange, this);
87
+ this.sharedModel.metadataChanged.disconnect(this._onMetadataChanged, this);
88
+ super.dispose();
143
89
  }
144
90
  /**
145
91
  * Handle a change to the trusted state.
@@ -150,125 +96,43 @@ export class CellModel extends CodeEditor.Model {
150
96
  /* no-op */
151
97
  }
152
98
  /**
153
- * When we initialize a cell model, we create a standalone model that cannot be shared in a YNotebook.
154
- * Call this function to re-initialize the local representation based on a fresh shared model (e.g. models.YFile or models.YCodeCell).
99
+ * Delete a metadata
155
100
  *
156
- * @param sharedModel
157
- * @param reinitialize Whether to reinitialize the shared model.
158
- */
159
- switchSharedModel(sharedModel, reinitialize) {
160
- if (reinitialize) {
161
- const newValue = sharedModel.getMetadata();
162
- if (newValue) {
163
- this._updateModelDBMetadata(newValue);
164
- }
165
- }
166
- super.switchSharedModel(sharedModel, reinitialize);
101
+ * @param key Metadata key
102
+ */
103
+ deleteMetadata(key) {
104
+ return this.sharedModel.deleteMetadata(key);
167
105
  }
168
106
  /**
169
- * Handle a change to the cell metadata modelDB and reflect it in the shared model.
107
+ * Get a metadata
108
+ *
109
+ * ### Notes
110
+ * This returns a copy of the key value.
111
+ *
112
+ * @param key Metadata key
170
113
  */
171
- onModelDBMetadataChange(sender, event) {
172
- const metadata = this.sharedModel.getMetadata();
173
- globalModelDBMutex(() => {
174
- switch (event.type) {
175
- case 'add':
176
- this._changeCellMetadata(metadata, event);
177
- break;
178
- case 'change':
179
- this._changeCellMetadata(metadata, event);
180
- break;
181
- case 'remove':
182
- delete metadata[event.key];
183
- break;
184
- default:
185
- throw new Error(`Invalid event type: ${event.type}`);
186
- }
187
- this.sharedModel.setMetadata(metadata);
188
- });
114
+ getMetadata(key) {
115
+ return this.sharedModel.getMetadata(key);
189
116
  }
190
117
  /**
191
- * Change the cell metadata for a given event.
118
+ * Set a metadata
192
119
  *
193
- * @param metadata The cell metadata.
194
- * @param event The event to handle.
195
- */
196
- _changeCellMetadata(metadata, event) {
197
- switch (event.key) {
198
- case 'jupyter':
199
- metadata.jupyter = event.newValue;
200
- break;
201
- case 'collapsed':
202
- metadata.collapsed = event.newValue;
203
- break;
204
- case 'name':
205
- metadata.name = event.newValue;
206
- break;
207
- case 'scrolled':
208
- metadata.scrolled = event.newValue;
209
- break;
210
- case 'tags':
211
- metadata.tags = event.newValue;
212
- break;
213
- case 'trusted':
214
- metadata.trusted = event.newValue;
215
- break;
216
- default:
217
- // The default is applied for custom metadata that are not
218
- // defined in the official nbformat but which are defined
219
- // by the user.
220
- metadata[event.key] = event.newValue;
120
+ * @param key Metadata key
121
+ * @param value Metadata value
122
+ */
123
+ setMetadata(key, value) {
124
+ if (typeof value === 'undefined') {
125
+ this.sharedModel.deleteMetadata(key);
126
+ }
127
+ else {
128
+ this.sharedModel.setMetadata(key, value);
221
129
  }
222
130
  }
223
131
  /**
224
- * Handle a change to the cell shared model and reflect it in modelDB.
225
- * We update the modeldb metadata when the shared model changes.
226
- *
227
- * This method overrides the CodeEditor protected _onSharedModelChanged
228
- * so we first call super._onSharedModelChanged
229
- *
230
- * @override CodeEditor._onSharedModelChanged
132
+ * Serialize the model to JSON.
231
133
  */
232
- _onSharedModelChanged(sender, change) {
233
- super._onSharedModelChanged(sender, change);
234
- globalModelDBMutex(() => {
235
- var _a;
236
- if (change.metadataChange) {
237
- const newValue = (_a = change.metadataChange) === null || _a === void 0 ? void 0 : _a.newValue;
238
- if (newValue) {
239
- this._updateModelDBMetadata(newValue);
240
- }
241
- }
242
- });
243
- }
244
- _updateModelDBMetadata(metadata) {
245
- Object.keys(metadata).map(key => {
246
- switch (key) {
247
- case 'collapsed':
248
- this.metadata.set('collapsed', metadata.jupyter);
249
- break;
250
- case 'jupyter':
251
- this.metadata.set('jupyter', metadata.jupyter);
252
- break;
253
- case 'name':
254
- this.metadata.set('name', metadata.name);
255
- break;
256
- case 'scrolled':
257
- this.metadata.set('scrolled', metadata.scrolled);
258
- break;
259
- case 'tags':
260
- this.metadata.set('tags', metadata.tags);
261
- break;
262
- case 'trusted':
263
- this.metadata.set('trusted', metadata.trusted);
264
- break;
265
- default:
266
- // The default is applied for custom metadata that are not
267
- // defined in the official nbformat but which are defined
268
- // by the user.
269
- this.metadata.set(key, metadata[key]);
270
- }
271
- });
134
+ toJSON() {
135
+ return this.sharedModel.toJSON();
272
136
  }
273
137
  /**
274
138
  * Handle a change to the observable value.
@@ -276,6 +140,9 @@ export class CellModel extends CodeEditor.Model {
276
140
  onGenericChange() {
277
141
  this.contentChanged.emit(void 0);
278
142
  }
143
+ _onMetadataChanged(sender, change) {
144
+ this._metadataChanged.emit(change);
145
+ }
279
146
  }
280
147
  /**
281
148
  * A base implementation for cell models with attachments.
@@ -285,19 +152,14 @@ export class AttachmentsCellModel extends CellModel {
285
152
  * Construct a new cell with optional attachments.
286
153
  */
287
154
  constructor(options) {
155
+ var _a;
288
156
  super(options);
289
- const factory = options.contentFactory || AttachmentsCellModel.defaultContentFactory;
290
- let attachments;
291
- const cell = options.cell;
292
- if (cell && (cell.cell_type === 'raw' || cell.cell_type === 'markdown')) {
293
- attachments = cell
294
- .attachments;
295
- }
296
- this._attachments = factory.createAttachmentsModel({
297
- values: attachments,
298
- modelDB: this.modelDB
299
- });
157
+ const factory = (_a = options.contentFactory) !== null && _a !== void 0 ? _a : AttachmentsCellModel.defaultContentFactory;
158
+ const values = this.sharedModel.getAttachments();
159
+ this._attachments = factory.createAttachmentsModel({ values });
300
160
  this._attachments.stateChanged.connect(this.onGenericChange, this);
161
+ this._attachments.changed.connect(this._onAttachmentsChange, this);
162
+ this.sharedModel.changed.connect(this._onSharedModelChanged, this);
301
163
  }
302
164
  /**
303
165
  * Get the attachments of the model.
@@ -305,15 +167,40 @@ export class AttachmentsCellModel extends CellModel {
305
167
  get attachments() {
306
168
  return this._attachments;
307
169
  }
170
+ /**
171
+ * Dispose of the resources held by the model.
172
+ */
173
+ dispose() {
174
+ if (this.isDisposed) {
175
+ return;
176
+ }
177
+ this._attachments.stateChanged.disconnect(this.onGenericChange, this);
178
+ this._attachments.changed.disconnect(this._onAttachmentsChange, this);
179
+ this._attachments.dispose();
180
+ this.sharedModel.changed.disconnect(this._onSharedModelChanged, this);
181
+ super.dispose();
182
+ }
308
183
  /**
309
184
  * Serialize the model to JSON.
310
185
  */
311
186
  toJSON() {
312
- const cell = super.toJSON();
313
- if (this.attachments.length) {
314
- cell.attachments = this.attachments.toJSON();
187
+ return super.toJSON();
188
+ }
189
+ /**
190
+ * Handle a change to the cell outputs modelDB and reflect it in the shared model.
191
+ */
192
+ _onAttachmentsChange(sender, event) {
193
+ const cell = this.sharedModel;
194
+ globalModelDBMutex(() => cell.setAttachments(sender.toJSON()));
195
+ }
196
+ /**
197
+ * Handle a change to the code cell value.
198
+ */
199
+ _onSharedModelChanged(slot, change) {
200
+ if (change.attachmentsChange) {
201
+ const cell = this.sharedModel;
202
+ globalModelDBMutex(() => { var _a; return this._attachments.fromJSON((_a = cell.getAttachments()) !== null && _a !== void 0 ? _a : {}); });
315
203
  }
316
- return cell;
317
204
  }
318
205
  }
319
206
  /**
@@ -341,6 +228,15 @@ export class AttachmentsCellModel extends CellModel {
341
228
  * An implementation of a raw cell model.
342
229
  */
343
230
  export class RawCellModel extends AttachmentsCellModel {
231
+ /**
232
+ * Construct a raw cell model from optional shared model.
233
+ */
234
+ constructor(options = {}) {
235
+ super({
236
+ cell_type: 'raw',
237
+ ...options
238
+ });
239
+ }
344
240
  /**
345
241
  * The type of the cell.
346
242
  */
@@ -351,9 +247,7 @@ export class RawCellModel extends AttachmentsCellModel {
351
247
  * Serialize the model to JSON.
352
248
  */
353
249
  toJSON() {
354
- const cell = super.toJSON();
355
- cell.id = this.id;
356
- return cell;
250
+ return super.toJSON();
357
251
  }
358
252
  }
359
253
  /**
@@ -361,10 +255,13 @@ export class RawCellModel extends AttachmentsCellModel {
361
255
  */
362
256
  export class MarkdownCellModel extends AttachmentsCellModel {
363
257
  /**
364
- * Construct a markdown cell model from optional cell content.
258
+ * Construct a markdown cell model from optional shared model.
365
259
  */
366
- constructor(options) {
367
- super(options);
260
+ constructor(options = {}) {
261
+ super({
262
+ cell_type: 'markdown',
263
+ ...options
264
+ });
368
265
  // Use the Github-flavored markdown mode.
369
266
  this.mimeType = 'text/x-ipythongfm';
370
267
  }
@@ -378,9 +275,7 @@ export class MarkdownCellModel extends AttachmentsCellModel {
378
275
  * Serialize the model to JSON.
379
276
  */
380
277
  toJSON() {
381
- const cell = super.toJSON();
382
- cell.id = this.id;
383
- return cell;
278
+ return super.toJSON();
384
279
  }
385
280
  }
386
281
  /**
@@ -390,77 +285,21 @@ export class CodeCellModel extends CellModel {
390
285
  /**
391
286
  * Construct a new code cell with optional original cell content.
392
287
  */
393
- constructor(options) {
288
+ constructor(options = {}) {
394
289
  var _a;
395
- super(options);
290
+ super({
291
+ cell_type: 'code',
292
+ ...options
293
+ });
396
294
  this._executedCode = '';
397
295
  this._isDirty = false;
398
- const factory = options.contentFactory || CodeCellModel.defaultContentFactory;
296
+ const factory = (_a = options === null || options === void 0 ? void 0 : options.contentFactory) !== null && _a !== void 0 ? _a : CodeCellModel.defaultContentFactory;
399
297
  const trusted = this.trusted;
400
- const cell = options.cell;
401
- let outputs = [];
402
- const executionCount = this.modelDB.createValue('executionCount');
403
- if (cell && cell.cell_type === 'code') {
404
- // Initialize from disk
405
- executionCount.set(cell.execution_count || null);
406
- outputs = (_a = cell.outputs) !== null && _a !== void 0 ? _a : [];
407
- // Add content loaded from disk to sharedModel
408
- globalModelDBMutex(() => {
409
- this.sharedModel.execution_count = cell.execution_count;
410
- this.sharedModel.setOutputs(outputs);
411
- });
412
- // If execution count is not null presume the input code was the latest executed
413
- // TODO load from the notebook file when the dirty state is stored in it
414
- if (cell.execution_count !== null) {
415
- // True if execution_count is null or undefined
416
- this._executedCode = this.value.text.trim();
417
- }
418
- }
419
- else {
420
- // Initialize from other clients
421
- executionCount.set(this.sharedModel.execution_count);
422
- outputs = this.sharedModel.getOutputs();
423
- }
424
- this.value.changed.connect(this._onValueChanged, this);
425
- executionCount.changed.connect(this._onExecutionCountChanged, this);
298
+ const outputs = this.sharedModel.getOutputs();
426
299
  this._outputs = factory.createOutputArea({ trusted, values: outputs });
300
+ this.sharedModel.changed.connect(this._onSharedModelChanged, this);
427
301
  this._outputs.changed.connect(this.onGenericChange, this);
428
- this._outputs.changed.connect(this.onModelDBOutputsChange, this);
429
- // We keep `collapsed` and `jupyter.outputs_hidden` metadata in sync, since
430
- // they are redundant in nbformat 4.4. See
431
- // https://github.com/jupyter/nbformat/issues/137
432
- this.metadata.changed.connect(Private.collapseChanged, this);
433
- // Sync `collapsed` and `jupyter.outputs_hidden` for the first time, giving
434
- // preference to `collapsed`.
435
- if (this.metadata.has('collapsed')) {
436
- const collapsed = this.metadata.get('collapsed');
437
- Private.collapseChanged(this.metadata, {
438
- type: 'change',
439
- key: 'collapsed',
440
- oldValue: collapsed,
441
- newValue: collapsed
442
- });
443
- }
444
- else if (this.metadata.has('jupyter')) {
445
- const jupyter = this.metadata.get('jupyter');
446
- if (jupyter.hasOwnProperty('outputs_hidden')) {
447
- Private.collapseChanged(this.metadata, {
448
- type: 'change',
449
- key: 'jupyter',
450
- oldValue: jupyter,
451
- newValue: jupyter
452
- });
453
- }
454
- }
455
- }
456
- switchSharedModel(sharedModel, reinitialize) {
457
- if (reinitialize) {
458
- this.executionCount = sharedModel.execution_count;
459
- this.outputs.clear();
460
- sharedModel.getOutputs().forEach(output => this._outputs.add(output));
461
- }
462
- super.switchSharedModel(sharedModel, reinitialize);
463
- this._setDirty(false);
302
+ this._outputs.changed.connect(this.onOutputsChange, this);
464
303
  }
465
304
  /**
466
305
  * The type of the cell.
@@ -472,16 +311,10 @@ export class CodeCellModel extends CellModel {
472
311
  * The execution count of the cell.
473
312
  */
474
313
  get executionCount() {
475
- return this.modelDB.has('executionCount')
476
- ? this.modelDB.getValue('executionCount')
477
- : null;
314
+ return this.sharedModel.execution_count || null;
478
315
  }
479
316
  set executionCount(newValue) {
480
- const oldValue = this.executionCount;
481
- if (newValue === oldValue) {
482
- return;
483
- }
484
- this.modelDB.setValue('executionCount', newValue || null);
317
+ this.sharedModel.execution_count = newValue || null;
485
318
  }
486
319
  /**
487
320
  * Whether the cell is dirty or not.
@@ -495,32 +328,16 @@ export class CodeCellModel extends CellModel {
495
328
  return this._isDirty;
496
329
  }
497
330
  /**
498
- * Set whether the cell is dirty or not.
331
+ * The cell outputs.
499
332
  */
500
- _setDirty(v) {
501
- if (v !== this._isDirty) {
502
- if (!v) {
503
- this._executedCode = this.value.text.trim();
504
- }
505
- this._isDirty = v;
506
- this.stateChanged.emit({
507
- name: 'isDirty',
508
- oldValue: !v,
509
- newValue: v
510
- });
511
- }
333
+ get outputs() {
334
+ return this._outputs;
512
335
  }
513
336
  clearExecution() {
514
337
  this.outputs.clear();
515
338
  this.executionCount = null;
516
339
  this._setDirty(false);
517
- this.metadata.delete('execution');
518
- }
519
- /**
520
- * The cell outputs.
521
- */
522
- get outputs() {
523
- return this._outputs;
340
+ this.sharedModel.deleteMetadata('execution');
524
341
  }
525
342
  /**
526
343
  * Dispose of the resources held by the model.
@@ -529,43 +346,49 @@ export class CodeCellModel extends CellModel {
529
346
  if (this.isDisposed) {
530
347
  return;
531
348
  }
349
+ this.sharedModel.changed.disconnect(this._onSharedModelChanged, this);
350
+ this._outputs.changed.disconnect(this.onGenericChange, this);
351
+ this._outputs.changed.disconnect(this.onOutputsChange, this);
532
352
  this._outputs.dispose();
533
353
  this._outputs = null;
534
354
  super.dispose();
535
355
  }
536
- /**
537
- * Serialize the model to JSON.
538
- */
539
- toJSON() {
540
- const cell = super.toJSON();
541
- cell.execution_count = this.executionCount || null;
542
- cell.outputs = this.outputs.toJSON();
543
- cell.id = this.id;
544
- return cell;
545
- }
546
356
  /**
547
357
  * Handle a change to the trusted state.
548
358
  */
549
359
  onTrustedChanged(trusted, args) {
360
+ const newTrusted = args.newValue;
550
361
  if (this._outputs) {
551
- this._outputs.trusted = args.newValue;
362
+ this._outputs.trusted = newTrusted;
363
+ }
364
+ if (newTrusted) {
365
+ const codeCell = this.sharedModel;
366
+ const metadata = codeCell.getMetadata();
367
+ metadata.trusted = true;
368
+ codeCell.setMetadata(metadata);
552
369
  }
553
370
  this.stateChanged.emit({
554
371
  name: 'trusted',
555
372
  oldValue: args.oldValue,
556
- newValue: args.newValue
373
+ newValue: newTrusted
557
374
  });
558
375
  }
376
+ /**
377
+ * Serialize the model to JSON.
378
+ */
379
+ toJSON() {
380
+ return super.toJSON();
381
+ }
559
382
  /**
560
383
  * Handle a change to the cell outputs modelDB and reflect it in the shared model.
561
384
  */
562
- onModelDBOutputsChange(sender, event) {
385
+ onOutputsChange(sender, event) {
563
386
  const codeCell = this.sharedModel;
564
387
  globalModelDBMutex(() => {
565
388
  switch (event.type) {
566
389
  case 'add': {
567
390
  const outputs = event.newValues.map(output => output.toJSON());
568
- codeCell.updateOutputs(event.newIndex, event.newIndex + outputs.length, outputs);
391
+ codeCell.updateOutputs(event.newIndex, event.newIndex, outputs);
569
392
  break;
570
393
  }
571
394
  case 'set': {
@@ -584,52 +407,42 @@ export class CodeCellModel extends CellModel {
584
407
  /**
585
408
  * Handle a change to the code cell value.
586
409
  */
587
- _onValueChanged() {
588
- if (this.executionCount !== null) {
589
- this._setDirty(this._executedCode !== this.value.text.trim());
410
+ _onSharedModelChanged(slot, change) {
411
+ if (change.outputsChange) {
412
+ globalModelDBMutex(() => {
413
+ this.outputs.clear();
414
+ slot.getOutputs().forEach(output => this._outputs.add(output));
415
+ });
590
416
  }
591
- }
592
- /**
593
- * Handle a change to the output shared model and reflect it in modelDB.
594
- * We update the modeldb metadata when the nbcell changes.
595
- *
596
- * This method overrides the CellModel protected _onSharedModelChanged
597
- * so we first call super._onSharedModelChanged
598
- *
599
- * @override CellModel._onSharedModelChanged
600
- */
601
- _onSharedModelChanged(sender, change) {
602
- super._onSharedModelChanged(sender, change);
603
- globalModelDBMutex(() => {
604
- if (change.outputsChange) {
605
- this.clearExecution();
606
- sender.getOutputs().forEach(output => this._outputs.add(output));
607
- }
608
- if (change.executionCountChange) {
609
- this.executionCount = change.executionCountChange.newValue
610
- ? change.executionCountChange.newValue
611
- : null;
417
+ if (change.executionCountChange) {
418
+ if (change.executionCountChange.newValue &&
419
+ (this.isDirty || !change.executionCountChange.oldValue)) {
420
+ this._setDirty(false);
612
421
  }
613
- });
422
+ this.stateChanged.emit({
423
+ name: 'executionCount',
424
+ oldValue: change.executionCountChange.oldValue,
425
+ newValue: change.executionCountChange.newValue
426
+ });
427
+ }
428
+ if (change.sourceChange && this.executionCount !== null) {
429
+ this._setDirty(this._executedCode !== this.sharedModel.getSource().trim());
430
+ }
614
431
  }
615
432
  /**
616
- * Handle a change to the execution count.
433
+ * Set whether the cell is dirty or not.
617
434
  */
618
- _onExecutionCountChanged(count, args) {
619
- const codeCell = this.sharedModel;
620
- globalModelDBMutex(() => {
621
- codeCell.execution_count = args.newValue
622
- ? args.newValue
623
- : null;
624
- });
625
- this.contentChanged.emit(void 0);
626
- this.stateChanged.emit({
627
- name: 'executionCount',
628
- oldValue: args.oldValue,
629
- newValue: args.newValue
630
- });
631
- if (args.newValue && this.isDirty) {
632
- this._setDirty(false);
435
+ _setDirty(v) {
436
+ if (!v) {
437
+ this._executedCode = this.sharedModel.getSource().trim();
438
+ }
439
+ if (v !== this._isDirty) {
440
+ this._isDirty = v;
441
+ this.stateChanged.emit({
442
+ name: 'isDirty',
443
+ oldValue: !v,
444
+ newValue: v
445
+ });
633
446
  }
634
447
  }
635
448
  }
@@ -654,34 +467,4 @@ export class CodeCellModel extends CellModel {
654
467
  */
655
468
  CodeCellModel.defaultContentFactory = new ContentFactory();
656
469
  })(CodeCellModel || (CodeCellModel = {}));
657
- var Private;
658
- (function (Private) {
659
- function collapseChanged(metadata, args) {
660
- if (args.key === 'collapsed') {
661
- const jupyter = (metadata.get('jupyter') || {});
662
- const { outputs_hidden } = jupyter, newJupyter = __rest(jupyter, ["outputs_hidden"]);
663
- if (outputs_hidden !== args.newValue) {
664
- if (args.newValue !== undefined) {
665
- newJupyter['outputs_hidden'] = args.newValue;
666
- }
667
- if (Object.keys(newJupyter).length === 0) {
668
- metadata.delete('jupyter');
669
- }
670
- else {
671
- metadata.set('jupyter', newJupyter);
672
- }
673
- }
674
- }
675
- else if (args.key === 'jupyter') {
676
- const jupyter = (args.newValue || {});
677
- if (jupyter.hasOwnProperty('outputs_hidden')) {
678
- metadata.set('collapsed', jupyter.outputs_hidden);
679
- }
680
- else {
681
- metadata.delete('collapsed');
682
- }
683
- }
684
- }
685
- Private.collapseChanged = collapseChanged;
686
- })(Private || (Private = {}));
687
470
  //# sourceMappingURL=model.js.map