123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import * as CodeMirror
- from 'codemirror';
- import 'codemirror/mode/meta';
- import {
- Contents, Kernel
- } from '@jupyterlab/services';
- import {
- defineSignal, ISignal
- } from 'phosphor/lib/core/signaling';
- import {
- Widget
- } from 'phosphor/lib/ui/widget';
- import {
- IChangedArgs
- } from '../common/interfaces';
- import {
- DocumentRegistry
- } from './index';
- /**
- * The default implementation of a document model.
- */
- export
- class DocumentModel implements DocumentRegistry.IModel {
- /**
- * Construct a new document model.
- */
- constructor(languagePreference?: string) {
- this._defaultLang = languagePreference || '';
- }
- /**
- * A signal emitted when the document content changes.
- */
- contentChanged: ISignal<DocumentRegistry.IModel, void>;
- /**
- * A signal emitted when the document state changes.
- */
- stateChanged: ISignal<DocumentRegistry.IModel, IChangedArgs<any>>;
- /**
- * Get whether the model factory has been disposed.
- */
- get isDisposed(): boolean {
- return this._isDisposed;
- }
- /**
- * 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;
- }
- // Define the signals for the `DocumentModel` class.
- defineSignal(DocumentModel.prototype, 'contentChanged');
- defineSignal(DocumentModel.prototype, 'stateChanged');
- /**
- * An implementation of a model factory for text files.
- */
- export
- class TextModelFactory implements DocumentRegistry.IModelFactory<DocumentRegistry.IModel> {
- /**
- * 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 contentType(): Contents.ContentType {
- return 'file';
- }
- /**
- * The format of the file.
- *
- * This is a read-only property.
- */
- get fileFormat(): Contents.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): DocumentRegistry.IModel {
- 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 contentType(): Contents.ContentType {
- return 'file';
- }
- /**
- * The format of the file.
- *
- * This is a read-only property.
- */
- get fileFormat(): Contents.FileFormat {
- return 'base64';
- }
- }
- /**
- * The default implemetation of a widget factory.
- */
- export
- abstract class ABCWidgetFactory<T extends Widget, U extends DocumentRegistry.IModel> implements DocumentRegistry.IWidgetFactory<T, U> {
- /**
- * Construct a new `ABCWidgetFactory`.
- */
- constructor(options: DocumentRegistry.IWidgetFactoryOptions) {
- this._name = options.name;
- this._defaultFor = options.defaultFor ? options.defaultFor.slice() : [];
- this._fileExtensions = options.fileExtensions.slice();
- this._modelName = options.modelName || 'text';
- this._preferKernel = !!options.preferKernel;
- this._canStartKernel = !!options.canStartKernel;
- }
- /**
- * A signal emitted when a widget is created.
- */
- widgetCreated: ISignal<DocumentRegistry.IWidgetFactory<T, U>, T>;
- /**
- * 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;
- }
- /**
- * The name of the widget to display in dialogs.
- */
- get name(): string {
- return this._name;
- }
- /**
- * The file extensions the widget can view.
- */
- get fileExtensions(): string[] {
- return this._fileExtensions.slice();
- }
- /**
- * The registered name of the model type used to create the widgets.
- */
- get modelName(): string {
- return this._modelName;
- }
- /**
- * The file extensions for which the factory should be the default.
- */
- get defaultFor(): string[] {
- return this._defaultFor.slice();
- }
- /**
- * Whether the widgets prefer having a kernel started.
- */
- get preferKernel(): boolean {
- return this._preferKernel;
- }
- /**
- * Whether the widgets can start a kernel when opened.
- */
- get canStartKernel(): boolean {
- return this._canStartKernel;
- }
- /**
- * 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: DocumentRegistry.IContext<U>, kernel?: Kernel.IModel): T;
- private _isDisposed = false;
- private _name: string;
- private _canStartKernel: boolean;
- private _preferKernel: boolean;
- private _modelName: string;
- private _fileExtensions: string[];
- private _defaultFor: string[];
- }
- // Define the signals for the `ABCWidgetFactory` class.
- defineSignal(ABCWidgetFactory.prototype, 'widgetCreated');
|