Selaa lähdekoodia

Clean up usage of model factories

Steven Silvester 8 vuotta sitten
vanhempi
commit
6cc1287b74
2 muutettua tiedostoa jossa 24 lisäystä ja 26 poistoa
  1. 9 8
      src/notebook/notebook/actions.ts
  2. 15 18
      src/notebook/notebook/model.ts

+ 9 - 8
src/notebook/notebook/actions.ts

@@ -340,16 +340,17 @@ namespace NotebookActions {
         return;
       }
       if (child.model.type !== value) {
+        let cell: nbformat.IBaseCell = child.model.toJSON();
         let newCell: ICellModel;
         switch (value) {
         case 'code':
-          newCell = model.contentFactory.createCodeCell(child.model.toJSON());
+          newCell = model.contentFactory.createCodeCell({ cell });
           break;
         case 'markdown':
-          newCell = model.contentFactory.createMarkdownCell(child.model.toJSON());
+          newCell = model.contentFactory.createMarkdownCell({ cell });
           break;
         default:
-          newCell = model.contentFactory.createRawCell(child.model.toJSON());
+          newCell = model.contentFactory.createRawCell({ cell });
         }
         cells.set(i, newCell);
       }
@@ -702,16 +703,16 @@ namespace NotebookActions {
     let newCells: ICellModel[] = [];
     widget.mode = 'command';
 
-    each(values, value => {
-      switch (value.cell_type) {
+    each(values, cell => {
+      switch (cell.cell_type) {
       case 'code':
-        newCells.push(model.contentFactory.createCodeCell(value));
+        newCells.push(model.contentFactory.createCodeCell({ cell }));
         break;
       case 'markdown':
-        newCells.push(model.contentFactory.createMarkdownCell(value));
+        newCells.push(model.contentFactory.createMarkdownCell({ cell }));
         break;
       default:
-        newCells.push(model.contentFactory.createRawCell(value));
+        newCells.push(model.contentFactory.createRawCell({ cell }));
         break;
       }
     });

+ 15 - 18
src/notebook/notebook/model.ts

@@ -107,11 +107,10 @@ class NotebookModel extends DocumentModel implements INotebookModel {
       options.contentFactory || NotebookModel.defaultContentFactory
     );
     this.contentFactory = factory;
-    let outputAreaFactory = factory.outputAreaFactory;
     this._cells = new ObservableUndoableVector<ICellModel>((cell: nbformat.IBaseCell) => {
       switch (cell.cell_type) {
         case 'code':
-          return factory.createCodeCell({ cell, outputAreaFactory });
+          return factory.createCodeCell({ cell });
         case 'markdown':
           return factory.createMarkdownCell({ cell });
         default:
@@ -119,7 +118,7 @@ class NotebookModel extends DocumentModel implements INotebookModel {
       }
     });
     // Add an initial code cell by default.
-    this._cells.pushBack(this._createCodeCell());
+    this._cells.pushBack(factory.createCodeCell({}));
     this._cells.changed.connect(this._onCellsChanged, this);
     if (options.languagePreference) {
       this._metadata['language_info'] = { name: options.languagePreference };
@@ -243,16 +242,17 @@ class NotebookModel extends DocumentModel implements INotebookModel {
    */
   fromJSON(value: nbformat.INotebookContent): void {
     let cells: ICellModel[] = [];
-    for (let data of value.cells) {
-      switch (data.cell_type) {
+    let factory = this.contentFactory;
+    for (let cell of value.cells) {
+      switch (cell.cell_type) {
       case 'code':
-        cells.push(new CodeCellModel(data));
+        cells.push(factory.createCodeCell({ cell }));
         break;
       case 'markdown':
-        cells.push(new MarkdownCellModel(data));
+        cells.push(factory.createMarkdownCell({ cell }));
         break;
       case 'raw':
-        cells.push(new RawCellModel(data));
+        cells.push(factory.createRawCell({ cell }));
         break;
       default:
         continue;
@@ -372,13 +372,14 @@ class NotebookModel extends DocumentModel implements INotebookModel {
     default:
       return;
     }
+    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.pushBack(this._createCodeCell());
+          this._cells.pushBack(factory.createCodeCell({}));
         }
       });
     }
@@ -394,15 +395,6 @@ class NotebookModel extends DocumentModel implements INotebookModel {
     this.contentChanged.emit(void 0);
   }
 
-  /**
-   * Create an empty code cell.
-   */
-  private _createCodeCell(): ICodeCellModel {
-    let factory = this.contentFactory;
-    let outputAreaFactory = factory.outputAreaFactory;
-    return factory.createCodeCell({ outputAreaFactory });
-  }
-
   private _cells: IObservableUndoableVector<ICellModel> = null;
   private _metadata: { [key: string]: any } = Private.createMetadata();
   private _cursors: { [key: string]: MetadataCursor } = Object.create(null);
@@ -505,8 +497,13 @@ namespace NotebookModel {
      *
      * @returns A new code cell. If a source cell is provided, the
      *   new cell will be intialized with the data from the source.
+     *   If the outputAreaFactory is not provided, the instance
+     *   level version will be used.
      */
     createCodeCell(options: CodeCellModel.IOptions): ICodeCellModel {
+      if (options.outputAreaFactory) {
+        options.outputAreaFactory = this.outputAreaFactory;
+      }
       return new CodeCellModel(options);
     }