history.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. return 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. }
  67. pushEntry(appState: AppState, elements: readonly ExcalidrawElement[]) {
  68. const newEntry = this.generateEntry(appState, elements);
  69. if (
  70. this.stateHistory.length > 0 &&
  71. this.stateHistory[this.stateHistory.length - 1] === newEntry
  72. ) {
  73. // If the last entry is the same as this one, ignore it
  74. return;
  75. }
  76. this.stateHistory.push(newEntry);
  77. // As a new entry was pushed, we invalidate the redo stack
  78. this.clearRedoStack();
  79. }
  80. restoreEntry(entry: string) {
  81. try {
  82. return JSON.parse(entry);
  83. } catch {
  84. return null;
  85. }
  86. }
  87. clearRedoStack() {
  88. this.redoStack.splice(0, this.redoStack.length);
  89. }
  90. redoOnce(): Result | null {
  91. if (this.redoStack.length === 0) {
  92. return null;
  93. }
  94. const entryToRestore = this.redoStack.pop();
  95. if (entryToRestore !== undefined) {
  96. this.stateHistory.push(entryToRestore);
  97. return this.restoreEntry(entryToRestore);
  98. }
  99. return null;
  100. }
  101. undoOnce(): Result | null {
  102. if (this.stateHistory.length === 1) {
  103. return null;
  104. }
  105. const currentEntry = this.stateHistory.pop();
  106. const entryToRestore = this.stateHistory[this.stateHistory.length - 1];
  107. if (currentEntry !== undefined) {
  108. this.redoStack.push(currentEntry);
  109. return this.restoreEntry(entryToRestore);
  110. }
  111. return null;
  112. }
  113. // Suspicious that this is called so many places. Seems error-prone.
  114. resumeRecording() {
  115. this.recording = true;
  116. }
  117. record(state: AppState, elements: readonly ExcalidrawElement[]) {
  118. if (this.recording) {
  119. this.pushEntry(state, elements);
  120. this.recording = false;
  121. }
  122. }
  123. }
  124. export const createHistory: () => { history: SceneHistory } = () => {
  125. const history = new SceneHistory();
  126. return { history };
  127. };