forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.ts
2120 lines (1930 loc) · 58.4 KB
/
widget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ArrayExt, each } from '@phosphor/algorithm';
import { JSONValue } from '@phosphor/coreutils';
import { Message } from '@phosphor/messaging';
import { MimeData } from '@phosphor/coreutils';
import { AttachedProperty } from '@phosphor/properties';
import { ISignal, Signal } from '@phosphor/signaling';
import { Drag, IDragEvent } from '@phosphor/dragdrop';
import { PanelLayout, Widget } from '@phosphor/widgets';
import { h, VirtualDOM } from '@phosphor/virtualdom';
import {
ICellModel,
Cell,
IMarkdownCellModel,
CodeCell,
MarkdownCell,
ICodeCellModel,
RawCell,
IRawCellModel
} from '@jupyterlab/cells';
import { IEditorMimeTypeService, CodeEditor } from '@jupyterlab/codeeditor';
import { IChangedArgs, nbformat } from '@jupyterlab/coreutils';
import { IObservableMap, IObservableList } from '@jupyterlab/observables';
import { RenderMimeRegistry } from '@jupyterlab/rendermime';
import { INotebookModel } from './model';
/**
* The data attribute added to a widget that has an active kernel.
*/
const KERNEL_USER = 'jpKernelUser';
/**
* The data attribute added to a widget that can run code.
*/
const CODE_RUNNER = 'jpCodeRunner';
/**
* The data attribute added to a widget that can undo.
*/
const UNDOER = 'jpUndoer';
/**
* The class name added to notebook widgets.
*/
const NB_CLASS = 'jp-Notebook';
/**
* The class name added to notebook widget cells.
*/
const NB_CELL_CLASS = 'jp-Notebook-cell';
/**
* The class name added to a notebook in edit mode.
*/
const EDIT_CLASS = 'jp-mod-editMode';
/**
* The class name added to a notebook in command mode.
*/
const COMMAND_CLASS = 'jp-mod-commandMode';
/**
* The class name added to the active cell.
*/
const ACTIVE_CLASS = 'jp-mod-active';
/**
* The class name added to selected cells.
*/
const SELECTED_CLASS = 'jp-mod-selected';
/**
* The class name added to an active cell when there are other selected cells.
*/
const OTHER_SELECTED_CLASS = 'jp-mod-multiSelected';
/**
* The class name added to unconfined images.
*/
const UNCONFINED_CLASS = 'jp-mod-unconfined';
/**
* The class name added to a drop target.
*/
const DROP_TARGET_CLASS = 'jp-mod-dropTarget';
/**
* The class name added to a drop source.
*/
const DROP_SOURCE_CLASS = 'jp-mod-dropSource';
/**
* The class name added to drag images.
*/
const DRAG_IMAGE_CLASS = 'jp-dragImage';
/**
* The class name added to singular drag images
*/
const SINGLE_DRAG_IMAGE_CLASS = 'jp-dragImage-singlePrompt';
/**
* The class name added to the drag image cell content.
*/
const CELL_DRAG_CONTENT_CLASS = 'jp-dragImage-content';
/**
* The class name added to the drag image cell content.
*/
const CELL_DRAG_PROMPT_CLASS = 'jp-dragImage-prompt';
/**
* The class name added to the drag image cell content.
*/
const CELL_DRAG_MULTIPLE_BACK = 'jp-dragImage-multipleBack';
/**
* The mimetype used for Jupyter cell data.
*/
const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
/**
* The threshold in pixels to start a drag event.
*/
const DRAG_THRESHOLD = 5;
/**
* The interactivity modes for the notebook.
*/
export type NotebookMode = 'command' | 'edit';
/**
* A widget which renders static non-interactive notebooks.
*
* #### Notes
* The widget model must be set separately and can be changed
* at any time. Consumers of the widget must account for a
* `null` model, and may want to listen to the `modelChanged`
* signal.
*/
export class StaticNotebook extends Widget {
/**
* Construct a notebook widget.
*/
constructor(options: StaticNotebook.IOptions) {
super();
this.addClass(NB_CLASS);
this.node.dataset[KERNEL_USER] = 'true';
this.node.dataset[UNDOER] = 'true';
this.rendermime = options.rendermime;
this.layout = new Private.NotebookPanelLayout();
this.contentFactory =
options.contentFactory || StaticNotebook.defaultContentFactory;
this.editorConfig =
options.editorConfig || StaticNotebook.defaultEditorConfig;
this._mimetypeService = options.mimeTypeService;
}
/**
* A signal emitted when the model of the notebook changes.
*/
get modelChanged(): ISignal<this, void> {
return this._modelChanged;
}
/**
* A signal emitted when the model content changes.
*
* #### Notes
* This is a convenience signal that follows the current model.
*/
get modelContentChanged(): ISignal<this, void> {
return this._modelContentChanged;
}
/**
* The cell factory used by the widget.
*/
readonly contentFactory: StaticNotebook.IContentFactory;
/**
* The Rendermime instance used by the widget.
*/
readonly rendermime: RenderMimeRegistry;
/**
* The model for the widget.
*/
get model(): INotebookModel {
return this._model;
}
set model(newValue: INotebookModel) {
newValue = newValue || null;
if (this._model === newValue) {
return;
}
let oldValue = this._model;
this._model = newValue;
if (oldValue && oldValue.modelDB.isCollaborative) {
oldValue.modelDB.connected.then(() => {
oldValue.modelDB.collaborators.changed.disconnect(
this._onCollaboratorsChanged,
this
);
});
}
if (newValue && newValue.modelDB.isCollaborative) {
newValue.modelDB.connected.then(() => {
newValue.modelDB.collaborators.changed.connect(
this._onCollaboratorsChanged,
this
);
});
}
// Trigger private, protected, and public changes.
this._onModelChanged(oldValue, newValue);
this.onModelChanged(oldValue, newValue);
this._modelChanged.emit(void 0);
}
/**
* Get the mimetype for code cells.
*/
get codeMimetype(): string {
return this._mimetype;
}
/**
* A read-only sequence of the widgets in the notebook.
*/
get widgets(): ReadonlyArray<Cell> {
return (this.layout as PanelLayout).widgets as ReadonlyArray<Cell>;
}
/**
* A configuration object for cell editor settings.
*/
get editorConfig(): StaticNotebook.IEditorConfig {
return this._editorConfig;
}
set editorConfig(value: StaticNotebook.IEditorConfig) {
this._editorConfig = value;
this._updateEditorConfig();
}
/**
* Dispose of the resources held by the widget.
*/
dispose() {
// Do nothing if already disposed.
if (this.isDisposed) {
return;
}
this._model = null;
super.dispose();
}
/**
* Handle a new model.
*
* #### Notes
* This method is called after the model change has been handled
* internally and before the `modelChanged` signal is emitted.
* The default implementation is a no-op.
*/
protected onModelChanged(
oldValue: INotebookModel,
newValue: INotebookModel
): void {
// No-op.
}
/**
* Handle changes to the notebook model content.
*
* #### Notes
* The default implementation emits the `modelContentChanged` signal.
*/
protected onModelContentChanged(model: INotebookModel, args: void): void {
this._modelContentChanged.emit(void 0);
}
/**
* Handle changes to the notebook model metadata.
*
* #### Notes
* The default implementation updates the mimetypes of the code cells
* when the `language_info` metadata changes.
*/
protected onMetadataChanged(
sender: IObservableMap<JSONValue>,
args: IObservableMap.IChangedArgs<JSONValue>
): void {
switch (args.key) {
case 'language_info':
this._updateMimetype();
break;
default:
break;
}
}
/**
* Handle a cell being inserted.
*
* The default implementation is a no-op
*/
protected onCellInserted(index: number, cell: Cell): void {
// This is a no-op.
}
/**
* Handle a cell being moved.
*
* The default implementation is a no-op
*/
protected onCellMoved(fromIndex: number, toIndex: number): void {
// This is a no-op.
}
/**
* Handle a cell being removed.
*
* The default implementation is a no-op
*/
protected onCellRemoved(index: number, cell: Cell): void {
// This is a no-op.
}
/**
* Handle a new model on the widget.
*/
private _onModelChanged(
oldValue: INotebookModel,
newValue: INotebookModel
): void {
let layout = this.layout as PanelLayout;
if (oldValue) {
oldValue.cells.changed.disconnect(this._onCellsChanged, this);
oldValue.metadata.changed.disconnect(this.onMetadataChanged, this);
oldValue.contentChanged.disconnect(this.onModelContentChanged, this);
// TODO: reuse existing cell widgets if possible. Remember to initially
// clear the history of each cell if we do this.
while (layout.widgets.length) {
this._removeCell(0);
}
}
if (!newValue) {
this._mimetype = 'text/plain';
return;
}
this._updateMimetype();
let cells = newValue.cells;
each(cells, (cell: ICellModel, i: number) => {
this._insertCell(i, cell);
});
cells.changed.connect(this._onCellsChanged, this);
newValue.contentChanged.connect(this.onModelContentChanged, this);
newValue.metadata.changed.connect(this.onMetadataChanged, this);
}
/**
* Handle a change cells event.
*/
private _onCellsChanged(
sender: IObservableList<ICellModel>,
args: IObservableList.IChangedArgs<ICellModel>
) {
let index = 0;
switch (args.type) {
case 'add':
index = args.newIndex;
each(args.newValues, value => {
this._insertCell(index++, value);
});
break;
case 'move':
this._moveCell(args.oldIndex, args.newIndex);
break;
case 'remove':
each(args.oldValues, value => {
this._removeCell(args.oldIndex);
});
break;
case 'set':
// TODO: reuse existing widgets if possible.
index = args.newIndex;
each(args.newValues, value => {
// Note: this ordering (insert then remove)
// is important for getting the active cell
// index for the editable notebook correct.
this._insertCell(index, value);
this._removeCell(index + 1);
index++;
});
break;
default:
return;
}
}
/**
* Create a cell widget and insert into the notebook.
*/
private _insertCell(index: number, cell: ICellModel): void {
let widget: Cell;
switch (cell.type) {
case 'code':
widget = this._createCodeCell(cell as ICodeCellModel);
widget.model.mimeType = this._mimetype;
break;
case 'markdown':
widget = this._createMarkdownCell(cell as IMarkdownCellModel);
break;
default:
widget = this._createRawCell(cell as IRawCellModel);
}
widget.addClass(NB_CELL_CLASS);
let layout = this.layout as PanelLayout;
layout.insertWidget(index, widget);
this.onCellInserted(index, widget);
}
/**
* Create a code cell widget from a code cell model.
*/
private _createCodeCell(model: ICodeCellModel): CodeCell {
let rendermime = this.rendermime;
let contentFactory = this.contentFactory;
const editorConfig = this.editorConfig.code;
let options = { editorConfig, model, rendermime, contentFactory };
return this.contentFactory.createCodeCell(options, this);
}
/**
* Create a markdown cell widget from a markdown cell model.
*/
private _createMarkdownCell(model: IMarkdownCellModel): MarkdownCell {
let rendermime = this.rendermime;
let contentFactory = this.contentFactory;
const editorConfig = this.editorConfig.markdown;
let options = { editorConfig, model, rendermime, contentFactory };
return this.contentFactory.createMarkdownCell(options, this);
}
/**
* Create a raw cell widget from a raw cell model.
*/
private _createRawCell(model: IRawCellModel): RawCell {
let contentFactory = this.contentFactory;
const editorConfig = this.editorConfig.raw;
let options = { editorConfig, model, contentFactory };
return this.contentFactory.createRawCell(options, this);
}
/**
* Move a cell widget.
*/
private _moveCell(fromIndex: number, toIndex: number): void {
let layout = this.layout as PanelLayout;
layout.insertWidget(toIndex, layout.widgets[fromIndex]);
this.onCellMoved(fromIndex, toIndex);
}
/**
* Remove a cell widget.
*/
private _removeCell(index: number): void {
let layout = this.layout as PanelLayout;
let widget = layout.widgets[index] as Cell;
widget.parent = null;
this.onCellRemoved(index, widget);
widget.dispose();
}
/**
* Update the mimetype of the notebook.
*/
private _updateMimetype(): void {
let info = this._model.metadata.get(
'language_info'
) as nbformat.ILanguageInfoMetadata;
if (!info) {
return;
}
this._mimetype = this._mimetypeService.getMimeTypeByLanguage(info);
each(this.widgets, widget => {
if (widget.model.type === 'code') {
widget.model.mimeType = this._mimetype;
}
});
}
/**
* Handle an update to the collaborators.
*/
private _onCollaboratorsChanged(): void {
// If there are selections corresponding to non-collaborators,
// they are stale and should be removed.
for (let i = 0; i < this.widgets.length; i++) {
let cell = this.widgets[i];
for (let key of cell.model.selections.keys()) {
if (!this._model.modelDB.collaborators.has(key)) {
cell.model.selections.delete(key);
}
}
}
}
/**
* Update editor settings for notebook cells.
*/
private _updateEditorConfig() {
for (let i = 0; i < this.widgets.length; i++) {
const cell = this.widgets[i];
let config: Partial<CodeEditor.IConfig>;
switch (cell.model.type) {
case 'code':
config = this._editorConfig.code;
break;
case 'markdown':
config = this._editorConfig.markdown;
break;
default:
config = this._editorConfig.raw;
break;
}
Object.keys(config).forEach((key: keyof CodeEditor.IConfig) => {
cell.editor.setOption(key, config[key]);
});
}
}
private _editorConfig = StaticNotebook.defaultEditorConfig;
private _mimetype = 'text/plain';
private _model: INotebookModel = null;
private _mimetypeService: IEditorMimeTypeService;
private _modelChanged = new Signal<this, void>(this);
private _modelContentChanged = new Signal<this, void>(this);
}
/**
* The namespace for the `StaticNotebook` class statics.
*/
export namespace StaticNotebook {
/**
* An options object for initializing a static notebook.
*/
export interface IOptions {
/**
* The rendermime instance used by the widget.
*/
rendermime: RenderMimeRegistry;
/**
* The language preference for the model.
*/
languagePreference?: string;
/**
* A factory for creating content.
*/
contentFactory?: IContentFactory;
/**
* A configuration object for the cell editor settings.
*/
editorConfig?: IEditorConfig;
/**
* The service used to look up mime types.
*/
mimeTypeService: IEditorMimeTypeService;
}
/**
* A factory for creating notebook content.
*
* #### Notes
* This extends the content factory of the cell itself, which extends the content
* factory of the output area and input area. The result is that there is a single
* factory for creating all child content of a notebook.
*/
export interface IContentFactory extends Cell.IContentFactory {
/**
* Create a new code cell widget.
*/
createCodeCell(
options: CodeCell.IOptions,
parent: StaticNotebook
): CodeCell;
/**
* Create a new markdown cell widget.
*/
createMarkdownCell(
options: MarkdownCell.IOptions,
parent: StaticNotebook
): MarkdownCell;
/**
* Create a new raw cell widget.
*/
createRawCell(options: RawCell.IOptions, parent: StaticNotebook): RawCell;
}
/**
* A config object for the cell editors.
*/
export interface IEditorConfig {
/**
* Config options for code cells.
*/
readonly code: Partial<CodeEditor.IConfig>;
/**
* Config options for markdown cells.
*/
readonly markdown: Partial<CodeEditor.IConfig>;
/**
* Config options for raw cells.
*/
readonly raw: Partial<CodeEditor.IConfig>;
}
/**
* Default configuration options for cell editors.
*/
export const defaultEditorConfig: IEditorConfig = {
code: {
...CodeEditor.defaultConfig,
lineWrap: 'off',
matchBrackets: true,
autoClosingBrackets: true
},
markdown: {
...CodeEditor.defaultConfig,
lineWrap: 'on',
matchBrackets: false,
autoClosingBrackets: false
},
raw: {
...CodeEditor.defaultConfig,
lineWrap: 'on',
matchBrackets: false,
autoClosingBrackets: false
}
};
/**
* The default implementation of an `IContentFactory`.
*/
export class ContentFactory extends Cell.ContentFactory
implements IContentFactory {
/**
* Create a new code cell widget.
*
* #### Notes
* If no cell content factory is passed in with the options, the one on the
* notebook content factory is used.
*/
createCodeCell(
options: CodeCell.IOptions,
parent: StaticNotebook
): CodeCell {
if (!options.contentFactory) {
options.contentFactory = this;
}
return new CodeCell(options);
}
/**
* Create a new markdown cell widget.
*
* #### Notes
* If no cell content factory is passed in with the options, the one on the
* notebook content factory is used.
*/
createMarkdownCell(
options: MarkdownCell.IOptions,
parent: StaticNotebook
): MarkdownCell {
if (!options.contentFactory) {
options.contentFactory = this;
}
return new MarkdownCell(options);
}
/**
* Create a new raw cell widget.
*
* #### Notes
* If no cell content factory is passed in with the options, the one on the
* notebook content factory is used.
*/
createRawCell(options: RawCell.IOptions, parent: StaticNotebook): RawCell {
if (!options.contentFactory) {
options.contentFactory = this;
}
return new RawCell(options);
}
}
/**
* A namespace for the staic notebook content factory.
*/
export namespace ContentFactory {
/**
* Options for the content factory.
*/
export interface IOptions extends Cell.ContentFactory.IOptions {}
}
/**
* Default content factory for the static notebook widget.
*/
export const defaultContentFactory: IContentFactory = new ContentFactory();
}
/**
* A notebook widget that supports interactivity.
*/
export class Notebook extends StaticNotebook {
/**
* Construct a notebook widget.
*/
constructor(options: Notebook.IOptions) {
super(Private.processNotebookOptions(options));
this.node.tabIndex = -1; // Allow the widget to take focus.
// Allow the node to scroll while dragging items.
this.node.setAttribute('data-p-dragscroll', 'true');
}
/**
* A signal emitted when the active cell changes.
*
* #### Notes
* This can be due to the active index changing or the
* cell at the active index changing.
*/
get activeCellChanged(): ISignal<this, Cell> {
return this._activeCellChanged;
}
/**
* A signal emitted when the state of the notebook changes.
*/
get stateChanged(): ISignal<this, IChangedArgs<any>> {
return this._stateChanged;
}
/**
* A signal emitted when the selection state of the notebook changes.
*/
get selectionChanged(): ISignal<this, void> {
return this._selectionChanged;
}
/**
* The interactivity mode of the notebook.
*/
get mode(): NotebookMode {
return this._mode;
}
set mode(newValue: NotebookMode) {
let activeCell = this.activeCell;
if (!activeCell) {
newValue = 'command';
}
if (newValue === this._mode) {
this._ensureFocus();
return;
}
// Post an update request.
this.update();
let oldValue = this._mode;
this._mode = newValue;
if (newValue === 'edit') {
// Edit mode deselects all cells.
each(this.widgets, widget => {
this.deselect(widget);
});
// Edit mode unrenders an active markdown widget.
if (activeCell instanceof MarkdownCell) {
activeCell.rendered = false;
}
activeCell.inputHidden = false;
} else {
// Focus on the notebook document, which blurs the active cell.
this.node.focus();
}
this._stateChanged.emit({ name: 'mode', oldValue, newValue });
this._ensureFocus();
}
/**
* The active cell index of the notebook.
*
* #### Notes
* The index will be clamped to the bounds of the notebook cells.
*/
get activeCellIndex(): number {
if (!this.model) {
return -1;
}
return this.model.cells.length ? this._activeCellIndex : -1;
}
set activeCellIndex(newValue: number) {
let oldValue = this._activeCellIndex;
if (!this.model || !this.model.cells.length) {
newValue = -1;
} else {
newValue = Math.max(newValue, 0);
newValue = Math.min(newValue, this.model.cells.length - 1);
}
this._activeCellIndex = newValue;
let cell = this.widgets[newValue];
if (cell !== this._activeCell) {
// Post an update request.
this.update();
this._activeCell = cell;
this._activeCellChanged.emit(cell);
}
if (this.mode === 'edit' && cell instanceof MarkdownCell) {
cell.rendered = false;
}
this._ensureFocus();
if (newValue === oldValue) {
return;
}
this._trimSelections();
this._stateChanged.emit({ name: 'activeCellIndex', oldValue, newValue });
}
/**
* Get the active cell widget.
*
* #### Notes
* This is a cell or `null` if there is no active cell.
*/
get activeCell(): Cell | null {
return this._activeCell;
}
/**
* Dispose of the resources held by the widget.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._activeCell = null;
super.dispose();
}
/**
* Select a cell widget.
*
* #### Notes
* It is a no-op if the value does not change.
* It will emit the `selectionChanged` signal.
*/
select(widget: Cell): void {
if (Private.selectedProperty.get(widget)) {
return;
}
Private.selectedProperty.set(widget, true);
this._selectionChanged.emit(void 0);
this.update();
}
/**
* Deselect a cell widget.
*
* #### Notes
* It is a no-op if the value does not change.
* It will emit the `selectionChanged` signal.
*/
deselect(widget: Cell): void {
if (!Private.selectedProperty.get(widget)) {
return;
}
Private.selectedProperty.set(widget, false);
this._selectionChanged.emit(void 0);
this.update();
}
/**
* Whether a cell is selected.
*/
isSelected(widget: Cell): boolean {
return Private.selectedProperty.get(widget);
}
/**
* Whether a cell is selected or is the active cell.
*/
isSelectedOrActive(widget: Cell): boolean {
if (widget === this._activeCell) {
return true;
}
return Private.selectedProperty.get(widget);
}
/**
* Deselect all of the cells.
*/
deselectAll(): void {
let changed = false;
each(this.widgets, widget => {
if (Private.selectedProperty.get(widget)) {
changed = true;
}
Private.selectedProperty.set(widget, false);
});
if (changed) {
this._selectionChanged.emit(void 0);
}
// Make sure we have a valid active cell.
this.activeCellIndex = this.activeCellIndex;
this.update();
}
/**
* Move the head of an existing contiguous selection to extend the selection.
*
* @param index - The new head of the existing selection.
*
* #### Notes
* If there is no existing selection, the active cell is considered an
* existing one-cell selection.
*
* If the new selection is a single cell, that cell becomes the active cell
* and all cells are deselected.
*
* There is no change if there are no cells (i.e., activeCellIndex is -1).
*/
extendContiguousSelectionTo(index: number): void {
let { head, anchor } = this.getContiguousSelection();
let i: number;
// Handle the case of no current selection.
if (anchor === null) {
if (index === this.activeCellIndex) {
// Already collapsed selection, nothing more to do.
return;
}
// We will start a new selection below.
head = this.activeCellIndex;
anchor = this.activeCellIndex;
}
// Move the active cell. We do this before the collapsing shortcut below.
this.activeCellIndex = index;
// Make sure the index is valid, according to the rules for setting and clipping the
// active cell index. This may change the index.
index = this.activeCellIndex;
// Collapse the selection if it is only the active cell.
if (index === anchor) {
this.deselectAll();
return;
}
let selectionChanged = false;
if (head < index) {
if (head < anchor) {
Private.selectedProperty.set(this.widgets[head], false);
selectionChanged = true;
}
// Toggle everything strictly between head and index except anchor.
for (i = head + 1; i < index; i++) {
if (i !== anchor) {
Private.selectedProperty.set(
this.widgets[i],
!Private.selectedProperty.get(this.widgets[i])
);