@jupyterlab/cells 4.0.0-alpha.2 → 4.0.0-alpha.20

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.
Files changed (46) hide show
  1. package/lib/celldragutils.d.ts +1 -7
  2. package/lib/celldragutils.js +12 -13
  3. package/lib/celldragutils.js.map +1 -1
  4. package/lib/index.d.ts +1 -0
  5. package/lib/index.js +1 -0
  6. package/lib/index.js.map +1 -1
  7. package/lib/inputarea.d.ts +9 -19
  8. package/lib/inputarea.js +16 -29
  9. package/lib/inputarea.js.map +1 -1
  10. package/lib/model.d.ts +121 -79
  11. package/lib/model.js +179 -392
  12. package/lib/model.js.map +1 -1
  13. package/lib/placeholder.d.ts +23 -19
  14. package/lib/placeholder.js +30 -35
  15. package/lib/placeholder.js.map +1 -1
  16. package/lib/resizeHandle.d.ts +0 -1
  17. package/lib/resizeHandle.js +9 -11
  18. package/lib/resizeHandle.js.map +1 -1
  19. package/lib/searchprovider.d.ts +31 -0
  20. package/lib/searchprovider.js +359 -0
  21. package/lib/searchprovider.js.map +1 -0
  22. package/lib/testutils.d.ts +22 -0
  23. package/lib/testutils.js +48 -0
  24. package/lib/testutils.js.map +1 -0
  25. package/lib/widget.d.ts +226 -42
  26. package/lib/widget.js +672 -270
  27. package/lib/widget.js.map +1 -1
  28. package/package.json +36 -33
  29. package/src/celldragutils.ts +214 -0
  30. package/src/collapser.tsx +150 -0
  31. package/src/headerfooter.ts +52 -0
  32. package/src/index.ts +17 -0
  33. package/src/inputarea.ts +292 -0
  34. package/src/model.ts +845 -0
  35. package/src/placeholder.ts +125 -0
  36. package/src/resizeHandle.ts +96 -0
  37. package/src/searchprovider.ts +433 -0
  38. package/src/testutils.ts +57 -0
  39. package/src/widget.ts +2290 -0
  40. package/style/collapser.css +5 -4
  41. package/style/headerfooter.css +3 -3
  42. package/style/index.css +4 -2
  43. package/style/index.js +4 -2
  44. package/style/inputarea.css +19 -17
  45. package/style/placeholder.css +1 -1
  46. package/style/widget.css +85 -7
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,73 +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 (!executionCount.get()) {
404
- if (cell && cell.cell_type === 'code') {
405
- executionCount.set(cell.execution_count || null);
406
- outputs = (_a = cell.outputs) !== null && _a !== void 0 ? _a : [];
407
- // If execution count is not null presume the input code was the latest executed
408
- // TODO load from the notebook file when the dirty state is stored in it
409
- if (cell.execution_count != null) {
410
- // True if execution_count is null or undefined
411
- this._executedCode = this.value.text.trim();
412
- }
413
- }
414
- else {
415
- executionCount.set(null);
416
- }
417
- }
418
- this.value.changed.connect(this._onValueChanged, this);
419
- executionCount.changed.connect(this._onExecutionCountChanged, this);
420
- globalModelDBMutex(() => {
421
- const sharedCell = this.sharedModel;
422
- sharedCell.setOutputs(outputs);
423
- });
298
+ const outputs = this.sharedModel.getOutputs();
424
299
  this._outputs = factory.createOutputArea({ trusted, values: outputs });
300
+ this.sharedModel.changed.connect(this._onSharedModelChanged, this);
425
301
  this._outputs.changed.connect(this.onGenericChange, this);
