UNPKG

12.2 kBTypeScriptView Raw
1import { ISignal, Signal } from '@lumino/signaling';
2import { IAttachmentsModel } from '@jupyterlab/attachments';
3import { CodeEditor } from '@jupyterlab/codeeditor';
4import { IChangedArgs } from '@jupyterlab/coreutils';
5import * as nbformat from '@jupyterlab/nbformat';
6import { ObservableValue } from '@jupyterlab/observables';
7import { IOutputAreaModel } from '@jupyterlab/outputarea';
8import { IMapChange, ISharedCell, ISharedCodeCell, ISharedMarkdownCell, ISharedRawCell } from '@jupyter/ydoc';
9/**
10 * The definition of a model object for a cell.
11 */
12export interface ICellModel extends CodeEditor.IModel {
13 /**
14 * The type of the cell.
15 */
16 readonly type: nbformat.CellType;
17 /**
18 * A unique identifier for the cell.
19 */
20 readonly id: string;
21 /**
22 * A signal emitted when the content of the model changes.
23 */
24 readonly contentChanged: ISignal<ICellModel, void>;
25 /**
26 * A signal emitted when a model state changes.
27 */
28 readonly stateChanged: ISignal<ICellModel, IChangedArgs<boolean, boolean, any>>;
29 /**
30 * Whether the cell is trusted.
31 */
32 trusted: boolean;
33 /**
34 * The metadata associated with the cell.
35 *
36 * ### Notes
37 * This is a copy of the metadata. Changing a part of it
38 * won't affect the model.
39 * As this returns a copy of all metadata, it is advised to
40 * use `getMetadata` to speed up the process of getting a single key.
41 */
42 readonly metadata: Omit<nbformat.IBaseCellMetadata, 'trusted'>;
43 /**
44 * Signal emitted when cell metadata changes.
45 */
46 readonly metadataChanged: ISignal<ICellModel, IMapChange>;
47 /**
48 * The cell shared model.
49 */
50 readonly sharedModel: ISharedCell;
51 /**
52 * Delete a metadata.
53 *
54 * @param key Metadata key
55 */
56 deleteMetadata(key: string): void;
57 /**
58 * Get a metadata
59 *
60 * ### Notes
61 * This returns a copy of the key value.
62 *
63 * @param key Metadata key
64 */
65 getMetadata(key: string): any;
66 /**
67 * Set a metadata
68 *
69 * @param key Metadata key
70 * @param value Metadata value
71 */
72 setMetadata(key: string, value: any): void;
73 /**
74 * Serialize the model to JSON.
75 */
76 toJSON(): nbformat.ICell;
77}
78/**
79 * The definition of a model cell object for a cell with attachments.
80 */
81export interface IAttachmentsCellModel extends ICellModel {
82 /**
83 * The cell attachments
84 */
85 readonly attachments: IAttachmentsModel;
86}
87/**
88 * The definition of a code cell.
89 */
90export interface ICodeCellModel extends ICellModel {
91 /**
92 * The type of the cell.
93 *
94 * #### Notes
95 * This is a read-only property.
96 */
97 readonly type: 'code';
98 /**
99 * Whether the code cell has been edited since the last run.
100 */
101 readonly isDirty: boolean;
102 /**
103 * Serialize the model to JSON.
104 */
105 toJSON(): nbformat.ICodeCell;
106 /**
107 * The code cell's prompt number. Will be null if the cell has not been run.
108 */
109 executionCount: nbformat.ExecutionCount;
110 /**
111 * The cell outputs.
112 */
113 readonly outputs: IOutputAreaModel;
114 /**
115 * Clear execution, outputs, and related metadata
116 */
117 clearExecution(): void;
118 /**
119 * The code cell shared model
120 */
121 readonly sharedModel: ISharedCodeCell;
122}
123/**
124 * The definition of a markdown cell.
125 */
126export interface IMarkdownCellModel extends IAttachmentsCellModel {
127 /**
128 * The type of the cell.
129 */
130 readonly type: 'markdown';
131 /**
132 * Serialize the model to JSON.
133 */
134 toJSON(): nbformat.IMarkdownCell;
135}
136/**
137 * The definition of a raw cell.
138 */
139export interface IRawCellModel extends IAttachmentsCellModel {
140 /**
141 * The type of the cell.
142 */
143 readonly type: 'raw';
144 /**
145 * Serialize the model to JSON.
146 */
147 toJSON(): nbformat.IRawCell;
148}
149export declare function isCodeCellModel(model: ICellModel): model is ICodeCellModel;
150export declare function isMarkdownCellModel(model: ICellModel): model is IMarkdownCellModel;
151export declare function isRawCellModel(model: ICellModel): model is IRawCellModel;
152/**
153 * An implementation of the cell model.
154 */
155export declare abstract class CellModel extends CodeEditor.Model implements ICellModel {
156 constructor(options?: CellModel.IOptions<ISharedCell>);
157 readonly sharedModel: ISharedCell;
158 /**
159 * The type of cell.
160 */
161 abstract get type(): nbformat.CellType;
162 /**
163 * A signal emitted when the state of the model changes.
164 */
165 readonly contentChanged: Signal<this, void>;
166 /**
167 * Signal emitted when cell metadata changes.
168 */
169 get metadataChanged(): ISignal<ICellModel, IMapChange>;
170 /**
171 * A signal emitted when a model state changes.
172 */
173 readonly stateChanged: Signal<this, IChangedArgs<any, any, "trusted" | "isDirty" | "executionCount">>;
174 /**
175 * The id for the cell.
176 */
177 get id(): string;
178 /**
179 * The metadata associated with the cell.
180 */
181 get metadata(): Omit<nbformat.IBaseCellMetadata, 'trusted'>;
182 /**
183 * The trusted state of the model.
184 */
185 get trusted(): boolean;
186 set trusted(newValue: boolean);
187 /**
188 * Dispose of the resources held by the model.
189 */
190 dispose(): void;
191 /**
192 * Handle a change to the trusted state.
193 *
194 * The default implementation is a no-op.
195 */
196 onTrustedChanged(trusted: CellModel, args: ObservableValue.IChangedArgs): void;
197 /**
198 * Delete a metadata
199 *
200 * @param key Metadata key
201 */
202 deleteMetadata(key: string): any;
203 /**
204 * Get a metadata
205 *
206 * ### Notes
207 * This returns a copy of the key value.
208 *
209 * @param key Metadata key
210 */
211 getMetadata(key: string): any;
212 /**
213 * Set a metadata
214 *
215 * @param key Metadata key
216 * @param value Metadata value
217 */
218 setMetadata(key: string, value: any): void;
219 /**
220 * Serialize the model to JSON.
221 */
222 toJSON(): nbformat.ICell;
223 /**
224 * Handle a change to the observable value.
225 */
226 protected onGenericChange(): void;
227 private _onMetadataChanged;
228 private _metadataChanged;
229 private _trusted;
230}
231/**
232 * The namespace for `CellModel` statics.
233 */
234export declare namespace CellModel {
235 /**
236 * The options used to initialize a `CellModel`.
237 */
238 interface IOptions<T extends ISharedCell> {
239 /**
240 * A unique identifier for the model.
241 */
242 id?: string;
243 /**
244 * The cell shared model.
245 */
246 sharedModel?: T;
247 /**
248 * The cell type
249 */
250 cell_type?: string;
251 /**
252 * Whether the cell is trusted or not.
253 */
254 trusted?: boolean;
255 }
256}
257/**
258 * A base implementation for cell models with attachments.
259 */
260export declare abstract class AttachmentsCellModel extends CellModel {
261 /**
262 * Construct a new cell with optional attachments.
263 */
264 constructor(options: AttachmentsCellModel.IOptions<ISharedCell>);
265 /**
266 * Get the attachments of the model.
267 */
268 get attachments(): IAttachmentsModel;
269 /**
270 * Dispose of the resources held by the model.
271 */
272 dispose(): void;
273 /**
274 * Serialize the model to JSON.
275 */
276 toJSON(): nbformat.IRawCell | nbformat.IMarkdownCell;
277 /**
278 * Handle a change to the cell outputs modelDB and reflect it in the shared model.
279 */
280 private _onAttachmentsChange;
281 /**
282 * Handle a change to the code cell value.
283 */
284 private _onSharedModelChanged;
285 private _attachments;
286}
287/**
288 * The namespace for `AttachmentsCellModel` statics.
289 */
290export declare namespace AttachmentsCellModel {
291 /**
292 * The options used to initialize a `AttachmentsCellModel`.
293 */
294 interface IOptions<T extends ISharedCell> extends CellModel.IOptions<T> {
295 /**
296 * The factory for attachment model creation.
297 */
298 contentFactory?: IContentFactory;
299 }
300 /**
301 * A factory for creating code cell model content.
302 */
303 interface IContentFactory {
304 /**
305 * Create an output area.
306 */
307 createAttachmentsModel(options: IAttachmentsModel.IOptions): IAttachmentsModel;
308 }
309 /**
310 * The default implementation of an `IContentFactory`.
311 */
312 class ContentFactory implements IContentFactory {
313 /**
314 * Create an attachments model.
315 */
316 createAttachmentsModel(options: IAttachmentsModel.IOptions): IAttachmentsModel;
317 }
318 /**
319 * The shared `ContentFactory` instance.
320 */
321 const defaultContentFactory: ContentFactory;
322}
323/**
324 * An implementation of a raw cell model.
325 */
326export declare class RawCellModel extends AttachmentsCellModel {
327 /**
328 * Construct a raw cell model from optional shared model.
329 */
330 constructor(options?: Omit<AttachmentsCellModel.IOptions<ISharedRawCell>, 'cell_type'>);
331 /**
332 * The type of the cell.
333 */
334 get type(): 'raw';
335 /**
336 * Serialize the model to JSON.
337 */
338 toJSON(): nbformat.IRawCell;
339}
340/**
341 * An implementation of a markdown cell model.
342 */
343export declare class MarkdownCellModel extends AttachmentsCellModel {
344 /**
345 * Construct a markdown cell model from optional shared model.
346 */
347 constructor(options?: Omit<AttachmentsCellModel.IOptions<ISharedMarkdownCell>, 'cell_type'>);
348 /**
349 * The type of the cell.
350 */
351 get type(): 'markdown';
352 /**
353 * Serialize the model to JSON.
354 */
355 toJSON(): nbformat.IMarkdownCell;
356}
357/**
358 * An implementation of a code cell Model.
359 */
360export declare class CodeCellModel extends CellModel implements ICodeCellModel {
361 /**
362 * Construct a new code cell with optional original cell content.
363 */
364 constructor(options?: CodeCellModel.IOptions);
365 /**
366 * The type of the cell.
367 */
368 get type(): 'code';
369 /**
370 * The execution count of the cell.
371 */
372 get executionCount(): nbformat.ExecutionCount;
373 set executionCount(newValue: nbformat.ExecutionCount);
374 /**
375 * Whether the cell is dirty or not.
376 *
377 * A cell is dirty if it is output is not empty and does not
378 * result of the input code execution.
379 */
380 get isDirty(): boolean;
381 /**
382 * The cell outputs.
383 */
384 get outputs(): IOutputAreaModel;
385 readonly sharedModel: ISharedCodeCell;
386 clearExecution(): void;
387 /**
388 * Dispose of the resources held by the model.
389 */
390 dispose(): void;
391 /**
392 * Handle a change to the trusted state.
393 */
394 onTrustedChanged(trusted: CellModel, args: ObservableValue.IChangedArgs): void;
395 /**
396 * Serialize the model to JSON.
397 */
398 toJSON(): nbformat.ICodeCell;
399 /**
400 * Handle a change to the cell outputs modelDB and reflect it in the shared model.
401 */
402 protected onOutputsChange(sender: IOutputAreaModel, event: IOutputAreaModel.ChangedArgs): void;
403 /**
404 * Handle a change to the code cell value.
405 */
406 private _onSharedModelChanged;
407 /**
408 * Set whether the cell is dirty or not.
409 */
410 private _setDirty;
411 private _executedCode;
412 private _isDirty;
413 private _outputs;
414}
415/**
416 * The namespace for `CodeCellModel` statics.
417 */
418export declare namespace CodeCellModel {
419 /**
420 * The options used to initialize a `CodeCellModel`.
421 */
422 interface IOptions extends Omit<CellModel.IOptions<ISharedCodeCell>, 'cell_type'> {
423 /**
424 * The factory for output area model creation.
425 */
426 contentFactory?: IContentFactory;
427 }
428 /**
429 * A factory for creating code cell model content.
430 */
431 interface IContentFactory {
432 /**
433 * Create an output area.
434 */
435 createOutputArea(options: IOutputAreaModel.IOptions): IOutputAreaModel;
436 }
437 /**
438 * The default implementation of an `IContentFactory`.
439 */
440 class ContentFactory implements IContentFactory {
441 /**
442 * Create an output area.
443 */
444 createOutputArea(options: IOutputAreaModel.IOptions): IOutputAreaModel;
445 }
446 /**
447 * The shared `ContentFactory` instance.
448 */
449 const defaultContentFactory: ContentFactory;
450}