mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Encapsulate undo and redo actions within history
This commit is contained in:
parent
01a922bf20
commit
9eeb4b10af
2 changed files with 25 additions and 14 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue