123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- /*-----------------------------------------------------------------------------
- | Copyright (c) Jupyter Development Team.
- | Distributed under the terms of the Modified BSD License.
- |----------------------------------------------------------------------------*/
- import {
- JSONExt, JSONValue
- } from '@phosphor/coreutils';
- import {
- ISignal, Signal
- } from '@phosphor/signaling';
- import {
- CodeEditor
- } from '@jupyterlab/codeeditor';
- import {
- IChangedArgs, nbformat, uuid
- } from '@jupyterlab/coreutils';
- import {
- IObservableJSON, IModelDB, IObservableValue, ObservableValue
- } from '@jupyterlab/coreutils';
- import {
- IOutputAreaModel, OutputAreaModel
- } from '@jupyterlab/outputarea';
- /**
- * The definition of a model object for a cell.
- */
- export
- interface ICellModel extends CodeEditor.IModel {
- /**
- * The type of the cell.
- */
- readonly type: nbformat.CellType;
- /**
- * A unique identifier for the cell.
- */
- readonly id: string;
- /**
- * A signal emitted when the content of the model changes.
- */
- readonly contentChanged: ISignal<ICellModel, void>;
- /**
- * A signal emitted when a model state changes.
- */
- readonly stateChanged: ISignal<ICellModel, IChangedArgs<any>>;
- /**
- * Whether the cell is trusted.
- */
- trusted: boolean;
- /**
- * The metadata associated with the cell.
- */
- readonly metadata: IObservableJSON;
- /**
- * Serialize the model to JSON.
- */
- toJSON(): nbformat.ICell;
- }
- /**
- * The definition of a code cell.
- */
- export
- interface ICodeCellModel extends ICellModel {
- /**
- * The type of the cell.
- *
- * #### Notes
- * This is a read-only property.
- */
- type: 'code';
- /**
- * The code cell's prompt number. Will be null if the cell has not been run.
- */
- executionCount: nbformat.ExecutionCount;
- /**
- * The cell outputs.
- */
- outputs: IOutputAreaModel;
- }
- /**
- * The definition of a markdown cell.
- */
- export
- interface IMarkdownCellModel extends ICellModel {
- /**
- * The type of the cell.
- */
- type: 'markdown';
- }
- /**
- * The definition of a raw cell.
- */
- export
- interface IRawCellModel extends ICellModel {
- /**
- * The type of the cell.
- */
- type: 'raw';
- }
- /**
- * An implementation of the cell model.
- */
- export
- class CellModel extends CodeEditor.Model implements ICellModel {
- /**
- * Construct a cell model from optional cell content.
- */
- constructor(options: CellModel.IOptions) {
- super({modelDB: options.modelDB});
- this.id = options.id || uuid();
- this.value.changed.connect(this.onGenericChange, this);
- let cellType = this.modelDB.createValue('type');
- cellType.set(this.type);
- let observableMetadata = this.modelDB.createMap('metadata');
- observableMetadata.changed.connect(this.onGenericChange, this);
- let cell = options.cell;
- let trusted = this.modelDB.createValue('trusted');
- trusted.changed.connect(this.onTrustedChanged, this);
- if (!cell) {
- trusted.set(false);
- return;
- }
- trusted.set(!!cell.metadata['trusted']);
- delete cell.metadata['trusted'];
- if (Array.isArray(cell.source)) {
- this.value.text = (cell.source as string[]).join('');
- } else {
- this.value.text = cell.source as string;
- }
- let metadata = JSONExt.deepCopy(cell.metadata);
- if (this.type !== 'raw') {
- delete metadata['format'];
- }
- if (this.type !== 'code') {
- delete metadata['collapsed'];
- delete metadata['scrolled'];
- }
- for (let key in metadata) {
- observableMetadata.set(key, metadata[key]);
- }
- }
- /**
- * The type of cell.
- */
- readonly type: nbformat.CellType;
- /**
- * A signal emitted when the state of the model changes.
- */
- readonly contentChanged = new Signal<this, void>(this);
- /**
- * A signal emitted when a model state changes.
- */
- readonly stateChanged = new Signal<this, IChangedArgs<any>>(this);
- /**
- * The id for the cell.
- */
- readonly id: string;
- /**
- * The metadata associated with the cell.
- */
- get metadata(): IObservableJSON {
- return this.modelDB.get('metadata') as IObservableJSON;
- }
- /**
- * Get the trusted state of the model.
- */
- get trusted(): boolean {
- return this.modelDB.getValue('trusted') as boolean;
- }
- /**
- * Set the trusted state of the model.
- */
- set trusted(newValue: boolean) {
- let oldValue = this.trusted;
- if (oldValue === newValue) {
- return;
- }
- this.modelDB.setValue('trusted', newValue);
- }
- /**
- * Serialize the model to JSON.
- */
- toJSON(): nbformat.ICell {
- let metadata: nbformat.IBaseCellMetadata = Object.create(null);
- for (let key of this.metadata.keys()) {
- let value = JSON.parse(JSON.stringify(this.metadata.get(key)));
- metadata[key] = value as JSONValue;
- }
- if (this.trusted) {
- metadata['trusted'] = true;
- }
- return {
- cell_type: this.type,
- source: this.value.text,
- metadata,
- } as nbformat.ICell;
- }
- /**
- * Handle a change to the trusted state.
- *
- * The default implementation is a no-op.
- */
- onTrustedChanged(trusted: IObservableValue, args: ObservableValue.IChangedArgs): void { /* no-op */ }
- /**
- * Handle a change to the observable value.
- */
- protected onGenericChange(): void {
- this.contentChanged.emit(void 0);
- }
- }
- /**
- * The namespace for `CellModel` statics.
- */
- export
- namespace CellModel {
- /**
- * The options used to initialize a `CellModel`.
- */
- export interface IOptions {
- /**
- * The source cell data.
- */
- cell?: nbformat.IBaseCell;
- /**
- * An IModelDB in which to store cell data.
- */
- modelDB?: IModelDB;
- /**
- * A unique identifier for this cell.
- */
- id?: string;
- }
- }
- /**
- * An implementation of a raw cell model.
- */
- export
- class RawCellModel extends CellModel {
- /**
- * The type of the cell.
- */
- get type(): 'raw' {
- return 'raw';
- }
- }
- /**
- * An implementation of a markdown cell model.
- */
- export
- class MarkdownCellModel extends CellModel {
- /**
- * Construct a markdown cell model from optional cell content.
- */
- constructor(options: CellModel.IOptions) {
- super(options);
- // Use the Github-flavored markdown mode.
- this.mimeType = 'text/x-ipythongfm';
- }
- /**
- * The type of the cell.
- */
- get type(): 'markdown' {
- return 'markdown';
- }
- }
- /**
- * An implementation of a code cell Model.
- */
- export
- class CodeCellModel extends CellModel implements ICodeCellModel {
- /**
- * Construct a new code cell with optional original cell content.
- */
- constructor(options: CodeCellModel.IOptions) {
- super(options);
- let factory = (options.contentFactory ||
- CodeCellModel.defaultContentFactory
- );
- let trusted = this.trusted;
- let cell = options.cell as nbformat.ICodeCell;
- let outputs: nbformat.IOutput[] = [];
- let executionCount = this.modelDB.createValue('executionCount');
- if (!executionCount.get()) {
- if (cell && cell.cell_type === 'code') {
- executionCount.set(cell.execution_count || null);
- outputs = cell.outputs;
- } else {
- executionCount.set(null);
- }
- }
- executionCount.changed.connect(this._onExecutionCountChanged, this);
- this._outputs = factory.createOutputArea({
- trusted,
- values: outputs,
- modelDB: this.modelDB
- });
- this._outputs.stateChanged.connect(this.onGenericChange, this);
- }
- /**
- * The type of the cell.
- */
- get type(): 'code' {
- return 'code';
- }
- /**
- * The execution count of the cell.
- */
- get executionCount(): nbformat.ExecutionCount {
- return this.modelDB.getValue('executionCount') as nbformat.ExecutionCount;
- }
- set executionCount(newValue: nbformat.ExecutionCount) {
- let oldValue = this.executionCount;
- if (newValue === oldValue) {
- return;
- }
- this.modelDB.setValue('executionCount', newValue || null);
- }
- /**
- * The cell outputs.
- */
- get outputs(): IOutputAreaModel {
- return this._outputs;
- }
- /**
- * Dispose of the resources held by the model.
- */
- dispose(): void {
- if (this.isDisposed) {
- return;
- }
- this._outputs.dispose();
- this._outputs = null;
- super.dispose();
- }
- /**
- * Serialize the model to JSON.
- */
- toJSON(): nbformat.ICodeCell {
- let cell = super.toJSON() as nbformat.ICodeCell;
- cell.execution_count = this.executionCount || null;
- cell.outputs = this.outputs.toJSON();
- return cell;
- }
- /**
- * Handle a change to the trusted state.
- */
- onTrustedChanged(trusted: IObservableValue, args: ObservableValue.IChangedArgs): void {
- if (this._outputs) {
- this._outputs.trusted = args.newValue as boolean;
- }
- this.stateChanged.emit({
- name: 'trusted',
- oldValue: args.oldValue,
- newValue: args.newValue
- });
- }
- /**
- * Handle a change to the execution count.
- */
- private _onExecutionCountChanged(count: IObservableValue, args: ObservableValue.IChangedArgs): void {
- this.contentChanged.emit(void 0);
- this.stateChanged.emit({
- name: 'executionCount',
- oldValue: args.oldValue,
- newValue: args.newValue });
- }
- private _outputs: IOutputAreaModel = null;
- }
- /**
- * The namespace for `CodeCellModel` statics.
- */
- export
- namespace CodeCellModel {
- /**
- * The options used to initialize a `CodeCellModel`.
- */
- export
- interface IOptions extends CellModel.IOptions {
- /**
- * The factory for output area model creation.
- */
- contentFactory?: IContentFactory;
- }
- /**
- * A factory for creating code cell model content.
- */
- export
- interface IContentFactory {
- /**
- * Create an output area.
- */
- createOutputArea(options: IOutputAreaModel.IOptions): IOutputAreaModel;
- }
- /**
- * The default implementation of an `IContentFactory`.
- */
- export
- class ContentFactory {
- /**
- * Create an output area.
- */
- createOutputArea(options: IOutputAreaModel.IOptions): IOutputAreaModel {
- return new OutputAreaModel(options);
- }
- }
- /**
- * The shared `ConetntFactory` instance.
- */
- export
- const defaultContentFactory = new ContentFactory();
- }
|