UNPKG

26.9 kBTypeScriptView Raw
1import { ISessionContext, Toolbar, ToolbarRegistry } from '@jupyterlab/apputils';
2import { CodeEditor } from '@jupyterlab/codeeditor';
3import { IChangedArgs as IChangedArgsGeneric } from '@jupyterlab/coreutils';
4import { IModelDB, IObservableList } from '@jupyterlab/observables';
5import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
6import { Contents, Kernel } from '@jupyterlab/services';
7import { ISharedDocument, ISharedFile } from '@jupyter/ydoc';
8import { ITranslator } from '@jupyterlab/translation';
9import { LabIcon } from '@jupyterlab/ui-components';
10import { IIterator } from '@lumino/algorithm';
11import { PartialJSONValue, ReadonlyPartialJSONValue } from '@lumino/coreutils';
12import { IDisposable } from '@lumino/disposable';
13import { ISignal } from '@lumino/signaling';
14import { DockLayout, Widget } from '@lumino/widgets';
15/**
16 * The document registry.
17 */
18export declare class DocumentRegistry implements IDisposable {
19 /**
20 * Construct a new document registry.
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 * The user setting `defaultViewers` took precedence on this one too.
127 */
128 defaultRenderedWidgetFactory(path: string): DocumentRegistry.WidgetFactory;
129 /**
130 * Get the default widget factory for a path.
131 *
132 * @param path - An optional file path to filter the results.
133 *
134 * @returns The default widget factory for an path.
135 *
136 * #### Notes
137 * This is equivalent to the first value in [[preferredWidgetFactories]].
138 */
139 defaultWidgetFactory(path?: string): DocumentRegistry.WidgetFactory;
140 /**
141 * Set overrides for the default widget factory for a file type.
142 *
143 * Normally, a widget factory informs the document registry which file types
144 * it should be the default for using the `defaultFor` option in the
145 * IWidgetFactoryOptions. This function can be used to override that after
146 * the fact.
147 *
148 * @param fileType: The name of the file type.
149 *
150 * @param factory: The name of the factory.
151 *
152 * #### Notes
153 * If `factory` is undefined, then any override will be unset, and the
154 * default factory will revert to the original value.
155 *
156 * If `factory` or `fileType` are not known to the docregistry, or
157 * if `factory` cannot open files of type `fileType`, this will throw
158 * an error.
159 */
160 setDefaultWidgetFactory(fileType: string, factory: string | undefined): void;
161 /**
162 * Create an iterator over the widget factories that have been registered.
163 *
164 * @returns A new iterator of widget factories.
165 */
166 widgetFactories(): IIterator<DocumentRegistry.WidgetFactory>;
167 /**
168 * Create an iterator over the model factories that have been registered.
169 *
170 * @returns A new iterator of model factories.
171 */
172 modelFactories(): IIterator<DocumentRegistry.ModelFactory>;
173 /**
174 * Create an iterator over the registered extensions for a given widget.
175 *
176 * @param widgetName - The name of the widget factory.
177 *
178 * @returns A new iterator over the widget extensions.
179 */
180 widgetExtensions(widgetName: string): IIterator<DocumentRegistry.WidgetExtension>;
181 /**
182 * Create an iterator over the file types that have been registered.
183 *
184 * @returns A new iterator of file types.
185 */
186 fileTypes(): IIterator<DocumentRegistry.IFileType>;
187 /**
188 * Get a widget factory by name.
189 *
190 * @param widgetName - The name of the widget factory.
191 *
192 * @returns A widget factory instance.
193 */
194 getWidgetFactory(widgetName: string): DocumentRegistry.WidgetFactory | undefined;
195 /**
196 * Get a model factory by name.
197 *
198 * @param name - The name of the model factory.
199 *
200 * @returns A model factory instance.
201 */
202 getModelFactory(name: string): DocumentRegistry.ModelFactory | undefined;
203 /**
204 * Get a file type by name.
205 */
206 getFileType(name: string): DocumentRegistry.IFileType | undefined;
207 /**
208 * Get a kernel preference.
209 *
210 * @param path - The file path.
211 *
212 * @param widgetName - The name of the widget factory.
213 *
214 * @param kernel - An optional existing kernel model.
215 *
216 * @returns A kernel preference.
217 */
218 getKernelPreference(path: string, widgetName: string, kernel?: Partial<Kernel.IModel>): ISessionContext.IKernelPreference | undefined;
219 /**
220 * Get the best file type given a contents model.
221 *
222 * @param model - The contents model of interest.
223 *
224 * @returns The best matching file type.
225 */
226 getFileTypeForModel(model: Partial<Contents.IModel>): DocumentRegistry.IFileType;
227 /**
228 * Get the file types that match a file name.
229 *
230 * @param path - The path of the file.
231 *
232 * @returns An ordered list of matching file types.
233 */
234 getFileTypesForPath(path: string): DocumentRegistry.IFileType[];
235 protected translator: ITranslator;
236 private _modelFactories;
237 private _widgetFactories;
238 private _defaultWidgetFactory;
239 private _defaultWidgetFactoryOverrides;
240 private _defaultWidgetFactories;
241 private _defaultRenderedWidgetFactories;
242 private _widgetFactoriesForFileType;
243 private _fileTypes;
244 private _extenders;
245 private _changed;
246 private _isDisposed;
247}
248/**
249 * The namespace for the `DocumentRegistry` class statics.
250 */
251export declare namespace DocumentRegistry {
252 /**
253 * The item to be added to document toolbar.
254 */
255 interface IToolbarItem extends ToolbarRegistry.IToolbarItem {
256 }
257 /**
258 * The options used to create a document registry.
259 */
260 interface IOptions {
261 /**
262 * The text model factory for the registry. A default instance will
263 * be used if not given.
264 */
265 textModelFactory?: ModelFactory;
266 /**
267 * The initial file types for the registry.
268 * The [[DocumentRegistry.defaultFileTypes]] will be used if not given.
269 */
270 initialFileTypes?: DocumentRegistry.IFileType[];
271 /**
272 * The application language translator.
273 */
274 translator?: ITranslator;
275 }
276 /**
277 * The interface for a document model.
278 */
279 interface IModel extends IDisposable {
280 /**
281 * A signal emitted when the document content changes.
282 */
283 contentChanged: ISignal<this, void>;
284 /**
285 * A signal emitted when the model state changes.
286 */
287 stateChanged: ISignal<this, IChangedArgsGeneric<any>>;
288 /**
289 * The dirty state of the model.
290 *
291 * #### Notes
292 * This should be cleared when the document is loaded from
293 * or saved to disk.
294 */
295 dirty: boolean;
296 /**
297 * The read-only state of the model.
298 */
299 readOnly: boolean;
300 /**
301 * The default kernel name of the document.
302 */
303 readonly defaultKernelName: string;
304 /**
305 * The default kernel language of the document.
306 */
307 readonly defaultKernelLanguage: string;
308 /**
309 * The underlying `IModelDB` instance in which model
310 * data is stored.
311 *
312 * ### Notes
313 * Making direct edits to the values stored in the`IModelDB`
314 * is not recommended, and may produce unpredictable results.
315 */
316 readonly modelDB: IModelDB;
317 /**
318 * The shared notebook model.
319 */
320 readonly sharedModel: ISharedDocument;
321 /**
322 * Whether this document model supports collaboration when the collaborative
323 * flag is enabled globally. Defaults to `false`.
324 */
325 readonly collaborative?: boolean;
326 /**
327 * Serialize the model to a string.
328 */
329 toString(): string;
330 /**
331 * Deserialize the model from a string.
332 *
333 * #### Notes
334 * Should emit a [contentChanged] signal.
335 */
336 fromString(value: string): void;
337 /**
338 * Serialize the model to JSON.
339 */
340 toJSON(): PartialJSONValue;
341 /**
342 * Deserialize the model from JSON.
343 *
344 * #### Notes
345 * Should emit a [contentChanged] signal.
346 */
347 fromJSON(value: ReadonlyPartialJSONValue): void;
348 /**
349 * Initialize model state after initial data load.
350 *
351 * #### Notes
352 * This function must be called after the initial data is loaded to set up
353 * initial model state, such as an initial undo stack, etc.
354 */
355 initialize(): void;
356 }
357 /**
358 * The interface for a document model that represents code.
359 */
360 interface ICodeModel extends IModel, CodeEditor.IModel {
361 sharedModel: ISharedFile;
362 }
363 /**
364 * The document context object.
365 */
366 interface IContext<T extends IModel> extends IDisposable {
367 /**
368 * A signal emitted when the path changes.
369 */
370 pathChanged: ISignal<this, string>;
371 /**
372 * A signal emitted when the contentsModel changes.
373 */
374 fileChanged: ISignal<this, Contents.IModel>;
375 /**
376 * A signal emitted on the start and end of a saving operation.
377 */
378 saveState: ISignal<this, SaveState>;
379 /**
380 * A signal emitted when the context is disposed.
381 */
382 disposed: ISignal<this, void>;
383 /**
384 * Configurable margin used to detect document modification conflicts, in milliseconds
385 */
386 lastModifiedCheckMargin: number;
387 /**
388 * The data model for the document.
389 */
390 readonly model: T;
391 /**
392 * The session context object associated with the context.
393 */
394 readonly sessionContext: ISessionContext;
395 /**
396 * The current path associated with the document.
397 */
398 readonly path: string;
399 /**
400 * The current local path associated with the document.
401 * If the document is in the default notebook file browser,
402 * this is the same as the path.
403 */
404 readonly localPath: string;
405 /**
406 * The document metadata, stored as a services contents model.
407 *
408 * #### Notes
409 * This will be null until the context is 'ready'. Since we only store
410 * metadata here, the `.contents` attribute will always be empty.
411 */
412 readonly contentsModel: Contents.IModel | null;
413 /**
414 * The url resolver for the context.
415 */
416 readonly urlResolver: IRenderMime.IResolver;
417 /**
418 * Whether the context is ready.
419 */
420 readonly isReady: boolean;
421 /**
422 * A promise that is fulfilled when the context is ready.
423 */
424 readonly ready: Promise<void>;
425 /**
426 * Rename the document.
427 */
428 rename(newName: string): Promise<void>;
429 /**
430 * Save the document contents to disk.
431 */
432 save(): Promise<void>;
433 /**
434 * Save the document to a different path chosen by the user.
435 */
436 saveAs(): Promise<void>;
437 /**
438 * Save the document to a different path chosen by the user.
439 */
440 download(): Promise<void>;
441 /**
442 * Revert the document contents to disk contents.
443 */
444 revert(): Promise<void>;
445 /**
446 * Create a checkpoint for the file.
447 *
448 * @returns A promise which resolves with the new checkpoint model when the
449 * checkpoint is created.
450 */
451 createCheckpoint(): Promise<Contents.ICheckpointModel>;
452 /**
453 * Delete a checkpoint for the file.
454 *
455 * @param checkpointID - The id of the checkpoint to delete.
456 *
457 * @returns A promise which resolves when the checkpoint is deleted.
458 */
459 deleteCheckpoint(checkpointID: string): Promise<void>;
460 /**
461 * Restore the file to a known checkpoint state.
462 *
463 * @param checkpointID - The optional id of the checkpoint to restore,
464 * defaults to the most recent checkpoint.
465 *
466 * @returns A promise which resolves when the checkpoint is restored.
467 */
468 restoreCheckpoint(checkpointID?: string): Promise<void>;
469 /**
470 * List available checkpoints for the file.
471 *
472 * @returns A promise which resolves with a list of checkpoint models for
473 * the file.
474 */
475 listCheckpoints(): Promise<Contents.ICheckpointModel[]>;
476 /**
477 * Add a sibling widget to the document manager.
478 *
479 * @param widget - The widget to add to the document manager.
480 *
481 * @param options - The desired options for adding the sibling.
482 *
483 * @returns A disposable used to remove the sibling if desired.
484 *
485 * #### Notes
486 * It is assumed that the widget has the same model and context
487 * as the original widget.
488 */
489 addSibling(widget: Widget, options?: IOpenOptions): IDisposable;
490 }
491 /**
492 * Document save state
493 */
494 type SaveState = 'started' | 'failed' | 'completed';
495 /**
496 * A type alias for a context.
497 */
498 type Context = IContext<IModel>;
499 /**
500 * A type alias for a code context.
501 */
502 type CodeContext = IContext<ICodeModel>;
503 /**
504 * The options used to initialize a widget factory.
505 */
506 interface IWidgetFactoryOptions<T extends Widget = Widget> {
507 /**
508 * The name of the widget to display in dialogs.
509 */
510 readonly name: string;
511 /**
512 * The file types the widget can view.
513 */
514 readonly fileTypes: ReadonlyArray<string>;
515 /**
516 * The file types for which the factory should be the default.
517 */
518 readonly defaultFor?: ReadonlyArray<string>;
519 /**
520 * The file types for which the factory should be the default for rendering,
521 * if that is different than the default factory (which may be for editing).
522 * If undefined, then it will fall back on the default file type.
523 */
524 readonly defaultRendered?: ReadonlyArray<string>;
525 /**
526 * Whether the widget factory is read only.
527 */
528 readonly readOnly?: boolean;
529 /**
530 * The registered name of the model type used to create the widgets.
531 */
532 readonly modelName?: string;
533 /**
534 * Whether the widgets prefer having a kernel started.
535 */
536 readonly preferKernel?: boolean;
537 /**
538 * Whether the widgets can start a kernel when opened.
539 */
540 readonly canStartKernel?: boolean;
541 /**
542 * Whether the kernel should be shutdown when the widget is closed.
543 */
544 readonly shutdownOnClose?: boolean;
545 /**
546 * The application language translator.
547 */
548 readonly translator?: ITranslator;
549 /**
550 * A function producing toolbar widgets, overriding the default toolbar widgets.
551 */
552 readonly toolbarFactory?: (widget: T) => DocumentRegistry.IToolbarItem[] | IObservableList<DocumentRegistry.IToolbarItem>;
553 }
554 /**
555 * The options used to open a widget.
556 */
557 interface IOpenOptions {
558 /**
559 * The reference widget id for the insert location.
560 *
561 * The default is `null`.
562 */
563 ref?: string | null;
564 /**
565 * The supported insertion modes.
566 *
567 * An insert mode is used to specify how a widget should be added
568 * to the main area relative to a reference widget.
569 */
570 mode?: DockLayout.InsertMode;
571 /**
572 * Whether to activate the widget. Defaults to `true`.
573 */
574 activate?: boolean;
575 /**
576 * The rank order of the widget among its siblings.
577 *
578 * #### Notes
579 * This field may be used or ignored depending on shell implementation.
580 */
581 rank?: number;
582 }
583 /**
584 * The interface for a widget factory.
585 */
586 interface IWidgetFactory<T extends IDocumentWidget, U extends IModel> extends IDisposable, IWidgetFactoryOptions {
587 /**
588 * A signal emitted when a new widget is created.
589 */
590 widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
591 /**
592 * Create a new widget given a context.
593 *
594 * @param source - A widget to clone
595 *
596 * #### Notes
597 * It should emit the [widgetCreated] signal with the new widget.
598 */
599 createNew(context: IContext<U>, source?: T): T;
600 }
601 /**
602 * A type alias for a standard widget factory.
603 */
604 type WidgetFactory = IWidgetFactory<IDocumentWidget, IModel>;
605 /**
606 * An interface for a widget extension.
607 */
608 interface IWidgetExtension<T extends Widget, U extends IModel> {
609 /**
610 * Create a new extension for a given widget.
611 */
612 createNew(widget: T, context: IContext<U>): IDisposable | void;
613 }
614 /**
615 * A type alias for a standard widget extension.
616 */
617 type WidgetExtension = IWidgetExtension<Widget, IModel>;
618 /**
619 * The interface for a model factory.
620 */
621 interface IModelFactory<T extends IModel> extends IDisposable {
622 /**
623 * The name of the model.
624 */
625 readonly name: string;
626 /**
627 * The content type of the file (defaults to `"file"`).
628 */
629 readonly contentType: Contents.ContentType;
630 /**
631 * The format of the file (defaults to `"text"`).
632 */
633 readonly fileFormat: Contents.FileFormat;
634 /**
635 * Whether the model is collaborative or not.
636 */
637 readonly collaborative?: boolean;
638 /**
639 * Create a new model for a given path.
640 *
641 * @param languagePreference - An optional kernel language preference.
642 * @param modelDB - An optional model storage.
643 * @param isInitialized - Whether the model is initialized or not.
644 * @param collaborationEnabled - Whether collaboration is enabled at the application level or not (default `false`).
645 *
646 * @returns A new document model.
647 */
648 createNew(languagePreference?: string, modelDB?: IModelDB, isInitialized?: boolean, collaborationEnabled?: boolean): T;
649 /**
650 * Get the preferred kernel language given a file path.
651 */
652 preferredLanguage(path: string): string;
653 }
654 /**
655 * A type alias for a standard model factory.
656 */
657 type ModelFactory = IModelFactory<IModel>;
658 /**
659 * A type alias for a code model factory.
660 */
661 type CodeModelFactory = IModelFactory<ICodeModel>;
662 /**
663 * An interface for a file type.
664 */
665 interface IFileType {
666 /**
667 * The name of the file type.
668 */
669 readonly name: string;
670 /**
671 * The mime types associated the file type.
672 */
673 readonly mimeTypes: ReadonlyArray<string>;
674 /**
675 * The extensions of the file type (e.g. `".txt"`). Can be a compound
676 * extension (e.g. `".table.json`).
677 */
678 readonly extensions: ReadonlyArray<string>;
679 /**
680 * An optional display name for the file type.
681 */
682 readonly displayName?: string;
683 /**
684 * An optional pattern for a file name (e.g. `^Dockerfile$`).
685 */
686 readonly pattern?: string;
687 /**
688 * The icon for the file type.
689 */
690 readonly icon?: LabIcon;
691 /**
692 * The icon class name for the file type.
693 */
694 readonly iconClass?: string;
695 /**
696 * The icon label for the file type.
697 */
698 readonly iconLabel?: string;
699 /**
700 * The content type of the new file.
701 */
702 readonly contentType: Contents.ContentType;
703 /**
704 * The format of the new file.
705 */
706 readonly fileFormat: Contents.FileFormat;
707 }
708 /**
709 * An arguments object for the `changed` signal.
710 */
711 interface IChangedArgs {
712 /**
713 * The type of the changed item.
714 */
715 readonly type: 'widgetFactory' | 'modelFactory' | 'widgetExtension' | 'fileType';
716 /**
717 * The name of the item or the widget factory being extended.
718 */
719 readonly name?: string;
720 /**
721 * Whether the item was added or removed.
722 */
723 readonly change: 'added' | 'removed';
724 }
725 /**
726 * The defaults used for a file type.
727 *
728 * @param translator - The application language translator.
729 *
730 * @returns The default file type.
731 */
732 function getFileTypeDefaults(translator?: ITranslator): IFileType;
733 /**
734 * The default text file type used by the document registry.
735 *
736 * @param translator - The application language translator.
737 *
738 * @returns The default text file type.
739 */
740 function getDefaultTextFileType(translator?: ITranslator): IFileType;
741 /**
742 * The default notebook file type used by the document registry.
743 *
744 * @param translator - The application language translator.
745 *
746 * @returns The default notebook file type.
747 */
748 function getDefaultNotebookFileType(translator?: ITranslator): IFileType;
749 /**
750 * The default directory file type used by the document registry.
751 *
752 * @param translator - The application language translator.
753 *
754 * @returns The default directory file type.
755 */
756 function getDefaultDirectoryFileType(translator?: ITranslator): IFileType;
757 /**
758 * The default file types used by the document registry.
759 *
760 * @param translator - The application language translator.
761 *
762 * @returns The default directory file types.
763 */
764 function getDefaultFileTypes(translator?: ITranslator): ReadonlyArray<Partial<IFileType>>;
765}
766/**
767 * An interface for a document widget.
768 */
769export interface IDocumentWidget<T extends Widget = Widget, U extends DocumentRegistry.IModel = DocumentRegistry.IModel> extends Widget {
770 /**
771 * The content widget.
772 */
773 readonly content: T;
774 /**
775 * The context associated with the document.
776 */
777 readonly context: DocumentRegistry.IContext<U>;
778 /**
779 * Whether the document has an auto-generated name or not.
780 *
781 * #### Notes
782 * A document has auto-generated name if its name is untitled and up
783 * to the instant the user saves it manually for the first time.
784 */
785 isUntitled?: boolean;
786 /**
787 * A promise resolving after the content widget is revealed.
788 */
789 readonly revealed: Promise<void>;
790 /**
791 * The toolbar for the widget.
792 */
793 readonly toolbar: Toolbar<Widget>;
794 /**
795 * Set URI fragment identifier.
796 */
797 setFragment(fragment: string): void;
798}