Browse Source

Add id readonly to cell models.

Ian Rose 8 years ago
parent
commit
f8f7e478db
3 changed files with 48 additions and 29 deletions
  1. 21 1
      packages/cells/src/model.ts
  2. 15 25
      packages/notebook/src/celllist.ts
  3. 12 3
      packages/notebook/src/model.ts

+ 21 - 1
packages/cells/src/model.ts

@@ -9,6 +9,10 @@ import {
   ISignal, Signal
 } from '@phosphor/signaling';
 
+import {
+  utils
+} from '@jupyterlab/services';
+
 import {
   CodeEditor
 } from '@jupyterlab/codeeditor';
@@ -36,6 +40,11 @@ interface ICellModel extends CodeEditor.IModel {
    */
   readonly type: nbformat.CellType;
 
+  /**
+   * A unique identifier for the cell.
+   */
+  readonly id: string;
+
   /**
    * A signal emitted when the content of the model changes.
    */
@@ -123,6 +132,8 @@ class CellModel extends CodeEditor.Model implements ICellModel {
   constructor(options: CellModel.IOptions) {
     super({modelDB: options.modelDB});
 
+    this._id = options.id || utils.uuid();
+
     this.value.changed.connect(this.onGenericChange, this);
 
     let cellType = this.modelDB.createValue('type');
@@ -175,6 +186,13 @@ class CellModel extends CodeEditor.Model implements ICellModel {
    */
   readonly stateChanged = new Signal<this, IChangedArgs<any>>(this);
 
+  /**
+   * The id for the cell.
+   */
+  get id(): string {
+    return this._id;
+  }
+
   /**
    * The metadata associated with the cell.
    */
@@ -232,6 +250,8 @@ class CellModel extends CodeEditor.Model implements ICellModel {
   protected onGenericChange(): void {
     this.contentChanged.emit(void 0);
   }
+
+  private _id: string = '';
 }
 
 
@@ -257,7 +277,7 @@ namespace CellModel {
     /**
      * A unique identifier for this cell.
      */
-    uuid?: string;
+    id?: string;
   }
 }
 

+ 15 - 25
packages/notebook/src/celllist.ts

@@ -208,11 +208,9 @@ class CellList implements IObservableUndoableVector<ICellModel> {
    * not be called by other actors.
    */
   set(index: number, cell: ICellModel): void {
-    // Generate a new uuid for the cell.
-    let id = (cell as any).modelDB.basePath;
     // Set the internal data structures.
-    this._cellMap.set(id, cell);
-    this._cellOrder.set(index, id);
+    this._cellMap.set(cell.id, cell);
+    this._cellOrder.set(index, cell.id);
   }
 
   /**
@@ -234,11 +232,9 @@ class CellList implements IObservableUndoableVector<ICellModel> {
    * not be called by other actors.
    */
   pushBack(cell: ICellModel): number {
-    // Generate a new uuid for the cell.
-    let id = (cell as any).modelDB.basePath;
     // Set the internal data structures.
-    this._cellMap.set(id, cell);
-    let num = this._cellOrder.pushBack(id);
+    this._cellMap.set(cell.id, cell);
+    let num = this._cellOrder.pushBack(cell.id);
     return num;
   }
 
@@ -288,11 +284,9 @@ class CellList implements IObservableUndoableVector<ICellModel> {
    * not be called by other actors.
    */
   insert(index: number, cell: ICellModel): number {
-    // Generate a new uuid for the cell.
-    let id = (cell as any).modelDB.basePath;
     // Set the internal data structures.
-    this._cellMap.set(id, cell);
-    let num = this._cellOrder.insert(index, id);
+    this._cellMap.set(cell.id, cell);
+    let num = this._cellOrder.insert(index, cell.id);
     return num;
   }
 
@@ -395,11 +389,9 @@ class CellList implements IObservableUndoableVector<ICellModel> {
   pushAll(cells: IterableOrArrayLike<ICellModel>): number {
     let newValues = toArray(cells);
     each(newValues, cell => {
-      // Generate a new uuid for the cell.
-      let id = (cell as any).modelDB.basePath;
       // Set the internal data structures.
-      this._cellMap.set(id, cell);
-      this._cellOrder.pushBack(id);
+      this._cellMap.set(cell.id, cell);
+      this._cellOrder.pushBack(cell.id);
     });
     return this.length;
   }
@@ -433,11 +425,9 @@ class CellList implements IObservableUndoableVector<ICellModel> {
   insertAll(index: number, cells: IterableOrArrayLike<ICellModel>): number {
     let newValues = toArray(cells);
     each(newValues, cell => {
-      // Generate a new uuid for the cell.
-      let id = (cell as any).modelDB.basePath;
-      this._cellMap.set(id, cell);
+      this._cellMap.set(cell.id, cell);
       this._cellOrder.beginCompoundOperation();
-      this._cellOrder.insert(index++, id);
+      this._cellOrder.insert(index++, cell.id);
       this._cellOrder.endCompoundOperation();
     });
     return this.length;
@@ -533,18 +523,18 @@ class CellList implements IObservableUndoableVector<ICellModel> {
       each(change.newValues, (id) => {
         if (!this._cellMap.has(id)) {
           let cellDB = this._factory.modelDB;
-          let cellType = (cellDB as any).getGoogleObject(id+'/type');
+          let cellType = cellDB.createValue(id+'/type');
           let cell: ICellModel;
-          switch (cellType) {
+          switch (cellType.get()) {
             case 'code':
-              cell = this._factory.createCodeCell({ uuid: id});
+              cell = this._factory.createCodeCell({ id: id});
               break;
             case 'markdown':
-              cell = this._factory.createMarkdownCell({ uuid: id});
+              cell = this._factory.createMarkdownCell({ id: id});
               break;
             case 'raw':
             default:
-              cell = this._factory.createRawCell({ uuid: id});
+              cell = this._factory.createRawCell({ id: id});
               break;
           }
           this._cellMap.set(id, cell);

+ 12 - 3
packages/notebook/src/model.ts

@@ -423,7 +423,10 @@ namespace NotebookModel {
         options.contentFactory = this.codeCellContentFactory;
       }
       if (this._modelDB) {
-        options.modelDB = this._modelDB.view(options.uuid || utils.uuid());
+        if (!options.id) {
+          options.id = utils.uuid();
+        }
+        options.modelDB = this._modelDB.view(options.id);
       }
       return new CodeCellModel(options);
     }
@@ -438,7 +441,10 @@ namespace NotebookModel {
      */
     createMarkdownCell(options: CellModel.IOptions): IMarkdownCellModel {
       if (this._modelDB) {
-        options.modelDB = this._modelDB.view(options.uuid || utils.uuid());
+        if (!options.id) {
+          options.id = utils.uuid();
+        }
+        options.modelDB = this._modelDB.view(options.id);
       }
       return new MarkdownCellModel(options);
     }
@@ -453,7 +459,10 @@ namespace NotebookModel {
      */
     createRawCell(options: CellModel.IOptions): IRawCellModel {
       if (this._modelDB) {
-        options.modelDB = this._modelDB.view(options.uuid || utils.uuid());
+        if (!options.id) {
+          options.id = utils.uuid();
+        }
+        options.modelDB = this._modelDB.view(options.id);
       }
      return new RawCellModel(options);
     }