@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/src/model.ts ADDED
@@ -0,0 +1,845 @@
1
+ /* -----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+
6
+ import { ISignal, Signal } from '@lumino/signaling';
7
+
8
+ import { AttachmentsModel, IAttachmentsModel } from '@jupyterlab/attachments';
9
+
10
+ import { CodeEditor } from '@jupyterlab/codeeditor';
11
+
12
+ import { IChangedArgs } from '@jupyterlab/coreutils';
13
+
14
+ import * as nbformat from '@jupyterlab/nbformat';
15
+
16
+ import { ObservableValue } from '@jupyterlab/observables';
17
+
18
+ import { IOutputAreaModel, OutputAreaModel } from '@jupyterlab/outputarea';
19
+
20
+ import {
21
+ CellChange,
22
+ createMutex,
23
+ createStandaloneCell,
24
+ IMapChange,
25
+ ISharedAttachmentsCell,
26
+ ISharedCell,
27
+ ISharedCodeCell,
28
+ ISharedMarkdownCell,
29
+ ISharedRawCell,
30
+ YCodeCell
31
+ } from '@jupyter/ydoc';
32
+
33
+ const globalModelDBMutex = createMutex();
34
+
35
+ /**
36
+ * The definition of a model object for a cell.
37
+ */
38
+ export interface ICellModel extends CodeEditor.IModel {
39
+ /**
40
+ * The type of the cell.
41
+ */
42
+ readonly type: nbformat.CellType;
43
+
44
+ /**
45
+ * A unique identifier for the cell.
46
+ */
47
+ readonly id: string;
48
+
49
+ /**
50
+ * A signal emitted when the content of the model changes.
51
+ */
52
+ readonly contentChanged: ISignal<ICellModel, void>;
53
+
54
+ /**
55
+ * A signal emitted when a model state changes.
56
+ */
57
+ readonly stateChanged: ISignal<
58
+ ICellModel,
59
+ IChangedArgs<boolean, boolean, any>
60
+ >;
61
+
62
+ /**
63
+ * Whether the cell is trusted.
64
+ */
65
+ trusted: boolean;
66
+
67
+ /**
68
+ * The metadata associated with the cell.
69
+ *
70
+ * ### Notes
71
+ * This is a copy of the metadata. Changing a part of it
72
+ * won't affect the model.
73
+ * As this returns a copy of all metadata, it is advised to
74
+ * use `getMetadata` to speed up the process of getting a single key.
75
+ */
76
+ readonly metadata: Omit<nbformat.IBaseCellMetadata, 'trusted'>;
77
+
78
+ /**
79
+ * Signal emitted when cell metadata changes.
80
+ */
81
+ readonly metadataChanged: ISignal<ICellModel, IMapChange>;
82
+
83
+ /**
84
+ * The cell shared model.
85
+ */
86
+ readonly sharedModel: ISharedCell;
87
+
88
+ /**
89
+ * Delete a metadata.
90
+ *
91
+ * @param key Metadata key
92
+ */
93
+ deleteMetadata(key: string): void;
94
+
95
+ /**
96
+ * Get a metadata
97
+ *
98
+ * ### Notes
99
+ * This returns a copy of the key value.
100
+ *
101
+ * @param key Metadata key
102
+ */
103
+ getMetadata(key: string): any;
104
+
105
+ /**
106
+ * Set a metadata
107
+ *
108
+ * @param key Metadata key
109
+ * @param value Metadata value
110
+ */
111
+ setMetadata(key: string, value: any): void;
112
+
113
+ /**
114
+ * Serialize the model to JSON.
115
+ */
116
+ toJSON(): nbformat.ICell;
117
+ }
118
+
119
+ /**
120
+ * The definition of a model cell object for a cell with attachments.
121
+ */
122
+ export interface IAttachmentsCellModel extends ICellModel {
123
+ /**
124
+ * The cell attachments
125
+ */
126
+ readonly attachments: IAttachmentsModel;
127
+ }
128
+
129
+ /**
130
+ * The definition of a code cell.
131
+ */
132
+ export interface ICodeCellModel extends ICellModel {
133
+ /**
134
+ * The type of the cell.
135
+ *
136
+ * #### Notes
137
+ * This is a read-only property.
138
+ */
139
+ readonly type: 'code';
140
+
141
+ /**
142
+ * Whether the code cell has been edited since the last run.
143
+ */
144
+ readonly isDirty: boolean;
145
+
146
+ /**
147
+ * Serialize the model to JSON.
148
+ */
149
+ toJSON(): nbformat.ICodeCell;
150
+
151
+ /**
152
+ * The code cell's prompt number. Will be null if the cell has not been run.
153
+ */
154
+ executionCount: nbformat.ExecutionCount;
155
+
156
+ /**
157
+ * The cell outputs.
158
+ */
159
+ readonly outputs: IOutputAreaModel;
160
+
161
+ /**
162
+ * Clear execution, outputs, and related metadata
163
+ */
164
+ clearExecution(): void;
165
+
166
+ /**
167
+ * The code cell shared model
168
+ */
169
+ readonly sharedModel: ISharedCodeCell;
170
+ }
171
+
172
+ /**
173
+ * The definition of a markdown cell.
174
+ */
175
+ export interface IMarkdownCellModel extends IAttachmentsCellModel {
176
+ /**
177
+ * The type of the cell.
178
+ */
179
+ readonly type: 'markdown';
180
+
181
+ /**
182
+ * Serialize the model to JSON.
183
+ */
184
+ toJSON(): nbformat.IMarkdownCell;
185
+ }
186
+
187
+ /**
188
+ * The definition of a raw cell.
189
+ */
190
+ export interface IRawCellModel extends IAttachmentsCellModel {
191
+ /**
192
+ * The type of the cell.
193
+ */
194
+ readonly type: 'raw';
195
+
196
+ /**
197
+ * Serialize the model to JSON.
198
+ */
199
+ toJSON(): nbformat.IRawCell;
200
+ }
201
+
202
+ export function isCodeCellModel(model: ICellModel): model is ICodeCellModel {
203
+ return model.type === 'code';
204
+ }
205
+
206
+ export function isMarkdownCellModel(
207
+ model: ICellModel
208
+ ): model is IMarkdownCellModel {
209
+ return model.type === 'markdown';
210
+ }
211
+
212
+ export function isRawCellModel(model: ICellModel): model is IRawCellModel {
213
+ return model.type === 'raw';
214
+ }
215
+
216
+ /**
217
+ * An implementation of the cell model.
218
+ */
219
+ export abstract class CellModel extends CodeEditor.Model implements ICellModel {
220
+ constructor(options: CellModel.IOptions<ISharedCell> = {}) {
221
+ const { cell_type, sharedModel, ...others } = options;
222
+ super({
223
+ sharedModel:
224
+ sharedModel ??
225
+ createStandaloneCell({
226
+ cell_type: cell_type ?? 'raw',
227
+ id: options.id
228
+ }),
229
+ ...others
230
+ });
231
+ this.standaloneModel = typeof options.sharedModel === 'undefined';
232
+ this.trusted = !!this.getMetadata('trusted') || !!options.trusted;
233
+
234
+ this.sharedModel.changed.connect(this.onGenericChange, this);
235
+ this.sharedModel.metadataChanged.connect(this._onMetadataChanged, this);
236
+ }
237
+
238
+ readonly sharedModel: ISharedCell;
239
+
240
+ /**
241
+ * The type of cell.
242
+ */
243
+ abstract get type(): nbformat.CellType;
244
+
245
+ /**
246
+ * A signal emitted when the state of the model changes.
247
+ */
248
+ readonly contentChanged = new Signal<this, void>(this);
249
+
250
+ /**
251
+ * Signal emitted when cell metadata changes.
252
+ */
253
+ get metadataChanged(): ISignal<ICellModel, IMapChange> {
254
+ return this._metadataChanged;
255
+ }
256
+
257
+ /**
258
+ * A signal emitted when a model state changes.
259
+ */
260
+ readonly stateChanged = new Signal<
261
+ this,
262
+ IChangedArgs<any, any, 'isDirty' | 'trusted' | 'executionCount'>
263
+ >(this);
264
+
265
+ /**
266
+ * The id for the cell.
267
+ */
268
+ get id(): string {
269
+ return this.sharedModel.getId();
270
+ }
271
+
272
+ /**
273
+ * The metadata associated with the cell.
274
+ */
275
+ get metadata(): Omit<nbformat.IBaseCellMetadata, 'trusted'> {
276
+ return this.sharedModel.metadata;
277
+ }
278
+
279
+ /**
280
+ * The trusted state of the model.
281
+ */
282
+ get trusted(): boolean {
283
+ return this._trusted;
284
+ }
285
+ set trusted(newValue: boolean) {
286
+ const oldValue = this.trusted;
287
+ if (oldValue !== newValue) {
288
+ this._trusted = newValue;
289
+ this.onTrustedChanged(this, { newValue, oldValue });
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Dispose of the resources held by the model.
295
+ */
296
+ dispose(): void {
297
+ if (this.isDisposed) {
298
+ return;
299
+ }
300
+ this.sharedModel.changed.disconnect(this.onGenericChange, this);
301
+ this.sharedModel.metadataChanged.disconnect(this._onMetadataChanged, this);
302
+ super.dispose();
303
+ }
304
+
305
+ /**
306
+ * Handle a change to the trusted state.
307
+ *
308
+ * The default implementation is a no-op.
309
+ */
310
+ onTrustedChanged(
311
+ trusted: CellModel,
312
+ args: ObservableValue.IChangedArgs
313
+ ): void {
314
+ /* no-op */
315
+ }
316
+
317
+ /**
318
+ * Delete a metadata
319
+ *
320
+ * @param key Metadata key
321
+ */
322
+ deleteMetadata(key: string): any {
323
+ return this.sharedModel.deleteMetadata(key);
324
+ }
325
+
326
+ /**
327
+ * Get a metadata
328
+ *
329
+ * ### Notes
330
+ * This returns a copy of the key value.
331
+ *
332
+ * @param key Metadata key
333
+ */
334
+ getMetadata(key: string): any {
335
+ return this.sharedModel.getMetadata(key);
336
+ }
337
+
338
+ /**
339
+ * Set a metadata
340
+ *
341
+ * @param key Metadata key
342
+ * @param value Metadata value
343
+ */
344
+ setMetadata(key: string, value: any): void {
345
+ if (typeof value === 'undefined') {
346
+ this.sharedModel.deleteMetadata(key);
347
+ } else {
348
+ this.sharedModel.setMetadata(key, value);
349
+ }
350
+ }
351
+
352
+ /**
353
+ * Serialize the model to JSON.
354
+ */
355
+ toJSON(): nbformat.ICell {
356
+ return this.sharedModel.toJSON();
357
+ }
358
+
359
+ /**
360
+ * Handle a change to the observable value.
361
+ */
362
+ protected onGenericChange(): void {
363
+ this.contentChanged.emit(void 0);
364
+ }
365
+
366
+ private _onMetadataChanged(sender: ISharedCell, change: IMapChange) {
367
+ this._metadataChanged.emit(change);
368
+ }
369
+
370
+ private _metadataChanged = new Signal<this, IMapChange>(this);
371
+ private _trusted = false;
372
+ }
373
+
374
+ /**
375
+ * The namespace for `CellModel` statics.
376
+ */
377
+ export namespace CellModel {
378
+ /**
379
+ * The options used to initialize a `CellModel`.
380
+ */
381
+ export interface IOptions<T extends ISharedCell> {
382
+ /**
383
+ * A unique identifier for the model.
384
+ */
385
+ id?: string;
386
+
387
+ /**
388
+ * The cell shared model.
389
+ */
390
+ sharedModel?: T;
391
+
392
+ /**
393
+ * The cell type
394
+ */
395
+ cell_type?: string;
396
+
397
+ /**
398
+ * Whether the cell is trusted or not.
399
+ */
400
+ trusted?: boolean;
401
+ }
402
+ }
403
+
404
+ /**
405
+ * A base implementation for cell models with attachments.
406
+ */
407
+ export abstract class AttachmentsCellModel extends CellModel {
408
+ /**
409
+ * Construct a new cell with optional attachments.
410
+ */
411
+ constructor(options: AttachmentsCellModel.IOptions<ISharedCell>) {
412
+ super(options);
413
+ const factory =
414
+ options.contentFactory ?? AttachmentsCellModel.defaultContentFactory;
415
+ const values = (
416
+ this.sharedModel as ISharedAttachmentsCell
417
+ ).getAttachments();
418
+ this._attachments = factory.createAttachmentsModel({ values });
419
+ this._attachments.stateChanged.connect(this.onGenericChange, this);
420
+ this._attachments.changed.connect(this._onAttachmentsChange, this);
421
+
422
+ this.sharedModel.changed.connect(this._onSharedModelChanged, this);
423
+ }
424
+
425
+ /**
426
+ * Get the attachments of the model.
427
+ */
428
+ get attachments(): IAttachmentsModel {
429
+ return this._attachments;
430
+ }
431
+
432
+ /**
433
+ * Dispose of the resources held by the model.
434
+ */
435
+ dispose(): void {
436
+ if (this.isDisposed) {
437
+ return;
438
+ }
439
+ this._attachments.stateChanged.disconnect(this.onGenericChange, this);
440
+ this._attachments.changed.disconnect(this._onAttachmentsChange, this);
441
+ this._attachments.dispose();
442
+ this.sharedModel.changed.disconnect(this._onSharedModelChanged, this);
443
+ super.dispose();
444
+ }
445
+
446
+ /**
447
+ * Serialize the model to JSON.
448
+ */
449
+ toJSON(): nbformat.IRawCell | nbformat.IMarkdownCell {
450
+ return super.toJSON() as nbformat.IRawCell | nbformat.IMarkdownCell;
451
+ }
452
+
453
+ /**
454
+ * Handle a change to the cell outputs modelDB and reflect it in the shared model.
455
+ */
456
+ private _onAttachmentsChange(
457
+ sender: IAttachmentsModel,
458
+ event: IAttachmentsModel.ChangedArgs
459
+ ): void {
460
+ const cell = this.sharedModel as ISharedAttachmentsCell;
461
+ globalModelDBMutex(() => cell.setAttachments(sender.toJSON()));
462
+ }
463
+
464
+ /**
465
+ * Handle a change to the code cell value.
466
+ */
467
+ private _onSharedModelChanged(
468
+ slot: ISharedAttachmentsCell,
469
+ change: CellChange
470
+ ): void {
471
+ if (change.attachmentsChange) {
472
+ const cell = this.sharedModel as ISharedAttachmentsCell;
473
+ globalModelDBMutex(() =>
474
+ this._attachments.fromJSON(cell.getAttachments() ?? {})
475
+ );
476
+ }
477
+ }
478
+
479
+ private _attachments: IAttachmentsModel;
480
+ }
481
+
482
+ /**
483
+ * The namespace for `AttachmentsCellModel` statics.
484
+ */
485
+ export namespace AttachmentsCellModel {
486
+ /**
487
+ * The options used to initialize a `AttachmentsCellModel`.
488
+ */
489
+ export interface IOptions<T extends ISharedCell>
490
+ extends CellModel.IOptions<T> {
491
+ /**
492
+ * The factory for attachment model creation.
493
+ */
494
+ contentFactory?: IContentFactory;
495
+ }
496
+
497
+ /**
498
+ * A factory for creating code cell model content.
499
+ */
500
+ export interface IContentFactory {
501
+ /**
502
+ * Create an output area.
503
+ */
504
+ createAttachmentsModel(
505
+ options: IAttachmentsModel.IOptions
506
+ ): IAttachmentsModel;
507
+ }
508
+
509
+ /**
510
+ * The default implementation of an `IContentFactory`.
511
+ */
512
+ export class ContentFactory implements IContentFactory {
513
+ /**
514
+ * Create an attachments model.
515
+ */
516
+ createAttachmentsModel(
517
+ options: IAttachmentsModel.IOptions
518
+ ): IAttachmentsModel {
519
+ return new AttachmentsModel(options);
520
+ }
521
+ }
522
+
523
+ /**
524
+ * The shared `ContentFactory` instance.
525
+ */
526
+ export const defaultContentFactory = new ContentFactory();
527
+ }
528
+
529
+ /**
530
+ * An implementation of a raw cell model.
531
+ */
532
+ export class RawCellModel extends AttachmentsCellModel {
533
+ /**
534
+ * Construct a raw cell model from optional shared model.
535
+ */
536
+ constructor(
537
+ options: Omit<
538
+ AttachmentsCellModel.IOptions<ISharedRawCell>,
539
+ 'cell_type'
540
+ > = {}
541
+ ) {
542
+ super({
543
+ cell_type: 'raw',
544
+ ...options
545
+ });
546
+ }
547
+
548
+ /**
549
+ * The type of the cell.
550
+ */
551
+ get type(): 'raw' {
552
+ return 'raw';
553
+ }
554
+
555
+ /**
556
+ * Serialize the model to JSON.
557
+ */
558
+ toJSON(): nbformat.IRawCell {
559
+ return super.toJSON() as nbformat.IRawCell;
560
+ }
561
+ }
562
+
563
+ /**
564
+ * An implementation of a markdown cell model.
565
+ */
566
+ export class MarkdownCellModel extends AttachmentsCellModel {
567
+ /**
568
+ * Construct a markdown cell model from optional shared model.
569
+ */
570
+ constructor(
571
+ options: Omit<
572
+ AttachmentsCellModel.IOptions<ISharedMarkdownCell>,
573
+ 'cell_type'
574
+ > = {}
575
+ ) {
576
+ super({
577
+ cell_type: 'markdown',
578
+ ...options
579
+ });
580
+ // Use the Github-flavored markdown mode.
581
+ this.mimeType = 'text/x-ipythongfm';
582
+ }
583
+
584
+ /**
585
+ * The type of the cell.
586
+ */
587
+ get type(): 'markdown' {
588
+ return 'markdown';
589
+ }
590
+
591
+ /**
592
+ * Serialize the model to JSON.
593
+ */
594
+ toJSON(): nbformat.IMarkdownCell {
595
+ return super.toJSON() as nbformat.IMarkdownCell;
596
+ }
597
+ }
598
+
599
+ /**
600
+ * An implementation of a code cell Model.
601
+ */
602
+ export class CodeCellModel extends CellModel implements ICodeCellModel {
603
+ /**
604
+ * Construct a new code cell with optional original cell content.
605
+ */
606
+ constructor(options: CodeCellModel.IOptions = {}) {
607
+ super({
608
+ cell_type: 'code',
609
+ ...options
610
+ });
611
+
612
+ const factory =
613
+ options?.contentFactory ?? CodeCellModel.defaultContentFactory;
614
+ const trusted = this.trusted;
615
+ const outputs = this.sharedModel.getOutputs();
616
+ this._outputs = factory.createOutputArea({ trusted, values: outputs });
617
+
618
+ this.sharedModel.changed.connect(this._onSharedModelChanged, this);
619
+ this._outputs.changed.connect(this.onGenericChange, this);
620
+ this._outputs.changed.connect(this.onOutputsChange, this);
621
+ }
622
+
623
+ /**
624
+ * The type of the cell.
625
+ */
626
+ get type(): 'code' {
627
+ return 'code';
628
+ }
629
+
630
+ /**
631
+ * The execution count of the cell.
632
+ */
633
+ get executionCount(): nbformat.ExecutionCount {
634
+ return this.sharedModel.execution_count || null;
635
+ }
636
+ set executionCount(newValue: nbformat.ExecutionCount) {
637
+ this.sharedModel.execution_count = newValue || null;
638
+ }
639
+
640
+ /**
641
+ * Whether the cell is dirty or not.
642
+ *
643
+ * A cell is dirty if it is output is not empty and does not
644
+ * result of the input code execution.
645
+ */
646
+ get isDirty(): boolean {
647
+ // Test could be done dynamically with this._executedCode
648
+ // but for performance reason, the diff status is stored in a boolean.
649
+ return this._isDirty;
650
+ }
651
+
652
+ /**
653
+ * The cell outputs.
654
+ */
655
+ get outputs(): IOutputAreaModel {
656
+ return this._outputs;
657
+ }
658
+
659
+ readonly sharedModel: ISharedCodeCell;
660
+
661
+ clearExecution(): void {
662
+ this.outputs.clear();
663
+ this.executionCount = null;
664
+ this._setDirty(false);
665
+ this.sharedModel.deleteMetadata('execution');
666
+ }
667
+
668
+ /**
669
+ * Dispose of the resources held by the model.
670
+ */
671
+ dispose(): void {
672
+ if (this.isDisposed) {
673
+ return;
674
+ }
675
+ this.sharedModel.changed.disconnect(this._onSharedModelChanged, this);
676
+ this._outputs.changed.disconnect(this.onGenericChange, this);
677
+ this._outputs.changed.disconnect(this.onOutputsChange, this);
678
+ this._outputs.dispose();
679
+ this._outputs = null!;
680
+ super.dispose();
681
+ }
682
+
683
+ /**
684
+ * Handle a change to the trusted state.
685
+ */
686
+ onTrustedChanged(
687
+ trusted: CellModel,
688
+ args: ObservableValue.IChangedArgs
689
+ ): void {
690
+ const newTrusted = args.newValue as boolean;
691
+ if (this._outputs) {
692
+ this._outputs.trusted = newTrusted;
693
+ }
694
+ if (newTrusted) {
695
+ const codeCell = this.sharedModel as YCodeCell;
696
+ const metadata = codeCell.getMetadata();
697
+ metadata.trusted = true;
698
+ codeCell.setMetadata(metadata);
699
+ }
700
+ this.stateChanged.emit({
701
+ name: 'trusted',
702
+ oldValue: args.oldValue as boolean,
703
+ newValue: newTrusted
704
+ });
705
+ }
706
+
707
+ /**
708
+ * Serialize the model to JSON.
709
+ */
710
+ toJSON(): nbformat.ICodeCell {
711
+ return super.toJSON() as nbformat.ICodeCell;
712
+ }
713
+
714
+ /**
715
+ * Handle a change to the cell outputs modelDB and reflect it in the shared model.
716
+ */
717
+ protected onOutputsChange(
718
+ sender: IOutputAreaModel,
719
+ event: IOutputAreaModel.ChangedArgs
720
+ ): void {
721
+ const codeCell = this.sharedModel as YCodeCell;
722
+ globalModelDBMutex(() => {
723
+ switch (event.type) {
724
+ case 'add': {
725
+ const outputs = event.newValues.map(output => output.toJSON());
726
+ codeCell.updateOutputs(event.newIndex, event.newIndex, outputs);
727
+ break;
728
+ }
729
+ case 'set': {
730
+ const newValues = event.newValues.map(output => output.toJSON());
731
+ codeCell.updateOutputs(
732
+ event.oldIndex,
733
+ event.oldIndex + newValues.length,
734
+ newValues
735
+ );
736
+ break;
737
+ }
738
+ case 'remove':
739
+ codeCell.updateOutputs(event.oldIndex, event.oldValues.length);
740
+ break;
741
+ default:
742
+ throw new Error(`Invalid event type: ${event.type}`);
743
+ }
744
+ });
745
+ }
746
+
747
+ /**
748
+ * Handle a change to the code cell value.
749
+ */
750
+ private _onSharedModelChanged(
751
+ slot: ISharedCodeCell,
752
+ change: CellChange
753
+ ): void {
754
+ if (change.outputsChange) {
755
+ globalModelDBMutex(() => {
756
+ this.outputs.clear();
757
+ slot.getOutputs().forEach(output => this._outputs.add(output));
758
+ });
759
+ }
760
+
761
+ if (change.executionCountChange) {
762
+ if (
763
+ change.executionCountChange.newValue &&
764
+ (this.isDirty || !change.executionCountChange.oldValue)
765
+ ) {
766
+ this._setDirty(false);
767
+ }
768
+ this.stateChanged.emit({
769
+ name: 'executionCount',
770
+ oldValue: change.executionCountChange.oldValue,
771
+ newValue: change.executionCountChange.newValue
772
+ });
773
+ }
774
+
775
+ if (change.sourceChange && this.executionCount !== null) {
776
+ this._setDirty(
777
+ this._executedCode !== this.sharedModel.getSource().trim()
778
+ );
779
+ }
780
+ }
781
+
782
+ /**
783
+ * Set whether the cell is dirty or not.
784
+ */
785
+ private _setDirty(v: boolean) {
786
+ if (!v) {
787
+ this._executedCode = this.sharedModel.getSource().trim();
788
+ }
789
+ if (v !== this._isDirty) {
790
+ this._isDirty = v;
791
+ this.stateChanged.emit({
792
+ name: 'isDirty',
793
+ oldValue: !v,
794
+ newValue: v
795
+ });
796
+ }
797
+ }
798
+
799
+ private _executedCode = '';
800
+ private _isDirty = false;
801
+ private _outputs: IOutputAreaModel;
802
+ }
803
+
804
+ /**
805
+ * The namespace for `CodeCellModel` statics.
806
+ */
807
+ export namespace CodeCellModel {
808
+ /**
809
+ * The options used to initialize a `CodeCellModel`.
810
+ */
811
+ export interface IOptions
812
+ extends Omit<CellModel.IOptions<ISharedCodeCell>, 'cell_type'> {
813
+ /**
814
+ * The factory for output area model creation.
815
+ */
816
+ contentFactory?: IContentFactory;
817
+ }
818
+
819
+ /**
820
+ * A factory for creating code cell model content.
821
+ */
822
+ export interface IContentFactory {
823
+ /**
824
+ * Create an output area.
825
+ */
826
+ createOutputArea(options: IOutputAreaModel.IOptions): IOutputAreaModel;
827
+ }
828
+
829
+ /**
830
+ * The default implementation of an `IContentFactory`.
831
+ */
832
+ export class ContentFactory implements IContentFactory {
833
+ /**
834
+ * Create an output area.
835
+ */
836
+ createOutputArea(options: IOutputAreaModel.IOptions): IOutputAreaModel {
837
+ return new OutputAreaModel(options);
838
+ }
839
+ }
840
+
841
+ /**
842
+ * The shared `ContentFactory` instance.
843
+ */
844
+ export const defaultContentFactory = new ContentFactory();
845
+ }