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/canvas: fix some issues
Adorian Doran4bc5b8cdfe51
diff --git a/apps/client/src/widgets/type_widgets/canvas/Canvas.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/Canvas.spec.tsxindex 7ee574433a..af6e5082da 100644--- a/apps/client/src/widgets/type_widgets/canvas/Canvas.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/Canvas.spec.tsx@@ -7,7 +7,11 @@ const excalidrawProps = vi.fn((_props: ExcalidrawProps) => undefined);vi.mock("@excalidraw/excalidraw", () => ({Excalidraw: (props: ExcalidrawProps) => {excalidrawProps(props);- return <div className="mock-excalidraw">{props.children as ComponentChildren}</div>;+ return (+ <div className="mock-excalidraw excalidraw-container">+ {props.children as ComponentChildren}+ </div>+ );}}));vi.mock("../../react/hooks", async (importOriginal) => ({@@ -21,11 +25,18 @@ describe("CanvasEditor", () => {let container: HTMLElement;let refresh: ReturnType<typeof vi.fn>;let apiRef: RefObject<ExcalidrawImperativeAPI>;+ /** The position of the container as Excalidraw last read it; happy-dom places it at 0, 0. */+ let offsets: { offsetLeft: number; offsetTop: number };beforeEach(() => {excalidrawProps.mockClear();- refresh = vi.fn();- apiRef = { current: { refresh } as unknown as ExcalidrawImperativeAPI };+ offsets = { offsetLeft: 0, offsetTop: 0 };+ refresh = vi.fn(() => {+ offsets = { offsetLeft: 0, offsetTop: 0 };+ });+ apiRef = {+ current: { refresh, getAppState: () => offsets } as unknown as ExcalidrawImperativeAPI+ };container = document.createElement("div");document.body.appendChild(container);});@@ -61,15 +72,22 @@ describe("CanvasEditor", () => {}}- it("reads its position again before pointer input when it is embedded in a note", async () => {- point(await mount(true));+ it("reads its position again before pointer input once the drawing moved", async () => {+ const excalidraw = await mount(true);+ point(excalidraw);+ expect(refresh).not.toHaveBeenCalled();- expect(refresh).toHaveBeenCalledTimes(2);+ // The note scrolled, so the position that Excalidraw read last is out of date.+ offsets = { offsetLeft: 0, offsetTop: 120 };+ point(excalidraw);+ expect(refresh).toHaveBeenCalledTimes(1);expect(excalidrawProps).toHaveBeenLastCalledWith(expect.objectContaining({ detectScroll: true }));});it("leaves the position to Excalidraw in a canvas note, which does not move", async () => {- point(await mount());+ const excalidraw = await mount();+ offsets = { offsetLeft: 0, offsetTop: 120 };+ point(excalidraw);expect(refresh).not.toHaveBeenCalled();expect(excalidrawProps).toHaveBeenLastCalledWith(expect.objectContaining({ detectScroll: false }));diff --git a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsxindex 23807dfd5d..0f89b43acf 100644--- a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx@@ -59,8 +59,19 @@ export function CanvasEditor({const noteDrop = useCanvasNoteDrop(apiRef, isReadOnly);// Excalidraw maps the pointer with the container position it read last. An embedded editor- // moves with the note, so it reads the position again before the pointer acts.- const refreshPosition = useCallback(() => apiRef.current?.refresh(), [ apiRef ]);+ // moves with the note, so it reads the position again before the pointer acts. `refresh()`+ // renders all of Excalidraw, so it runs only once the position changed.+ const refreshPosition = useCallback((e: PointerEvent) => {+ const api = apiRef.current;+ const container = (e.currentTarget as HTMLElement).querySelector(".excalidraw-container");+ if (!api || !container) return;++ const { left, top } = container.getBoundingClientRect();+ const { offsetLeft, offsetTop } = api.getAppState();+ if (left !== offsetLeft || top !== offsetTop) {+ api.refresh();+ }+ }, [ apiRef ]);/** Use excalidraw's native zoom instead of the global zoom. */const onWheel = useCallback((e: MouseEvent) => {diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.cssindex f297397214..15cffc4cf3 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css@@ -94,9 +94,10 @@width: 100% !important;}-/* The tools, undo and redo, and zoom are buttons of the embed's toolbar, which fullscreen hides.- `CanvasTools` clicks the hidden undo and redo buttons. */-.include-note-content:not(:fullscreen) .canvas-drawing .excalidraw {+/* While `CanvasEmbedTools` sets `data-embed-tools`, the tools, undo and redo, and zoom are buttons+ of the embed's toolbar, which fullscreen hides. `CanvasTools` clicks the hidden undo and redo+ buttons. */+.include-note-content:not(:fullscreen) .canvas-drawing-editor[data-embed-tools] .excalidraw {.App-toolbar-container > .App-toolbar,.mobile-misc-tools-container > label:has([data-testid="toolbar-lock"], [data-testid="toolbar-hand"]diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsxindex 16b74b2b3a..3dee6971de 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx@@ -78,7 +78,7 @@ export async function renderCanvasDrawingPicture(entity: FNote | FAttachment) {/*** Moves the focus into Excalidraw when the embed box around the drawing holds it. Excalidraw- * renders its container only once its language loads, after the box may have taken the focus.+ * renders its container only once its language loads, which can be after the box took the focus.*/function useFocusFromEmbedBox(rootRef: RefObject<HTMLElement>) {useEffect(() => {diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsxindex b74a0f0fb2..d90a639ef5 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx@@ -10,7 +10,9 @@ vi.mock("@excalidraw/excalidraw", () => ({}));const { default: CanvasEmbedTools } = await import("./CanvasEmbedTools");-const { getContentEmbedTools } = await import("../text/content_embed_tools");+const {+ getContentEmbedTools, watchContentEmbedTools+} = await import("../text/content_embed_tools");const MAIN_TOOL_IDS = ["hand", "rectangle", "diamond", "ellipse", "arrow", "line", "freedraw", "text", "image"@@ -110,6 +112,27 @@ describe("CanvasEmbedTools", () => {expect(provider.getTools()).toEqual([]);});+ it("tells the editor around the drawing, and marks the drawing, about its tools", async () => {+ const inside = vi.fn();+ const outside = vi.fn();+ const stopInside = watchContentEmbedTools(container, inside);+ const stopOutside = watchContentEmbedTools(document.createElement("div"), outside);++ const { embed } = await mount();+ const root = embed.querySelector(":scope > div");+ expect(inside).toHaveBeenCalledTimes(1);+ expect(outside).not.toHaveBeenCalled();+ expect(root?.hasAttribute("data-embed-tools")).toBe(true);++ await act(async () => render(null, container));+ expect(root?.hasAttribute("data-embed-tools")).toBe(false);++ stopInside();+ stopOutside();+ await mount();+ expect(inside).toHaveBeenCalledTimes(1);+ });+it("notifies its listeners when a tool, the zoom or the history changes", async () => {const { provider } = await mount();const listener = vi.fn();diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsxindex 50a1fc7be5..c83ecef639 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx@@ -71,7 +71,10 @@ export default function CanvasEmbedTools({ rootRef, apiRef }: CanvasEmbedToolsPrconst disconnect = tools.connect(api);const unregister = registerContentEmbedTools(root, tools);+ // `CanvasDrawing.css` hides Excalidraw's own controls while these replace them.+ root.setAttribute("data-embed-tools", "");return () => {+ root.removeAttribute("data-embed-tools");unregister();disconnect();};diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsxindex 434612cd97..8cab928c3a 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx@@ -15,10 +15,12 @@ import { type RefObject, render } from "preact";import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import type FAttachment from "../../../entities/fattachment";import type FNote from "../../../entities/fnote";+import type { AttachmentEditor } from "../../../services/content_renderer";import server from "../../../services/server";import { buildNote } from "../../../test/easy-froca";-import useCanvasPersistence from "./persistence";+import useCanvasPersistence, { useCanvasDrawingPersistence } from "./persistence";interface FakeElement {id: string;@@ -201,3 +203,145 @@ describe("useCanvasPersistence content loading (#10279)", () => {expect(loadedElementIds(updateScene)).toEqual([ "b1" ]);});});++describe("useCanvasDrawingPersistence", () => {+ const IMAGE = { id: "f1", dataURL: "data:image/png;base64,AA==", mimeType: "image/png" };+ const UNUSED_IMAGE = { ...IMAGE, id: "f2" };+ let container: HTMLElement;+ let drawingProps: ReturnType<typeof useCanvasDrawingPersistence> | undefined;+ let sceneElements: FakeElement[];+ let appState: Record<string, unknown>;++ function DrawingProbe({ attachment, editor, apiRef }: {+ attachment: FAttachment;+ editor: AttachmentEditor | undefined;+ apiRef: RefObject<ExcalidrawImperativeAPI>;+ }) {+ drawingProps = useCanvasDrawingPersistence(attachment, editor, apiRef, "light");+ return null;+ }++ beforeEach(() => {+ drawingProps = undefined;+ sceneElements = [];+ appState = {+ scrollX: 1, scrollY: 2, zoom: { value: 1 }, gridModeEnabled: false,+ viewBackgroundColor: "transparent", theme: "light"+ };+ container = document.createElement("div");+ document.body.appendChild(container);+ });++ afterEach(() => {+ render(null, container);+ container.remove();+ });++ function buildAttachment(content: object) {+ const getBlob = vi.fn(async () => ({ content: JSON.stringify(content) }));+ return { attachment: { attachmentId: "a1", getBlob } as unknown as FAttachment, getBlob };+ }++ function buildEditor(unsavedContent?: string) {+ return {+ canEdit: () => true,+ getUnsavedContent: vi.fn(() => unsavedContent),+ scheduleSave: vi.fn(),+ release: vi.fn()+ } satisfies AttachmentEditor;+ }++ async function mount(attachment: FAttachment, editor: AttachmentEditor | undefined) {+ const api = {+ getSceneElements: () => sceneElements,+ getAppState: () => appState,+ getFiles: () => ({ f1: IMAGE, f2: UNUSED_IMAGE })+ } as unknown as ExcalidrawImperativeAPI;+ const apiRef = { current: api } as RefObject<ExcalidrawImperativeAPI>;+ await act(async () => {+ render(+ <DrawingProbe attachment={attachment} editor={editor} apiRef={apiRef} />,+ container+ );+ });+ return await (drawingProps?.initialData as Promise<ExcalidrawInitialDataState>);+ }++ function change() {+ drawingProps?.onChange?.([], appState as never, {});+ }++ it("loads the drawing with its images, over the note unless it has a background", async () => {+ const elements = [ { id: "r1", type: "rectangle", version: 3 } ];+ const { attachment } = buildAttachment({ elements, files: { f1: IMAGE }, appState: {} });+ expect(await mount(attachment, buildEditor())).toEqual({+ elements,+ appState: { viewBackgroundColor: "transparent", theme: "light" },+ files: { f1: IMAGE }+ });++ render(null, container);+ const colored = buildAttachment({ elements, appState: { viewBackgroundColor: "#ffc9c9" } });+ expect((await mount(colored.attachment, buildEditor())).appState)+ .toEqual({ viewBackgroundColor: "#ffc9c9", theme: "light" });+ });++ it("loads the content that the note has not saved yet over the saved one", async () => {+ const { attachment, getBlob } = buildAttachment({ elements: [] });+ const unsaved = JSON.stringify({ elements: [ { id: "u1", type: "line", version: 1 } ] });++ const initialData = await mount(attachment, buildEditor(unsaved));+ expect(initialData.elements?.map((element) => element.id)).toEqual([ "u1" ]);+ expect(getBlob).not.toHaveBeenCalled();+ });++ it("saves a change to the scene or to its background, with the images it uses", async () => {+ const loaded = [+ { id: "r1", type: "rectangle", version: 3 },+ { id: "i1", type: "image", version: 1, fileId: "f1" }+ ];+ const { attachment } = buildAttachment({ elements: loaded, files: { f1: IMAGE } });+ const editor = buildEditor();+ await mount(attachment, editor);++ // Excalidraw reports its empty scene, then the loaded one, before any edit.+ change();+ sceneElements = loaded;+ change();+ expect(editor.scheduleSave).not.toHaveBeenCalled();++ sceneElements = [ { ...loaded[0], version: 4 }, loaded[1] ];+ change();+ expect(editor.scheduleSave).toHaveBeenCalledTimes(1);+ const [ savedAttachment, getContent ] = editor.scheduleSave.mock.calls[0];+ expect(savedAttachment).toBe(attachment);+ expect(JSON.parse(getContent())).toEqual({+ type: "excalidraw",+ version: 2,+ elements: sceneElements,+ files: { f1: IMAGE },+ appState: {+ scrollX: 1, scrollY: 2, zoom: { value: 1 }, gridModeEnabled: false,+ viewBackgroundColor: "transparent"+ }+ });++ appState = { ...appState, viewBackgroundColor: "#ffc9c9" };+ change();+ change();+ expect(editor.scheduleSave).toHaveBeenCalledTimes(2);++ render(null, container);+ expect(editor.release).toHaveBeenCalledWith("a1");+ });++ it("saves nothing without an editor", async () => {+ const loaded = [ { id: "r1", type: "rectangle", version: 3 } ];+ const { attachment } = buildAttachment({ elements: loaded });+ const initialData = await mount(attachment, undefined);+ expect(initialData.elements).toEqual(loaded);++ sceneElements = [ { ...loaded[0], version: 9 } ];+ expect(() => change()).not.toThrow();+ });+});diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsxindex 1f43bd5f97..3a871a28a1 100644--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx@@ -364,6 +364,27 @@ describe("ContentEmbed focused on mount", () => {expect(document.activeElement).toBe(contentBox());});+ it("leaves the focus where it went when it left the editor before the box mounted", () => {+ const editable = element(`<div class="ck-editor__editable" tabindex="0"></div>`);+ const field = element(`<input>`);+ document.body.append(editable, field);+ editable.append(container);++ field.focus();+ const firstContent = element(`<div><div tabindex="0"></div></div>`);+ renderBox({ content: firstContent, isFocusedOnMount: true });+ expect(document.activeElement).toBe(field);++ act(() => render(null, container));+ editable.focus();+ const content = element(`<div><div tabindex="0"></div></div>`);+ renderBox({ content, isFocusedOnMount: true });+ expect(document.activeElement).toBe(content.querySelector("[tabindex]"));++ editable.remove();+ field.remove();+ });+it("leaves the focus where it is by default", () => {const content = element(`<div class="rendered-content"><div tabindex="0"></div></div>`);renderBox({ content });diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsxindex 1230eac4f4..6b07b134d8 100644--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx@@ -64,7 +64,9 @@ export default function ContentEmbed({}, [ content ]);useLayoutEffect(() => {- if (isFocusedOnMount) {+ // The box mounts after an upload, by which time the focus can have left the editor.+ const editable = contentRef.current?.closest(".ck-editor__editable");+ if (isFocusedOnMount && (!editable || editable.contains(document.activeElement))) {(content.querySelector<HTMLElement>(FOCUSABLE_SELECTOR) ?? contentRef.current)?.focus();}}, []);diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex 0b67d686c4..a0afe5d725 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -33,7 +33,7 @@ import { setEditorNoteId } from "../../react/NoteStore";import { TypeWidgetProps } from "../type_widget";import AttachmentSaves from "./attachment_saves";import CKEditorWithWatchdog, { CKEditorApi, NotificationEventData, NotificationEventInfo } from "./CKEditorWithWatchdog";-import { getContentEmbedTools } from "./content_embed_tools";+import { getContentEmbedTools, watchContentEmbedTools } from "./content_embed_tools";import getTemplates, { updateTemplateCache } from "./snippets.js";import linkEmbedService from "../../../services/link_embed";import { usesClassicToolbar } from "./toolbar";@@ -317,6 +317,14 @@ export default function EditableText({ note, parentComponent, ntxId, noteContextuseEffect(() => () => stopWatchingEmbedsRef.current?.(), []);+ // The toolbar of an embed reads the buttons of its content when the editor updates, so+ // content that adds them after its embed was selected has the editor update again.+ useEffect(() => {+ const container = containerRef.current;+ if (!container) return;+ return watchContentEmbedTools(container, () => watchdogRef.current?.editor?.ui.update());+ }, []);+useTriliumEvent("entitiesReloaded", ({ loadResults }) => {const editor = watchdogRef.current?.editor as CKTextEditor | null | undefined;if (editor && note) {diff --git a/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts b/apps/client/src/widgets/type_widgets/text/content_embed_tools.tsindex 38b9a2a65b..f752358f27 100644--- a/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts+++ b/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts@@ -26,6 +26,7 @@ export interface ContentEmbedToolProvider {}const providers = new Map<HTMLElement, ContentEmbedToolProvider>();+const watchers = new Set<{ container: HTMLElement; callback: () => void }>();/*** Adds the buttons of `provider` to the toolbar of the embed that contains `element`, until the@@ -36,11 +37,28 @@ export function registerContentEmbedTools(provider: ContentEmbedToolProvider) {providers.set(element, provider);+ for (const { container, callback } of watchers) {+ if (container.contains(element)) {+ callback();+ }+ }return () => {providers.delete(element);};}+/**+ * Calls `callback` when content inside `container` adds its buttons, as a canvas drawing does+ * once it renders after its embed was selected. Stops when the returned function is called.+ */+export function watchContentEmbedTools(container: HTMLElement, callback: () => void) {+ const watcher = { container, callback };+ watchers.add(watcher);+ return () => {+ watchers.delete(watcher);+ };+}+/** The buttons that the content of `embed` adds to its toolbar, or `null`. */export function getContentEmbedTools(embed: HTMLElement) {for (const [ element, provider ] of providers) {