1 | import { ISessionContext, Toolbar, ToolbarRegistry } from '@jupyterlab/apputils';
|
2 | import { CodeEditor } from '@jupyterlab/codeeditor';
|
3 | import { IChangedArgs as IChangedArgsGeneric } from '@jupyterlab/coreutils';
|
4 | import { IModelDB, IObservableList } from '@jupyterlab/observables';
|
5 | import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
|
6 | import { Contents, Kernel } from '@jupyterlab/services';
|
7 | import * as models from '@jupyterlab/shared-models';
|
8 | import { ITranslator } from '@jupyterlab/translation';
|
9 | import { LabIcon } from '@jupyterlab/ui-components';
|
10 | import { IIterator } from '@lumino/algorithm';
|
11 | import { PartialJSONValue, ReadonlyPartialJSONValue } from '@lumino/coreutils';
|
12 | import { IDisposable } from '@lumino/disposable';
|
13 | import { ISignal } from '@lumino/signaling';
|
14 | import { DockLayout, Widget } from '@lumino/widgets';
|
15 |
|
16 |
|
17 |
|
18 | export declare class DocumentRegistry implements IDisposable {
|
19 | |
20 |
|
21 |
|
22 | constructor(options?: DocumentRegistry.IOptions);
|
23 | /**
|
24 | * A signal emitted when the registry has changed.
|
25 | */
|
26 | get changed(): ISignal<this, DocumentRegistry.IChangedArgs>;
|
27 | /**
|
28 | * Get whether the document registry has been disposed.
|
29 | */
|
30 | get isDisposed(): boolean;
|
31 | /**
|
32 | * Dispose of the resources held by the document registry.
|
33 | */
|
34 | dispose(): void;
|
35 | /**
|
36 | * Add a widget factory to the registry.
|
37 | *
|
38 | * @param factory - The factory instance to register.
|
39 | *
|
40 | * @returns A disposable which will unregister the factory.
|
41 | *
|
42 | * #### Notes
|
43 | * If a factory with the given `'name'` is already registered,
|
44 | * a warning will be logged, and this will be a no-op.
|
45 | * If `'*'` is given as a default extension, the factory will be registered
|
46 | * as the global default.
|
47 | * If an extension or global default is already registered, this factory
|
48 | * will override the existing default.
|
49 | * The factory cannot be named an empty string or the string `'default'`.
|
50 | */
|
51 | addWidgetFactory(factory: DocumentRegistry.WidgetFactory): IDisposable;
|
52 | /**
|
53 | * Add a model factory to the registry.
|
54 | *
|
55 | * @param factory - The factory instance.
|
56 | *
|
57 | * @returns A disposable which will unregister the factory.
|
58 | *
|
59 | * #### Notes
|
60 | * If a factory with the given `name` is already registered, or
|
61 | * the given factory is already registered, a warning will be logged
|
62 | * and this will be a no-op.
|
63 | */
|
64 | addModelFactory(factory: DocumentRegistry.ModelFactory): IDisposable;
|
65 | /**
|
66 | * Add a widget extension to the registry.
|
67 | *
|
68 | * @param widgetName - The name of the widget factory.
|
69 | *
|
70 | * @param extension - A widget extension.
|
71 | *
|
72 | * @returns A disposable which will unregister the extension.
|
73 | *
|
74 | * #### Notes
|
75 | * If the extension is already registered for the given
|
76 | * widget name, a warning will be logged and this will be a no-op.
|
77 | */
|
78 | addWidgetExtension(widgetName: string, extension: DocumentRegistry.WidgetExtension): IDisposable;
|
79 | /**
|
80 | * Add a file type to the document registry.
|
81 | *
|
82 | * @param fileType - The file type object to register.
|
83 | * @param factories - Optional factories to use for the file type.
|
84 | *
|
85 | * @returns A disposable which will unregister the command.
|
86 | *
|
87 | * #### Notes
|
88 | * These are used to populate the "Create New" dialog.
|
89 | *
|
90 | * If no default factory exists for the file type, the first factory will
|
91 | * be defined as default factory.
|
92 | */
|
93 | addFileType(fileType: Partial<DocumentRegistry.IFileType>, factories?: string[]): IDisposable;
|
94 | /**
|
95 | * Get a list of the preferred widget factories.
|
96 | *
|
97 | * @param path - The file path to filter the results.
|
98 | *
|
99 | * @returns A new array of widget factories.
|
100 | *
|
101 | * #### Notes
|
102 | * Only the widget factories whose associated model factory have
|
103 | * been registered will be returned.
|
104 | * The first item is considered the default. The returned array
|
105 | * has widget factories in the following order:
|
106 | * - path-specific default factory
|
107 | * - path-specific default rendered factory
|
108 | * - global default factory
|
109 | * - all other path-specific factories
|
110 | * - all other global factories
|
111 | */
|
112 | preferredWidgetFactories(path: string): DocumentRegistry.WidgetFactory[];
|
113 | /**
|
114 | * Get the default rendered widget factory for a path.
|
115 | *
|
116 | * @param path - The path to for which to find a widget factory.
|
117 | *
|
118 | * @returns The default rendered widget factory for the path.
|
119 | *
|
120 | * ### Notes
|
121 | * If the widget factory has registered a separate set of `defaultRendered`
|
122 | * file types and there is a match in that set, this returns that.
|
123 | * Otherwise, this returns the same widget factory as
|
124 | * [[defaultWidgetFactory]].
|
125 | */
|
126 | defaultRenderedWidgetFactory(path: string): DocumentRegistry.WidgetFactory;
|
127 | /**
|
128 | * Get the default widget factory for a path.
|
129 | *
|
130 | * @param path - An optional file path to filter the results.
|
131 | *
|
132 | * @returns The default widget factory for an path.
|
133 | *
|
134 | * #### Notes
|
135 | * This is equivalent to the first value in [[preferredWidgetFactories]].
|
136 | */
|
137 | defaultWidgetFactory(path?: string): DocumentRegistry.WidgetFactory;
|
138 | /**
|
139 | * Set overrides for the default widget factory for a file type.
|
140 | *
|
141 | * Normally, a widget factory informs the document registry which file types
|
142 | * it should be the default for using the `defaultFor` option in the
|
143 | * IWidgetFactoryOptions. This function can be used to override that after
|
144 | * the fact.
|
145 | *
|
146 | * @param fileType: The name of the file type.
|
147 | *
|
148 | * @param factory: The name of the factory.
|
149 | *
|
150 | * #### Notes
|
151 | * If `factory` is undefined, then any override will be unset, and the
|
152 | * default factory will revert to the original value.
|
153 | *
|
154 | * If `factory` or `fileType` are not known to the docregistry, or
|
155 | * if `factory` cannot open files of type `fileType`, this will throw
|
156 | * an error.
|
157 | */
|
158 | setDefaultWidgetFactory(fileType: string, factory: string | undefined): void;
|
159 | /**
|
160 | * Create an iterator over the widget factories that have been registered.
|
161 | *
|
162 | * @returns A new iterator of widget factories.
|
163 | */
|
164 | widgetFactories(): IIterator<DocumentRegistry.WidgetFactory>;
|
165 | /**
|
166 | * Create an iterator over the model factories that have been registered.
|
167 | *
|
168 | * @returns A new iterator of model factories.
|
169 | */
|
170 | modelFactories(): IIterator<DocumentRegistry.ModelFactory>;
|
171 | /**
|
172 | * Create an iterator over the registered extensions for a given widget.
|
173 | *
|
174 | * @param widgetName - The name of the widget factory.
|
175 | *
|
176 | * @returns A new iterator over the widget extensions.
|
177 | */
|
178 | widgetExtensions(widgetName: string): IIterator<DocumentRegistry.WidgetExtension>;
|
179 | /**
|
180 | * Create an iterator over the file types that have been registered.
|
181 | *
|
182 | * @returns A new iterator of file types.
|
183 | */
|
184 | fileTypes(): IIterator<DocumentRegistry.IFileType>;
|
185 | /**
|
186 | * Get a widget factory by name.
|
187 | *
|
188 | * @param widgetName - The name of the widget factory.
|
189 | *
|
190 | * @returns A widget factory instance.
|
191 | */
|
192 | getWidgetFactory(widgetName: string): DocumentRegistry.WidgetFactory | undefined;
|
193 | /**
|
194 | * Get a model factory by name.
|
195 | *
|
196 | * @param name - The name of the model factory.
|
197 | *
|
198 | * @returns A model factory instance.
|
199 | */
|
200 | getModelFactory(name: string): DocumentRegistry.ModelFactory | undefined;
|
201 | /**
|
202 | * Get a file type by name.
|
203 | */
|
204 | getFileType(name: string): DocumentRegistry.IFileType | undefined;
|
205 | /**
|
206 | * Get a kernel preference.
|
207 | *
|
208 | * @param path - The file path.
|
209 | *
|
210 | * @param widgetName - The name of the widget factory.
|
211 | *
|
212 | * @param kernel - An optional existing kernel model.
|
213 | *
|
214 | * @returns A kernel preference.
|
215 | */
|
216 | getKernelPreference(path: string, widgetName: string, kernel?: Partial<Kernel.IModel>): ISessionContext.IKernelPreference | undefined;
|
217 | /**
|
218 | * Get the best file type given a contents model.
|
219 | *
|
220 | * @param model - The contents model of interest.
|
221 | *
|
222 | * @returns The best matching file type.
|
223 | */
|
224 | getFileTypeForModel(model: Partial<Contents.IModel>): DocumentRegistry.IFileType;
|
225 | /**
|
226 | * Get the file types that match a file name.
|
227 | *
|
228 | * @param path - The path of the file.
|
229 | *
|
230 | * @returns An ordered list of matching file types.
|
231 | */
|
232 | getFileTypesForPath(path: string): DocumentRegistry.IFileType[];
|
233 | protected translator: ITranslator;
|
234 | private _modelFactories;
|
235 | private _widgetFactories;
|
236 | private _defaultWidgetFactory;
|
237 | private _defaultWidgetFactoryOverrides;
|
238 | private _defaultWidgetFactories;
|
239 | private _defaultRenderedWidgetFactories;
|
240 | private _widgetFactoriesForFileType;
|
241 | private _fileTypes;
|
242 | private _extenders;
|
243 | private _changed;
|
244 | private _isDisposed;
|
245 | }
|
246 | /**
|
247 | * The namespace for the `DocumentRegistry` class statics.
|
248 | */
|
249 | export declare namespace DocumentRegistry {
|
250 | |
251 |
|
252 |
|
253 | interface IToolbarItem extends ToolbarRegistry.IToolbarItem {
|
254 | }
|
255 | |
256 |
|
257 |
|
258 | interface IOptions {
|
259 | |
260 |
|
261 |
|
262 |
|
263 | textModelFactory?: ModelFactory;
|
264 | |
265 |
|
266 |
|
267 |
|
268 | initialFileTypes?: DocumentRegistry.IFileType[];
|
269 | |
270 |
|
271 |
|
272 | translator?: ITranslator;
|
273 | }
|
274 | |
275 |
|
276 |
|
277 | interface IModel extends IDisposable {
|
278 | |
279 |
|
280 |
|
281 | contentChanged: ISignal<this, void>;
|
282 | |
283 |
|
284 |
|
285 | stateChanged: ISignal<this, IChangedArgsGeneric<any>>;
|
286 | |
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | dirty: boolean;
|
294 | |
295 |
|
296 |
|
297 | readOnly: boolean;
|
298 | |
299 |
|
300 |
|
301 | readonly defaultKernelName: string;
|
302 | |
303 |
|
304 |
|
305 | readonly defaultKernelLanguage: string;
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | readonly modelDB: IModelDB;
|
315 | |
316 |
|
317 |
|
318 | readonly sharedModel: models.ISharedDocument;
|
319 | |
320 |
|
321 |
|
322 | toString(): string;
|
323 | |
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | fromString(value: string): void;
|
330 | |
331 |
|
332 |
|
333 | toJSON(): PartialJSONValue;
|
334 | |
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 | fromJSON(value: ReadonlyPartialJSONValue): void;
|
341 | |
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | initialize(): void;
|
349 | }
|
350 | |
351 |
|
352 |
|
353 | interface ICodeModel extends IModel, CodeEditor.IModel {
|
354 | sharedModel: models.ISharedFile;
|
355 | }
|
356 | |
357 |
|
358 |
|
359 | interface IContext<T extends IModel> extends IDisposable {
|
360 | |
361 |
|
362 |
|
363 | pathChanged: ISignal<this, string>;
|
364 | |
365 |
|
366 |
|
367 | fileChanged: ISignal<this, Contents.IModel>;
|
368 | |
369 |
|
370 |
|
371 | saveState: ISignal<this, SaveState>;
|
372 | |
373 |
|
374 |
|
375 | disposed: ISignal<this, void>;
|
376 | |
377 |
|
378 |
|
379 | lastModifiedCheckMargin: number;
|
380 | |
381 |
|
382 |
|
383 | readonly model: T;
|
384 | |
385 |
|
386 |
|
387 | readonly sessionContext: ISessionContext;
|
388 | |
389 |
|
390 |
|
391 | readonly path: string;
|
392 | |
393 |
|
394 |
|
395 |
|
396 |
|
397 | readonly localPath: string;
|
398 | |
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 | readonly contentsModel: Contents.IModel | null;
|
406 | |
407 |
|
408 |
|
409 | readonly urlResolver: IRenderMime.IResolver;
|
410 | |
411 |
|
412 |
|
413 | readonly isReady: boolean;
|
414 | |
415 |
|
416 |
|
417 | readonly ready: Promise<void>;
|
418 | |
419 |
|
420 |
|
421 | rename(newName: string): Promise<void>;
|
422 | |
423 |
|
424 |
|
425 | save(): Promise<void>;
|
426 | |
427 |
|
428 |
|
429 | saveAs(): Promise<void>;
|
430 | |
431 |
|
432 |
|
433 | download(): Promise<void>;
|
434 | |
435 |
|
436 |
|
437 | revert(): Promise<void>;
|
438 | |
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 | createCheckpoint(): Promise<Contents.ICheckpointModel>;
|
445 | |
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 | deleteCheckpoint(checkpointID: string): Promise<void>;
|
453 | |
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 | restoreCheckpoint(checkpointID?: string): Promise<void>;
|
462 | |
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 | listCheckpoints(): Promise<Contents.ICheckpointModel[]>;
|
469 | |
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 | addSibling(widget: Widget, options?: IOpenOptions): IDisposable;
|
483 | }
|
484 | |
485 |
|
486 |
|
487 | type SaveState = 'started' | 'failed' | 'completed';
|
488 | |
489 |
|
490 |
|
491 | type Context = IContext<IModel>;
|
492 | |
493 |
|
494 |
|
495 | type CodeContext = IContext<ICodeModel>;
|
496 | |
497 |
|
498 |
|
499 | interface IWidgetFactoryOptions<T extends Widget = Widget> {
|
500 | |
501 |
|
502 |
|
503 | readonly name: string;
|
504 | |
505 |
|
506 |
|
507 | readonly fileTypes: ReadonlyArray<string>;
|
508 | |
509 |
|
510 |
|
511 | readonly defaultFor?: ReadonlyArray<string>;
|
512 | |
513 |
|
514 |
|
515 |
|
516 |
|
517 | readonly defaultRendered?: ReadonlyArray<string>;
|
518 | |
519 |
|
520 |
|
521 | readonly readOnly?: boolean;
|
522 | |
523 |
|
524 |
|
525 | readonly modelName?: string;
|
526 | |
527 |
|
528 |
|
529 | readonly preferKernel?: boolean;
|
530 | |
531 |
|
532 |
|
533 | readonly canStartKernel?: boolean;
|
534 | |
535 |
|
536 |
|
537 | readonly shutdownOnClose?: boolean;
|
538 | |
539 |
|
540 |
|
541 | readonly translator?: ITranslator;
|
542 | |
543 |
|
544 |
|
545 | readonly toolbarFactory?: (widget: T) => DocumentRegistry.IToolbarItem[] | IObservableList<DocumentRegistry.IToolbarItem>;
|
546 | }
|
547 | |
548 |
|
549 |
|
550 | interface IOpenOptions {
|
551 | |
552 |
|
553 |
|
554 |
|
555 |
|
556 | ref?: string | null;
|
557 | |
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 | mode?: DockLayout.InsertMode;
|
564 | |
565 |
|
566 |
|
567 | activate?: boolean;
|
568 | |
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 | rank?: number;
|
575 | }
|
576 | |
577 |
|
578 |
|
579 | interface IWidgetFactory<T extends IDocumentWidget, U extends IModel> extends IDisposable, IWidgetFactoryOptions {
|
580 | |
581 |
|
582 |
|
583 | widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
|
584 | |
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 | createNew(context: IContext<U>, source?: T): T;
|
593 | }
|
594 | |
595 |
|
596 |
|
597 | type WidgetFactory = IWidgetFactory<IDocumentWidget, IModel>;
|
598 | |
599 |
|
600 |
|
601 | interface IWidgetExtension<T extends Widget, U extends IModel> {
|
602 | |
603 |
|
604 |
|
605 | createNew(widget: T, context: IContext<U>): IDisposable | void;
|
606 | }
|
607 | |
608 |
|
609 |
|
610 | type WidgetExtension = IWidgetExtension<Widget, IModel>;
|
611 | |
612 |
|
613 |
|
614 | interface IModelFactory<T extends IModel> extends IDisposable {
|
615 | |
616 |
|
617 |
|
618 | readonly name: string;
|
619 | |
620 |
|
621 |
|
622 | readonly contentType: Contents.ContentType;
|
623 | |
624 |
|
625 |
|
626 | readonly fileFormat: Contents.FileFormat;
|
627 | |
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 | createNew(languagePreference?: string, modelDB?: IModelDB, isInitialized?: boolean): T;
|
637 | |
638 |
|
639 |
|
640 | preferredLanguage(path: string): string;
|
641 | }
|
642 | |
643 |
|
644 |
|
645 | type ModelFactory = IModelFactory<IModel>;
|
646 | |
647 |
|
648 |
|
649 | type CodeModelFactory = IModelFactory<ICodeModel>;
|
650 | |
651 |
|
652 |
|
653 | interface IFileType {
|
654 | |
655 |
|
656 |
|
657 | readonly name: string;
|
658 | |
659 |
|
660 |
|
661 | readonly mimeTypes: ReadonlyArray<string>;
|
662 | |
663 |
|
664 |
|
665 |
|
666 | readonly extensions: ReadonlyArray<string>;
|
667 | |
668 |
|
669 |
|
670 | readonly displayName?: string;
|
671 | |
672 |
|
673 |
|
674 | readonly pattern?: string;
|
675 | |
676 |
|
677 |
|
678 | readonly icon?: LabIcon;
|
679 | |
680 |
|
681 |
|
682 | readonly iconClass?: string;
|
683 | |
684 |
|
685 |
|
686 | readonly iconLabel?: string;
|
687 | |
688 |
|
689 |
|
690 | readonly contentType: Contents.ContentType;
|
691 | |
692 |
|
693 |
|
694 | readonly fileFormat: Contents.FileFormat;
|
695 | }
|
696 | |
697 |
|
698 |
|
699 | interface IChangedArgs {
|
700 | |
701 |
|
702 |
|
703 | readonly type: 'widgetFactory' | 'modelFactory' | 'widgetExtension' | 'fileType';
|
704 | |
705 |
|
706 |
|
707 | readonly name?: string;
|
708 | |
709 |
|
710 |
|
711 | readonly change: 'added' | 'removed';
|
712 | }
|
713 | |
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 | function getFileTypeDefaults(translator?: ITranslator): IFileType;
|
721 | |
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 | function getDefaultTextFileType(translator?: ITranslator): IFileType;
|
729 | |
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 | function getDefaultNotebookFileType(translator?: ITranslator): IFileType;
|
737 | |
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 | function getDefaultDirectoryFileType(translator?: ITranslator): IFileType;
|
745 | |
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 | function getDefaultFileTypes(translator?: ITranslator): ReadonlyArray<Partial<IFileType>>;
|
753 | }
|
754 |
|
755 |
|
756 |
|
757 | export interface IDocumentWidget<T extends Widget = Widget, U extends DocumentRegistry.IModel = DocumentRegistry.IModel> extends Widget {
|
758 | |
759 |
|
760 |
|
761 | readonly content: T;
|
762 | |
763 |
|
764 |
|
765 | readonly context: DocumentRegistry.IContext<U>;
|
766 | |
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 | isUntitled?: boolean;
|
774 | |
775 |
|
776 |
|
777 | readonly revealed: Promise<void>;
|
778 | |
779 |
|
780 |
|
781 | readonly toolbar: Toolbar<Widget>;
|
782 | |
783 |
|
784 |
|
785 | setFragment(fragment: string): void;
|
786 | }
|