Browse Source

Clear the notebook model undo history when the model is set.

Jason Grout 7 years ago
parent
commit
7b618baac7
2 changed files with 15 additions and 20 deletions
  1. 6 19
      packages/notebook/src/panel.ts
  2. 9 1
      packages/notebook/src/widget.ts

+ 6 - 19
packages/notebook/src/panel.ts

@@ -6,10 +6,6 @@ import {
   Kernel, KernelMessage
 } from '@jupyterlab/services';
 
-import {
-  each
-} from '@phosphor/algorithm';
-
 import {
   PromiseDelegate, Token
 } from '@phosphor/coreutils';
@@ -85,28 +81,19 @@ class NotebookPanel extends DocumentWidget<Notebook, INotebookModel> {
       if (this.isDisposed) {
         return;
       }
-      let model = this.context.model;
-
-      // TODO: this logic is being called too soon - the cells aren't available yet, and the widgets aren't created.
-      // Clear the undo state of the cells.
-      if (model) {
-        model.cells.clearUndo();
-        each(this.content.widgets, widget => {
-          widget.editor.clearHistory();
-        });
-      }
 
-      // If we have just a single empty code cell, change the notebook mode to
-      // edit mode. This happens for example when we have a new notebook.
+      // The notebook widget is now ready to be shown.
+      notebookReady.resolve(undefined);
+    });
+
+    this.ready.then(() => {
+      // Set the document edit mode on initial open if it looks like a new document.
       if (this.content.widgets.length === 1) {
         let cellModel = this.content.widgets[0].model;
         if (cellModel.type === 'code' && cellModel.value.text === '') {
           this.content.mode = 'edit';
         }
       }
-
-      // The notebook widget is now ready to be shown.
-      notebookReady.resolve(undefined);
     });
 
   }

+ 9 - 1
packages/notebook/src/widget.ts

@@ -372,7 +372,8 @@ class StaticNotebook extends Widget {
       oldValue.cells.changed.disconnect(this._onCellsChanged, this);
       oldValue.metadata.changed.disconnect(this.onMetadataChanged, this);
       oldValue.contentChanged.disconnect(this.onModelContentChanged, this);
-      // TODO: reuse existing cell widgets if possible.
+      // TODO: reuse existing cell widgets if possible. Remember to initially
+      // clear the history of each cell if we do this.
       while (layout.widgets.length) {
         this._removeCell(0);
       }
@@ -1309,9 +1310,16 @@ class Notebook extends StaticNotebook {
    * Handle a new model.
    */
   protected onModelChanged(oldValue: INotebookModel, newValue: INotebookModel): void {
+    super.onModelChanged(oldValue, newValue);
+
     // Try to set the active cell index to 0.
     // It will be set to `-1` if there is no new model or the model is empty.
     this.activeCellIndex = 0;
+
+    // Clear the undo history of each cell.
+    if (this.model) {
+      this.model.cells.clearUndo();
+    }
   }
 
   /**