Forráskód Böngészése

Updates based on notebook implementation

Steven Silvester 8 éve
szülő
commit
f044b4c0d4

+ 6 - 0
src/codemirror/codemirror-ipython.ts

@@ -27,3 +27,9 @@ CodeMirror.defineMode('ipython', (config: CodeMirror.EditorConfiguration, modeOp
 });
 
 CodeMirror.defineMIME('text/x-ipython', 'ipython');
+CodeMirror.modeInfo.push({
+  ext: [],
+  mime: 'text/x-ipython',
+  mode: 'ipython',
+  name: 'ipython'
+});

+ 4 - 3
src/docmanager/context.ts

@@ -328,8 +328,9 @@ class ContextManager implements IDisposable {
     if (!session) {
       let path = contextEx.path;
       let sOptions = {
-        notebook: { path },
-        kernel: { options }
+        notebookPath: path,
+        kernelName: options.name,
+        kernelId: options.id
       };
       return this._startSession(id, sOptions);
     } else {
@@ -381,7 +382,7 @@ class ContextManager implements IDisposable {
     if (model.readOnly) {
       return Promise.reject(new Error('Read only'));
     }
-    if (opts.format === 'json') {
+    if (opts.type === 'notebook' || opts.format === 'json') {
       opts.content = model.toJSON();
     } else {
       opts.content = model.toString();

+ 7 - 0
src/docmanager/default.ts

@@ -167,6 +167,13 @@ class DocumentModel implements IDocumentModel {
     this.fromString(JSON.parse(value));
   }
 
+  /**
+   * Initialize the model state.
+   */
+  initialize(): void {
+    // No action necessary.
+  }
+
   private _text = '';
   private _defaultLang = '';
   private _dirty = false;

+ 33 - 4
src/docmanager/index.ts

@@ -21,7 +21,7 @@ import {
 } from 'phosphor-panel';
 
 import {
-  ISignal
+  ISignal, Signal
 } from 'phosphor-signaling';
 
 import {
@@ -117,6 +117,11 @@ interface IDocumentModel extends IDisposable {
    * Should emit a [contentChanged] signal.
    */
   fromJSON(value: any): void;
+
+  /**
+   * Initialize the model state.
+   */
+  initialize(): void;
 }
 
 
@@ -432,7 +437,7 @@ class DocumentManager implements IDisposable {
       for (let option of options.defaultFor) {
         if (option === '.*') {
           this._defaultWidgetFactory = name;
-        } else if (option in options.fileExtensions) {
+        } else if (options.fileExtensions.indexOf(option) !== -1) {
           this._defaultWidgetFactories[option] = name;
         }
       }
@@ -512,7 +517,7 @@ class DocumentManager implements IDisposable {
         continue;
       }
       let exts = options.fileExtensions;
-      if ((ext in exts) || ('.*' in exts)) {
+      if ((exts.indexOf(ext) !== -1) || (exts.indexOf('.*') !== -1)) {
         factories.push(name);
       }
     }
@@ -550,7 +555,7 @@ class DocumentManager implements IDisposable {
    *
    * @param kernel - An optional kernel name/id to override the default.
    */
-  open(path: string, widgetName='default', kernel?: IKernelId): Widget {
+  open(path: string, widgetName='default', kernel?: IKernelId): DocumentWidget {
     if (widgetName === 'default') {
       widgetName = this.listWidgetFactories(path)[0];
     }
@@ -572,6 +577,7 @@ class DocumentManager implements IDisposable {
     widget = this._createWidget(widgetName, id);
     // Load the contents from disk.
     this._contextManager.revert(id).then(() => {
+      model.initialize();
       this._populateWidget(widget, kernel);
     });
     return widget;
@@ -601,6 +607,7 @@ class DocumentManager implements IDisposable {
     // Save the contents to disk to get a valid contentsModel for the
     // context.
     this._contextManager.save(id).then(() => {
+      model.initialize();
       this._populateWidget(widget, kernel);
     });
     return widget;
@@ -747,6 +754,13 @@ class DocumentManager implements IDisposable {
  */
 export
 class DocumentWidget extends Widget {
+  /**
+   * A signal emitted when the document widget is populated.
+   */
+  get populated(): ISignal<DocumentWidget, Widget> {
+    return Private.populatedSignal.bind(this);
+  }
+
   /**
    * Construct a new document widget.
    */
@@ -781,6 +795,14 @@ class DocumentWidget extends Widget {
     return this._manager.getContext(this._id);
   }
 
+  /**
+   * The content widget used by the document widget.
+   */
+  get content(): Widget {
+    let layout = this.layout as PanelLayout;
+    return layout.childAt(0);
+  }
+
   /**
    * Bring up a dialog to select a kernel.
    */
@@ -834,6 +856,7 @@ class DocumentWidget extends Widget {
     });
     // Add the child widget to the layout.
     (this.layout as PanelLayout).addChild(child);
+    this.populated.emit(child);
   }
 
   /**
@@ -930,6 +953,12 @@ class DocumentWidget extends Widget {
  * A private namespace for DocumentManager data.
  */
 namespace Private {
+  /**
+   * A signal emitted when the document widget is populated.
+   */
+  export
+  const populatedSignal = new Signal<DocumentWidget, Widget>();
+
   /**
    * An extended interface for a model factory and its options.
    */

+ 8 - 0
typings/codemirror/codemirror.d.ts

@@ -32,6 +32,14 @@ declare module CodeMirror {
         [key: string]: any;
     }
 
+    interface modeinfo {
+      ext: string[];
+      mime: string;
+      mode: string;
+      name: string;
+    }
+    var modeInfo: modeinfo[];
+
     var version: string;
 
     /** If you want to define extra methods in terms of the CodeMirror API, it is possible to use defineExtension.