-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
default.ts
523 lines (456 loc) · 11.7 KB
/
default.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { Mode } from '@jupyterlab/codemirror';
import { Contents } from '@jupyterlab/services';
import { JSONValue } from '@phosphor/coreutils';
import { ISignal, Signal } from '@phosphor/signaling';
import { Widget } from '@phosphor/widgets';
import { MainAreaWidget } from '@jupyterlab/apputils';
import { CodeEditor } from '@jupyterlab/codeeditor';
import { IChangedArgs, PathExt } from '@jupyterlab/coreutils';
import { IModelDB } from '@jupyterlab/observables';
import { DocumentRegistry, IDocumentWidget } from './index';
/**
* The default implementation of a document model.
*/
export class DocumentModel extends CodeEditor.Model
implements DocumentRegistry.ICodeModel {
/**
* Construct a new document model.
*/
constructor(languagePreference?: string, modelDB?: IModelDB) {
super({ modelDB });
this._defaultLang = languagePreference || '';
this.value.changed.connect(
this.triggerContentChange,
this
);
}
/**
* A signal emitted when the document content changes.
*/
get contentChanged(): ISignal<this, void> {
return this._contentChanged;
}
/**
* A signal emitted when the document state changes.
*/
get stateChanged(): ISignal<this, IChangedArgs<any>> {
return this._stateChanged;
}
/**
* The dirty state of the document.
*/
get dirty(): boolean {
return this._dirty;
}
set dirty(newValue: boolean) {
if (newValue === this._dirty) {
return;
}
let oldValue = this._dirty;
this._dirty = newValue;
this.triggerStateChange({ name: 'dirty', oldValue, newValue });
}
/**
* The read only state of the document.
*/
get readOnly(): boolean {
return this._readOnly;
}
set readOnly(newValue: boolean) {
if (newValue === this._readOnly) {
return;
}
let oldValue = this._readOnly;
this._readOnly = newValue;
this.triggerStateChange({ name: 'readOnly', oldValue, newValue });
}
/**
* The default kernel name of the document.
*
* #### Notes
* This is a read-only property.
*/
get defaultKernelName(): string {
return '';
}
/**
* The default kernel language of the document.
*
* #### Notes
* This is a read-only property.
*/
get defaultKernelLanguage(): string {
return this._defaultLang;
}
/**
* Serialize the model to a string.
*/
toString(): string {
return this.value.text;
}
/**
* Deserialize the model from a string.
*
* #### Notes
* Should emit a [contentChanged] signal.
*/
fromString(value: string): void {
this.value.text = value;
}
/**
* Serialize the model to JSON.
*/
toJSON(): JSONValue {
return JSON.parse(this.value.text || 'null');
}
/**
* Deserialize the model from JSON.
*
* #### Notes
* Should emit a [contentChanged] signal.
*/
fromJSON(value: JSONValue): void {
this.fromString(JSON.stringify(value));
}
/**
* Initialize the model with its current state.
*/
initialize(): void {
return;
}
/**
* Trigger a state change signal.
*/
protected triggerStateChange(args: IChangedArgs<any>): void {
this._stateChanged.emit(args);
}
/**
* Trigger a content changed signal.
*/
protected triggerContentChange(): void {
this._contentChanged.emit(void 0);
this.dirty = true;
}
private _defaultLang = '';
private _dirty = false;
private _readOnly = false;
private _contentChanged = new Signal<this, void>(this);
private _stateChanged = new Signal<this, IChangedArgs<any>>(this);
}
/**
* An implementation of a model factory for text files.
*/
export class TextModelFactory implements DocumentRegistry.CodeModelFactory {
/**
* The name of the model type.
*
* #### Notes
* This is a read-only property.
*/
get name(): string {
return 'text';
}
/**
* The type of the file.
*
* #### Notes
* This is a read-only property.
*/
get contentType(): Contents.ContentType {
return 'file';
}
/**
* The format of the file.
*
* This is a read-only property.
*/
get fileFormat(): Contents.FileFormat {
return 'text';
}
/**
* Get whether the model factory has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources held by the model factory.
*/
dispose(): void {
this._isDisposed = true;
}
/**
* Create a new model.
*
* @param languagePreference - An optional kernel language preference.
*
* @returns A new document model.
*/
createNew(
languagePreference?: string,
modelDB?: IModelDB
): DocumentRegistry.ICodeModel {
return new DocumentModel(languagePreference, modelDB);
}
/**
* Get the preferred kernel language given a file path.
*/
preferredLanguage(path: string): string {
let mode = Mode.findByFileName(path);
return mode && mode.mode;
}
private _isDisposed = false;
}
/**
* An implementation of a model factory for base64 files.
*/
export class Base64ModelFactory extends TextModelFactory {
/**
* The name of the model type.
*
* #### Notes
* This is a read-only property.
*/
get name(): string {
return 'base64';
}
/**
* The type of the file.
*
* #### Notes
* This is a read-only property.
*/
get contentType(): Contents.ContentType {
return 'file';
}
/**
* The format of the file.
*
* This is a read-only property.
*/
get fileFormat(): Contents.FileFormat {
return 'base64';
}
}
/**
* The default implementation of a widget factory.
*/
export abstract class ABCWidgetFactory<
T extends IDocumentWidget,
U extends DocumentRegistry.IModel = DocumentRegistry.IModel
> implements DocumentRegistry.IWidgetFactory<T, U> {
/**
* Construct a new `ABCWidgetFactory`.
*/
constructor(options: DocumentRegistry.IWidgetFactoryOptions<T>) {
this._name = options.name;
this._readOnly = options.readOnly === undefined ? false : options.readOnly;
this._defaultFor = options.defaultFor ? options.defaultFor.slice() : [];
this._defaultRendered = (options.defaultRendered || []).slice();
this._fileTypes = options.fileTypes.slice();
this._modelName = options.modelName || 'text';
this._preferKernel = !!options.preferKernel;
this._canStartKernel = !!options.canStartKernel;
this._toolbarFactory = options.toolbarFactory;
}
/**
* A signal emitted when a widget is created.
*/
get widgetCreated(): ISignal<DocumentRegistry.IWidgetFactory<T, U>, T> {
return this._widgetCreated;
}
/**
* Get whether the model factory has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources held by the document manager.
*/
dispose(): void {
this._isDisposed = true;
}
/**
* Whether the widget factory is read only.
*/
get readOnly(): boolean {
return this._readOnly;
}
/**
* The name of the widget to display in dialogs.
*/
get name(): string {
return this._name;
}
/**
* The file types the widget can view.
*/
get fileTypes(): string[] {
return this._fileTypes.slice();
}
/**
* The registered name of the model type used to create the widgets.
*/
get modelName(): string {
return this._modelName;
}
/**
* The file types for which the factory should be the default.
*/
get defaultFor(): string[] {
return this._defaultFor.slice();
}
/**
* The file types for which the factory should be the default for
* rendering a document model, if different from editing.
*/
get defaultRendered(): string[] {
return this._defaultRendered.slice();
}
/**
* Whether the widgets prefer having a kernel started.
*/
get preferKernel(): boolean {
return this._preferKernel;
}
/**
* Whether the widgets can start a kernel when opened.
*/
get canStartKernel(): boolean {
return this._canStartKernel;
}
/**
* Create a new widget given a document model and a context.
*
* #### Notes
* It should emit the [widgetCreated] signal with the new widget.
*/
createNew(context: DocumentRegistry.IContext<U>): T {
// Create the new widget
const widget = this.createNewWidget(context);
// Add toolbar items
let items: DocumentRegistry.IToolbarItem[];
if (this._toolbarFactory) {
items = this._toolbarFactory(widget);
} else {
items = this.defaultToolbarFactory(widget);
}
items.forEach(({ name, widget: item }) => {
widget.toolbar.addItem(name, item);
});
// Emit widget created signal
this._widgetCreated.emit(widget);
return widget;
}
/**
* Create a widget for a context.
*/
protected abstract createNewWidget(context: DocumentRegistry.IContext<U>): T;
/**
* Default factory for toolbar items to be added after the widget is created.
*/
protected defaultToolbarFactory(widget: T): DocumentRegistry.IToolbarItem[] {
return [];
}
private _toolbarFactory: (
widget: T
) => DocumentRegistry.IToolbarItem[] | undefined;
private _isDisposed = false;
private _name: string;
private _readOnly: boolean;
private _canStartKernel: boolean;
private _preferKernel: boolean;
private _modelName: string;
private _fileTypes: string[];
private _defaultFor: string[];
private _defaultRendered: string[];
private _widgetCreated = new Signal<DocumentRegistry.IWidgetFactory<T, U>, T>(
this
);
}
/**
* The class name added to a dirty widget.
*/
const DIRTY_CLASS = 'jp-mod-dirty';
/**
* A document widget implementation.
*/
export class DocumentWidget<
T extends Widget = Widget,
U extends DocumentRegistry.IModel = DocumentRegistry.IModel
> extends MainAreaWidget<T> implements IDocumentWidget<T, U> {
constructor(options: DocumentWidget.IOptions<T, U>) {
// Include the context ready promise in the widget reveal promise
options.reveal = Promise.all([options.reveal, options.context.ready]);
super(options);
this.context = options.context;
// Handle context path changes
this.context.pathChanged.connect(
this._onPathChanged,
this
);
this._onPathChanged(this.context, this.context.path);
// Listen for changes in the dirty state.
this.context.model.stateChanged.connect(
this._onModelStateChanged,
this
);
this.context.ready.then(() => {
this._handleDirtyState();
});
}
/**
* Set URI fragment identifier.
*/
setFragment(fragment: string): void {
/* no-op */
}
/**
* Handle a path change.
*/
private _onPathChanged(
sender: DocumentRegistry.IContext<U>,
path: string
): void {
this.title.label = PathExt.basename(sender.localPath);
}
/**
* Handle a change to the context model state.
*/
private _onModelStateChanged(
sender: DocumentRegistry.IModel,
args: IChangedArgs<any>
): void {
if (args.name === 'dirty') {
this._handleDirtyState();
}
}
/**
* Handle the dirty state of the context model.
*/
private _handleDirtyState(): void {
if (this.context.model.dirty) {
this.title.className += ` ${DIRTY_CLASS}`;
} else {
this.title.className = this.title.className.replace(DIRTY_CLASS, '');
}
}
readonly context: DocumentRegistry.IContext<U>;
}
export namespace DocumentWidget {
export interface IOptions<
T extends Widget = Widget,
U extends DocumentRegistry.IModel = DocumentRegistry.IModel
> extends MainAreaWidget.IOptions<T> {
context: DocumentRegistry.IContext<U>;
}
export interface IOptionsOptionalContent<
T extends Widget = Widget,
U extends DocumentRegistry.IModel = DocumentRegistry.IModel
> extends MainAreaWidget.IOptionsOptionalContent<T> {
context: DocumentRegistry.IContext<U>;
}
}