浏览代码

Add changed signals and complete the editor widget

Steven Silvester 9 年之前
父节点
当前提交
71eefb1dbd
共有 2 个文件被更改,包括 70 次插入2 次删除
  1. 39 1
      src/docmanager/default.ts
  2. 31 1
      src/docmanager/index.ts

+ 39 - 1
src/docmanager/default.ts

@@ -6,6 +6,14 @@ import {
   IKernelSpecId
 } from 'jupyter-js-services';
 
+import {
+  ISignal, Signal
+} from 'phosphor-signaling';
+
+import {
+  loadModeByFileName
+} from '../codemirror';
+
 import {
   CodeMirrorWidget
 } from '../codemirror/widget';
@@ -20,12 +28,21 @@ import {
  */
 export
 class DocumentModel implements IDocumentModel {
-
+  /**
+   * Construct a new document model.
+   */
   constructor(path: string, kernelSpecs: IKernelSpecId[]) {
     // Use the path and the kernel specs to set the default
     // kernel and language.
   }
 
+  /**
+   * A signal emitted when the document content changes.
+   */
+  get contentChanged(): ISignal<IDocumentModel, string> {
+    return Private.contentChangedSignal.bind(this);
+  }
+
   /**
    * Serialize the model.
    */
@@ -38,6 +55,7 @@ class DocumentModel implements IDocumentModel {
    */
   deserialize(value: string): void {
     this._text = value;
+    this.contentChanged.emit(value);
   }
 
   /**
@@ -94,6 +112,14 @@ class EditorWidget extends CodeMirrorWidget {
     super();
     this._model = model;
     this._context = context;
+    this.editor.getDoc().setValue(model.serialize());
+    loadModeByFileName(this.editor, context.path);
+    this._context.pathChanged.connect((c, path) => {
+      loadModeByFileName(this.editor, path);
+    });
+    model.contentChanged.connect((m, text) => {
+      this.editor.getDoc().setValue(text);
+    });
   }
 
   private _model: IDocumentModel = null;
@@ -135,3 +161,15 @@ class WidgetFactory implements IWidgetFactory<EditorWidget> {
   }
 }
 
+
+/**
+ * A private namespace for data.
+ */
+namespace Private {
+  /**
+   * A signal emitted when a document content changes.
+   */
+  export
+  const contentChangedSignal = new Signal<IDocumentModel, string>();
+
+}

+ 31 - 1
src/docmanager/index.ts

@@ -24,6 +24,11 @@ import {
  * The interface for a document model.
  */
 export interface IDocumentModel {
+  /**
+   * A signal emitted when the document content changes.
+   */
+  contentChanged: ISignal<IDocumentModel, any>;
+
   /**
    * Serialize the model.  It should return a JSON object or a string.
    */
@@ -31,16 +36,25 @@ export interface IDocumentModel {
 
   /**
    * Deserialize the model from a string or a JSON object.
+   *
+   * #### Notes
+   * Should emit a [contentChanged] signal.
    */
   deserialize(value: any): void;
 
   /**
    * The default kernel name for the the document.
+   *
+   * #### Notes
+   * This is a read-only property.
    */
   defaultKernelName: string;
 
   /**
    * The default kernel language for the document.
+   *
+   * #### Notes
+   * This is a read-only property.
    */
   defaultKernelLanguage: string;
 }
@@ -71,11 +85,27 @@ export interface IDocumentContext {
    */
   kernelChanged: ISignal<IDocumentContext, IKernel>;
 
+  /**
+   * A signal emitted when the path changes.
+   */
+  pathChanged: ISignal<IDocumentContext, string>;
+
   /**
    * The current kernel associated with the document.
+   *
+   * #### Notes
+   * This is a read-only property.
    */
   kernel: IKernel;
 
+  /**
+   * The current path associated with the document.
+   *
+   * #### Notes
+   * This is a read-only property.
+   */
+  path: string;
+
   /**
    * Change the current kernel associated with the document.
    */
@@ -169,7 +199,7 @@ interface IModelFactory {
   /**
    * Create a new model.
    */
-   createNew(path: string, kernelSpecs: IKernelSpecId[]): IDocumentModel;
+  createNew(path: string, kernelSpecs: IKernelSpecId[]): IDocumentModel;
 }