123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import * as CodeMirror
- from 'codemirror';
- import {
- IContents, IKernel
- } from 'jupyter-js-services';
- import {
- IChangedArgs
- } from 'phosphor-properties';
- import {
- ISignal, Signal
- } from 'phosphor-signaling';
- import {
- Widget
- } from 'phosphor-widget';
- import {
- IDocumentModel, IWidgetFactory, IDocumentContext, IModelFactory
- } from './index';
- /**
- * The default implementation of a document model.
- */
- export
- class DocumentModel implements IDocumentModel {
- /**
- * Construct a new document model.
- */
- constructor(languagePreference?: string) {
- this._defaultLang = languagePreference || '';
- }
- /**
- * Get whether the model factory has been disposed.
- */
- get isDisposed(): boolean {
- return this._isDisposed;
- }
- /**
- * A signal emitted when the document content changes.
- */
- get contentChanged(): ISignal<IDocumentModel, void> {
- return Private.contentChangedSignal.bind(this);
- }
- /**
- * A signal emitted when the document state changes.
- */
- get stateChanged(): ISignal<IDocumentModel, IChangedArgs<any>> {
- return Private.stateChangedSignal.bind(this);
- }
- /**
- * The dirty state of the document.
- */
- get dirty(): boolean {
- return this._dirty;
- }
- set dirty(newValue: boolean) {
- if (newValue === this._dirty) {
- return;
- }
- let oldValue = this._dirty;
- this._dirty = newValue;
- this.stateChanged.emit({ name: 'dirty', oldValue, newValue });
- }
- /**
- * The read only state of the document.
- */
- get readOnly(): boolean {
- return this._readOnly;
- }
- set readOnly(newValue: boolean) {
- if (newValue === this._readOnly) {
- return;
- }
- let oldValue = this._readOnly;
- this._readOnly = newValue;
- this.stateChanged.emit({ name: 'readOnly', oldValue, newValue });
- }
- /**
- * The default kernel name of the document.
- *
- * #### Notes
- * This is a read-only property.
- */
- get defaultKernelName(): string {
- return '';
- }
- /**
- * The default kernel language of the document.
- *
- * #### Notes
- * This is a read-only property.
- */
- get defaultKernelLanguage(): string {
- return this._defaultLang;
- }
- /**
- * Dispose of the resources held by the document manager.
- */
- dispose(): void {
- this._isDisposed = true;
- }
- /**
- * Serialize the model to a string.
- */
- toString(): string {
- return this._text;
- }
- /**
- * Deserialize the model from a string.
- *
- * #### Notes
- * Should emit a [contentChanged] signal.
- */
- fromString(value: string): void {
- if (this._text === value) {
- return;
- }
- this._text = value;
- this.contentChanged.emit(void 0);
- this.dirty = true;
- }
- /**
- * Serialize the model to JSON.
- */
- toJSON(): any {
- return JSON.stringify(this._text);
- }
- /**
- * Deserialize the model from JSON.
- *
- * #### Notes
- * Should emit a [contentChanged] signal.
- */
- fromJSON(value: any): void {
- this.fromString(JSON.parse(value));
- }
- private _text = '';
- private _defaultLang = '';
- private _dirty = false;
- private _readOnly = false;
- private _isDisposed = false;
- }
- /**
- * An implementation of a model factory for text files.
- */
- export
- class TextModelFactory implements IModelFactory {
- /**
- * The name of the model type.
- *
- * #### Notes
- * This is a read-only property.
- */
- get name(): string {
- return 'text';
- }
- /**
- * The type of the file.
- *
- * #### Notes
- * This is a read-only property.
- */
- get fileType(): IContents.FileType {
- return 'file';
- }
- /**
- * The format of the file.
- *
- * This is a read-only property.
- */
- get fileFormat(): IContents.FileFormat {
- return 'text';
- }
- /**
- * Get whether the model factory has been disposed.
- */
- get isDisposed(): boolean {
- return this._isDisposed;
- }
- /**
- * Dispose of the resources held by the model factory.
- */
- dispose(): void {
- this._isDisposed = true;
- }
- /**
- * Create a new model.
- *
- * @param languagePreference - An optional kernel language preference.
- *
- * @returns A new document model.
- */
- createNew(languagePreference?: string): IDocumentModel {
- return new DocumentModel(languagePreference);
- }
- /**
- * Get the preferred kernel language given an extension.
- */
- preferredLanguage(ext: string): string {
- let mode = CodeMirror.findModeByExtension(ext.slice(1));
- if (mode) {
- return mode.mode;
- }
- }
- private _isDisposed = false;
- }
- /**
- * An implementation of a model factory for base64 files.
- */
- export
- class Base64ModelFactory extends TextModelFactory {
- /**
- * The name of the model type.
- *
- * #### Notes
- * This is a read-only property.
- */
- get name(): string {
- return 'base64';
- }
- /**
- * The type of the file.
- *
- * #### Notes
- * This is a read-only property.
- */
- get fileType(): IContents.FileType {
- return 'file';
- }
- /**
- * The format of the file.
- *
- * This is a read-only property.
- */
- get fileFormat(): IContents.FileFormat {
- return 'base64';
- }
- }
- /**
- * The default implemetation of a widget factory.
- */
- export
- abstract class ABCWidgetFactory<T extends Widget, U extends IDocumentModel> implements IWidgetFactory<T, U> {
- /**
- * A signal emitted when a widget is created.
- */
- get widgetCreated(): ISignal<IWidgetFactory<T, U>, T> {
- return Private.widgetCreatedSignal.bind(this);
- }
- /**
- * Get whether the model factory has been disposed.
- */
- get isDisposed(): boolean {
- return this._isDisposed;
- }
- /**
- * Dispose of the resources held by the document manager.
- */
- dispose(): void {
- this._isDisposed = true;
- }
- /**
- * Create a new widget given a document model and a context.
- *
- * #### Notes
- * It should emit the [widgetCreated] signal with the new widget.
- */
- abstract createNew(context: IDocumentContext<U>, kernel?: IKernel.IModel): T;
- private _isDisposed = false;
- }
- /**
- * A private namespace for data.
- */
- namespace Private {
- /**
- * A signal emitted when a document content changes.
- */
- export
- const contentChangedSignal = new Signal<IDocumentModel, void>();
- /**
- * A signal emitted when a widget is created.
- */
- export
- const widgetCreatedSignal = new Signal<IWidgetFactory<Widget, IDocumentModel>, Widget>();
- /**
- * A signal emitted when a document dirty state changes.
- */
- export
- const stateChangedSignal = new Signal<IDocumentModel, IChangedArgs<any>>();
- }
|