/
widget.ts
1031 lines (918 loc) · 27.1 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 { IClientSession } from '@jupyterlab/apputils';
import {
Cell,
CellDragUtils,
CellModel,
CodeCell,
CodeCellModel,
ICodeCellModel,
isCodeCellModel,
IRawCellModel,
RawCell,
RawCellModel
} from '@jupyterlab/cells';
import { IEditorMimeTypeService, CodeEditor } from '@jupyterlab/codeeditor';
import { nbformat } from '@jupyterlab/coreutils';
import { IObservableList, ObservableList } from '@jupyterlab/observables';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { KernelMessage } from '@jupyterlab/services';
import { each } from '@phosphor/algorithm';
import { MimeData, JSONObject } from '@phosphor/coreutils';
import { Drag } from '@phosphor/dragdrop';
import { Message } from '@phosphor/messaging';
import { ISignal, Signal } from '@phosphor/signaling';
import { Panel, PanelLayout, Widget } from '@phosphor/widgets';
import { ConsoleHistory, IConsoleHistory } from './history';
/**
* The data attribute added to a widget that has an active kernel.
*/
const KERNEL_USER = 'jpKernelUser';
/**
* The data attribute added to a widget can run code.
*/
const CODE_RUNNER = 'jpCodeRunner';
/**
* The class name added to console widgets.
*/
const CONSOLE_CLASS = 'jp-CodeConsole';
/**
* The class added to console cells
*/
const CONSOLE_CELL_CLASS = 'jp-Console-cell';
/**
* The class name added to the console banner.
*/
const BANNER_CLASS = 'jp-CodeConsole-banner';
/**
* The class name of the active prompt cell.
*/
const PROMPT_CLASS = 'jp-CodeConsole-promptCell';
/**
* The class name of the panel that holds cell content.
*/
const CONTENT_CLASS = 'jp-CodeConsole-content';
/**
* The class name of the panel that holds prompts.
*/
const INPUT_CLASS = 'jp-CodeConsole-input';
/**
* The timeout in ms for execution requests to the kernel.
*/
const EXECUTION_TIMEOUT = 250;
/**
* The mimetype used for Jupyter cell data.
*/
const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
/**
* A widget containing a Jupyter console.
*
* #### Notes
* The CodeConsole class is intended to be used within a ConsolePanel
* instance. Under most circumstances, it is not instantiated by user code.
*/
export class CodeConsole extends Widget {
/**
* Construct a console widget.
*/
constructor(options: CodeConsole.IOptions) {
super();
this.addClass(CONSOLE_CLASS);
this.node.dataset[KERNEL_USER] = 'true';
this.node.dataset[CODE_RUNNER] = 'true';
this.node.tabIndex = -1; // Allow the widget to take focus.
// Create the panels that hold the content and input.
const layout = (this.layout = new PanelLayout());
this._cells = new ObservableList<Cell>();
this._content = new Panel();
this._input = new Panel();
this.contentFactory =
options.contentFactory || CodeConsole.defaultContentFactory;
this.modelFactory = options.modelFactory || CodeConsole.defaultModelFactory;
this.rendermime = options.rendermime;
this.session = options.session;
this._mimeTypeService = options.mimeTypeService;
// Add top-level CSS classes.
this._content.addClass(CONTENT_CLASS);
this._input.addClass(INPUT_CLASS);
// Insert the content and input panes into the widget.
layout.addWidget(this._content);
layout.addWidget(this._input);
this._history = new ConsoleHistory({
session: this.session
});
this._onKernelChanged();
this.session.kernelChanged.connect(this._onKernelChanged, this);
this.session.statusChanged.connect(this._onKernelStatusChanged, this);
}
/**
* A signal emitted when the console finished executing its prompt cell.
*/
get executed(): ISignal<this, Date> {
return this._executed;
}
/**
* A signal emitted when a new prompt cell is created.
*/
get promptCellCreated(): ISignal<this, CodeCell> {
return this._promptCellCreated;
}
/**
* The content factory used by the console.
*/
readonly contentFactory: CodeConsole.IContentFactory;
/**
* The model factory for the console widget.
*/
readonly modelFactory: CodeConsole.IModelFactory;
/**
* The rendermime instance used by the console.
*/
readonly rendermime: IRenderMimeRegistry;
/**
* The client session used by the console.
*/
readonly session: IClientSession;
/**
* The list of content cells in the console.
*
* #### Notes
* This list does not include the current banner or the prompt for a console.
* It may include previous banners as raw cells.
*/
get cells(): IObservableList<Cell> {
return this._cells;
}
/*
* The console input prompt cell.
*/
get promptCell(): CodeCell | null {
let inputLayout = this._input.layout as PanelLayout;
return (inputLayout.widgets[0] as CodeCell) || null;
}
/**
* Add a new cell to the content panel.
*
* @param cell - The code cell widget being added to the content panel.
*
* @param msgId - The optional execution message id for the cell.
*
* #### Notes
* This method is meant for use by outside classes that want to add cells to a
* console. It is distinct from the `inject` method in that it requires
* rendered code cell widgets and does not execute them (though it can store
* the execution message id).
*/
addCell(cell: CodeCell, msgId?: string) {
cell.addClass(CONSOLE_CELL_CLASS);
this._content.addWidget(cell);
this._cells.push(cell);
if (msgId) {
this._msgIds.set(msgId, cell);
this._msgIdCells.set(cell, msgId);
}
cell.disposed.connect(this._onCellDisposed, this);
this.update();
}
/**
* Add a banner cell.
*/
addBanner() {
if (this._banner) {
// An old banner just becomes a normal cell now.
let cell = this._banner;
this._cells.push(this._banner);
cell.disposed.connect(this._onCellDisposed, this);
}
// Create the banner.
let model = this.modelFactory.createRawCell({});
model.value.text = '...';
let banner = (this._banner = new RawCell({
model,
contentFactory: this.contentFactory
})).initializeState();
banner.addClass(BANNER_CLASS);
banner.readOnly = true;
this._content.addWidget(banner);
}
/**
* Clear the code cells.
*/
clear(): void {
// Dispose all the content cells
let cells = this._cells;
while (cells.length > 0) {
cells.get(0).dispose();
}
}
/**
* Create a new cell with the built-in factory.
*/
createCodeCell(): CodeCell {
let factory = this.contentFactory;
let options = this._createCodeCellOptions();
let cell = factory.createCodeCell(options);
cell.readOnly = true;
cell.model.mimeType = this._mimetype;
return cell;
}
/**
* Dispose of the resources held by the widget.
*/
dispose() {
// Do nothing if already disposed.
if (this.isDisposed) {
return;
}
this._cells.clear();
this._msgIdCells = null;
this._msgIds = null;
this._history.dispose();
super.dispose();
}
/**
* Execute the current prompt.
*
* @param force - Whether to force execution without checking code
* completeness.
*
* @param timeout - The length of time, in milliseconds, that the execution
* should wait for the API to determine whether code being submitted is
* incomplete before attempting submission anyway. The default value is `250`.
*/
execute(force = false, timeout = EXECUTION_TIMEOUT): Promise<void> {
if (this.session.status === 'dead') {
return Promise.resolve(void 0);
}
const promptCell = this.promptCell;
if (!promptCell) {
return Promise.reject('Cannot execute without a prompt cell');
}
promptCell.model.trusted = true;
if (force) {
// Create a new prompt cell before kernel execution to allow typeahead.
this.newPromptCell();
return this._execute(promptCell);
}
// Check whether we should execute.
return this._shouldExecute(timeout).then(should => {
if (this.isDisposed) {
return;
}
if (should) {
// Create a new prompt cell before kernel execution to allow typeahead.
this.newPromptCell();
this.promptCell!.editor.focus();
return this._execute(promptCell);
} else {
// add a newline if we shouldn't execute
promptCell.editor.newIndentedLine();
}
});
}
/**
* Get a cell given a message id.
*
* @param msgId - The message id.
*/
getCell(msgId: string): CodeCell | undefined {
return this._msgIds.get(msgId);
}
/**
* Inject arbitrary code for the console to execute immediately.
*
* @param code - The code contents of the cell being injected.
*
* @returns A promise that indicates when the injected cell's execution ends.
*/
inject(code: string, metadata: JSONObject = {}): Promise<void> {
let cell = this.createCodeCell();
cell.model.value.text = code;
for (let key of Object.keys(metadata)) {
cell.model.metadata.set(key, metadata[key]);
}
this.addCell(cell);
return this._execute(cell);
}
/**
* Insert a line break in the prompt cell.
*/
insertLinebreak(): void {
let promptCell = this.promptCell;
if (!promptCell) {
return;
}
promptCell.editor.newIndentedLine();
}
/**
* Serialize the output.
*
* #### Notes
* This only serializes the code cells and the prompt cell if it exists, and
* skips any old banner cells.
*/
serialize(): nbformat.ICodeCell[] {
const cells: nbformat.ICodeCell[] = [];
each(this._cells, cell => {
let model = cell.model;
if (isCodeCellModel(model)) {
cells.push(model.toJSON());
}
});
if (this.promptCell) {
cells.push(this.promptCell.model.toJSON());
}
return cells;
}
/**
* Handle `mousedown` events for the widget.
*/
private _evtMouseDown(event: MouseEvent): void {
const { button, shiftKey } = event;
// We only handle main or secondary button actions.
if (
!(button === 0 || button === 2) ||
// Shift right-click gives the browser default behavior.
(shiftKey && button === 2)
) {
return;
}
let target = event.target as HTMLElement;
let cellFilter = (node: HTMLElement) =>
node.classList.contains(CONSOLE_CELL_CLASS);
let cellIndex = CellDragUtils.findCell(target, this._cells, cellFilter);
if (cellIndex === -1) {
// `event.target` sometimes gives an orphaned node in
// Firefox 57, which can have `null` anywhere in its parent line. If we fail
// to find a cell using `event.target`, try again using a target
// reconstructed from the position of the click event.
target = document.elementFromPoint(
event.clientX,
event.clientY
) as HTMLElement;
cellIndex = CellDragUtils.findCell(target, this._cells, cellFilter);
}
if (cellIndex === -1) {
return;
}
const cell = this._cells.get(cellIndex);
let targetArea: CellDragUtils.ICellTargetArea = CellDragUtils.detectTargetArea(
cell,
event.target as HTMLElement
);
if (targetArea === 'prompt') {
this._dragData = {
pressX: event.clientX,
pressY: event.clientY,
index: cellIndex
};
this._focusedCell = cell;
document.addEventListener('mouseup', this, true);
document.addEventListener('mousemove', this, true);
event.preventDefault();
}
}
/**
* Handle `mousemove` event of widget
*/
private _evtMouseMove(event: MouseEvent) {
const data = this._dragData;
if (
CellDragUtils.shouldStartDrag(
data.pressX,
data.pressY,
event.clientX,
event.clientY
)
) {
void this._startDrag(data.index, event.clientX, event.clientY);
}
}
/**
* Start a drag event
*/
private _startDrag(
index: number,
clientX: number,
clientY: number
): Promise<void> {
const cellModel = this._focusedCell.model as ICodeCellModel;
let selected: nbformat.ICell[] = [cellModel.toJSON()];
const dragImage = CellDragUtils.createCellDragImage(
this._focusedCell,
selected
);
this._drag = new Drag({
mimeData: new MimeData(),
dragImage,
proposedAction: 'copy',
supportedActions: 'copy',
source: this
});
this._drag.mimeData.setData(JUPYTER_CELL_MIME, selected);
const textContent = cellModel.value.text;
this._drag.mimeData.setData('text/plain', textContent);
this._focusedCell = null;
document.removeEventListener('mousemove', this, true);
document.removeEventListener('mouseup', this, true);
return this._drag.start(clientX, clientY).then(() => {
if (this.isDisposed) {
return;
}
this._drag = null;
this._dragData = null;
});
}
/**
* Handle the DOM events for the widget.
*
* @param event -The DOM event sent to the widget.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the notebook panel's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'keydown':
this._evtKeyDown(event as KeyboardEvent);
break;
case 'mousedown':
this._evtMouseDown(event as MouseEvent);
break;
case 'mousemove':
this._evtMouseMove(event as MouseEvent);
break;
case 'mouseup':
this._evtMouseUp(event as MouseEvent);
break;
default:
break;
}
}
/**
* Handle `after_attach` messages for the widget.
*/
protected onAfterAttach(msg: Message): void {
let node = this.node;
node.addEventListener('keydown', this, true);
node.addEventListener('click', this);
node.addEventListener('mousedown', this);
// Create a prompt if necessary.
if (!this.promptCell) {
this.newPromptCell();
} else {
this.promptCell.editor.focus();
this.update();
}
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
let node = this.node;
node.removeEventListener('keydown', this, true);
node.removeEventListener('click', this);
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
let editor = this.promptCell && this.promptCell.editor;
if (editor) {
editor.focus();
}
this.update();
}
/**
* Make a new prompt cell.
*/
protected newPromptCell(): void {
let promptCell = this.promptCell;
let input = this._input;
// Make the last prompt read-only, clear its signals, and move to content.
if (promptCell) {
promptCell.readOnly = true;
promptCell.removeClass(PROMPT_CLASS);
Signal.clearData(promptCell.editor);
let child = input.widgets[0];
child.parent = null;
this.addCell(promptCell);
}
// Create the new prompt cell.
let factory = this.contentFactory;
let options = this._createCodeCellOptions();
promptCell = factory.createCodeCell(options);
promptCell.model.mimeType = this._mimetype;
promptCell.addClass(PROMPT_CLASS);
this._input.addWidget(promptCell);
// Suppress the default "Enter" key handling.
let editor = promptCell.editor;
editor.addKeydownHandler(this._onEditorKeydown);
this._history.editor = editor;
this._promptCellCreated.emit(promptCell);
}
/**
* Handle `update-request` messages.
*/
protected onUpdateRequest(msg: Message): void {
Private.scrollToBottom(this._content.node);
}
/**
* Handle the `'keydown'` event for the widget.
*/
private _evtKeyDown(event: KeyboardEvent): void {
let editor = this.promptCell && this.promptCell.editor;
if (!editor) {
return;
}
if (event.keyCode === 13 && !editor.hasFocus()) {
event.preventDefault();
editor.focus();
} else if (event.keyCode === 27 && editor.hasFocus()) {
// Set to command mode
event.preventDefault();
event.stopPropagation();
this.node.focus();
}
}
/**
* Handle the `'mouseup'` event for the widget.
*/
private _evtMouseUp(event: MouseEvent): void {
if (
this.promptCell &&
this.promptCell.node.contains(event.target as HTMLElement)
) {
this.promptCell.editor.focus();
}
}
/**
* Execute the code in the current prompt cell.
*/
private _execute(cell: CodeCell): Promise<void> {
let source = cell.model.value.text;
this._history.push(source);
// If the source of the console is just "clear", clear the console as we
// do in IPython or QtConsole.
if (source === 'clear' || source === '%clear') {
this.clear();
return Promise.resolve(void 0);
}
cell.model.contentChanged.connect(this.update, this);
let onSuccess = (value: KernelMessage.IExecuteReplyMsg) => {
if (this.isDisposed) {
return;
}
if (value && value.content.status === 'ok') {
let content = value.content;
// Use deprecated payloads for backwards compatibility.
if (content.payload && content.payload.length) {
let setNextInput = content.payload.filter(i => {
return (i as any).source === 'set_next_input';
})[0];
if (setNextInput) {
let text = (setNextInput as any).text;
// Ignore the `replace` value and always set the next cell.
cell.model.value.text = text;
}
}
} else if (value && value.content.status === 'error') {
each(this._cells, (cell: CodeCell) => {
if (cell.model.executionCount === null) {
cell.setPrompt('');
}
});
}
cell.model.contentChanged.disconnect(this.update, this);
this.update();
this._executed.emit(new Date());
};
let onFailure = () => {
if (this.isDisposed) {
return;
}
cell.model.contentChanged.disconnect(this.update, this);
this.update();
};
return CodeCell.execute(cell, this.session).then(onSuccess, onFailure);
}
/**
* Update the console based on the kernel info.
*/
private _handleInfo(info: KernelMessage.IInfoReplyMsg['content']): void {
if (info.status !== 'ok') {
this._banner.model.value.text = 'Error in getting kernel banner';
return;
}
this._banner.model.value.text = info.banner;
let lang = info.language_info as nbformat.ILanguageInfoMetadata;
this._mimetype = this._mimeTypeService.getMimeTypeByLanguage(lang);
if (this.promptCell) {
this.promptCell.model.mimeType = this._mimetype;
}
}
/**
* Create the options used to initialize a code cell widget.
*/
private _createCodeCellOptions(): CodeCell.IOptions {
let contentFactory = this.contentFactory;
let modelFactory = this.modelFactory;
let model = modelFactory.createCodeCell({});
let rendermime = this.rendermime;
return { model, rendermime, contentFactory };
}
/**
* Handle cell disposed signals.
*/
private _onCellDisposed(sender: Cell, args: void): void {
if (!this.isDisposed) {
this._cells.removeValue(sender);
const msgId = this._msgIdCells.get(sender as CodeCell);
if (msgId) {
this._msgIdCells.delete(sender as CodeCell);
this._msgIds.delete(msgId);
}
}
}
/**
* Test whether we should execute the prompt cell.
*/
private _shouldExecute(timeout: number): Promise<boolean> {
const promptCell = this.promptCell;
if (!promptCell) {
return Promise.resolve(false);
}
let model = promptCell.model;
let code = model.value.text;
return new Promise<boolean>((resolve, reject) => {
let timer = setTimeout(() => {
resolve(true);
}, timeout);
let kernel = this.session.kernel;
if (!kernel) {
resolve(false);
return;
}
kernel
.requestIsComplete({ code })
.then(isComplete => {
clearTimeout(timer);
if (this.isDisposed) {
resolve(false);
}
if (isComplete.content.status !== 'incomplete') {
resolve(true);
return;
}
resolve(false);
})
.catch(() => {
resolve(true);
});
});
}
/**
* Handle a keydown event on an editor.
*/
private _onEditorKeydown(editor: CodeEditor.IEditor, event: KeyboardEvent) {
// Suppress "Enter" events.
return event.keyCode === 13;
}
/**
* Handle a change to the kernel.
*/
private _onKernelChanged(): void {
this.clear();
if (this._banner) {
this._banner.dispose();
this._banner = null;
}
this.addBanner();
}
/**
* Handle a change to the kernel status.
*/
private _onKernelStatusChanged(): void {
if (this.session.status === 'connected') {
// we just had a kernel restart or reconnect - reset banner
let kernel = this.session.kernel;
if (!kernel) {
return;
}
kernel.ready
.then(() => {
if (this.isDisposed || !kernel || !kernel.info) {
return;
}
this._handleInfo(this.session.kernel.info);
})
.catch(err => {
console.error('could not get kernel info');
});
} else if (this.session.status === 'restarting') {
this.addBanner();
this._handleInfo(this.session.kernel.info);
}
}
private _banner: RawCell = null;
private _cells: IObservableList<Cell>;
private _content: Panel;
private _executed = new Signal<this, Date>(this);
private _history: IConsoleHistory;
private _input: Panel;
private _mimetype = 'text/x-ipython';
private _mimeTypeService: IEditorMimeTypeService;
private _msgIds = new Map<string, CodeCell>();
private _msgIdCells = new Map<CodeCell, string>();
private _promptCellCreated = new Signal<this, CodeCell>(this);
private _dragData: { pressX: number; pressY: number; index: number } = null;
private _drag: Drag = null;
private _focusedCell: Cell = null;
}
/**
* A namespace for CodeConsole statics.
*/
export namespace CodeConsole {
/**
* The initialization options for a console widget.
*/
export interface IOptions {
/**
* The content factory for the console widget.
*/
contentFactory: IContentFactory;
/**
* The model factory for the console widget.
*/
modelFactory?: IModelFactory;
/**
* The mime renderer for the console widget.
*/
rendermime: IRenderMimeRegistry;
/**
* The client session for the console widget.
*/
session: IClientSession;
/**
* The service used to look up mime types.
*/
mimeTypeService: IEditorMimeTypeService;
}
/**
* A content factory for console children.
*/
export interface IContentFactory extends Cell.IContentFactory {
/**
* Create a new code cell widget.
*/
createCodeCell(options: CodeCell.IOptions): CodeCell;
/**
* Create a new raw cell widget.
*/
createRawCell(options: RawCell.IOptions): RawCell;
}
/**
* Default implementation of `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): CodeCell {
if (!options.contentFactory) {
options.contentFactory = this;
}
return new CodeCell(options).initializeState();
}
/**
* 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): RawCell {
if (!options.contentFactory) {
options.contentFactory = this;
}
return new RawCell(options).initializeState();
}
}
/**
* A namespace for the code console content factory.
*/
export namespace ContentFactory {
/**
* An initialize options for `ContentFactory`.
*/
export interface IOptions extends Cell.IContentFactory {}
}
/**
* A default content factory for the code console.
*/
export const defaultContentFactory: IContentFactory = new ContentFactory();
/**
* A model factory for a console widget.
*/
export interface IModelFactory {
/**
* The factory for code cell content.
*/
readonly codeCellContentFactory: CodeCellModel.IContentFactory;
/**
* Create a new code cell.
*
* @param options - The options used to create the cell.
*
* @returns A new code cell. If a source cell is provided, the
* new cell will be initialized with the data from the source.
*/
createCodeCell(options: CodeCellModel.IOptions): ICodeCellModel;
/**
* Create a new raw cell.
*
* @param options - The options used to create the cell.
*
* @returns A new raw cell. If a source cell is provided, the
* new cell will be initialized with the data from the source.
*/
createRawCell(options: CellModel.IOptions): IRawCellModel;
}
/**
* The default implementation of an `IModelFactory`.
*/
export class ModelFactory {
/**
* Create a new cell model factory.
*/
constructor(options: IModelFactoryOptions = {}) {
this.codeCellContentFactory =
options.codeCellContentFactory || CodeCellModel.defaultContentFactory;
}
/**
* The factory for output area models.
*/
readonly codeCellContentFactory: CodeCellModel.IContentFactory;
/**
* Create a new code cell.
*
* @param source - The data to use for the original source data.
*
* @returns A new code cell. If a source cell is provided, the
* new cell will be initialized with the data from the source.
* If the contentFactory is not provided, the instance
* `codeCellContentFactory` will be used.
*/
createCodeCell(options: CodeCellModel.IOptions): ICodeCellModel {
if (!options.contentFactory) {
options.contentFactory = this.codeCellContentFactory;
}
return new CodeCellModel(options);
}
/**
* Create a new raw cell.
*
* @param source - The data to use for the original source data.
*
* @returns A new raw cell. If a source cell is provided, the
* new cell will be initialized with the data from the source.
*/
createRawCell(options: CellModel.IOptions): IRawCellModel {
return new RawCellModel(options);
}