UNPKG

26.4 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 * as models from '@jupyterlab/shared-models';
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 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 */
249export declare namespace DocumentRegistry {
250 /**
251 * The item to be added to document toolbar.
252 */
253 interface IToolbarItem extends ToolbarRegistry.IToolbarItem {
254 }
255 /**
256 * The options used to create a document registry.
257 */
258 interface IOptions {
259 /**
260 * The text model factory for the registry. A default instance will
261 * be used if not given.
262 */
263 textModelFactory?: ModelFactory;
264 /**
265 * The initial file types for the registry.
266 * The [[DocumentRegistry.defaultFileTypes]] will be used if not given.
267 */
268 initialFileTypes?: DocumentRegistry.IFileType[];
269 /**
270 * The application language translator.
271 */
272 translator?: ITranslator;
273 }
274 /**
275 * The interface for a document model.
276 */
277 interface IModel extends IDisposable {
278 /**
279 * A signal emitted when the document content changes.
280 */
281 contentChanged: ISignal<this, void>;
282 /**
283 * A signal emitted when the model state changes.
284 */
285 stateChanged: ISignal<this, IChangedArgsGeneric<any>>;
286 /**
287 * The dirty state of the model.
288 *
289 * #### Notes
290 * This should be cleared when the document is loaded from
291 * or saved to disk.
292 */
293 dirty: boolean;
294 /**
295 * The read-only state of the model.
296 */
297 readOnly: boolean;
298 /**
299 * The default kernel name of the document.
300 */
301 readonly defaultKernelName: string;
302 /**
303 * The default kernel language of the document.
304 */
305 readonly defaultKernelLanguage: string;
306 /**
307 * The underlying `IModelDB` instance in which model
308 * data is stored.
309 *
310 * ### Notes
311 * Making direct edits to the values stored in the`IModelDB`
312 * is not recommended, and may produce unpredictable results.
313 */
314 readonly modelDB: IModelDB;
315 /**
316 * The shared notebook model.
317 */
318 readonly sharedModel: models.ISharedDocument;
319 /**
320 * Serialize the model to a string.
321 */
322 toString(): string;
323 /**
324 * Deserialize the model from a string.
325 *
326 * #### Notes
327 * Should emit a [contentChanged] signal.
328 */
329 fromString(value: string): void;
330 /**
331 * Serialize the model to JSON.
332 */
333 toJSON(): PartialJSONValue;
334 /**
335 * Deserialize the model from JSON.
336 *
337 * #### Notes
338 * Should emit a [contentChanged] signal.
339 */
340 fromJSON(value: ReadonlyPartialJSONValue): void;
341 /**
342 * Initialize model state after initial data load.
343 *
344 * #### Notes
345 * This function must be called after the initial data is loaded to set up
346 * initial model state, such as an initial undo stack, etc.
347 */
348 initialize(): void;
349 }
350 /**
351 * The interface for a document model that represents code.
352 */
353 interface ICodeModel extends IModel, CodeEditor.IModel {
354 sharedModel: models.ISharedFile;
355 }
356 /**
357 * The document context object.
358 */
359 interface IContext<T extends IModel> extends IDisposable {
360 /**
361 * A signal emitted when the path changes.
362 */
363 pathChanged: ISignal<this, string>;
364 /**
365 * A signal emitted when the contentsModel changes.
366 */
367 fileChanged: ISignal<this, Contents.IModel>;
368 /**
369 * A signal emitted on the start and end of a saving operation.
370 */
371 saveState: ISignal<this, SaveState>;
372 /**
373 * A signal emitted when the context is disposed.
374 */
375 disposed: ISignal<this, void>;
376 /**
377 * Configurable margin used to detect document modification conflicts, in milliseconds
378 */
379 lastModifiedCheckMargin: number;
380 /**
381 * The data model for the document.
382 */
383 readonly model: T;
384 /**
385 * The session context object associated with the context.
386 */
387 readonly sessionContext: ISessionContext;
388 /**
389 * The current path associated with the document.
390 */
391 readonly path: string;
392 /**
393 * The current local path associated with the document.
394 * If the document is in the default notebook file browser,
395 * this is the same as the path.
396 */
397 readonly localPath: string;
398 /**
399 * The document metadata, stored as a services contents model.
400 *
401 * #### Notes
402 * This will be null until the context is 'ready'. Since we only store
403 * metadata here, the `.contents` attribute will always be empty.
404 */
405 readonly contentsModel: Contents.IModel | null;
406 /**
407 * The url resolver for the context.
408 */
409 readonly urlResolver: IRenderMime.IResolver;
410 /**
411 * Whether the context is ready.
412 */
413 readonly isReady: boolean;
414 /**
415 * A promise that is fulfilled when the context is ready.
416 */
417 readonly ready: Promise<void>;
418 /**
419 * Rename the document.
420 */
421 rename(newName: string): Promise<void>;
422 /**
423 * Save the document contents to disk.
424 */
425 save(): Promise<void>;
426 /**
427 * Save the document to a different path chosen by the user.
428 */
429 saveAs(): Promise<void>;
430 /**
431 * Save the document to a different path chosen by the user.
432 */
433 download(): Promise<void>;
434 /**
435 * Revert the document contents to disk contents.
436 */
437 revert(): Promise<void>;
438 /**
439 * Create a checkpoint for the file.
440 *
441 * @returns A promise which resolves with the new checkpoint model when the
442 * checkpoint is created.
443 */
444 createCheckpoint(): Promise<Contents.ICheckpointModel>;
445 /**
446 * Delete a checkpoint for the file.
447 *
448 * @param checkpointID - The id of the checkpoint to delete.
449 *
450 * @returns A promise which resolves when the checkpoint is deleted.
451 */
452 deleteCheckpoint(checkpointID: string): Promise<void>;
453 /**
454 * Restore the file to a known checkpoint state.
455 *
456 * @param checkpointID - The optional id of the checkpoint to restore,
457 * defaults to the most recent checkpoint.
458 *
459 * @returns A promise which resolves when the checkpoint is restored.
460 */
461 restoreCheckpoint(checkpointID?: string): Promise<void>;
462 /**
463 * List available checkpoints for the file.
464 *
465 * @returns A promise which resolves with a list of checkpoint models for
466 * the file.
467 */
468 listCheckpoints(): Promise<Contents.ICheckpointModel[]>;
469 /**
470 * Add a sibling widget to the document manager.
471 *
472 * @param widget - The widget to add to the document manager.
473 *
474 * @param options - The desired options for adding the sibling.
475 *
476 * @returns A disposable used to remove the sibling if desired.
477 *
478 * #### Notes
479 * It is assumed that the widget has the same model and context
480 * as the original widget.
481 */
482 addSibling(widget: Widget, options?: IOpenOptions): IDisposable;
483 }
484 /**
485 * Document save state
486 */
487 type SaveState = 'started' | 'failed' | 'completed';
488 /**
489 * A type alias for a context.
490 */
491 type Context = IContext<IModel>;
492 /**
493 * A type alias for a code context.
494 */
495 type CodeContext = IContext<ICodeModel>;
496 /**
497 * The options used to initialize a widget factory.
498 */
499 interface IWidgetFactoryOptions<T extends Widget = Widget> {
500 /**
501 * The name of the widget to display in dialogs.
502 */
503 readonly name: string;
504 /**
505 * The file types the widget can view.
506 */
507 readonly fileTypes: ReadonlyArray<string>;
508 /**
509 * The file types for which the factory should be the default.
510 */
511 readonly defaultFor?: ReadonlyArray<string>;
512 /**
513 * The file types for which the factory should be the default for rendering,
514 * if that is different than the default factory (which may be for editing).
515 * If undefined, then it will fall back on the default file type.
516 */
517 readonly defaultRendered?: ReadonlyArray<string>;
518 /**
519 * Whether the widget factory is read only.
520 */
521 readonly readOnly?: boolean;
522 /**
523 * The registered name of the model type used to create the widgets.
524 */
525 readonly modelName?: string;
526 /**
527 * Whether the widgets prefer having a kernel started.
528 */
529 readonly preferKernel?: boolean;
530 /**
531 * Whether the widgets can start a kernel when opened.
532 */
533 readonly canStartKernel?: boolean;
534 /**
535 * Whether the kernel should be shutdown when the widget is closed.
536 */
537 readonly shutdownOnClose?: boolean;
538 /**
539 * The application language translator.
540 */
541 readonly translator?: ITranslator;
542 /**
543 * A function producing toolbar widgets, overriding the default toolbar widgets.
544 */
545 readonly toolbarFactory?: (widget: T) => DocumentRegistry.IToolbarItem[] | IObservableList<DocumentRegistry.IToolbarItem>;
546 }
547 /**
548 * The options used to open a widget.
549 */
550 interface IOpenOptions {
551 /**
552 * The reference widget id for the insert location.
553 *
554 * The default is `null`.
555 */
556 ref?: string | null;
557 /**
558 * The supported insertion modes.
559 *
560 * An insert mode is used to specify how a widget should be added
561 * to the main area relative to a reference widget.
562 */
563 mode?: DockLayout.InsertMode;
564 /**
565 * Whether to activate the widget. Defaults to `true`.
566 */
567 activate?: boolean;
568 /**
569 * The rank order of the widget among its siblings.
570 *
571 * #### Notes
572 * This field may be used or ignored depending on shell implementation.
573 */
574 rank?: number;
575 }
576 /**
577 * The interface for a widget factory.
578 */
579 interface IWidgetFactory<T extends IDocumentWidget, U extends IModel> extends IDisposable, IWidgetFactoryOptions {
580 /**
581 * A signal emitted when a new widget is created.
582 */
583 widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
584 /**
585 * Create a new widget given a context.
586 *
587 * @param source - A widget to clone
588 *
589 * #### Notes
590 * It should emit the [widgetCreated] signal with the new widget.
591 */
592 createNew(context: IContext<U>, source?: T): T;
593 }
594 /**
595 * A type alias for a standard widget factory.
596 */
597 type WidgetFactory = IWidgetFactory<IDocumentWidget, IModel>;
598 /**
599 * An interface for a widget extension.
600 */
601 interface IWidgetExtension<T extends Widget, U extends IModel> {
602 /**
603 * Create a new extension for a given widget.
604 */
605 createNew(widget: T, context: IContext<U>): IDisposable | void;
606 }
607 /**
608 * A type alias for a standard widget extension.
609 */
610 type WidgetExtension = IWidgetExtension<Widget, IModel>;
611 /**
612 * The interface for a model factory.
613 */
614 interface IModelFactory<T extends IModel> extends IDisposable {
615 /**
616 * The name of the model.
617 */
618 readonly name: string;
619 /**
620 * The content type of the file (defaults to `"file"`).
621 */
622 readonly contentType: Contents.ContentType;
623 /**
624 * The format of the file (defaults to `"text"`).
625 */
626 readonly fileFormat: Contents.FileFormat;
627 /**
628 * Create a new model for a given path.
629 *
630 * @param languagePreference - An optional kernel language preference.
631 * @param modelDB - An optional modelDB.
632 * @param isInitialized - An optional flag to check if the model is initialized.
633 *
634 * @returns A new document model.
635 */
636 createNew(languagePreference?: string, modelDB?: IModelDB, isInitialized?: boolean): T;
637 /**
638 * Get the preferred kernel language given a file path.
639 */
640 preferredLanguage(path: string): string;
641 }
642 /**
643 * A type alias for a standard model factory.
644 */
645 type ModelFactory = IModelFactory<IModel>;
646 /**
647 * A type alias for a code model factory.
648 */
649 type CodeModelFactory = IModelFactory<ICodeModel>;
650 /**
651 * An interface for a file type.
652 */
653 interface IFileType {
654 /**
655 * The name of the file type.
656 */
657 readonly name: string;
658 /**
659 * The mime types associated the file type.
660 */
661 readonly mimeTypes: ReadonlyArray<string>;
662 /**
663 * The extensions of the file type (e.g. `".txt"`). Can be a compound
664 * extension (e.g. `".table.json`).
665 */
666 readonly extensions: ReadonlyArray<string>;
667 /**
668 * An optional display name for the file type.
669 */
670 readonly displayName?: string;
671 /**
672 * An optional pattern for a file name (e.g. `^Dockerfile$`).
673 */
674 readonly pattern?: string;
675 /**
676 * The icon for the file type.
677 */
678 readonly icon?: LabIcon;
679 /**
680 * The icon class name for the file type.
681 */
682 readonly iconClass?: string;
683 /**
684 * The icon label for the file type.
685 */
686 readonly iconLabel?: string;
687 /**
688 * The content type of the new file.
689 */
690 readonly contentType: Contents.ContentType;
691 /**
692 * The format of the new file.
693 */
694 readonly fileFormat: Contents.FileFormat;
695 }
696 /**
697 * An arguments object for the `changed` signal.
698 */
699 interface IChangedArgs {
700 /**
701 * The type of the changed item.
702 */
703 readonly type: 'widgetFactory' | 'modelFactory' | 'widgetExtension' | 'fileType';
704 /**
705 * The name of the item or the widget factory being extended.
706 */
707 readonly name?: string;
708 /**
709 * Whether the item was added or removed.
710 */
711 readonly change: 'added' | 'removed';
712 }
713 /**
714 * The defaults used for a file type.
715 *
716 * @param translator - The application language translator.
717 *
718 * @returns The default file type.
719 */
720 function getFileTypeDefaults(translator?: ITranslator): IFileType;
721 /**
722 * The default text file type used by the document registry.
723 *
724 * @param translator - The application language translator.
725 *
726 * @returns The default text file type.
727 */
728 function getDefaultTextFileType(translator?: ITranslator): IFileType;
729 /**
730 * The default notebook file type used by the document registry.
731 *
732 * @param translator - The application language translator.
733 *
734 * @returns The default notebook file type.
735 */
736 function getDefaultNotebookFileType(translator?: ITranslator): IFileType;
737 /**
738 * The default directory file type used by the document registry.
739 *
740 * @param translator - The application language translator.
741 *
742 * @returns The default directory file type.
743 */
744 function getDefaultDirectoryFileType(translator?: ITranslator): IFileType;
745 /**
746 * The default file types used by the document registry.
747 *
748 * @param translator - The application language translator.
749 *
750 * @returns The default directory file types.
751 */
752 function getDefaultFileTypes(translator?: ITranslator): ReadonlyArray<Partial<IFileType>>;
753}
754/**
755 * An interface for a document widget.
756 */
757export interface IDocumentWidget<T extends Widget = Widget, U extends DocumentRegistry.IModel = DocumentRegistry.IModel> extends Widget {
758 /**
759 * The content widget.
760 */
761 readonly content: T;
762 /**
763 * The context associated with the document.
764 */
765 readonly context: DocumentRegistry.IContext<U>;
766 /**
767 * Whether the document has an auto-generated name or not.
768 *
769 * #### Notes
770 * A document has auto-generated name if its name is untitled and up
771 * to the instant the user saves it manually for the first time.
772 */
773 isUntitled?: boolean;
774 /**
775 * A promise resolving after the content widget is revealed.
776 */
777 readonly revealed: Promise<void>;
778 /**
779 * The toolbar for the widget.
780 */
781 readonly toolbar: Toolbar<Widget>;
782 /**
783 * Set URI fragment identifier.
784 */
785 setFragment(fragment: string): void;
786}