Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/canvas: improve undo history performance
Adorian Doranf4f719f06706
diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.spec.tsxindex 432fa7b0a0..666494d85a 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.spec.tsx@@ -75,6 +75,31 @@ describe("DrawingHistory", () => {expect(drawing.appState.viewBackgroundColor).toBe("#ffc9c9");});+ it("compares the elements only to record a change, not to attach or show a saved scene", () => {+ const { host, steps } = createHost();+ const drawing = createDrawing([ RECTANGLE, ELLIPSE ]);+ const mount = new DrawingHistory(host).mount();+ // `getChangedKeys()` compares each property as JSON.+ const stringify = vi.spyOn(JSON, "stringify");+ try {+ mount.attach(drawing.api, drawing.root, createStore([]));+ mount.show({+ elements: [ { ...RECTANGLE, x: 30, version: 5 }, ELLIPSE ], appState: {}, files: {}+ });+ expect(stringify).not.toHaveBeenCalled();++ drawing.input("pointerdown");+ drawing.setElements([ { ...RECTANGLE, x: 40, version: 6 }, ELLIPSE ]);+ mount.record();+ expect(stringify).toHaveBeenCalled();+ } finally {+ stringify.mockRestore();+ }+ expect(steps).toHaveLength(1);+ expect(steps[0].undo()).toBe(true);+ expect(drawing.getElements()[0]).toMatchObject({ x: 30 });+ });+it("joins a change that the user did not make to the last step, while it is the last", () => {const { host, steps, setLatest } = createHost();const drawing = createDrawing([]);diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsxindex 0a7518ad65..e32df60099 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx@@ -221,7 +221,7 @@ export default class DrawingHistory {const appState = api.getAppState();if (isInteracting(appState)) return;- const changes = commitElements(drawing);+ const changes = takeChanges(drawing);const step: DrawingStep = { changes, files: getChangedFiles(api, changes) };if (appState.viewBackgroundColor !== drawing.background) {step.background = [ drawing.background, appState.viewBackgroundColor ];@@ -360,20 +360,32 @@ export function isInteracting(appState: AppState) {}/** Takes the elements that changed since the last call as recorded, and returns their changes. */-function commitElements(drawing: AttachedDrawing) {+function takeChanges(drawing: AttachedDrawing) {const changes = new Map<string, ElementChange>();+ for (const [ before, after ] of commitElements(drawing)) {+ const keys = getChangedKeys(before, after);+ if (keys.length) {+ changes.set(after.id, { before, after, keys });+ }+ }+ return changes;+}++/**+ * Takes the elements that changed since the last call as recorded, and returns each with the+ * element that it replaces, if any.+ */+function commitElements(drawing: AttachedDrawing) {+ const committed: [ ExcalidrawElement | undefined, ExcalidrawElement ][] = [];for (const element of drawing.api.getSceneElementsIncludingDeleted()) {const before = drawing.committed.get(element.id);if (before?.version === element.version) continue;const after = structuredClone(element);drawing.committed.set(element.id, after);- const keys = getChangedKeys(before, after);- if (keys.length) {- changes.set(element.id, { before, after, keys });- }+ committed.push([ before, after ]);}- return changes;+ return committed;}/** Adds the changes of `next`, which follow those of `step`, to `step`. */