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/text editor/drawing canvas: fix an issue
Adorian Doran57cb6e6d68bb
diff --git a/.claude/skills/developing-content-embeds/SKILL.md b/.claude/skills/developing-content-embeds/SKILL.mdindex 200a40fd41..bca6cd5781 100644--- a/.claude/skills/developing-content-embeds/SKILL.md+++ b/.claude/skills/developing-content-embeds/SKILL.md@@ -171,8 +171,9 @@ changes. Three layers, with one recorder per editor in the middle:replacement button copies Excalidraw's `ToolButton` markup. Without a mounted Excalidraw, a stepreads the drawing through the `DrawingStore` it attached with (the unsaved content, else theblob, so a save made elsewhere since stays), patches it and writes it back; the steps queue one- after another. A mount that loaded before such a step (an undone Editable toggle remounts) reads- it again at attach. Undo sorts by fractional index when a step sets `index`,+ after another and count in `revision` once written. They also patch an Excalidraw that attached+ during the read (patches are idempotent), and a mount that loaded before a written step (an+ undone Editable toggle remounts) reads the drawing again at attach. Undo sorts by fractional index when a step sets `index`,as Excalidraw renumbers an out-of-order array instead of reordering it.`excalidraw_contract.spec.ts` pins the Excalidraw internals this relies on.- **`EmbedHistories`** (`text/embed_history.ts`, one per `EditableText`) gives a `ContentHistory`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 c9ddda119d..b2524da1d9 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@@ -139,6 +139,73 @@ describe("DrawingHistory", () => {expect(store.write).toHaveBeenCalledOnce();});+ it("shows a step that saves while an Excalidraw loads in that Excalidraw", async () => {+ const { host, steps } = createHost();+ const history = new DrawingHistory(host);+ const moved = { ...RECTANGLE, x: 10, version: 2 };+ const store = createStore([ RECTANGLE ]);+ const first = createDrawing([ RECTANGLE ]);+ const firstMount = history.mount();+ firstMount.attach(first.api, first.root, store);+ first.input("pointerdown");+ first.setElements([ moved ]);+ firstMount.record();+ firstMount.detach();+ store.setElements([ moved ]);++ // Attached while the step reads the drawing.+ const read = deferScene();+ store.read.mockImplementationOnce(() => read.promise);+ steps[0].undo();+ const nextMount = history.mount();+ const next = createDrawing([ moved ]);+ nextMount.attach(next.api, next.root, store);+ read.resolve({ elements: [ moved ], appState: {}, files: {} });+ await vi.waitFor(() => expect(next.getElements()).toMatchObject([ { x: 0 } ]));+ expect(store.getElements()).toMatchObject([ { x: 0 } ]);+ nextMount.detach();++ // Loaded while the step reads the drawing, attached once it saved.+ const nextRead = deferScene();+ store.read.mockImplementationOnce(() => nextRead.promise);+ steps[0].redo();+ const laterMount = history.mount();+ nextRead.resolve({ elements: next.getElements(), appState: {}, files: {} });+ await vi.waitFor(() => expect(store.write).toHaveBeenCalledTimes(2));+ const later = createDrawing(next.getElements());+ laterMount.attach(later.api, later.root, store);+ await vi.waitFor(() => expect(later.getElements()).toMatchObject([ { x: 10 } ]));+ });++ it("shows the steps saved since an Excalidraw loaded, also while another one saves", async () => {+ const { host, steps } = createHost();+ const history = new DrawingHistory(host);+ const shape = { ...RECTANGLE, y: 0 };+ const store = createStore([ shape ]);+ const first = createDrawing([ shape ]);+ const firstMount = history.mount();+ firstMount.attach(first.api, first.root, store);+ first.input("pointerdown");+ first.setElements([ { ...shape, x: 10, version: 2 } ]);+ firstMount.record();+ first.input("pointerdown");+ first.setElements([ { ...shape, x: 10, y: 5, version: 3 } ]);+ firstMount.record();+ firstMount.detach();+ store.setElements(first.getElements());++ const nextMount = history.mount();+ steps[1].undo();+ await vi.waitFor(() => expect(store.write).toHaveBeenCalledOnce());+ const read = deferScene();+ store.read.mockImplementationOnce(() => read.promise);+ steps[0].undo();+ const next = createDrawing(first.getElements());+ nextMount.attach(next.api, next.root, store);+ read.resolve({ elements: store.getElements(), appState: {}, files: {} });+ await vi.waitFor(() => expect(next.getElements()).toMatchObject([ { x: 0, y: 0 } ]));+ });+it("undoes and redoes the note with the keys and the buttons of Excalidraw", () => {const { host, setEnabled } = createHost();const drawing = createDrawing([]);@@ -264,6 +331,15 @@ function createHost() {};}+/** A read of the saved drawing, which the spec resolves. */+function deferScene() {+ let resolve: (scene: DrawingScene) => void = () => {};+ const promise = new Promise<DrawingScene>((done) => {+ resolve = done;+ });+ return { promise, resolve };+}+/** The saved drawing, which shows `elements`. */function createStore(elements: ExcalidrawElement[]) {let saved: DrawingScene = { elements, appState: {}, files: {} };diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsxindex 1ad332d8a9..d744c13c84 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx@@ -92,7 +92,10 @@ export const HISTORY_ICON_PATHS: Record<HistoryCommand, string> = {* drawing, or the drawing as saved, through the last `DrawingStore`, while none does.*/export default class DrawingHistory {- /** Counts the steps applied, so that an Excalidraw that loaded before one shows it. */+ /**+ * Counts the steps applied, as they reach the drawing, so that an Excalidraw that loaded+ * before one shows it.+ */private revision = 0;private readonly drawings = new Set<AttachedDrawing>();private store: DrawingStore | undefined;@@ -174,8 +177,8 @@ export default class DrawingHistory {/** Shows the drawing of `store` in `drawing` once the steps applied through it saved. */private showStoredScene(drawing: AttachedDrawing, store: DrawingStore) {- const revision = this.revision;this.queueStoreUpdate(async () => {+ const revision = this.revision;const scene = await store.read();if (revision === this.revision && this.drawings.has(drawing)) {showScene(drawing, scene);@@ -227,9 +230,7 @@ export default class DrawingHistory {private apply(step: DrawingStep, isUndo: boolean) {const [ first ] = this.drawings;if (first) {- for (const drawing of this.drawings) {- applyToDrawing(drawing, step, isUndo);- }+ this.applyToDrawings(step, isUndo);// Deferred until after the Undo button of the toolbar focuses the text editor.queueMicrotask(() => {first.root?.querySelector<HTMLElement>(".excalidraw-container")?.focus();@@ -239,13 +240,20 @@ export default class DrawingHistory {const { store } = this;this.queueStoreUpdate(async () => {store.write(applyToScene(await store.read(), step, isUndo));+ // An Excalidraw that attached meanwhile loaded the drawing before the step.+ this.applyToDrawings(step, isUndo);});} else {return false;}+ return true;+ }+ private applyToDrawings(step: DrawingStep, isUndo: boolean) {+ for (const drawing of this.drawings) {+ applyToDrawing(drawing, step, isUndo);+ }this.revision++;- return true;}}