diff --git a/src/history.ts b/src/history.ts index 2f829b078..20bdddef1 100644 --- a/src/history.ts +++ b/src/history.ts @@ -34,6 +34,29 @@ class SceneHistory { this.skipRecording(); } + redoOnce(elements: ExcalidrawElement[]) { + const currentEntry = this.generateCurrentEntry(elements); + const entryToRestore = this.redoStack.pop(); + if (entryToRestore !== undefined) { + this.restoreEntry(elements, entryToRestore); + this.stateHistory.push(currentEntry); + } + } + + undoOnce(elements: ExcalidrawElement[]) { + const currentEntry = this.generateCurrentEntry(elements); + let entryToRestore = this.stateHistory.pop(); + + // If nothing was changed since last, take the previous one + if (currentEntry === entryToRestore) { + entryToRestore = this.stateHistory.pop(); + } + if (entryToRestore !== undefined) { + this.restoreEntry(elements, entryToRestore); + this.redoStack.push(currentEntry); + } + } + isRecording() { return this.recording; } diff --git a/src/index.tsx b/src/index.tsx index eb711352f..e210b3212 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -206,22 +206,10 @@ class App extends React.Component<{}, AppState> { const currentEntry = history.generateCurrentEntry(elements); if (event.shiftKey) { // Redo action - const entryToRestore = history.redoStack.pop(); - if (entryToRestore !== undefined) { - history.restoreEntry(elements, entryToRestore); - history.stateHistory.push(currentEntry); - } + history.redoOnce(elements); } else { // undo action - let lastEntry = history.stateHistory.pop(); - // If nothing was changed since last, take the previous one - if (currentEntry === lastEntry) { - lastEntry = history.stateHistory.pop(); - } - if (lastEntry !== undefined) { - history.restoreEntry(elements, lastEntry); - history.redoStack.push(currentEntry); - } + history.undoOnce(elements); } this.forceUpdate(); event.preventDefault();