forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.ts
1737 lines (1567 loc) · 45.3 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 marked from 'marked';
import { AttachmentsResolver } from '@jupyterlab/attachments';
import { ISessionContext } from '@jupyterlab/apputils';
import { IChangedArgs, ActivityMonitor, URLExt } from '@jupyterlab/coreutils';
import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
import { DirListing } from '@jupyterlab/filebrowser';
import * as nbformat from '@jupyterlab/nbformat';
import { IObservableMap, IObservableJSON } from '@jupyterlab/observables';
import {
OutputArea,
SimplifiedOutputArea,
IOutputPrompt,
OutputPrompt,
IStdin,
Stdin
} from '@jupyterlab/outputarea';
import {
IRenderMime,
MimeModel,
IRenderMimeRegistry,
imageRendererFactory
} from '@jupyterlab/rendermime';
import { KernelMessage, Kernel } from '@jupyterlab/services';
import {
JSONValue,
PromiseDelegate,
JSONObject,
UUID,
PartialJSONValue
} from '@lumino/coreutils';
import { some, filter, toArray } from '@lumino/algorithm';
import { IDragEvent } from '@lumino/dragdrop';
import { Message } from '@lumino/messaging';
import { PanelLayout, Panel, Widget } from '@lumino/widgets';
import { InputCollapser, OutputCollapser } from './collapser';
import {
CellHeader,
CellFooter,
ICellHeader,
ICellFooter
} from './headerfooter';
import { InputArea, IInputPrompt, InputPrompt } from './inputarea';
import {
IAttachmentsCellModel,
ICellModel,
ICodeCellModel,
IMarkdownCellModel,
IRawCellModel
} from './model';
import { InputPlaceholder, OutputPlaceholder } from './placeholder';
import { Signal } from '@lumino/signaling';
import { addIcon } from '@jupyterlab/ui-components';
/**
* The CSS class added to cell widgets.
*/
const CELL_CLASS = 'jp-Cell';
/**
* The CSS class added to the cell header.
*/
const CELL_HEADER_CLASS = 'jp-Cell-header';
/**
* The CSS class added to the cell footer.
*/
const CELL_FOOTER_CLASS = 'jp-Cell-footer';
/**
* The CSS class added to the cell input wrapper.
*/
const CELL_INPUT_WRAPPER_CLASS = 'jp-Cell-inputWrapper';
/**
* The CSS class added to the cell output wrapper.
*/
const CELL_OUTPUT_WRAPPER_CLASS = 'jp-Cell-outputWrapper';
/**
* The CSS class added to the cell input area.
*/
const CELL_INPUT_AREA_CLASS = 'jp-Cell-inputArea';
/**
* The CSS class added to the cell output area.
*/
const CELL_OUTPUT_AREA_CLASS = 'jp-Cell-outputArea';
/**
* The CSS class added to the cell input collapser.
*/
const CELL_INPUT_COLLAPSER_CLASS = 'jp-Cell-inputCollapser';
/**
* The CSS class added to the cell output collapser.
*/
const CELL_OUTPUT_COLLAPSER_CLASS = 'jp-Cell-outputCollapser';
/**
* The class name added to the cell when readonly.
*/
const READONLY_CLASS = 'jp-mod-readOnly';
/**
* The class name added to code cells.
*/
const CODE_CELL_CLASS = 'jp-CodeCell';
/**
* The class name added to markdown cells.
*/
const MARKDOWN_CELL_CLASS = 'jp-MarkdownCell';
/**
* The class name added to rendered markdown output widgets.
*/
const MARKDOWN_OUTPUT_CLASS = 'jp-MarkdownOutput';
const MARKDOWN_HEADING_COLLAPSED = 'jp-MarkdownHeadingCollapsed';
const HEADING_COLLAPSER_CLASS = 'jp-collapseHeadingButton';
const SHOW_HIDDEN_CELLS_CLASS = 'jp-showHiddenCellsButton';
/**
* The class name added to raw cells.
*/
const RAW_CELL_CLASS = 'jp-RawCell';
/**
* The class name added to a rendered input area.
*/
const RENDERED_CLASS = 'jp-mod-rendered';
const NO_OUTPUTS_CLASS = 'jp-mod-noOutputs';
/**
* The text applied to an empty markdown cell.
*/
const DEFAULT_MARKDOWN_TEXT = 'Type Markdown and LaTeX: $ α^2 $';
/**
* The timeout to wait for change activity to have ceased before rendering.
*/
const RENDER_TIMEOUT = 1000;
/**
* The mime type for a rich contents drag object.
*/
const CONTENTS_MIME_RICH = 'application/x-jupyter-icontentsrich';
/** ****************************************************************************
* Cell
******************************************************************************/
/**
* A base cell widget.
*/
export class Cell<T extends ICellModel = ICellModel> extends Widget {
/**
* Construct a new base cell widget.
*/
constructor(options: Cell.IOptions<T>) {
super();
this.addClass(CELL_CLASS);
const model = (this._model = options.model);
const contentFactory = (this.contentFactory =
options.contentFactory || Cell.defaultContentFactory);
this.layout = new PanelLayout();
// Header
const header = contentFactory.createCellHeader();
header.addClass(CELL_HEADER_CLASS);
(this.layout as PanelLayout).addWidget(header);
// Input
const inputWrapper = (this._inputWrapper = new Panel());
inputWrapper.addClass(CELL_INPUT_WRAPPER_CLASS);
const inputCollapser = new InputCollapser();
inputCollapser.addClass(CELL_INPUT_COLLAPSER_CLASS);
const input = (this._input = new InputArea({
model,
contentFactory,
updateOnShow: options.updateEditorOnShow
}));
input.addClass(CELL_INPUT_AREA_CLASS);
inputWrapper.addWidget(inputCollapser);
inputWrapper.addWidget(input);
(this.layout as PanelLayout).addWidget(inputWrapper);
this._inputPlaceholder = new InputPlaceholder(() => {
this.inputHidden = !this.inputHidden;
});
// Footer
const footer = this.contentFactory.createCellFooter();
footer.addClass(CELL_FOOTER_CLASS);
(this.layout as PanelLayout).addWidget(footer);
// Editor settings
if (options.editorConfig) {
let editorOptions: any = {};
Object.keys(options.editorConfig).forEach(
(key: keyof CodeEditor.IConfig) => {
editorOptions[key] = options.editorConfig?.[key] ?? null;
}
);
this.editor.setOptions(editorOptions);
}
model.metadata.changed.connect(this.onMetadataChanged, this);
}
/**
* Initialize view state from model.
*
* #### Notes
* Should be called after construction. For convenience, returns this, so it
* can be chained in the construction, like `new Foo().initializeState();`
*/
initializeState(): this {
this.loadCollapseState();
this.loadEditableState();
return this;
}
/**
* The content factory used by the widget.
*/
readonly contentFactory: Cell.IContentFactory;
/**
* Get the prompt node used by the cell.
*/
get promptNode(): HTMLElement {
if (!this._inputHidden) {
return this._input.promptNode;
} else {
return (this._inputPlaceholder!.node as HTMLElement)
.firstElementChild as HTMLElement;
}
}
/**
* Get the CodeEditorWrapper used by the cell.
*/
get editorWidget(): CodeEditorWrapper {
return this._input.editorWidget;
}
/**
* Get the CodeEditor used by the cell.
*/
get editor(): CodeEditor.IEditor {
return this._input.editor;
}
/**
* Get the model used by the cell.
*/
get model(): T {
return this._model;
}
/**
* Get the input area for the cell.
*/
get inputArea(): InputArea {
return this._input;
}
/**
* The read only state of the cell.
*/
get readOnly(): boolean {
return this._readOnly;
}
set readOnly(value: boolean) {
if (value === this._readOnly) {
return;
}
this._readOnly = value;
if (this.syncEditable) {
this.saveEditableState();
}
this.update();
}
/**
* Save view editable state to model
*/
saveEditableState() {
const { metadata } = this.model;
const current = metadata.get('editable');
if (
(this.readOnly && current === false) ||
(!this.readOnly && current === undefined)
) {
return;
}
if (this.readOnly) {
this.model.metadata.set('editable', false);
} else {
this.model.metadata.delete('editable');
}
}
/**
* Load view editable state from model.
*/
loadEditableState() {
this.readOnly = this.model.metadata.get('editable') === false;
}
/**
* A promise that resolves when the widget renders for the first time.
*/
get ready(): Promise<void> {
return Promise.resolve(undefined);
}
/**
* Set the prompt for the widget.
*/
setPrompt(value: string): void {
this._input.setPrompt(value);
}
/**
* The view state of input being hidden.
*/
get inputHidden(): boolean {
return this._inputHidden;
}
set inputHidden(value: boolean) {
if (this._inputHidden === value) {
return;
}
const layout = this._inputWrapper.layout as PanelLayout;
if (value) {
this._input.parent = null;
layout.addWidget(this._inputPlaceholder);
} else {
this._inputPlaceholder.parent = null;
layout.addWidget(this._input);
}
this._inputHidden = value;
if (this.syncCollapse) {
this.saveCollapseState();
}
this.handleInputHidden(value);
}
/**
* Save view collapse state to model
*/
saveCollapseState() {
const jupyter = { ...(this.model.metadata.get('jupyter') as any) };
if (
(this.inputHidden && jupyter.source_hidden === true) ||
(!this.inputHidden && jupyter.source_hidden === undefined)
) {
return;
}
if (this.inputHidden) {
jupyter.source_hidden = true;
} else {
delete jupyter.source_hidden;
}
if (Object.keys(jupyter).length === 0) {
this.model.metadata.delete('jupyter');
} else {
this.model.metadata.set('jupyter', jupyter);
}
}
/**
* Revert view collapse state from model.
*/
loadCollapseState() {
const jupyter = (this.model.metadata.get('jupyter') as any) || {};
this.inputHidden = !!jupyter.source_hidden;
}
/**
* Handle the input being hidden.
*
* #### Notes
* This is called by the `inputHidden` setter so that subclasses
* can perform actions upon the input being hidden without accessing
* private state.
*/
protected handleInputHidden(value: boolean): void {
return;
}
/**
* Whether to sync the collapse state to the cell model.
*/
get syncCollapse(): boolean {
return this._syncCollapse;
}
set syncCollapse(value: boolean) {
if (this._syncCollapse === value) {
return;
}
this._syncCollapse = value;
if (value) {
this.loadCollapseState();
}
}
/**
* Whether to sync the editable state to the cell model.
*/
get syncEditable(): boolean {
return this._syncEditable;
}
set syncEditable(value: boolean) {
if (this._syncEditable === value) {
return;
}
this._syncEditable = value;
if (value) {
this.loadEditableState();
}
}
/**
* Clone the cell, using the same model.
*/
clone(): Cell<T> {
const constructor = this.constructor as typeof Cell;
return new constructor({
model: this.model,
contentFactory: this.contentFactory
});
}
/**
* Dispose of the resources held by the widget.
*/
dispose() {
// Do nothing if already disposed.
if (this.isDisposed) {
return;
}
this._input = null!;
this._model = null!;
this._inputWrapper = null!;
this._inputPlaceholder = null!;
super.dispose();
}
/**
* Handle `after-attach` messages.
*/
protected onAfterAttach(msg: Message): void {
this.update();
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this.editor.focus();
}
/**
* Handle `fit-request` messages.
*/
protected onFitRequest(msg: Message): void {
// need this for for when a theme changes font size
this.editor.refresh();
}
/**
* Handle `update-request` messages.
*/
protected onUpdateRequest(msg: Message): void {
if (!this._model) {
return;
}
// Handle read only state.
if (this.editor.getOption('readOnly') !== this._readOnly) {
this.editor.setOption('readOnly', this._readOnly);
this.toggleClass(READONLY_CLASS, this._readOnly);
}
}
/**
* Handle changes in the metadata.
*/
protected onMetadataChanged(
model: IObservableJSON,
args: IObservableMap.IChangedArgs<PartialJSONValue | undefined>
): void {
switch (args.key) {
case 'jupyter':
if (this.syncCollapse) {
this.loadCollapseState();
}
break;
case 'editable':
if (this.syncEditable) {
this.loadEditableState();
}
break;
default:
break;
}
}
private _readOnly = false;
private _model: T;
private _inputHidden = false;
private _input: InputArea;
private _inputWrapper: Widget;
private _inputPlaceholder: InputPlaceholder;
private _syncCollapse = false;
private _syncEditable = false;
}
/**
* The namespace for the `Cell` class statics.
*/
export namespace Cell {
/**
* An options object for initializing a cell widget.
*/
export interface IOptions<T extends ICellModel> {
/**
* The model used by the cell.
*/
model: T;
/**
* The factory object for customizable cell children.
*/
contentFactory?: IContentFactory;
/**
* The configuration options for the text editor widget.
*/
editorConfig?: Partial<CodeEditor.IConfig>;
/**
* Whether to send an update request to the editor when it is shown.
*/
updateEditorOnShow?: boolean;
/**
* The maximum number of output items to display in cell output.
*/
maxNumberOutputs?: number;
}
/**
* The factory object for customizable cell children.
*
* This is used to allow users of cells to customize child content.
*
* This inherits from `OutputArea.IContentFactory` to avoid needless nesting and
* provide a single factory object for all notebook/cell/outputarea related
* widgets.
*/
export interface IContentFactory
extends OutputArea.IContentFactory,
InputArea.IContentFactory {
/**
* Create a new cell header for the parent widget.
*/
createCellHeader(): ICellHeader;
/**
* Create a new cell header for the parent widget.
*/
createCellFooter(): ICellFooter;
}
/**
* The default implementation of an `IContentFactory`.
*
* This includes a CodeMirror editor factory to make it easy to use out of the box.
*/
export class ContentFactory implements IContentFactory {
/**
* Create a content factory for a cell.
*/
constructor(options: ContentFactory.IOptions = {}) {
this._editorFactory =
options.editorFactory || InputArea.defaultEditorFactory;
}
/**
* The readonly editor factory that create code editors
*/
get editorFactory(): CodeEditor.Factory {
return this._editorFactory;
}
/**
* Create a new cell header for the parent widget.
*/
createCellHeader(): ICellHeader {
return new CellHeader();
}
/**
* Create a new cell header for the parent widget.
*/
createCellFooter(): ICellFooter {
return new CellFooter();
}
/**
* Create an input prompt.
*/
createInputPrompt(): IInputPrompt {
return new InputPrompt();
}
/**
* Create the output prompt for the widget.
*/
createOutputPrompt(): IOutputPrompt {
return new OutputPrompt();
}
/**
* Create an stdin widget.
*/
createStdin(options: Stdin.IOptions): IStdin {
return new Stdin(options);
}
private _editorFactory: CodeEditor.Factory;
}
/**
* A namespace for cell content factory.
*/
export namespace ContentFactory {
/**
* Options for the content factory.
*/
export interface IOptions {
/**
* The editor factory used by the content factory.
*
* If this is not passed, a default CodeMirror editor factory
* will be used.
*/
editorFactory?: CodeEditor.Factory;
}
}
/**
* The default content factory for cells.
*/
export const defaultContentFactory = new ContentFactory();
}
/** ****************************************************************************
* CodeCell
******************************************************************************/
/**
* A widget for a code cell.
*/
export class CodeCell extends Cell<ICodeCellModel> {
/**
* Construct a code cell widget.
*/
constructor(options: CodeCell.IOptions) {
super(options);
this.addClass(CODE_CELL_CLASS);
// Only save options not handled by parent constructor.
const rendermime = (this._rendermime = options.rendermime);
const contentFactory = this.contentFactory;
const model = this.model;
// Insert the output before the cell footer.
const outputWrapper = (this._outputWrapper = new Panel());
outputWrapper.addClass(CELL_OUTPUT_WRAPPER_CLASS);
const outputCollapser = new OutputCollapser();
outputCollapser.addClass(CELL_OUTPUT_COLLAPSER_CLASS);
const output = (this._output = new OutputArea({
model: model.outputs,
rendermime,
contentFactory: contentFactory,
maxNumberOutputs: options.maxNumberOutputs
}));
output.addClass(CELL_OUTPUT_AREA_CLASS);
// Set a CSS if there are no outputs, and connect a signal for future
// changes to the number of outputs. This is for conditional styling
// if there are no outputs.
if (model.outputs.length === 0) {
this.addClass(NO_OUTPUTS_CLASS);
}
output.outputLengthChanged.connect(this._outputLengthHandler, this);
outputWrapper.addWidget(outputCollapser);
outputWrapper.addWidget(output);
(this.layout as PanelLayout).insertWidget(2, outputWrapper);
this._outputPlaceholder = new OutputPlaceholder(() => {
this.outputHidden = !this.outputHidden;
});
model.stateChanged.connect(this.onStateChanged, this);
}
/**
* Initialize view state from model.
*
* #### Notes
* Should be called after construction. For convenience, returns this, so it
* can be chained in the construction, like `new Foo().initializeState();`
*/
initializeState(): this {
super.initializeState();
this.loadScrolledState();
this.setPrompt(`${this.model.executionCount || ''}`);
return this;
}
/**
* Get the output area for the cell.
*/
get outputArea(): OutputArea {
return this._output;
}
/**
* The view state of output being collapsed.
*/
get outputHidden(): boolean {
return this._outputHidden;
}
set outputHidden(value: boolean) {
if (this._outputHidden === value) {
return;
}
const layout = this._outputWrapper.layout as PanelLayout;
if (value) {
layout.removeWidget(this._output);
layout.addWidget(this._outputPlaceholder);
if (this.inputHidden && !this._outputWrapper.isHidden) {
this._outputWrapper.hide();
}
} else {
if (this._outputWrapper.isHidden) {
this._outputWrapper.show();
}
layout.removeWidget(this._outputPlaceholder);
layout.addWidget(this._output);
}
this._outputHidden = value;
if (this.syncCollapse) {
this.saveCollapseState();
}
}
/**
* Save view collapse state to model
*/
saveCollapseState() {
// Because collapse state for a code cell involves two different pieces of
// metadata (the `collapsed` and `jupyter` metadata keys), we block reacting
// to changes in metadata until we have fully committed our changes.
// Otherwise setting one key can trigger a write to the other key to
// maintain the synced consistency.
this._savingMetadata = true;
try {
super.saveCollapseState();
const metadata = this.model.metadata;
const collapsed = this.model.metadata.get('collapsed');
if (
(this.outputHidden && collapsed === true) ||
(!this.outputHidden && collapsed === undefined)
) {
return;
}
// Do not set jupyter.outputs_hidden since it is redundant. See
// and https://github.com/jupyter/nbformat/issues/137
if (this.outputHidden) {
metadata.set('collapsed', true);
} else {
metadata.delete('collapsed');
}
} finally {
this._savingMetadata = false;
}
}
/**
* Revert view collapse state from model.
*
* We consider the `collapsed` metadata key as the source of truth for outputs
* being hidden.
*/
loadCollapseState() {
super.loadCollapseState();
this.outputHidden = !!this.model.metadata.get('collapsed');
}
/**
* Whether the output is in a scrolled state?
*/
get outputsScrolled(): boolean {
return this._outputsScrolled;
}
set outputsScrolled(value: boolean) {
this.toggleClass('jp-mod-outputsScrolled', value);
this._outputsScrolled = value;
if (this.syncScrolled) {
this.saveScrolledState();
}
}
/**
* Save view collapse state to model
*/
saveScrolledState() {
const { metadata } = this.model;
const current = metadata.get('scrolled');
if (
(this.outputsScrolled && current === true) ||
(!this.outputsScrolled && current === undefined)
) {
return;
}
if (this.outputsScrolled) {
metadata.set('scrolled', true);
} else {
metadata.delete('scrolled');
}
}
/**
* Revert view collapse state from model.
*/
loadScrolledState() {
const metadata = this.model.metadata;
// We don't have the notion of 'auto' scrolled, so we make it false.
if (metadata.get('scrolled') === 'auto') {
this.outputsScrolled = false;
} else {
this.outputsScrolled = !!metadata.get('scrolled');
}
}
/**
* Whether to sync the scrolled state to the cell model.
*/
get syncScrolled(): boolean {
return this._syncScrolled;
}
set syncScrolled(value: boolean) {
if (this._syncScrolled === value) {
return;
}
this._syncScrolled = value;
if (value) {
this.loadScrolledState();
}
}
/**
* Handle the input being hidden.
*
* #### Notes
* This method is called by the case cell implementation and is
* subclasses here so the code cell can watch to see when input
* is hidden without accessing private state.
*/
protected handleInputHidden(value: boolean): void {
if (!value && this._outputWrapper.isHidden) {
this._outputWrapper.show();
} else if (value && !this._outputWrapper.isHidden && this._outputHidden) {
this._outputWrapper.hide();
}
}
/**
* Clone the cell, using the same model.
*/
clone(): CodeCell {
const constructor = this.constructor as typeof CodeCell;
return new constructor({
model: this.model,
contentFactory: this.contentFactory,
rendermime: this._rendermime
});
}
/**
* Clone the OutputArea alone, returning a simplified output area, using the same model.
*/
cloneOutputArea(): OutputArea {
return new SimplifiedOutputArea({
model: this.model.outputs!,
contentFactory: this.contentFactory,
rendermime: this._rendermime
});
}
/**
* Dispose of the resources used by the widget.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._output.outputLengthChanged.disconnect(
this._outputLengthHandler,
this
);
this._rendermime = null!;
this._output = null!;
this._outputWrapper = null!;
this._outputPlaceholder = null!;
super.dispose();
}
/**
* Handle changes in the model.
*/
protected onStateChanged(model: ICellModel, args: IChangedArgs<any>): void {
switch (args.name) {
case 'executionCount':
this.setPrompt(`${(model as ICodeCellModel).executionCount || ''}`);
break;
default:
break;
}
}
/**
* Handle changes in the metadata.
*/
protected onMetadataChanged(
model: IObservableJSON,
args: IObservableMap.IChangedArgs<JSONValue>
): void {
if (this._savingMetadata) {
// We are in middle of a metadata transaction, so don't react to it.
return;
}
switch (args.key) {
case 'scrolled':
if (this.syncScrolled) {
this.loadScrolledState();
}
break;
case 'collapsed':
if (this.syncCollapse) {
this.loadCollapseState();
}
break;
default:
break;
}
super.onMetadataChanged(model, args);