Browse Source

History improvements (#337)

* Simplified redoOnce.

* Help mental model.

* Move clear redo stack where it belongs.

* Not needed anymore as we check for same state.
Enzo Ferey 5 năm trước cách đây
mục cha
commit
88a9cee8bb
2 tập tin đã thay đổi với 12 bổ sung8 xóa
  1. 11 6
      src/history.ts
  2. 1 2
      src/index.tsx

+ 11 - 6
src/history.ts

@@ -22,13 +22,14 @@ class SceneHistory {
       // If the last entry is the same as this one, ignore it
       return;
     }
+
     this.stateHistory.push(newEntry);
+
+    // As a new entry was pushed, we invalidate the redo stack
+    this.clearRedoStack();
   }
 
   restoreEntry(entry: string) {
-    // When restoring, we shouldn't add an history entry otherwise we'll be stuck with it and can't go back
-    this.skipRecording();
-
     try {
       return JSON.parse(entry);
     } catch {
@@ -40,11 +41,15 @@ class SceneHistory {
     this.redoStack.splice(0, this.redoStack.length);
   }
 
-  redoOnce(elements: readonly ExcalidrawElement[]) {
-    const currentEntry = this.generateCurrentEntry(elements);
+  redoOnce() {
+    if (this.redoStack.length === 0) {
+      return null;
+    }
+
     const entryToRestore = this.redoStack.pop();
+
     if (entryToRestore !== undefined) {
-      this.stateHistory.push(currentEntry);
+      this.stateHistory.push(entryToRestore);
       return this.restoreEntry(entryToRestore);
     }
 

+ 1 - 2
src/index.tsx

@@ -291,7 +291,7 @@ export class App extends React.Component<{}, AppState> {
     } else if (event[META_KEY] && event.code === "KeyZ") {
       if (event.shiftKey) {
         // Redo action
-        const data = history.redoOnce(elements);
+        const data = history.redoOnce();
         if (data !== null) {
           elements = data;
         }
@@ -1026,7 +1026,6 @@ export class App extends React.Component<{}, AppState> {
     this.saveDebounced();
     if (history.isRecording()) {
       history.pushEntry(history.generateCurrentEntry(elements));
-      history.clearRedoStack();
     }
   }
 }