浏览代码

Make a cell clone method to handle creating a new cell widget.

Jason Grout 7 年之前
父节点
当前提交
10d07f6917
共有 3 个文件被更改,包括 39 次插入14 次删除
  1. 35 0
      packages/cells/src/widget.ts
  2. 1 6
      packages/notebook-extension/src/index.ts
  3. 3 8
      packages/notebook/src/widget.ts

+ 35 - 0
packages/cells/src/widget.ts

@@ -311,6 +311,17 @@ class Cell extends Widget {
     return;
   }
 
+  /**
+   * Clone the cell, using the same model.
+   */
+  clone() {
+    let constructor = this.constructor as typeof Cell;
+    return new constructor({
+      model: this.model,
+      contentFactory: this.contentFactory
+    });
+  }
+
   /**
    * Dispose of the resources held by the widget.
    */
@@ -609,6 +620,18 @@ class CodeCell extends Cell {
     }
   }
 
+  /**
+   * Clone the cell, using the same model.
+   */
+  clone() {
+    let constructor = this.constructor as typeof CodeCell;
+    return new constructor({
+      model: this.model,
+      contentFactory: this.contentFactory,
+      rendermime: this._rendermime
+    });
+  }
+
   /**
    * Dispose of the resources used by the widget.
    */
@@ -849,6 +872,18 @@ class MarkdownCell extends Cell {
     return Promise.resolve(void 0);
   }
 
+  /**
+   * Clone the cell, using the same model.
+   */
+  clone() {
+    let constructor = this.constructor as typeof MarkdownCell;
+    return new constructor({
+      model: this.model,
+      contentFactory: this.contentFactory,
+      rendermime: this._rendermime
+    });
+  }
+
   private _monitor: ActivityMonitor<any, any> = null;
   private _renderer: IRenderMime.IRenderer = null;
   private _rendermime: RenderMime;

+ 1 - 6
packages/notebook-extension/src/index.ts

@@ -1018,16 +1018,11 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
     execute: args => {
       const current = getCurrent(args);
       let nb = current.notebook;
-      let index = nb.activeCellIndex;
-      let cellwidget = nb.widgets[index];
-
       let p = new Panel();
       p.id = `Cell-${Math.random()}`;
       p.title.closable = true;
       p.title.label = 'Cell';
-
-      let newCell = nb.createCell(cellwidget.model);
-      p.addWidget(newCell);
+      p.addWidget(nb.activeCell.clone());
       shell.addToMainArea(p);
     },
     isEnabled: hasWidget

+ 3 - 8
packages/notebook/src/widget.ts

@@ -398,13 +398,6 @@ class StaticNotebook extends Widget {
    * Create a cell widget and insert into the notebook.
    */
   private _insertCell(index: number, cell: ICellModel): void {
-    let widget  = this.createCell(cell);
-    let layout = this.layout as PanelLayout;
-    layout.insertWidget(index, widget);
-    this.onCellInserted(index, widget);
-  }
-
-  createCell(cell: ICellModel): Cell {
     let widget: Cell;
     switch (cell.type) {
     case 'code':
@@ -418,7 +411,9 @@ class StaticNotebook extends Widget {
       widget = this._createRawCell(cell as IRawCellModel);
     }
     widget.addClass(NB_CELL_CLASS);
-    return widget;
+    let layout = this.layout as PanelLayout;
+    layout.insertWidget(index, widget);
+    this.onCellInserted(index, widget);
   }
 
   /**