mirror of
				https://github.com/excalidraw/excalidraw.git
				synced 2025-11-03 20:34:40 +01:00 
			
		
		
		
	Implement redo (#191)
This commit is contained in:
		
				
					committed by
					
						
						Christopher Chedeau
					
				
			
			
				
	
			
			
			
						parent
						
							3bbcb9cbdc
						
					
				
				
					commit
					ea534dd535
				
			@@ -27,6 +27,8 @@ const DEFAULT_PROJECT_NAME = `excalidraw-${getDateTime()}`;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
let skipHistory = false;
 | 
					let skipHistory = false;
 | 
				
			||||||
const stateHistory: string[] = [];
 | 
					const stateHistory: string[] = [];
 | 
				
			||||||
 | 
					const redoStack: string[] = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function generateHistoryCurrentEntry() {
 | 
					function generateHistoryCurrentEntry() {
 | 
				
			||||||
  return JSON.stringify(
 | 
					  return JSON.stringify(
 | 
				
			||||||
    elements.map(element => ({ ...element, isSelected: false }))
 | 
					    elements.map(element => ({ ...element, isSelected: false }))
 | 
				
			||||||
@@ -1351,13 +1353,25 @@ class App extends React.Component<{}, AppState> {
 | 
				
			|||||||
    } else if (shapesShortcutKeys.includes(event.key.toLowerCase())) {
 | 
					    } else if (shapesShortcutKeys.includes(event.key.toLowerCase())) {
 | 
				
			||||||
      this.setState({ elementType: findElementByKey(event.key) });
 | 
					      this.setState({ elementType: findElementByKey(event.key) });
 | 
				
			||||||
    } else if (event.metaKey && event.code === "KeyZ") {
 | 
					    } else if (event.metaKey && event.code === "KeyZ") {
 | 
				
			||||||
      let lastEntry = stateHistory.pop();
 | 
					      const currentEntry = generateHistoryCurrentEntry();
 | 
				
			||||||
      // If nothing was changed since last, take the previous one
 | 
					      if (event.shiftKey) {
 | 
				
			||||||
      if (generateHistoryCurrentEntry() === lastEntry) {
 | 
					        // Redo action
 | 
				
			||||||
        lastEntry = stateHistory.pop();
 | 
					        const entryToRestore = redoStack.pop();
 | 
				
			||||||
      }
 | 
					        if (entryToRestore !== undefined) {
 | 
				
			||||||
      if (lastEntry !== undefined) {
 | 
					          restoreHistoryEntry(entryToRestore);
 | 
				
			||||||
        restoreHistoryEntry(lastEntry);
 | 
					          stateHistory.push(currentEntry);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        // undo action
 | 
				
			||||||
 | 
					        let lastEntry = stateHistory.pop();
 | 
				
			||||||
 | 
					        // If nothing was changed since last, take the previous one
 | 
				
			||||||
 | 
					        if (currentEntry === lastEntry) {
 | 
				
			||||||
 | 
					          lastEntry = stateHistory.pop();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (lastEntry !== undefined) {
 | 
				
			||||||
 | 
					          restoreHistoryEntry(lastEntry);
 | 
				
			||||||
 | 
					          redoStack.push(currentEntry);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      this.forceUpdate();
 | 
					      this.forceUpdate();
 | 
				
			||||||
      event.preventDefault();
 | 
					      event.preventDefault();
 | 
				
			||||||
@@ -2046,6 +2060,7 @@ class App extends React.Component<{}, AppState> {
 | 
				
			|||||||
    save(this.state);
 | 
					    save(this.state);
 | 
				
			||||||
    if (!skipHistory) {
 | 
					    if (!skipHistory) {
 | 
				
			||||||
      pushHistoryEntry(generateHistoryCurrentEntry());
 | 
					      pushHistoryEntry(generateHistoryCurrentEntry());
 | 
				
			||||||
 | 
					      redoStack.splice(0, redoStack.length);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    skipHistory = false;
 | 
					    skipHistory = false;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user