Procházet zdrojové kódy

Add createCell function to notebook cell factory so different cell types
may be programmatically generated.

Ian Rose před 6 roky
rodič
revize
44c64883c6
1 změnil soubory, kde provedl 38 přidání a 0 odebrání
  1. 38 0
      packages/notebook/src/model.ts

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

@@ -385,6 +385,19 @@ export namespace NotebookModel {
      */
     modelDB: IModelDB;
 
+    /**
+     * Create a new cell by cell type.
+     *
+     * @param type:  the type of the cell to create.
+     *
+     * @param options: the cell creation options.
+     *
+     * #### Notes
+     * This method is intended to be a convenience method to programmaticaly
+     * call the other cell creation methods in the factory.
+     */
+    createCell(type: nbformat.CellType, opts: CellModel.IOptions): ICellModel;
+
     /**
      * Create a new code cell.
      *
@@ -444,6 +457,31 @@ export namespace NotebookModel {
      */
     readonly modelDB: IModelDB | undefined;
 
+    /**
+     * Create a new cell by cell type.
+     *
+     * @param type:  the type of the cell to create.
+     *
+     * @param options: the cell creation options.
+     *
+     * #### Notes
+     * This method is intended to be a convenience method to programmaticaly
+     * call the other cell creation methods in the factory.
+     */
+    createCell(type: nbformat.CellType, opts: CellModel.IOptions): ICellModel {
+      switch (type) {
+        case 'code':
+          return this.createCodeCell(opts);
+          break;
+        case 'markdown':
+          return this.createMarkdownCell(opts);
+          break;
+        case 'raw':
+        default:
+          return this.createRawCell(opts);
+      }
+    }
+
     /**
      * Create a new code cell.
      *