426
- this._outputs.changed.connect(this.onModelDBOutputsChange, this);
427
- // We keep `collapsed` and `jupyter.outputs_hidden` metadata in sync, since
428
- // they are redundant in nbformat 4.4. See
429
- // https://github.com/jupyter/nbformat/issues/137
430
- this.metadata.changed.connect(Private.collapseChanged, this);
431
- // Sync `collapsed` and `jupyter.outputs_hidden` for the first time, giving
432
- // preference to `collapsed`.
433
- if (this.metadata.has('collapsed')) {
434
- const collapsed = this.metadata.get('collapsed');
435
- Private.collapseChanged(this.metadata, {
436
- type: 'change',
437
- key: 'collapsed',
438
- oldValue: collapsed,
439
- newValue: collapsed
440
- });
441
- }
442
- else if (this.metadata.has('jupyter')) {
443
- const jupyter = this.metadata.get('jupyter');
444
- if (jupyter.hasOwnProperty('outputs_hidden')) {
445
- Private.collapseChanged(this.metadata, {
446
- type: 'change',
447
- key: 'jupyter',
448
- oldValue: jupyter,
449
- newValue: jupyter
450
- });
451
- }
452
- }
453
- }
454
- switchSharedModel(sharedModel, reinitialize) {
455
- if (reinitialize) {
456
- this.clearExecution();
457
- sharedModel.getOutputs().forEach(output => this._outputs.add(output));
458
- }
459
- super.switchSharedModel(sharedModel, reinitialize);
302
+ this._outputs.changed.connect(this.onOutputsChange, this);
460
303
  }
461
304
  /**
462
305
  * The type of the cell.
@@ -468,16 +311,10 @@ export class CodeCellModel extends CellModel {
468
311
  * The execution count of the cell.
469
312
  */
470
313
  get executionCount() {
471
- return this.modelDB.has('executionCount')
472
- ? this.modelDB.getValue('executionCount')
473
- : null;
314
+ return this.sharedModel.execution_count || null;
474
315
  }
475
316
  set executionCount(newValue) {
476
- const oldValue = this.executionCount;
477
- if (newValue === oldValue) {
478
- return;
479
- }
480
- this.modelDB.setValue('executionCount', newValue || null);
317
+ this.sharedModel.execution_count = newValue || null;
481
318
  }
482
319
  /**
483
320
  * Whether the cell is dirty or not.
@@ -491,32 +328,16 @@ export class CodeCellModel extends CellModel {
491
328
  return this._isDirty;
492
329
  }
493
330
  /**
494
- * Set whether the cell is dirty or not.
331
+ * The cell outputs.
495
332
  */
496
- _setDirty(v) {
497
- if (v !== this._isDirty) {
498
- if (!v) {
499
- this._executedCode = this.value.text.trim();
500
- }
501
- this._isDirty = v;
502
- this.stateChanged.emit({
503
- name: 'isDirty',
504
- oldValue: !v,
505
- newValue: v
506
- });
507
- }
333
+ get outputs() {
334
+ return this._outputs;
508
335
  }
509
336
  clearExecution() {
510
337
  this.outputs.clear();
511
338
  this.executionCount = null;
512
339
  this._setDirty(false);
513
- this.metadata.delete('execution');
514
- }
515
- /**
516
- * The cell outputs.
517
- */
518
- get outputs() {
519
- return this._outputs;
340
+ this.sharedModel.deleteMetadata('execution');
520
341
  }
521
342
  /**
522
343
  * Dispose of the resources held by the model.
@@ -525,48 +346,54 @@ export class CodeCellModel extends CellModel {
525
346
  if (this.isDisposed) {
526
347
  return;
527
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);
528
352
  this._outputs.dispose();
529
353
  this._outputs = null;
530
354
  super.dispose();
531
355
  }
532
- /**
533
- * Serialize the model to JSON.
534
- */
535
- toJSON() {
536
- const cell = super.toJSON();
537
- cell.execution_count = this.executionCount || null;
538
- cell.outputs = this.outputs.toJSON();
539
- cell.id = this.id;
540
- return cell;
541
- }
542
356
  /**
543
357
  * Handle a change to the trusted state.
544
358
  */
545
359
  onTrustedChanged(trusted, args) {
360
+ const newTrusted = args.newValue;
546
361
  if (this._outputs) {
547
- 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);
548
369
  }
549
370
  this.stateChanged.emit({
550
371
  name: 'trusted',
551
372
  oldValue: args.oldValue,
552
- newValue: args.newValue
373
+ newValue: newTrusted
553
374
  });
554
375
  }
376
+ /**
377
+ * Serialize the model to JSON.
378
+ */
379
+ toJSON() {
380
+ return super.toJSON();
381
+ }
555
382
  /**
556
383
  * Handle a change to the cell outputs modelDB and reflect it in the shared model.
557
384
  */
