UNPKG

3.1 kBPlain TextView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3
4import type { ISharedNotebook } from '@jupyter/ydoc';
5import { DocumentRegistry } from '@jupyterlab/docregistry';
6import { Contents } from '@jupyterlab/services';
7import { INotebookModel, NotebookModel } from './model';
8
9/**
10 * A model factory for notebooks.
11 */
12export class NotebookModelFactory
13 implements DocumentRegistry.IModelFactory<INotebookModel>
14{
15 /**
16 * Construct a new notebook model factory.
17 */
18 constructor(options: NotebookModelFactory.IOptions = {}) {
19 this._disableDocumentWideUndoRedo =
20 options.disableDocumentWideUndoRedo ?? true;
21 this._collaborative = options.collaborative ?? true;
22 }
23
24 /**
25 * Define the disableDocumentWideUndoRedo property.
26 *
27 * @experimental
28 * @alpha
29 */
30 get disableDocumentWideUndoRedo(): boolean {
31 return this._disableDocumentWideUndoRedo;
32 }
33 set disableDocumentWideUndoRedo(disableDocumentWideUndoRedo: boolean) {
34 this._disableDocumentWideUndoRedo = disableDocumentWideUndoRedo;
35 }
36
37 /**
38 * The name of the model.
39 */
40 get name(): string {
41 return 'notebook';
42 }
43
44 /**
45 * The content type of the file.
46 */
47 get contentType(): Contents.ContentType {
48 return 'notebook';
49 }
50
51 /**
52 * The format of the file.
53 */
54 get fileFormat(): Contents.FileFormat {
55 return 'json';
56 }
57
58 /**
59 * Whether the model is collaborative or not.
60 */
61 get collaborative(): boolean {
62 return this._collaborative;
63 }
64
65 /**
66 * Get whether the model factory has been disposed.
67 */
68 get isDisposed(): boolean {
69 return this._disposed;
70 }
71
72 /**
73 * Dispose of the model factory.
74 */
75 dispose(): void {
76 this._disposed = true;
77 }
78
79 /**
80 * Create a new model for a given path.
81 *
82 * @param languagePreference - An optional kernel language preference.
83 *
84 * @returns A new document model.
85 */
86 createNew(
87 options: DocumentRegistry.IModelOptions<ISharedNotebook> = {}
88 ): INotebookModel {
89 return new NotebookModel({
90 languagePreference: options.languagePreference,
91 sharedModel: options.sharedModel,
92 collaborationEnabled: options.collaborationEnabled && this.collaborative,
93 disableDocumentWideUndoRedo: this._disableDocumentWideUndoRedo
94 });
95 }
96
97 /**
98 * Get the preferred kernel language given a path.
99 */
100 preferredLanguage(path: string): string {
101 return '';
102 }
103
104 /**
105 * Defines if the document can be undo/redo.
106 */
107 private _disableDocumentWideUndoRedo: boolean;
108 private _disposed = false;
109 private _collaborative: boolean;
110}
111
112/**
113 * The namespace for notebook model factory statics.
114 */
115export namespace NotebookModelFactory {
116 /**
117 * The options used to initialize a NotebookModelFactory.
118 */
119 export interface IOptions {
120 /**
121 * Whether the model is collaborative or not.
122 */
123 collaborative?: boolean;
124
125 /**
126 * Defines if the document can be undo/redo.
127 *
128 * Default: true
129 *
130 * @experimental
131 * @alpha
132 */
133 disableDocumentWideUndoRedo?: boolean;
134 }
135}