UNPKG

11 kBTypeScriptView Raw
1import { CellModel, CodeCellModel, ICellModel, ICodeCellModel, IMarkdownCellModel, IRawCellModel } from '@jupyterlab/cells';
2import { IChangedArgs } from '@jupyterlab/coreutils';
3import { DocumentRegistry } from '@jupyterlab/docregistry';
4import * as nbformat from '@jupyterlab/nbformat';
5import { IModelDB, IObservableJSON, IObservableUndoableList } from '@jupyterlab/observables';
6import * as models from '@jupyterlab/shared-models';
7import { ITranslator } from '@jupyterlab/translation';
8import { ISignal } from '@lumino/signaling';
9/**
10 * The definition of a model object for a notebook widget.
11 */
12export interface INotebookModel extends DocumentRegistry.IModel {
13 /**
14 * The list of cells in the notebook.
15 */
16 readonly cells: IObservableUndoableList<ICellModel>;
17 /**
18 * The cell model factory for the notebook.
19 */
20 readonly contentFactory: NotebookModel.IContentFactory;
21 /**
22 * The major version number of the nbformat.
23 */
24 readonly nbformat: number;
25 /**
26 * The minor version number of the nbformat.
27 */
28 readonly nbformatMinor: number;
29 /**
30 * The metadata associated with the notebook.
31 */
32 readonly metadata: IObservableJSON;
33 /**
34 * The array of deleted cells since the notebook was last run.
35 */
36 readonly deletedCells: string[];
37 /**
38 * If the model is initialized or not.
39 */
40 isInitialized: boolean;
41 readonly sharedModel: models.ISharedNotebook;
42}
43/**
44 * An implementation of a notebook Model.
45 */
46export declare class NotebookModel implements INotebookModel {
47 /**
48 * Construct a new notebook model.
49 */
50 constructor(options?: NotebookModel.IOptions);
51 /**
52 * A signal emitted when the document content changes.
53 */
54 get contentChanged(): ISignal<this, void>;
55 /**
56 * A signal emitted when the document state changes.
57 */
58 get stateChanged(): ISignal<this, IChangedArgs<any>>;
59 /**
60 * The dirty state of the document.
61 */
62 get dirty(): boolean;
63 set dirty(newValue: boolean);
64 /**
65 * The read only state of the document.
66 */
67 get readOnly(): boolean;
68 set readOnly(newValue: boolean);
69 /**
70 * The metadata associated with the notebook.
71 */
72 get metadata(): IObservableJSON;
73 /**
74 * Get the observable list of notebook cells.
75 */
76 get cells(): IObservableUndoableList<ICellModel>;
77 /**
78 * The major version number of the nbformat.
79 */
80 get nbformat(): number;
81 /**
82 * The minor version number of the nbformat.
83 */
84 get nbformatMinor(): number;
85 /**
86 * The default kernel name of the document.
87 */
88 get defaultKernelName(): string;
89 /**
90 * A list of deleted cells for the notebook..
91 */
92 get deletedCells(): string[];
93 /**
94 * If the model is initialized or not.
95 */
96 get isInitialized(): boolean;
97 /**
98 * The default kernel language of the document.
99 */
100 get defaultKernelLanguage(): string;
101 /**
102 * Dispose of the resources held by the model.
103 */
104 dispose(): void;
105 /**
106 * Serialize the model to a string.
107 */
108 toString(): string;
109 /**
110 * Deserialize the model from a string.
111 *
112 * #### Notes
113 * Should emit a [contentChanged] signal.
114 */
115 fromString(value: string): void;
116 /**
117 * Serialize the model to JSON.
118 */
119 toJSON(): nbformat.INotebookContent;
120 /**
121 * Deserialize the model from JSON.
122 *
123 * #### Notes
124 * Should emit a [contentChanged] signal.
125 */
126 fromJSON(value: nbformat.INotebookContent): void;
127 /**
128 * Initialize the model with its current state.
129 *
130 * # Notes
131 * Adds an empty code cell if the model is empty
132 * and clears undo state.
133 */
134 initialize(): void;
135 /**
136 * Handle a change in the cells list.
137 */
138 private _onCellsChanged;
139 private _onStateChanged;
140 private _onMetadataChanged;
141 /**
142 * Make sure we have the required metadata fields.
143 */
144 private _ensureMetadata;
145 /**
146 * Trigger a state change signal.
147 */
148 protected triggerStateChange(args: IChangedArgs<any>): void;
149 /**
150 * Trigger a content changed signal.
151 */
152 protected triggerContentChange(): void;
153 /**
154 * Whether the model is disposed.
155 */
156 get isDisposed(): boolean;
157 /**
158 * The cell model factory for the notebook.
159 */
160 readonly contentFactory: NotebookModel.IContentFactory;
161 /**
162 * The shared notebook model.
163 */
164 readonly sharedModel: models.ISharedNotebook;
165 /**
166 * A mutex to update the shared model.
167 */
168 protected readonly _modelDBMutex: (f: () => void) => void;
169 /**
170 * The underlying `IModelDB` instance in which model
171 * data is stored.
172 */
173 readonly modelDB: IModelDB;
174 private _readOnly;
175 private _contentChanged;
176 private _stateChanged;
177 private _trans;
178 private _cells;
179 private _nbformat;
180 private _nbformatMinor;
181 private _deletedCells;
182 private _isInitialized;
183 private _isDisposed;
184}
185/**
186 * The namespace for the `NotebookModel` class statics.
187 */
188export declare namespace NotebookModel {
189 /**
190 * An options object for initializing a notebook model.
191 */
192 interface IOptions {
193 /**
194 * The language preference for the model.
195 */
196 languagePreference?: string;
197 /**
198 * A factory for creating cell models.
199 *
200 * The default is a shared factory instance.
201 */
202 contentFactory?: IContentFactory;
203 /**
204 * A modelDB for storing notebook data.
205 */
206 modelDB?: IModelDB;
207 /**
208 * Language translator.
209 */
210 translator?: ITranslator;
211 /**
212 * If the model is initialized or not.
213 */
214 isInitialized?: boolean;
215 /**
216 * Defines if the document can be undo/redo.
217 */
218 disableDocumentWideUndoRedo?: boolean;
219 }
220 /**
221 * A factory for creating notebook model content.
222 */
223 interface IContentFactory {
224 /**
225 * The factory for output area models.
226 */
227 readonly codeCellContentFactory: CodeCellModel.IContentFactory;
228 /**
229 * The IModelDB in which to put data for the notebook model.
230 */
231 modelDB: IModelDB | undefined;
232 /**
233 * Create a new cell by cell type.
234 *
235 * @param type: the type of the cell to create.
236 *
237 * @param options: the cell creation options.
238 *
239 * #### Notes
240 * This method is intended to be a convenience method to programmatically
241 * call the other cell creation methods in the factory.
242 */
243 createCell(type: nbformat.CellType, options: CellModel.IOptions): ICellModel;
244 /**
245 * Create a new code cell.
246 *
247 * @param options - The options used to create the cell.
248 *
249 * @returns A new code cell. If a source cell is provided, the
250 * new cell will be initialized with the data from the source.
251 */
252 createCodeCell(options: CodeCellModel.IOptions): ICodeCellModel;
253 /**
254 * Create a new markdown cell.
255 *
256 * @param options - The options used to create the cell.
257 *
258 * @returns A new markdown cell. If a source cell is provided, the
259 * new cell will be initialized with the data from the source.
260 */
261 createMarkdownCell(options: CellModel.IOptions): IMarkdownCellModel;
262 /**
263 * Create a new raw cell.
264 *
265 * @param options - The options used to create the cell.
266 *
267 * @returns A new raw cell. If a source cell is provided, the
268 * new cell will be initialized with the data from the source.
269 */
270 createRawCell(options: CellModel.IOptions): IRawCellModel;
271 /**
272 * Clone the content factory with a new IModelDB.
273 */
274 clone(modelDB: IModelDB): IContentFactory;
275 }
276 /**
277 * The default implementation of an `IContentFactory`.
278 */
279 class ContentFactory {
280 /**
281 * Create a new cell model factory.
282 */
283 constructor(options: ContentFactory.IOptions);
284 /**
285 * The factory for code cell content.
286 */
287 readonly codeCellContentFactory: CodeCellModel.IContentFactory;
288 /**
289 * The IModelDB in which to put the notebook data.
290 */
291 readonly modelDB: IModelDB | undefined;
292 /**
293 * Create a new cell by cell type.
294 *
295 * @param type: the type of the cell to create.
296 *
297 * @param options: the cell creation options.
298 *
299 * #### Notes
300 * This method is intended to be a convenience method to programmatically
301 * call the other cell creation methods in the factory.
302 */
303 createCell(type: nbformat.CellType, options: CellModel.IOptions): ICellModel;
304 /**
305 * Create a new code cell.
306 *
307 * @param source - The data to use for the original source data.
308 *
309 * @returns A new code cell. If a source cell is provided, the
310 * new cell will be initialized with the data from the source.
311 * If the contentFactory is not provided, the instance
312 * `codeCellContentFactory` will be used.
313 */
314 createCodeCell(options: CodeCellModel.IOptions): ICodeCellModel;
315 /**
316 * Create a new markdown cell.
317 *
318 * @param source - The data to use for the original source data.
319 *
320 * @returns A new markdown cell. If a source cell is provided, the
321 * new cell will be initialized with the data from the source.
322 */
323 createMarkdownCell(options: CellModel.IOptions): IMarkdownCellModel;
324 /**
325 * Create a new raw cell.
326 *
327 * @param source - The data to use for the original source data.
328 *
329 * @returns A new raw cell. If a source cell is provided, the
330 * new cell will be initialized with the data from the source.
331 */
332 createRawCell(options: CellModel.IOptions): IRawCellModel;
333 /**
334 * Clone the content factory with a new IModelDB.
335 */
336 clone(modelDB: IModelDB): ContentFactory;
337 }
338 /**
339 * A namespace for the notebook model content factory.
340 */
341 namespace ContentFactory {
342 /**
343 * The options used to initialize a `ContentFactory`.
344 */
345 interface IOptions {
346 /**
347 * The factory for code cell model content.
348 */
349 codeCellContentFactory?: CodeCellModel.IContentFactory;
350 /**
351 * The modelDB in which to place new content.
352 */
353 modelDB?: IModelDB;
354 }
355 }
356 /**
357 * The default `ContentFactory` instance.
358 */
359 const defaultContentFactory: ContentFactory;
360}