This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
/
widget.ts
544 lines (476 loc) · 11.9 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';
import {
IKernel
} from 'jupyter-js-services';
import {
loadModeByMIME
} from 'jupyter-js-ui/lib/codemirror';
import {
RenderMime
} from 'jupyter-js-ui/lib/rendermime';
import {
Message
} from 'phosphor-messaging';
import {
PanelLayout
} from 'phosphor-panel';
import {
IChangedArgs
} from 'phosphor-properties';
import {
Widget
} from 'phosphor-widget';
import {
nbformat
} from '../notebook/nbformat';
import {
OutputAreaWidget, ObservableOutputs, executeCode
} from '../output-area';
import {
sanitize
} from 'sanitizer';
import {
IMetadataCursor
} from '../common/metadata';
import {
CellEditorWidget
} from './editor';
import {
ICodeCellModel, ICellModel
} from './model';
/**
* The class name added to cell widgets.
*/
const CELL_CLASS = 'jp-Cell';
/**
* The class name added to input area widgets.
*/
const INPUT_CLASS = 'jp-InputArea';
/**
* The class name added to the prompt area of cell.
*/
const PROMPT_CLASS = 'jp-InputArea-prompt';
/**
* The class name added to the editor area of the cell.
*/
const EDITOR_CLASS = 'jp-InputArea-editor';
/**
* The class name added to the cell when collapsed.
*/
const COLLAPSED_CLASS = 'jp-mod-collapsed';
/**
* 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 the markdown cell renderer widget.
*/
const RENDERER_CLASS = 'jp-MarkdownCell-renderer';
/**
* The class name added to raw cells.
*/
const RAW_CELL_CLASS = 'jp-RawCell';
/**
* The class name added to a rendered markdown cell.
*/
const RENDERED_CLASS = 'jp-mod-rendered';
/**
* The text applied to an empty markdown cell.
*/
const DEFAULT_MARKDOWN_TEXT = 'Type Markdown and LaTeX: $ α^2 $';
/**
* A base cell widget.
*/
export
class BaseCellWidget extends Widget {
/**
* Create a new cell editor for the widget.
*/
static createCellEditor(model: ICellModel): CellEditorWidget {
return new CellEditorWidget(model);
}
/**
* Create a new input area for the widget.
*/
static createInputArea(editor: CellEditorWidget): InputAreaWidget {
return new InputAreaWidget(editor);
}
/**
* Construct a new base cell widget.
*/
constructor(model: ICellModel) {
super();
this.addClass(CELL_CLASS);
this._model = model;
let ctor = this.constructor as typeof BaseCellWidget;
this._editor = ctor.createCellEditor(model);
this._input = ctor.createInputArea(this._editor);
// Set the editor mode to be the default MIME type.
loadModeByMIME(this._editor.editor, this._mimetype);
this.layout = new PanelLayout();
(this.layout as PanelLayout).addChild(this._input);
model.metadataChanged.connect(this.onMetadataChanged, this);
this._trustedCursor = model.getMetadata('trusted');
this._trusted = this._trustedCursor.getValue();
}
/**
* Get the model used by the widget.
*
* #### Notes
* This is a read-only property.
*/
get model(): ICellModel {
return this._model;
}
/**
* Get the editor widget used by the cell.
*
* #### Notes
* This is a ready-only property.
*/
get editor(): CellEditorWidget {
return this._editor;
}
/**
* The mimetype used by the cell.
*/
get mimetype(): string {
return this._mimetype;
}
set mimetype(value: string) {
if (!value) {
return;
}
if (this._mimetype === value) {
return;
}
this._mimetype = value;
loadModeByMIME(this.editor.editor, value);
}
/**
* 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;
this.update();
}
/**
* The trusted state of the cell.
*/
get trusted(): boolean {
return this._trusted;
}
set trusted(value: boolean) {
this._trustedCursor.setValue(value);
}
/**
* Focus the widget.
*/
focus(): void {
this.editor.editor.focus();
}
/**
* Set the prompt for the widget.
*/
setPrompt(value: string): void {
this._input.setPrompt(value);
}
/**
* Toggle whether the input area is shown.
*/
toggleInput(value: boolean): void {
if (value) {
this._input.show();
this.focus();
} else {
this._input.hide();
}
}
/**
* Dispose of the resources held by the widget.
*/
dispose() {
// Do nothing if already disposed.
if (this.isDisposed) {
return;
}
this._model = null;
this._input = null;
this._editor = null;
this._trustedCursor = null;
super.dispose();
}
/**
* Handle `after-attach` messages.
*/
protected onAfterAttach(msg: Message): void {
this.update();
}
/**
* Handle `update_request` messages.
*/
protected onUpdateRequest(message: Message): void {
// Handle read only state.
let option = this._readOnly ? 'nocursor' : false;
this.editor.editor.setOption('readOnly', option);
this.toggleClass(READONLY_CLASS, this._readOnly);
}
/**
* Handle changes in the model.
*/
protected onMetadataChanged(model: ICellModel, args: IChangedArgs<any>): void {
switch (args.name) {
case 'trusted':
this._trusted = this._trustedCursor.getValue();
this.update();
break;
default:
break;
}
}
private _input: InputAreaWidget = null;
private _editor: CellEditorWidget = null;
private _model: ICellModel = null;
private _mimetype = 'text/plain';
private _readOnly = false;
private _trustedCursor: IMetadataCursor = null;
private _trusted = false;
}
/**
* A widget for a code cell.
*/
export
class CodeCellWidget extends BaseCellWidget {
/**
* Create an output area widget.
*/
static createOutput(outputs: ObservableOutputs, rendermime: RenderMime<Widget>): OutputAreaWidget {
return new OutputAreaWidget(outputs, rendermime);
}
/**
* Construct a code cell widget.
*/
constructor(model: ICodeCellModel, rendermime: RenderMime<Widget>) {
super(model);
this._rendermime = rendermime;
this.addClass(CODE_CELL_CLASS);
let constructor = this.constructor as typeof CodeCellWidget;
this._output = constructor.createOutput(model.outputs, rendermime);
this._output.trusted = this.trusted;
(this.layout as PanelLayout).addChild(this._output);
this._collapsedCursor = model.getMetadata('collapsed');
this._scrolledCursor = model.getMetadata('scrolled');
this.setPrompt(String(model.executionCount));
model.stateChanged.connect(this.onModelChanged, this);
}
/**
* Dispose of the resources used by the widget.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._rendermime = null;
this._collapsedCursor = null;
this._scrolledCursor = null;
this._output = null;
super.dispose();
}
/**
* Execute the cell given a kernel.
*/
execute(kernel: IKernel): Promise<void> {
let model = this.model as ICodeCellModel;
let code = model.source;
if (!code.trim()) {
model.executionCount = null;
return Promise.resolve(void 0);
}
model.executionCount = null;
this.setPrompt('*');
this.trusted = true;
let outputs = model.outputs;
return executeCode(code, kernel, outputs).then(reply => {
model.executionCount = reply.execution_count;
});
}
/**
* Handle `update_request` messages.
*/
protected onUpdateRequest(message: Message): void {
this.toggleClass(COLLAPSED_CLASS, this._collapsedCursor.getValue());
// TODO: handle scrolled state.
this._output.trusted = this.trusted;
super.onUpdateRequest(message);
}
/**
* Handle changes in the model.
*/
protected onModelChanged(model: ICodeCellModel, args: IChangedArgs<any>): void {
switch (args.name) {
case 'executionCount':
this.setPrompt(String(model.executionCount));
break;
default:
break;
}
}
/**
* Handle changes in the metadata.
*/
protected onMetadataChanged(model: ICodeCellModel, args: IChangedArgs<any>): void {
switch (args.name) {
case 'collapsed':
case 'scrolled':
this.update();
break;
default:
break;
}
super.onMetadataChanged(model, args);
}
private _output: OutputAreaWidget = null;
private _rendermime: RenderMime<Widget> = null;
private _collapsedCursor: IMetadataCursor = null;
private _scrolledCursor: IMetadataCursor = null;
}
/**
* A widget for a Markdown cell.
*
* #### Notes
* Things get complicated if we want the rendered text to update
* any time the text changes, the text editor model changes,
* or the input area model changes. We don't support automatically
* updating the rendered text in all of these cases.
*/
export
class MarkdownCellWidget extends BaseCellWidget {
/**
* Construct a Markdown cell widget.
*/
constructor(model: ICellModel, rendermime: RenderMime<Widget>) {
super(model);
this._rendermime = rendermime;
this.addClass(MARKDOWN_CELL_CLASS);
// Insist on the Github-flavored markdown mode.
this.mimetype = 'text/x-ipythongfm';
this._renderer = new Widget();
this._renderer.addClass(RENDERER_CLASS);
(this.layout as PanelLayout).addChild(this._renderer);
}
/**
* Whether the cell is rendered.
*/
get rendered(): boolean {
return this._rendered;
}
set rendered(value: boolean) {
if (value === this._rendered) {
return;
}
this._rendered = value;
this.update();
}
/**
* Dispose of the resource held by the widget.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._renderer = null;
this._rendermime = null;
super.dispose();
}
/**
* Handle `update_request` messages.
*/
protected onUpdateRequest(message: Message): void {
let model = this.model;
if (this.rendered) {
let text = model.source || DEFAULT_MARKDOWN_TEXT;
// Do not re-render if the text has not changed.
if (text !== this._prev) {
text = sanitize(text);
let bundle: nbformat.MimeBundle = { 'text/markdown': text };
this._renderer.dispose();
this._renderer = this._rendermime.render(bundle) || new Widget();
this._renderer.addClass(RENDERER_CLASS);
(this.layout as PanelLayout).addChild(this._renderer);
}
this._prev = text;
this._renderer.show();
this.toggleInput(false);
this.addClass(RENDERED_CLASS);
} else {
this._renderer.hide();
this.toggleInput(true);
this.removeClass(RENDERED_CLASS);
}
super.onUpdateRequest(message);
}
private _rendermime: RenderMime<Widget> = null;
private _renderer: Widget = null;
private _rendered = true;
private _prev = '';
}
/**
* A widget for a raw cell.
*/
export
class RawCellWidget extends BaseCellWidget {
/**
* Construct a raw cell widget.
*/
constructor(model: ICellModel) {
super(model);
this.addClass(RAW_CELL_CLASS);
}
}
/**
* An input area widget, which hosts a prompt and an editor widget.
*/
export
class InputAreaWidget extends Widget {
/**
* Construct an input area widget.
*/
constructor(editor: CellEditorWidget) {
super();
this.addClass(INPUT_CLASS);
editor.addClass(EDITOR_CLASS);
this.layout = new PanelLayout();
let prompt = new Widget();
prompt.addClass(PROMPT_CLASS);
let layout = this.layout as PanelLayout;
layout.addChild(prompt);
layout.addChild(editor);
}
/**
* Set the prompt of the input area.
*/
setPrompt(value: string): void {
let prompt = (this.layout as PanelLayout).childAt(0);
if (value === 'null') {
value = ' ';
}
let text = `In [${value || ' '}]:`;
prompt.node.textContent = text;
}
}