undo-manager.js 815 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. export default class UndoManager {
  2. constructor() {
  3. this.actionStack = []
  4. this.undoneStack = []
  5. }
  6. pushAction(action) {
  7. this.undoneStack = []
  8. this.actionStack.push(action)
  9. action.activate()
  10. }
  11. undoLastAction() {
  12. if (this.actionStack.length === 0) {
  13. return
  14. }
  15. const action = this.actionStack.pop()
  16. this.undoneStack.push(action)
  17. action.undo()
  18. }
  19. redoLastUndoneAction() {
  20. if (this.undoneStack.length === 0) {
  21. return
  22. }
  23. const action = this.undoneStack.pop()
  24. this.actionStack.push(action)
  25. action.activate()
  26. }
  27. get safeToPushAction() {
  28. // Is it safe to push a new action? That is, since pushing a new action
  29. // clears the undone actions stack, will any undone actions be lost?
  30. return this.undoStack.length === 0
  31. }
  32. }