UNPKG

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