history.ts 3.9 KB

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