558
- onModelDBOutputsChange(sender, event) {
385
+ onOutputsChange(sender, event) {
559
386
  const codeCell = this.sharedModel;
560
387
  globalModelDBMutex(() => {
561
388
  switch (event.type) {
562
389
  case 'add': {
563
390
  const outputs = event.newValues.map(output => output.toJSON());
564
- codeCell.updateOutputs(event.newIndex, event.newIndex + outputs.length, outputs);
391
+ codeCell.updateOutputs(event.newIndex, event.newIndex, outputs);
565
392
  break;
566
393
  }
567
394
  case 'set': {
568
395
  const newValues = event.newValues.map(output => output.toJSON());
569
- codeCell.updateOutputs(event.oldIndex, event.oldValues.length, newValues);
396
+ codeCell.updateOutputs(event.oldIndex, event.oldIndex + newValues.length, newValues);
570
397
  break;
571
398
  }
572
399
  case 'remove':
@@ -580,52 +407,42 @@ export class CodeCellModel extends CellModel {
580
407
  /**
581
408
  * Handle a change to the code cell value.
582
409
  */
583
- _onValueChanged() {
584
- if (this.executionCount !== null) {
585
- 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
+ });
586
416
  }
587
- }
588
- /**
589
- * Handle a change to the output shared model and reflect it in modelDB.
590
- * We update the modeldb metadata when the nbcell changes.
591
- *
592
- * This method overrides the CellModel protected _onSharedModelChanged
593
- * so we first call super._onSharedModelChanged
594
- *
595
- * @override CellModel._onSharedModelChanged
596
- */
597
- _onSharedModelChanged(sender, change) {
598
- super._onSharedModelChanged(sender, change);
599
- globalModelDBMutex(() => {
600
- if (change.outputsChange) {
601
- this.clearExecution();
602
- sender.getOutputs().forEach(output => this._outputs.add(output));
603
- }
604
- if (change.executionCountChange) {
605
- this.executionCount = change.executionCountChange.newValue
606
- ? change.executionCountChange.newValue
607
- : null;
417
+ if (change.executionCountChange) {
418
+ if (change.executionCountChange.newValue &&
419
+ (this.isDirty || !change.executionCountChange.oldValue)) {
420
+ this._setDirty(false);
608
421
  }
609
- });
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
+ }
610
431
  }
611
432
  /**
612
- * Handle a change to the execution count.
433
+ * Set whether the cell is dirty or not.
613
434
  */
614
- _onExecutionCountChanged(count, args) {
615
- const codeCell = this.sharedModel;
616
- globalModelDBMutex(() => {
617
- codeCell.execution_count = args.newValue
618
- ? args.newValue
619
- : null;
620
- });
621
- this.contentChanged.emit(void 0);
622
- this.stateChanged.emit({
623
- name: 'executionCount',
624
- oldValue: args.oldValue,
625
- newValue: args.newValue
626
- });
627
- if (args.newValue && this.isDirty) {
628
- 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
+ });
629
446
  }
630
447
  }
631
448
  }
@@ -650,34 +467,4 @@ export class CodeCellModel extends CellModel {
650
467
  */
651
468
  CodeCellModel.defaultContentFactory = new ContentFactory();
652
469
  })(CodeCellModel || (CodeCellModel = {}));
653
- var Private;
654
- (function (Private) {
655
- function collapseChanged(metadata, args) {
656
- if (args.key === 'collapsed') {
657
- const jupyter = (metadata.get('jupyter') || {});
658
- const { outputs_hidden } = jupyter, newJupyter = __rest(jupyter, ["outputs_hidden"]);
659
- if (outputs_hidden !== args.newValue) {
660
- if (args.newValue !== undefined) {
661
- newJupyter['outputs_hidden'] = args.newValue;
662
- }
663
- if (Object.keys(newJupyter).length === 0) {
664
- metadata.delete('jupyter');
665
- }
666
- else {
667
- metadata.set('jupyter', newJupyter);
668
- }
669
- }
670
- }
671
- else if (args.key === 'jupyter') {
672
- const jupyter = (args.newValue || {});
673
- if (jupyter.hasOwnProperty('outputs_hidden')) {
674
- metadata.set('collapsed', jupyter.outputs_hidden);
675
- }
676
- else {
677
- metadata.delete('collapsed');
678
- }
679
- }
680
- }
681
- Private.collapseChanged = collapseChanged;
682
- })(Private || (Private = {}));
683
470
  //# sourceMappingURL=model.js.map