瀏覽代碼

Account for model setting

Steven Silvester 9 年之前
父節點
當前提交
bc32ebb66e
共有 3 個文件被更改,包括 37 次插入14 次删除
  1. 22 14
      src/notebook/notebook/default-toolbar.ts
  2. 12 0
      src/notebook/notebook/panel.ts
  3. 3 0
      src/notebook/notebook/trust.ts

+ 22 - 14
src/notebook/notebook/default-toolbar.ts

@@ -5,10 +5,6 @@ import {
   IKernel, KernelStatus
 } from 'jupyter-js-services';
 
-import {
-  showDialog
-} from '../../dialog';
-
 import {
   IDocumentContext
 } from '../../docregistry';
@@ -268,18 +264,37 @@ class CellTypeSwitcher extends Widget {
   constructor(panel: NotebookPanel) {
     super();
     this.addClass(TOOLBAR_CELLTYPE);
+
     let select = this.node.firstChild as HTMLSelectElement;
-    // Set the initial value.
-    let index = panel.content.activeCellIndex;
-    select.value = panel.model.cells.get(index).type;
+    // Change current cell type on a change in the dropdown.
+    select.addEventListener('change', event => {
+      if (!this._changeGuard) {
+        NotebookActions.changeCellType(panel.content, select.value);
+      }
+    });
     // Follow the type of the current cell.
     panel.content.stateChanged.connect((sender, args) => {
+      if (!panel.model) {
+        return;
+      }
       if (args.name === 'activeCellIndex') {
         this._changeGuard = true;
         select.value = panel.model.cells.get(args.newValue).type;
         this._changeGuard = false;
       }
     });
+
+    panel.content.modelChanged.connect(() => { this.followModel(); });
+    if (panel.model) {
+      this.followModel();
+    }
+  }
+
+  followModel(): void {
+    let select = this.node.firstChild as HTMLSelectElement;
+    // Set the initial value.
+    let index = panel.content.activeCellIndex;
+    select.value = panel.model.cells.get(index).type;
     // Follow a change in the cells.
     panel.content.model.cells.changed.connect((sender, args) => {
       index = panel.content.activeCellIndex;
@@ -287,15 +302,8 @@ class CellTypeSwitcher extends Widget {
       select.value = panel.model.cells.get(index).type;
       this._changeGuard = false;
     });
-    // Change current cell type on a change in the dropdown.
-    select.addEventListener('change', event => {
-      if (!this._changeGuard) {
-        NotebookActions.changeCellType(panel.content, select.value);
-      }
-    });
   }
 
-
   private _changeGuard = false;
 }
 

+ 12 - 0
src/notebook/notebook/panel.ts

@@ -266,6 +266,9 @@ class NotebookPanel extends Widget {
    * Handle a change in the kernel by updating the document metadata.
    */
   protected handleKernelChange(kernel: IKernel): void {
+    if (!this.model) {
+      return;
+    }
     kernel.kernelInfo().then(info => {
       let infoCursor = this.model.getMetadata('language_info');
       infoCursor.setValue(info.language_info);
@@ -305,6 +308,9 @@ class NotebookPanel extends Widget {
    * Handle a text changed signal from an editor.
    */
   protected onTextChange(editor: CellEditorWidget, change: ITextChange): void {
+    if (!this.model) {
+      return;
+    }
     let line = change.newValue.split('\n')[change.line];
     let model = this._completion.model;
     // If last character entered is not whitespace, update completion.
@@ -326,6 +332,9 @@ class NotebookPanel extends Widget {
    * Handle a completion requested signal from an editor.
    */
   protected onCompletionRequest(editor: CellEditorWidget, change: ICompletionRequest): void {
+    if (!this.model) {
+      return;
+    }
     let kernel = this.context.kernel;
     if (!kernel) {
       return;
@@ -362,6 +371,9 @@ class NotebookPanel extends Widget {
    * Handle a completion selected signal from the completion widget.
    */
   protected onCompletionSelect(widget: CompletionWidget, value: string): void {
+    if (!this.model) {
+      return;
+    }
     let patch = this._completion.model.createPatch(value);
     let cell = this._content.childAt(this._content.activeCellIndex);
     let editor = cell.editor.editor;

+ 3 - 0
src/notebook/notebook/trust.ts

@@ -28,6 +28,9 @@ const TRUST_MESSAGE = '<p>A trusted Jupyter notebook may execute hidden maliciou
  */
 export
 function trustNotebook(model: INotebookModel, host?: HTMLElement): Promise<void> {
+  if (!model) {
+    return;
+  }
   // Do nothing if already trusted.
   let cells = model.cells;
   let trusted = true;