1 | import { ISessionContext, ToolbarRegistry } from '@jupyterlab/apputils';
|
2 | import { CodeEditor } from '@jupyterlab/codeeditor';
|
3 | import { IChangedArgs as IChangedArgsGeneric } from '@jupyterlab/coreutils';
|
4 | import { IObservableList } from '@jupyterlab/observables';
|
5 | import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
|
6 | import { Contents, Kernel } from '@jupyterlab/services';
|
7 | import { ISharedDocument, ISharedFile } from '@jupyter/ydoc';
|
8 | import { ITranslator } from '@jupyterlab/translation';
|
9 | import { LabIcon, Toolbar } from '@jupyterlab/ui-components';
|
10 | import { PartialJSONValue, ReadonlyPartialJSONValue } from '@lumino/coreutils';
|
11 | import { IDisposable } from '@lumino/disposable';
|
12 | import { ISignal } from '@lumino/signaling';
|
13 | import { DockLayout, Widget } from '@lumino/widgets';
|
14 |
|
15 |
|
16 |
|
17 | export declare class DocumentRegistry implements IDisposable {
|
18 | |
19 |
|
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 | */
|
250 | export declare namespace DocumentRegistry {
|
251 | |
252 |
|
253 |
|
254 | interface IToolbarItem extends ToolbarRegistry.IToolbarItem {
|
255 | }
|
256 | |
257 |
|
258 |
|
259 | interface IOptions {
|
260 | |
261 |
|
262 |
|
263 |
|
264 | textModelFactory?: ModelFactory;
|
265 | |
266 |
|
267 |
|
268 |
|
269 | initialFileTypes?: DocumentRegistry.IFileType[];
|
270 | |
271 |
|
272 |
|
273 | translator?: ITranslator;
|
274 | }
|
275 | |
276 |
|
277 |
|
278 | interface IModel extends IDisposable {
|
279 | |
280 |
|
281 |
|
282 | contentChanged: ISignal<this, void>;
|
283 | |
284 |
|
285 |
|
286 | stateChanged: ISignal<this, IChangedArgsGeneric<any>>;
|
287 | |
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | dirty: boolean;
|
295 | |
296 |
|
297 |
|
298 | readOnly: boolean;
|
299 | |
300 |
|
301 |
|
302 | readonly defaultKernelName: string;
|
303 | |
304 |
|
305 |
|
306 | readonly defaultKernelLanguage: string;
|
307 | |
308 |
|
309 |
|
310 | readonly sharedModel: ISharedDocument;
|
311 | |
312 |
|
313 |
|
314 |
|
315 | readonly collaborative?: boolean;
|
316 | |
317 |
|
318 |
|
319 | toString(): string;
|
320 | |
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | fromString(value: string): void;
|
327 | |
328 |
|
329 |
|
330 | toJSON(): PartialJSONValue;
|
331 | |
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | fromJSON(value: ReadonlyPartialJSONValue): void;
|
338 | }
|
339 | |
340 |
|
341 |
|
342 | interface ICodeModel extends IModel, CodeEditor.IModel {
|
343 | sharedModel: ISharedFile;
|
344 | }
|
345 | |
346 |
|
347 |
|
348 | interface IContext<T extends IModel> extends IDisposable {
|
349 | |
350 |
|
351 |
|
352 | pathChanged: ISignal<this, string>;
|
353 | |
354 |
|
355 |
|
356 | fileChanged: ISignal<this, Omit<Contents.IModel, 'content'>>;
|
357 | |
358 |
|
359 |
|
360 | saveState: ISignal<this, SaveState>;
|
361 | |
362 |
|
363 |
|
364 | disposed: ISignal<this, void>;
|
365 | |
366 |
|
367 |
|
368 | lastModifiedCheckMargin: number;
|
369 | |
370 |
|
371 |
|
372 | readonly model: T;
|
373 | |
374 |
|
375 |
|
376 | readonly sessionContext: ISessionContext;
|
377 | |
378 |
|
379 |
|
380 | readonly path: string;
|
381 | |
382 |
|
383 |
|
384 |
|
385 |
|
386 | readonly localPath: string;
|
387 | |
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | readonly contentsModel: Omit<Contents.IModel, 'content'> | null;
|
395 | |
396 |
|
397 |
|
398 | readonly urlResolver: IRenderMime.IResolver;
|
399 | |
400 |
|
401 |
|
402 | readonly isReady: boolean;
|
403 | |
404 |
|
405 |
|
406 | readonly ready: Promise<void>;
|
407 | |
408 |
|
409 |
|
410 | rename(newName: string): Promise<void>;
|
411 | |
412 |
|
413 |
|
414 | save(): Promise<void>;
|
415 | |
416 |
|
417 |
|
418 | saveAs(): Promise<void>;
|
419 | |
420 |
|
421 |
|
422 | download(): Promise<void>;
|
423 | |
424 |
|
425 |
|
426 | revert(): Promise<void>;
|
427 | |
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 | createCheckpoint(): Promise<Contents.ICheckpointModel>;
|
434 | |
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 | deleteCheckpoint(checkpointID: string): Promise<void>;
|
442 | |
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 | restoreCheckpoint(checkpointID?: string): Promise<void>;
|
451 | |
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 | listCheckpoints(): Promise<Contents.ICheckpointModel[]>;
|
458 | |
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 | addSibling(widget: Widget, options?: IOpenOptions): IDisposable;
|
472 | }
|
473 | |
474 |
|
475 |
|
476 | type SaveState = 'started' | 'failed' | 'completed';
|
477 | |
478 |
|
479 |
|
480 | type Context = IContext<IModel>;
|
481 | |
482 |
|
483 |
|
484 | type CodeContext = IContext<ICodeModel>;
|
485 | |
486 |
|
487 |
|
488 | interface IWidgetFactoryOptions<T extends Widget = Widget> extends Omit<IRenderMime.IDocumentWidgetFactoryOptions, 'primaryFileType' | 'toolbarFactory'> {
|
489 | |
490 |
|
491 |
|
492 | readonly autoStartDefault?: boolean;
|
493 | |
494 |
|
495 |
|
496 | readonly readOnly?: boolean;
|
497 | |
498 |
|
499 |
|
500 | readonly preferKernel?: boolean;
|
501 | |
502 |
|
503 |
|
504 | readonly canStartKernel?: boolean;
|
505 | |
506 |
|
507 |
|
508 | readonly shutdownOnClose?: boolean;
|
509 | |
510 |
|
511 |
|
512 | readonly toolbarFactory?: (widget: T) => DocumentRegistry.IToolbarItem[] | IObservableList<DocumentRegistry.IToolbarItem>;
|
513 | }
|
514 | |
515 |
|
516 |
|
517 | interface IOpenOptions {
|
518 | |
519 |
|
520 |
|
521 |
|
522 |
|
523 | ref?: string | null;
|
524 | |
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 | mode?: DockLayout.InsertMode;
|
531 | |
532 |
|
533 |
|
534 | activate?: boolean;
|
535 | |
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 | rank?: number;
|
542 | |
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 | type?: string;
|
550 | }
|
551 | |
552 |
|
553 |
|
554 | interface IWidgetFactory<T extends IDocumentWidget, U extends IModel> extends IDisposable, IWidgetFactoryOptions {
|
555 | |
556 |
|
557 |
|
558 | widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
|
559 | |
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 | createNew(context: IContext<U>, source?: T): T;
|
568 | }
|
569 | |
570 |
|
571 |
|
572 | type WidgetFactory = IWidgetFactory<IDocumentWidget, IModel>;
|
573 | |
574 |
|
575 |
|
576 | interface IWidgetExtension<T extends Widget, U extends IModel> {
|
577 | |
578 |
|
579 |
|
580 | createNew(widget: T, context: IContext<U>): IDisposable | void;
|
581 | }
|
582 | |
583 |
|
584 |
|
585 | type WidgetExtension = IWidgetExtension<Widget, IModel>;
|
586 | |
587 |
|
588 |
|
589 | interface IModelFactory<T extends IModel, U extends ISharedDocument = ISharedDocument> extends IDisposable {
|
590 | |
591 |
|
592 |
|
593 | readonly name: string;
|
594 | |
595 |
|
596 |
|
597 | readonly contentType: Contents.ContentType;
|
598 | |
599 |
|
600 |
|
601 | readonly fileFormat: Contents.FileFormat;
|
602 | |
603 |
|
604 |
|
605 | readonly collaborative?: boolean;
|
606 | |
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 | createNew(options?: IModelOptions<U>): T;
|
614 | |
615 |
|
616 |
|
617 | preferredLanguage(path: string): string;
|
618 | }
|
619 | |
620 |
|
621 |
|
622 | interface IModelOptions<T extends ISharedDocument = ISharedDocument> {
|
623 | |
624 |
|
625 |
|
626 | languagePreference?: string;
|
627 | |
628 |
|
629 |
|
630 | sharedModel?: T;
|
631 | |
632 |
|
633 |
|
634 | collaborationEnabled?: boolean;
|
635 | }
|
636 | |
637 |
|
638 |
|
639 | type ModelFactory = IModelFactory<IModel>;
|
640 | |
641 |
|
642 |
|
643 | type CodeModelFactory = IModelFactory<ICodeModel>;
|
644 | |
645 |
|
646 |
|
647 | interface IFileType extends IRenderMime.IFileType {
|
648 | |
649 |
|
650 |
|
651 | readonly icon?: LabIcon;
|
652 | |
653 |
|
654 |
|
655 | readonly contentType: Contents.ContentType;
|
656 | |
657 |
|
658 |
|
659 | readonly fileFormat: Contents.FileFormat;
|
660 | }
|
661 | |
662 |
|
663 |
|
664 | interface IChangedArgs {
|
665 | |
666 |
|
667 |
|
668 | readonly type: 'widgetFactory' | 'modelFactory' | 'widgetExtension' | 'fileType';
|
669 | |
670 |
|
671 |
|
672 | readonly name?: string;
|
673 | |
674 |
|
675 |
|
676 | readonly change: 'added' | 'removed';
|
677 | }
|
678 | |
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 | function getFileTypeDefaults(translator?: ITranslator): IFileType;
|
686 | |
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 | function getDefaultTextFileType(translator?: ITranslator): IFileType;
|
694 | |
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 | function getDefaultNotebookFileType(translator?: ITranslator): IFileType;
|
702 | |
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 | function getDefaultDirectoryFileType(translator?: ITranslator): IFileType;
|
710 | |
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 | function getDefaultFileTypes(translator?: ITranslator): ReadonlyArray<Partial<IFileType>>;
|
718 | }
|
719 |
|
720 |
|
721 |
|
722 | export interface IDocumentWidget<T extends Widget = Widget, U extends DocumentRegistry.IModel = DocumentRegistry.IModel> extends Widget {
|
723 | |
724 |
|
725 |
|
726 | readonly content: T;
|
727 | |
728 |
|
729 |
|
730 | readonly context: DocumentRegistry.IContext<U>;
|
731 | |
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 | isUntitled?: boolean;
|
739 | |
740 |
|
741 |
|
742 | readonly revealed: Promise<void>;
|
743 | |
744 |
|
745 |
|
746 | readonly toolbar: Toolbar<Widget>;
|
747 | |
748 |
|
749 |
|
750 | setFragment(fragment: string): void;
|
751 | }
|