瀏覽代碼

Make the notebook widget responsible for adding new cells.

Ian Rose 6 年之前
父節點
當前提交
e55aba60e0
共有 3 個文件被更改,包括 39 次插入48 次删除
  1. 18 4
      packages/notebook/src/actions.tsx
  2. 0 40
      packages/notebook/src/model.ts
  3. 21 4
      packages/notebook/src/widget.ts

+ 18 - 4
packages/notebook/src/actions.tsx

@@ -294,7 +294,10 @@ export namespace NotebookActions {
 
     const state = Private.getState(notebook);
     const model = notebook.model;
-    const cell = model.contentFactory.createCell(model.defaultCell, {});
+    const cell = model.contentFactory.createCell(
+      notebook.notebookConfig.defaultCell,
+      {}
+    );
 
     model.cells.insert(notebook.activeCellIndex + 1, cell);
 
@@ -449,7 +452,10 @@ export namespace NotebookActions {
     const model = notebook.model;
 
     if (notebook.activeCellIndex === notebook.widgets.length - 1) {
-      const cell = model.contentFactory.createCell(model.defaultCell, {});
+      const cell = model.contentFactory.createCell(
+        notebook.notebookConfig.defaultCell,
+        {}
+      );
 
       model.cells.push(cell);
       notebook.activeCellIndex++;
@@ -487,7 +493,10 @@ export namespace NotebookActions {
     const state = Private.getState(notebook);
     const promise = Private.runSelected(notebook, session);
     const model = notebook.model;
-    const cell = model.contentFactory.createCell(model.defaultCell, {});
+    const cell = model.contentFactory.createCell(
+      notebook.notebookConfig.defaultCell,
+      {}
+    );
 
     model.cells.insert(notebook.activeCellIndex + 1, cell);
     notebook.activeCellIndex++;
@@ -1661,7 +1670,12 @@ namespace Private {
       // within the compound operation to make the deletion of
       // a notebook's last cell undoable.
       if (!cells.length) {
-        cells.push(model.contentFactory.createCell(model.defaultCell, {}));
+        cells.push(
+          model.contentFactory.createCell(
+            notebook.notebookConfig.defaultCell,
+            {}
+          )
+        );
       }
       cells.endCompoundOperation();
 

+ 0 - 40
packages/notebook/src/model.ts

@@ -61,11 +61,6 @@ export interface INotebookModel extends DocumentRegistry.IModel {
    * The array of deleted cells since the notebook was last run.
    */
   readonly deletedCells: string[];
-
-  /**
-   * The default cell type for new cells.
-   */
-  defaultCell: nbformat.CellType;
 }
 
 /**
@@ -78,13 +73,8 @@ export class NotebookModel extends DocumentModel implements INotebookModel {
   constructor(options: NotebookModel.IOptions = {}) {
     super(options.languagePreference, options.modelDB);
     let factory = options.contentFactory || NotebookModel.defaultContentFactory;
-    this._defaultCell = options.defaultCell || 'code';
     this.contentFactory = factory.clone(this.modelDB.view('cells'));
     this._cells = new CellList(this.modelDB, this.contentFactory);
-    // Add an initial code cell by default.
-    if (!this._cells.length) {
-      this._cells.push(factory.createCell(this.defaultCell, {}));
-    }
     this._cells.changed.connect(this._onCellsChanged, this);
 
     // Handle initial metadata.
@@ -156,19 +146,6 @@ export class NotebookModel extends DocumentModel implements INotebookModel {
     return info ? info.name : '';
   }
 
-  /**
-   * The default cell type for new cells.
-   */
-  get defaultCell(): nbformat.CellType {
-    return this._defaultCell;
-  }
-  set defaultCell(value: nbformat.CellType) {
-    if (this._defaultCell === value) {
-      return;
-    }
-    this._defaultCell = value;
-  }
-
   /**
    * Dispose of the resources held by the model.
    */
@@ -334,17 +311,6 @@ export class NotebookModel extends DocumentModel implements INotebookModel {
       default:
         break;
     }
-    let factory = this.contentFactory;
-    // Add code cell if there are no cells remaining.
-    if (!this.cells.length) {
-      // Add the cell in a new context to avoid triggering another
-      // cell changed event during the handling of this signal.
-      requestAnimationFrame(() => {
-        if (!this.isDisposed && !this.cells.length) {
-          this.cells.push(factory.createCell(this.defaultCell, {}));
-        }
-      });
-    }
     this.triggerContentChange();
   }
 
@@ -362,7 +328,6 @@ export class NotebookModel extends DocumentModel implements INotebookModel {
   }
 
   private _cells: CellList;
-  private _defaultCell: nbformat.CellType = 'code';
   private _nbformat = nbformat.MAJOR_VERSION;
   private _nbformatMinor = nbformat.MINOR_VERSION;
   private _deletedCells: string[];
@@ -392,11 +357,6 @@ export namespace NotebookModel {
      * A modelDB for storing notebook data.
      */
     modelDB?: IModelDB;
-
-    /**
-     * A default cell type for new cells.
-     */
-    defaultCell?: nbformat.CellType;
   }
 
   /**

+ 21 - 4
packages/notebook/src/widget.ts

@@ -214,7 +214,6 @@ export class StaticNotebook extends Widget {
     }
     let oldValue = this._model;
     this._model = newValue;
-    this._model.defaultCell = this._notebookConfig.defaultCell;
 
     if (oldValue && oldValue.modelDB.isCollaborative) {
       void oldValue.modelDB.connected.then(() => {
@@ -383,6 +382,11 @@ export class StaticNotebook extends Widget {
     }
     this._updateMimetype();
     let cells = newValue.cells;
+    if (!cells.length) {
+      cells.push(
+        newValue.contentFactory.createCell(this.notebookConfig.defaultCell, {})
+      );
+    }
     each(cells, (cell: ICellModel, i: number) => {
       this._insertCell(i, cell);
     });
@@ -413,6 +417,22 @@ export class StaticNotebook extends Widget {
         each(args.oldValues, value => {
           this._removeCell(args.oldIndex);
         });
+        // Add code cell if there are no cells remaining.
+        if (!sender.length) {
+          const model = this.model;
+          // Add the cell in a new context to avoid triggering another
+          // cell changed event during the handling of this signal.
+          requestAnimationFrame(() => {
+            if (!model.isDisposed && !model.cells.length) {
+              model.cells.push(
+                model.contentFactory.createCell(
+                  this.notebookConfig.defaultCell,
+                  {}
+                )
+              );
+            }
+          });
+        }
         break;
       case 'set':
         // TODO: reuse existing widgets if possible.
@@ -603,9 +623,6 @@ export class StaticNotebook extends Widget {
       'jp-mod-scrollPastEnd',
       this._notebookConfig.scrollPastEnd
     );
-    if (this._model) {
-      this._model.defaultCell = this._notebookConfig.defaultCell;
-    }
   }
 
   private _editorConfig = StaticNotebook.defaultEditorConfig;