-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
registry.ts
1593 lines (1459 loc) · 42.6 KB
/
registry.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 { ISessionContext, ToolbarRegistry } from '@jupyterlab/apputils';
import { CodeEditor } from '@jupyterlab/codeeditor';
import {
IChangedArgs as IChangedArgsGeneric,
PathExt
} from '@jupyterlab/coreutils';
import { IObservableList } from '@jupyterlab/observables';
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
import { Contents, Kernel } from '@jupyterlab/services';
import { ISharedDocument, ISharedFile } from '@jupyter/ydoc';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import {
fileIcon,
folderIcon,
imageIcon,
jsonIcon,
juliaIcon,
LabIcon,
markdownIcon,
notebookIcon,
pdfIcon,
pythonIcon,
rKernelIcon,
spreadsheetIcon,
Toolbar,
yamlIcon
} from '@jupyterlab/ui-components';
import { ArrayExt, find } from '@lumino/algorithm';
import { PartialJSONValue, ReadonlyPartialJSONValue } from '@lumino/coreutils';
import { DisposableDelegate, IDisposable } from '@lumino/disposable';
import { ISignal, Signal } from '@lumino/signaling';
import { DockLayout, Widget } from '@lumino/widgets';
import { TextModelFactory } from './default';
/**
* The document registry.
*/
export class DocumentRegistry implements IDisposable {
/**
* Construct a new document registry.
*/
constructor(options: DocumentRegistry.IOptions = {}) {
const factory = options.textModelFactory;
this.translator = options.translator || nullTranslator;
if (factory && factory.name !== 'text') {
throw new Error('Text model factory must have the name `text`');
}
this._modelFactories['text'] = factory || new TextModelFactory(true);
const fts =
options.initialFileTypes ||
DocumentRegistry.getDefaultFileTypes(this.translator);
fts.forEach(ft => {
const value: DocumentRegistry.IFileType = {
...DocumentRegistry.getFileTypeDefaults(this.translator),
...ft
};
this._fileTypes.push(value);
});
}
/**
* A signal emitted when the registry has changed.
*/
get changed(): ISignal<this, DocumentRegistry.IChangedArgs> {
return this._changed;
}
/**
* Get whether the document registry has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources held by the document registry.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
for (const modelName in this._modelFactories) {
this._modelFactories[modelName].dispose();
}
for (const widgetName in this._widgetFactories) {
this._widgetFactories[widgetName].dispose();
}
for (const widgetName in this._extenders) {
this._extenders[widgetName].length = 0;
}
this._fileTypes.length = 0;
Signal.clearData(this);
}
/**
* Add a widget factory to the registry.
*
* @param factory - The factory instance to register.
*
* @returns A disposable which will unregister the factory.
*
* #### Notes
* If a factory with the given `'name'` is already registered,
* a warning will be logged, and this will be a no-op.
* If `'*'` is given as a default extension, the factory will be registered
* as the global default.
* If an extension or global default is already registered, this factory
* will override the existing default.
* The factory cannot be named an empty string or the string `'default'`.
*/
addWidgetFactory(factory: DocumentRegistry.WidgetFactory): IDisposable {
const name = factory.name.toLowerCase();
if (!name || name === 'default') {
throw Error('Invalid factory name');
}
if (this._widgetFactories[name]) {
console.warn(`Duplicate registered factory ${name}`);
return new DisposableDelegate(Private.noOp);
}
this._widgetFactories[name] = factory;
for (const ft of factory.defaultFor || []) {
if (factory.fileTypes.indexOf(ft) === -1) {
continue;
}
if (ft === '*') {
this._defaultWidgetFactory = name;
} else {
this._defaultWidgetFactories[ft] = name;
}
}
for (const ft of factory.defaultRendered || []) {
if (factory.fileTypes.indexOf(ft) === -1) {
continue;
}
this._defaultRenderedWidgetFactories[ft] = name;
}
// For convenience, store a mapping of file type name -> name
for (const ft of factory.fileTypes) {
if (!this._widgetFactoriesForFileType[ft]) {
this._widgetFactoriesForFileType[ft] = [];
}
this._widgetFactoriesForFileType[ft].push(name);
}
this._changed.emit({
type: 'widgetFactory',
name,
change: 'added'
});
return new DisposableDelegate(() => {
delete this._widgetFactories[name];
if (this._defaultWidgetFactory === name) {
this._defaultWidgetFactory = '';
}
for (const ext of Object.keys(this._defaultWidgetFactories)) {
if (this._defaultWidgetFactories[ext] === name) {
delete this._defaultWidgetFactories[ext];
}
}
for (const ext of Object.keys(this._defaultRenderedWidgetFactories)) {
if (this._defaultRenderedWidgetFactories[ext] === name) {
delete this._defaultRenderedWidgetFactories[ext];
}
}
for (const ext of Object.keys(this._widgetFactoriesForFileType)) {
ArrayExt.removeFirstOf(this._widgetFactoriesForFileType[ext], name);
if (this._widgetFactoriesForFileType[ext].length === 0) {
delete this._widgetFactoriesForFileType[ext];
}
}
for (const ext of Object.keys(this._defaultWidgetFactoryOverrides)) {
if (this._defaultWidgetFactoryOverrides[ext] === name) {
delete this._defaultWidgetFactoryOverrides[ext];
}
}
this._changed.emit({
type: 'widgetFactory',
name,
change: 'removed'
});
});
}
/**
* Add a model factory to the registry.
*
* @param factory - The factory instance.
*
* @returns A disposable which will unregister the factory.
*
* #### Notes
* If a factory with the given `name` is already registered, or
* the given factory is already registered, a warning will be logged
* and this will be a no-op.
*/
addModelFactory(factory: DocumentRegistry.ModelFactory): IDisposable {
const name = factory.name.toLowerCase();
if (this._modelFactories[name]) {
console.warn(`Duplicate registered factory ${name}`);
return new DisposableDelegate(Private.noOp);
}
this._modelFactories[name] = factory;
this._changed.emit({
type: 'modelFactory',
name,
change: 'added'
});
return new DisposableDelegate(() => {
delete this._modelFactories[name];
this._changed.emit({
type: 'modelFactory',
name,
change: 'removed'
});
});
}
/**
* Add a widget extension to the registry.
*
* @param widgetName - The name of the widget factory.
*
* @param extension - A widget extension.
*
* @returns A disposable which will unregister the extension.
*
* #### Notes
* If the extension is already registered for the given
* widget name, a warning will be logged and this will be a no-op.
*/
addWidgetExtension(
widgetName: string,
extension: DocumentRegistry.WidgetExtension
): IDisposable {
widgetName = widgetName.toLowerCase();
if (!(widgetName in this._extenders)) {
this._extenders[widgetName] = [];
}
const extenders = this._extenders[widgetName];
const index = ArrayExt.firstIndexOf(extenders, extension);
if (index !== -1) {
console.warn(`Duplicate registered extension for ${widgetName}`);
return new DisposableDelegate(Private.noOp);
}
this._extenders[widgetName].push(extension);
this._changed.emit({
type: 'widgetExtension',
name: widgetName,
change: 'added'
});
return new DisposableDelegate(() => {
ArrayExt.removeFirstOf(this._extenders[widgetName], extension);
this._changed.emit({
type: 'widgetExtension',
name: widgetName,
change: 'removed'
});
});
}
/**
* Add a file type to the document registry.
*
* @param fileType - The file type object to register.
* @param factories - Optional factories to use for the file type.
*
* @returns A disposable which will unregister the command.
*
* #### Notes
* These are used to populate the "Create New" dialog.
*
* If no default factory exists for the file type, the first factory will
* be defined as default factory.
*/
addFileType(
fileType: Partial<DocumentRegistry.IFileType>,
factories?: string[]
): IDisposable {
const value: DocumentRegistry.IFileType = {
...DocumentRegistry.getFileTypeDefaults(this.translator),
...fileType,
// fall back to fileIcon if needed
...(!(fileType.icon || fileType.iconClass) && { icon: fileIcon })
};
this._fileTypes.push(value);
// Add the filetype to the factory - filetype mapping
// We do not change the factory itself
if (factories) {
const fileTypeName = value.name.toLowerCase();
factories
.map(factory => factory.toLowerCase())
.forEach(factory => {
if (!this._widgetFactoriesForFileType[fileTypeName]) {
this._widgetFactoriesForFileType[fileTypeName] = [];
}
if (
!this._widgetFactoriesForFileType[fileTypeName].includes(factory)
) {
this._widgetFactoriesForFileType[fileTypeName].push(factory);
}
});
if (!this._defaultWidgetFactories[fileTypeName]) {
this._defaultWidgetFactories[fileTypeName] =
this._widgetFactoriesForFileType[fileTypeName][0];
}
}
this._changed.emit({
type: 'fileType',
name: value.name,
change: 'added'
});
return new DisposableDelegate(() => {
ArrayExt.removeFirstOf(this._fileTypes, value);
if (factories) {
const fileTypeName = value.name.toLowerCase();
for (const name of factories.map(factory => factory.toLowerCase())) {
ArrayExt.removeFirstOf(
this._widgetFactoriesForFileType[fileTypeName],
name
);
}
if (
this._defaultWidgetFactories[fileTypeName] ===
factories[0].toLowerCase()
) {
delete this._defaultWidgetFactories[fileTypeName];
}
}
this._changed.emit({
type: 'fileType',
name: fileType.name,
change: 'removed'
});
});
}
/**
* Get a list of the preferred widget factories.
*
* @param path - The file path to filter the results.
*
* @returns A new array of widget factories.
*
* #### Notes
* Only the widget factories whose associated model factory have
* been registered will be returned.
* The first item is considered the default. The returned array
* has widget factories in the following order:
* - path-specific default factory
* - path-specific default rendered factory
* - global default factory
* - all other path-specific factories
* - all other global factories
*/
preferredWidgetFactories(path: string): DocumentRegistry.WidgetFactory[] {
const factories = new Set<string>();
// Get the ordered matching file types.
const fts = this.getFileTypesForPath(PathExt.basename(path));
// Start with any user overrides for the defaults.
fts.forEach(ft => {
if (ft.name in this._defaultWidgetFactoryOverrides) {
factories.add(this._defaultWidgetFactoryOverrides[ft.name]);
}
});
// Next add the file type default factories.
fts.forEach(ft => {
if (ft.name in this._defaultWidgetFactories) {
factories.add(this._defaultWidgetFactories[ft.name]);
}
});
// Add the file type default rendered factories.
fts.forEach(ft => {
if (ft.name in this._defaultRenderedWidgetFactories) {
factories.add(this._defaultRenderedWidgetFactories[ft.name]);
}
});
// Add the global default factory.
if (this._defaultWidgetFactory) {
factories.add(this._defaultWidgetFactory);
}
// Add the file type factories in registration order.
for (const ft of fts) {
if (ft.name in this._widgetFactoriesForFileType) {
for (const n of this._widgetFactoriesForFileType[ft.name]) {
factories.add(n);
}
}
}
// Add the rest of the global factories, in registration order.
if ('*' in this._widgetFactoriesForFileType) {
for (const n of this._widgetFactoriesForFileType['*']) {
factories.add(n);
}
}
// Construct the return list, checking to make sure the corresponding
// model factories are registered.
const factoryList: DocumentRegistry.WidgetFactory[] = [];
for (const name of factories) {
const factory = this._widgetFactories[name];
if (!factory) {
continue;
}
const modelName = factory.modelName || 'text';
if (modelName in this._modelFactories) {
factoryList.push(factory);
}
}
return factoryList;
}
/**
* Get the default rendered widget factory for a path.
*
* @param path - The path to for which to find a widget factory.
*
* @returns The default rendered widget factory for the path.
*
* ### Notes
* If the widget factory has registered a separate set of `defaultRendered`
* file types and there is a match in that set, this returns that.
* Otherwise, this returns the same widget factory as
* [[defaultWidgetFactory]].
*
* The user setting `defaultViewers` took precedence on this one too.
*/
defaultRenderedWidgetFactory(path: string): DocumentRegistry.WidgetFactory {
// Get the matching file types.
const ftNames = this.getFileTypesForPath(PathExt.basename(path)).map(
ft => ft.name
);
// Start with any user overrides for the defaults.
for (const name in ftNames) {
if (name in this._defaultWidgetFactoryOverrides) {
return this._widgetFactories[this._defaultWidgetFactoryOverrides[name]];
}
}
// Find if a there is a default rendered factory for this type.
for (const name in ftNames) {
if (name in this._defaultRenderedWidgetFactories) {
return this._widgetFactories[
this._defaultRenderedWidgetFactories[name]
];
}
}
// Fallback to the default widget factory
return this.defaultWidgetFactory(path);
}
/**
* Get the default widget factory for a path.
*
* @param path - An optional file path to filter the results.
*
* @returns The default widget factory for an path.
*
* #### Notes
* This is equivalent to the first value in [[preferredWidgetFactories]].
*/
defaultWidgetFactory(path?: string): DocumentRegistry.WidgetFactory {
if (!path) {
return this._widgetFactories[this._defaultWidgetFactory];
}
return this.preferredWidgetFactories(path)[0];
}
/**
* Set overrides for the default widget factory for a file type.
*
* Normally, a widget factory informs the document registry which file types
* it should be the default for using the `defaultFor` option in the
* IWidgetFactoryOptions. This function can be used to override that after
* the fact.
*
* @param fileType The name of the file type.
*
* @param factory The name of the factory.
*
* #### Notes
* If `factory` is undefined, then any override will be unset, and the
* default factory will revert to the original value.
*
* If `factory` or `fileType` are not known to the docregistry, or
* if `factory` cannot open files of type `fileType`, this will throw
* an error.
*/
setDefaultWidgetFactory(fileType: string, factory: string | undefined): void {
fileType = fileType.toLowerCase();
if (!this.getFileType(fileType)) {
throw Error(`Cannot find file type ${fileType}`);
}
if (!factory) {
if (this._defaultWidgetFactoryOverrides[fileType]) {
delete this._defaultWidgetFactoryOverrides[fileType];
}
return;
}
if (!this.getWidgetFactory(factory)) {
throw Error(`Cannot find widget factory ${factory}`);
}
factory = factory.toLowerCase();
const factories = this._widgetFactoriesForFileType[fileType];
if (
factory !== this._defaultWidgetFactory &&
!(factories && factories.includes(factory))
) {
throw Error(`Factory ${factory} cannot view file type ${fileType}`);
}
this._defaultWidgetFactoryOverrides[fileType] = factory;
}
/**
* Create an iterator over the widget factories that have been registered.
*
* @returns A new iterator of widget factories.
*/
*widgetFactories(): IterableIterator<DocumentRegistry.WidgetFactory> {
for (const name in this._widgetFactories) {
yield this._widgetFactories[name];
}
}
/**
* Create an iterator over the model factories that have been registered.
*
* @returns A new iterator of model factories.
*/
*modelFactories(): IterableIterator<DocumentRegistry.ModelFactory> {
for (const name in this._modelFactories) {
yield this._modelFactories[name];
}
}
/**
* Create an iterator over the registered extensions for a given widget.
*
* @param widgetName - The name of the widget factory.
*
* @returns A new iterator over the widget extensions.
*/
*widgetExtensions(
widgetName: string
): IterableIterator<DocumentRegistry.WidgetExtension> {
widgetName = widgetName.toLowerCase();
if (widgetName in this._extenders) {
for (const extension of this._extenders[widgetName]) {
yield extension;
}
}
}
/**
* Create an iterator over the file types that have been registered.
*
* @returns A new iterator of file types.
*/
*fileTypes(): IterableIterator<DocumentRegistry.IFileType> {
for (const type of this._fileTypes) {
yield type;
}
}
/**
* Get a widget factory by name.
*
* @param widgetName - The name of the widget factory.
*
* @returns A widget factory instance.
*/
getWidgetFactory(
widgetName: string
): DocumentRegistry.WidgetFactory | undefined {
return this._widgetFactories[widgetName.toLowerCase()];
}
/**
* Get a model factory by name.
*
* @param name - The name of the model factory.
*
* @returns A model factory instance.
*/
getModelFactory(name: string): DocumentRegistry.ModelFactory | undefined {
return this._modelFactories[name.toLowerCase()];
}
/**
* Get a file type by name.
*/
getFileType(name: string): DocumentRegistry.IFileType | undefined {
name = name.toLowerCase();
return find(this._fileTypes, fileType => {
return fileType.name.toLowerCase() === name;
});
}
/**
* Get a kernel preference.
*
* @param path - The file path.
*
* @param widgetName - The name of the widget factory.
*
* @param kernel - An optional existing kernel model.
*
* @returns A kernel preference.
*/
getKernelPreference(
path: string,
widgetName: string,
kernel?: Partial<Kernel.IModel>
): ISessionContext.IKernelPreference | undefined {
widgetName = widgetName.toLowerCase();
const widgetFactory = this._widgetFactories[widgetName];
if (!widgetFactory) {
return void 0;
}
const modelFactory = this.getModelFactory(
widgetFactory.modelName || 'text'
);
if (!modelFactory) {
return void 0;
}
const language = modelFactory.preferredLanguage(PathExt.basename(path));
const name = kernel && kernel.name;
const id = kernel && kernel.id;
return {
id,
name,
language,
shouldStart: widgetFactory.preferKernel,
canStart: widgetFactory.canStartKernel,
shutdownOnDispose: widgetFactory.shutdownOnClose,
autoStartDefault: widgetFactory.autoStartDefault
};
}
/**
* Get the best file type given a contents model.
*
* @param model - The contents model of interest.
*
* @returns The best matching file type.
*/
getFileTypeForModel(
model: Partial<Contents.IModel>
): DocumentRegistry.IFileType {
let ft: DocumentRegistry.IFileType | null = null;
if (model.name || model.path) {
const name = model.name || PathExt.basename(model.path!);
const fts = this.getFileTypesForPath(name);
if (fts.length > 0) {
ft = fts[0];
}
}
switch (model.type) {
case 'directory':
if (ft !== null && ft.contentType === 'directory') {
return ft;
}
return (
find(this._fileTypes, ft => ft.contentType === 'directory') ||
DocumentRegistry.getDefaultDirectoryFileType(this.translator)
);
case 'notebook':
if (ft !== null && ft.contentType === 'notebook') {
return ft;
}
return (
find(this._fileTypes, ft => ft.contentType === 'notebook') ||
DocumentRegistry.getDefaultNotebookFileType(this.translator)
);
default:
// Find the best matching extension.
if (ft !== null) {
return ft;
}
return (
this.getFileType('text') ||
DocumentRegistry.getDefaultTextFileType(this.translator)
);
}
}
/**
* Get the file types that match a file name.
*
* @param path - The path of the file.
*
* @returns An ordered list of matching file types.
*/
getFileTypesForPath(path: string): DocumentRegistry.IFileType[] {
const fts: DocumentRegistry.IFileType[] = [];
const name = PathExt.basename(path);
// Look for a pattern match first.
let ft = find(this._fileTypes, ft => {
return !!(ft.pattern && name.match(ft.pattern) !== null);
});
if (ft) {
fts.push(ft);
}
// Then look by extension name, starting with the longest
let ext = Private.extname(name);
while (ext.length > 1) {
const ftSubset = this._fileTypes.filter(ft =>
// In Private.extname, the extension is transformed to lower case
ft.extensions.map(extension => extension.toLowerCase()).includes(ext)
);
fts.push(...ftSubset);
ext = '.' + ext.split('.').slice(2).join('.');
}
return fts;
}
protected translator: ITranslator;
private _modelFactories: {
[key: string]: DocumentRegistry.ModelFactory;
} = Object.create(null);
private _widgetFactories: {
[key: string]: DocumentRegistry.WidgetFactory;
} = Object.create(null);
private _defaultWidgetFactory = '';
private _defaultWidgetFactoryOverrides: {
[key: string]: string;
} = Object.create(null);
private _defaultWidgetFactories: { [key: string]: string } =
Object.create(null);
private _defaultRenderedWidgetFactories: {
[key: string]: string;
} = Object.create(null);
private _widgetFactoriesForFileType: {
[key: string]: string[];
} = Object.create(null);
private _fileTypes: DocumentRegistry.IFileType[] = [];
private _extenders: {
[key: string]: DocumentRegistry.WidgetExtension[];
} = Object.create(null);
private _changed = new Signal<this, DocumentRegistry.IChangedArgs>(this);
private _isDisposed = false;
}
/**
* The namespace for the `DocumentRegistry` class statics.
*/
export namespace DocumentRegistry {
/**
* The item to be added to document toolbar.
*/
export interface IToolbarItem extends ToolbarRegistry.IToolbarItem {}
/**
* The options used to create a document registry.
*/
export interface IOptions {
/**
* The text model factory for the registry. A default instance will
* be used if not given.
*/
textModelFactory?: ModelFactory;
/**
* The initial file types for the registry.
* The [[DocumentRegistry.defaultFileTypes]] will be used if not given.
*/
initialFileTypes?: DocumentRegistry.IFileType[];
/**
* The application language translator.
*/
translator?: ITranslator;
}
/**
* The interface for a document model.
*/
export interface IModel extends IDisposable {
/**
* A signal emitted when the document content changes.
*/
contentChanged: ISignal<this, void>;
/**
* A signal emitted when the model state changes.
*/
stateChanged: ISignal<this, IChangedArgsGeneric<any>>;
/**
* The dirty state of the model.
*
* #### Notes
* This should be cleared when the document is loaded from
* or saved to disk.
*/
dirty: boolean;
/**
* The read-only state of the model.
*/
readOnly: boolean;
/**
* The default kernel name of the document.
*/
readonly defaultKernelName: string;
/**
* The default kernel language of the document.
*/
readonly defaultKernelLanguage: string;
/**
* The shared notebook model.
*/
readonly sharedModel: ISharedDocument;
/**
* Whether this document model supports collaboration when the collaborative
* flag is enabled globally. Defaults to `false`.
*/
readonly collaborative?: boolean;
/**
* Serialize the model to a string.
*/
toString(): string;
/**
* Deserialize the model from a string.
*
* #### Notes
* Should emit a [contentChanged] signal.
*/
fromString(value: string): void;
/**
* Serialize the model to JSON.
*/
toJSON(): PartialJSONValue;
/**
* Deserialize the model from JSON.
*
* #### Notes
* Should emit a [contentChanged] signal.
*/
fromJSON(value: ReadonlyPartialJSONValue): void;
}
/**
* The interface for a document model that represents code.
*/
export interface ICodeModel extends IModel, CodeEditor.IModel {
sharedModel: ISharedFile;
}
/**
* The document context object.
*/
export interface IContext<T extends IModel> extends IDisposable {
/**
* A signal emitted when the path changes.
*/
pathChanged: ISignal<this, string>;
/**
* A signal emitted when the contentsModel changes.
*/
fileChanged: ISignal<this, Omit<Contents.IModel, 'content'>>;
/**
* A signal emitted on the start and end of a saving operation.
*/
saveState: ISignal<this, SaveState>;
/**
* A signal emitted when the context is disposed.
*/
disposed: ISignal<this, void>;
/**
* Configurable margin used to detect document modification conflicts, in milliseconds
*/
lastModifiedCheckMargin: number;
/**
* The data model for the document.
*/
readonly model: T;
/**
* The session context object associated with the context.
*/
readonly sessionContext: ISessionContext;
/**
* The current path associated with the document.
*/
readonly path: string;
/**
* The current local path associated with the document.
* If the document is in the default notebook file browser,
* this is the same as the path.
*/
readonly localPath: string;
/**
* The document metadata, stored as a services contents model.
*
* #### Notes
* This will be null until the context is 'ready'. Since we only store
* metadata here, the `content` attribute is removed.
*/
readonly contentsModel: Omit<Contents.IModel, 'content'> | null;
/**
* The url resolver for the context.
*/
readonly urlResolver: IRenderMime.IResolver;
/**
* Whether the context is ready.
*/
readonly isReady: boolean;
/**
* A promise that is fulfilled when the context is ready.
*/
readonly ready: Promise<void>;
/**
* Rename the document.
*/
rename(newName: string): Promise<void>;
/**
* Save the document contents to disk.
*/
save(): Promise<void>;
/**
* Save the document to a different path chosen by the user.
*/
saveAs(): Promise<void>;
/**
* Save the document to a different path chosen by the user.
*/
download(): Promise<void>;
/**
* Revert the document contents to disk contents.
*/
revert(): Promise<void>;
/**
* Create a checkpoint for the file.
*
* @returns A promise which resolves with the new checkpoint model when the
* checkpoint is created.
*/
createCheckpoint(): Promise<Contents.ICheckpointModel>;
/**
* Delete a checkpoint for the file.
*
* @param checkpointID - The id of the checkpoint to delete.
*
* @returns A promise which resolves when the checkpoint is deleted.
*/
deleteCheckpoint(checkpointID: string): Promise<void>;
/**
* Restore the file to a known checkpoint state.
*
* @param checkpointID - The optional id of the checkpoint to restore,
* defaults to the most recent checkpoint.
*
* @returns A promise which resolves when the checkpoint is restored.