history.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { AppState } from "./types";
  2. import { ExcalidrawElement } from "./element/types";
  3. import { clearAppStatePropertiesForHistory } from "./appState";
  4. import { newElementWith } from "./element/mutateElement";
  5. import { isLinearElement } from "./element/typeChecks";
  6. type Result = {
  7. appState: AppState;
  8. elements: ExcalidrawElement[];
  9. };
  10. export class SceneHistory {
  11. private recording: boolean = true;
  12. private stateHistory: string[] = [];
  13. private redoStack: string[] = [];
  14. clear() {
  15. this.stateHistory.length = 0;
  16. this.redoStack.length = 0;
  17. }
  18. private generateEntry(
  19. appState: AppState,
  20. elements: readonly ExcalidrawElement[],
  21. ) {
  22. return JSON.stringify({
  23. appState: clearAppStatePropertiesForHistory(appState),
  24. elements: elements.reduce((elements, element) => {
  25. if (
  26. isLinearElement(element) &&
  27. appState.multiElement &&
  28. appState.multiElement.id === element.id
  29. ) {
  30. // don't store multi-point arrow if still has only one point
  31. if (
  32. appState.multiElement &&
  33. appState.multiElement.id === element.id &&
  34. element.points.length < 2
  35. ) {
  36. return elements;
  37. }
  38. elements.push(
  39. newElementWith(element, {
  40. // don't store last point if not committed
  41. points:
  42. element.lastCommittedPoint !==
  43. element.points[element.points.length - 1]
  44. ? element.points.slice(0, -1)
  45. : element.points,
  46. // don't regenerate versionNonce else this will short-circuit our
  47. // bail-on-no-change logic in pushEntry()
  48. versionNonce: element.versionNonce,
  49. }),
  50. );
  51. } else {
  52. elements.push(
  53. newElementWith(element, { versionNonce: element.versionNonce }),
  54. );
  55. }
  56. return elements;
  57. }, [] as Mutable<typeof elements>),
  58. });
  59. }
  60. pushEntry(appState: AppState, elements: readonly ExcalidrawElement[]) {
  61. const newEntry = this.generateEntry(appState, elements);
  62. if (
  63. this.stateHistory.length > 0 &&
  64. this.stateHistory[this.stateHistory.length - 1] === newEntry
  65. ) {
  66. // If the last entry is the same as this one, ignore it
  67. return;
  68. }
  69. this.stateHistory.push(newEntry);
  70. // As a new entry was pushed, we invalidate the redo stack
  71. this.clearRedoStack();
  72. }
  73. restoreEntry(entry: string) {
  74. try {
  75. return JSON.parse(entry);
  76. } catch {
  77. return null;
  78. }
  79. }
  80. clearRedoStack() {
  81. this.redoStack.splice(0, this.redoStack.length);
  82. }
  83. redoOnce(): Result | null {
  84. if (this.redoStack.length === 0) {
  85. return null;
  86. }
  87. const entryToRestore = this.redoStack.pop();
  88. if (entryToRestore !== undefined) {
  89. this.stateHistory.push(entryToRestore);
  90. return this.restoreEntry(entryToRestore);
  91. }
  92. return null;
  93. }
  94. undoOnce(): Result | null {
  95. if (this.stateHistory.length === 1) {
  96. return null;
  97. }
  98. const currentEntry = this.stateHistory.pop();
  99. const entryToRestore = this.stateHistory[this.stateHistory.length - 1];
  100. if (currentEntry !== undefined) {
  101. this.redoStack.push(currentEntry);
  102. return this.restoreEntry(entryToRestore);
  103. }
  104. return null;
  105. }
  106. // Suspicious that this is called so many places. Seems error-prone.
  107. resumeRecording() {
  108. this.recording = true;
  109. }
  110. record(state: AppState, elements: readonly ExcalidrawElement[]) {
  111. if (this.recording) {
  112. this.pushEntry(state, elements);
  113. this.recording = false;
  114. }
  115. }
  116. }
  117. export const createHistory: () => { history: SceneHistory } = () => {
  118. const history = new SceneHistory();
  119. return { history };
  120. };