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 some issues
Adorian Doranbfbaa0dbfa9e
diff --git a/.claude/skills/developing-content-embeds/SKILL.md b/.claude/skills/developing-content-embeds/SKILL.mdindex eb50be3c65..64e9111f87 100644--- a/.claude/skills/developing-content-embeds/SKILL.md+++ b/.claude/skills/developing-content-embeds/SKILL.md@@ -171,6 +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 steppatches the scene the last one left and saves it through the `write` it attached with; a mountthat loaded before such a step (an undone Editable toggle remounts) gets the scene at attach.+ `DrawingPicture` passes each content it loads to `load()`, so a step patches what was saved+ elsewhere since, not the old scene. 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`(`host` + `write`) per note ID, attachment ID or `InlineEmbedSource`, as `ContentEditor.history`.diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsxindex 83b45a64f1..6827b011d7 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx@@ -23,6 +23,7 @@ import { useEditableEmbed } from "../text/editable_embed";import { CanvasEditor } from "./Canvas";import CanvasDrawingMenu from "./CanvasDrawingMenu";import CanvasEmbedTools, { CanvasTools } from "./CanvasEmbedTools";+import type { DrawingScene } from "./drawing_history";import { type DrawingView, getBoundedView, getCenteredView, getFittedView } from "./drawing_view";import {exportDrawingPicture, loadDrawingFiles, parseContent, useCanvasDrawingPersistence@@ -203,12 +204,14 @@ function DrawingPicture({let isCurrent = true;// Starts on the next frame, so that a drawing whose embed turns out editable renders none.const frame = requestAnimationFrame(async () => {+ const loadHistoryScene = editor?.history?.drawing?.load();const content = editor?.getUnsavedContent()?? (await attachment.getBlob())?.content ?? "";const key = `${colorScheme}\n${content}`;if (key === renderedKeyRef.current) return;const rendered = await renderPictureScene(attachment, content, colorScheme === "dark");+ loadHistoryScene?.(rendered.drawing);if (isCurrent) {renderedKeyRef.current = key;setScene(rendered);@@ -266,6 +269,8 @@ interface ScenePicture {}interface PictureScene {+ /** The drawing that the content saves. */+ drawing: DrawingScene;/** The picture of the content, or `null` for an empty drawing. */picture: ScenePicture | null;/** The shapes that are not deleted. */@@ -290,14 +295,16 @@ async function renderPictureScene(const background = viewBackgroundColor && viewBackgroundColor !== "transparent"? viewBackgroundColor: null;+ const files = (await loadDrawingFiles(attachment, scene)).files;+ const drawing = { elements: scene.elements ?? [], appState: scene.appState ?? {}, files };if (!elements.length) {- return { picture: null, elements, savedView, background };+ return { drawing, picture: null, elements, savedView, background };}const svg = await exportToSvg({elements,appState: { ...scene.appState, exportBackground: false, exportWithDarkMode: isDark },- files: (await loadDrawingFiles(attachment, scene)).files,+ files,exportPadding: 0});const width = Number(svg.getAttribute("width"));@@ -306,6 +313,7 @@ async function renderPictureScene(// above the frames, so it is placed by the bottom right corner of the shapes.const [ , , maxX, maxY ] = getCommonBounds(getRootElements(elements));return {+ drawing,picture: { svg, x: maxX - width, y: maxY - height, width, height },elements,savedView,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 71256c8ad6..5bb9ccef81 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@@ -135,6 +135,32 @@ describe("DrawingHistory", () => {expect(write).toHaveBeenCalledOnce();});+ it("applies a step to the drawing as saved elsewhere while no Excalidraw shows it", () => {+ const { host, steps } = createHost();+ const history = new DrawingHistory(host);+ const write = vi.fn();+ const drawing = createDrawing([ RECTANGLE ]);+ const mount = history.mount();+ mount.attach(drawing.api, drawing.root, write);+ drawing.input("pointerdown");+ drawing.setElements([ { ...RECTANGLE, x: 10, version: 2 } ]);+ mount.record();+ mount.detach();++ history.load()({+ elements: [ { ...RECTANGLE, x: 10, version: 2 }, ELLIPSE ], appState: {}, files: {}+ });+ steps[0].undo();+ expect(write.mock.calls[0][0].elements).toMatchObject([ { id: "r1", x: 0 }, { id: "e1" } ]);++ // A load that started before the step is older than the drawing the step leaves.+ const staleLoad = history.load();+ steps[0].redo();+ staleLoad({ elements: [ RECTANGLE ], appState: {}, files: {} });+ steps[0].undo();+ expect(write.mock.calls[2][0].elements).toMatchObject([ { id: "r1", x: 0 }, { id: "e1" } ]);+ });+it("undoes and redoes the note with the keys and the buttons of Excalidraw", () => {const { host, setEnabled } = createHost();const drawing = createDrawing([]);@@ -188,6 +214,19 @@ describe("applyChanges", () => {.toEqual([ "r1", "e1", "i1" ]);expect(applyChanges(elements, deleteEllipse, false)).toEqual(elements);});++ it("puts back the z-order of the elements that a change moved", () => {+ const sendToBack = new Map<string, ElementChange>([ [ "e1", {+ before: ELLIPSE,+ after: { ...ELLIPSE, index: "Zz", version: 2 } as ExcalidrawElement,+ keys: [ "index" ]+ } ] ]);+ const sentToBack = [ { ...ELLIPSE, index: "Zz" } as ExcalidrawElement, RECTANGLE, IMAGE ];+ expect(applyChanges(sentToBack, sendToBack, true).map(({ id }) => id))+ .toEqual([ "r1", "e1", "i1" ]);+ expect(applyChanges([ RECTANGLE, ELLIPSE, IMAGE ], sendToBack, false).map(({ id }) => id))+ .toEqual([ "e1", "r1", "i1" ]);+ });});describe("getChangedKeys", () => {diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsxindex 6159f483e5..b37439a4af 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx@@ -14,10 +14,10 @@ export type SavedAppState = Pick<AppState, "scrollX" | "scrollY" | "zoom" | "gridModeEnabled" | "viewBackgroundColor">;-/** A drawing as Excalidraw holds it, deleted elements included. */+/** A drawing as Excalidraw holds it, deleted elements included, or as its content saves it. */export interface DrawingScene {elements: readonly ExcalidrawElement[];- appState: SavedAppState;+ appState: Partial<SavedAppState>;files: BinaryFiles;}@@ -92,7 +92,10 @@ export default class DrawingHistory {/** Counts the steps applied, so that an Excalidraw that loaded before one shows it. */private revision = 0;private readonly drawings = new Set<AttachedDrawing>();- /** The drawing of the last Excalidraw that went away, for the steps applied without one. */+ /**+ * The drawing of the last Excalidraw that went away, or as it saved since, for the steps+ * applied without one.+ */private scene: DrawingScene | undefined;private write: ((scene: DrawingScene) => void) | undefined;private lastStep: RecordedStep | undefined;@@ -117,6 +120,19 @@ export default class DrawingHistory {};}+ /**+ * Takes the drawing as its content saves it, which loads now, for the steps applied while no+ * Excalidraw shows it. A step applied before the returned call ends the load in vain.+ */+ load(): (scene: DrawingScene) => void {+ const revision = this.revision;+ return (scene) => {+ if (revision === this.revision && !this.drawings.size) {+ this.scene = scene;+ }+ };+ }+private attach(api: ExcalidrawImperativeAPI,root: HTMLElement | null,@@ -124,21 +140,22 @@ export default class DrawingHistory {revision: number) {const scene = revision === this.revision ? undefined : this.getLatestScene();+ const background = scene?.appState.viewBackgroundColor+ ?? api.getAppState().viewBackgroundColor;if (scene) {addMissingFiles(api, scene.files);api.updateScene({elements: scene.elements,- appState: { viewBackgroundColor: scene.appState.viewBackgroundColor },+ appState: { viewBackgroundColor: background },captureUpdate: CaptureUpdateAction.NEVER});}- const { viewBackgroundColor } = scene?.appState ?? api.getAppState();const drawing: AttachedDrawing = {api,root,committed: new Map(),- background: viewBackgroundColor,+ background,hasInput: false,stopListening: () => {}};@@ -243,8 +260,9 @@ export function getDrawingHistory(history: ContentHistory) {}/**- * `elements` with the changes undone, or made again. An element that `elements` lacks, as one that- * was deleted before the drawing saved, comes back from the change.+ * `elements` with the changes undone, or made again, in the order of their fractional indices. An+ * element that `elements` lacks, as one that was deleted before the drawing saved, comes back from+ * the change.*/export function applyChanges(elements: readonly ExcalidrawElement[],@@ -265,6 +283,9 @@ export function applyChanges(insertByIndex(result, newElementWith(structuredClone(target), {}, true));}}+ if ([ ...changes.values() ].some(({ keys }) => keys.includes("index"))) {+ sortByIndex(result);+ }return result;}@@ -383,6 +404,19 @@ function insertByIndex(elements: ExcalidrawElement[], element: ExcalidrawElement}}+/**+ * Orders `elements` by their fractional indices, as Excalidraw stacks them, unless one has none.+ * Excalidraw would otherwise give the elements new indices in the order of the array.+ */+function sortByIndex(elements: ExcalidrawElement[]) {+ if (elements.some(({ index }) => index === null)) return;++ elements.sort((first, second) => {+ const [ a, b ] = [ first.index ?? "", second.index ?? "" ];+ return a < b ? -1 : a > b ? 1 : 0;+ });+}+/** The images of the elements in `changes` that `api` has loaded. */function getChangedFiles(api: ExcalidrawImperativeAPI, changes: Map<string, ElementChange>) {const loaded = api.getFiles();diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.ts b/apps/client/src/widgets/type_widgets/canvas/persistence.tsindex 1a0e46f0a5..5c12d42086 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.ts+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.ts@@ -710,7 +710,7 @@ async function getData(api: ExcalidrawImperativeAPI, appStateToCompare: RefObjec/** The scene as the content of a canvas note or of a canvas drawing stores it. */function buildSceneContent(elements: readonly NonDeletedExcalidrawElement[],- appState: SavedAppState,+ appState: Partial<SavedAppState>,files: Record<string, BinaryFileData>) {return {@@ -722,7 +722,8 @@ function buildSceneContent(scrollX: appState.scrollX,scrollY: appState.scrollY,zoom: appState.zoom,- ...getImportantAppState(appState)+ gridModeEnabled: appState.gridModeEnabled,+ viewBackgroundColor: appState.viewBackgroundColor}};}