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: fix reset and lag when two instances of the same Excalidraw canvas/embedded content are displayed in the same time
Adorian Doran0fb2fe44eec1
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex 1448260cc2..3bbcec68c4 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -193,6 +193,12 @@ const PREVIEWED_TYPES = new Set(["llmChat"]);+/**+ * Content types whose interactive rendering reloads itself after each save: `CanvasDrawing`, and+ * `useSavedContent()` in `CodeEmbed` and `MarkdownEmbed`.+ */+const SELF_UPDATING_TYPES = new Set([ "canvasDrawing", "code", "markdown" ]);+/** The box size a new embed of blocks of a note, an excerpt, starts with. */export const EXCERPT_BOX_SIZE: BoxSize = "full";@@ -727,9 +733,9 @@ async function renderSourceEmbed(const $container = $('<div class="source-embed">');const container = $container.get(0);if (container) {- await mountInteractiveWidget(- h(AttachmentSourceEmbed, { entity, editor, content, component }), container- );+ await mountInteractiveWidget(h(AttachmentSourceEmbed, {+ entity, editor, content, component, containerRef: { current: container }+ }), container);}$renderedContent.append($container);}@@ -1166,6 +1172,15 @@ function getBoxSize(type: string, hasPreview: boolean): BoxSize {return "medium";}+/**+ * Whether the interactive rendering of `type`, as `getRenderedContent()` returns it, reloads itself+ * after each save of the note or attachment. Source embeds use `useSavedContent()` too.+ */+function isSelfUpdatingType(type: string) {+ return SELF_UPDATING_TYPES.has(type) || !!getSourceEmbedTypeByContentType(type);+}++/** The type of content that `getRenderedContent()` renders `entity` as. */function getRenderingType(entity: FNote | FAttachment) {const type = getContentType(entity);if (!entity.isProtected) {@@ -1223,6 +1238,8 @@ function getAttachedFileContentType(mime: string, size = 0, fileName?: string) {export default {getRenderedContent,+ getRenderingType,+ isSelfUpdatingType,disposeInteractiveContent,mountInteractiveWidget};diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsxindex c6aee1e8c6..f18f21ae5f 100644--- a/apps/client/src/widgets/react/hooks.spec.tsx+++ b/apps/client/src/widgets/react/hooks.spec.tsx@@ -18,7 +18,8 @@ import {useMediaQuery,useNoteLabelBoolean,useStaticTooltip,- useTooltip+ useTooltip,+ useWhenShown} from "./hooks";/**@@ -698,3 +699,54 @@ describe("useMediaQuery", () => {vi.restoreAllMocks();});});++describe("useWhenShown", () => {+ it("runs at once while shown, otherwise the last callback once the element shows", async () => {+ const observers: { callback: () => void; disconnect: ReturnType<typeof vi.fn> }[] = [];+ const { ResizeObserver } = globalThis;+ globalThis.ResizeObserver = class {+ disconnect = vi.fn();+ constructor(callback: () => void) {+ observers.push({ callback, disconnect: this.disconnect });+ }+ observe() {}+ } as unknown as typeof ResizeObserver;+ let whenShown: ((callback: () => void) => void) | undefined;+ function Consumer() {+ const ref = useRef<HTMLDivElement>(null);+ whenShown = useWhenShown(ref);+ return <div ref={ref} />;+ }+ const host = document.body.appendChild(document.createElement("div"));++ try {+ await act(async () => { render(<Consumer />, host); });+ const runs: string[] = [];+ whenShown?.(() => runs.push("shown"));+ expect(runs).toEqual([ "shown" ]);++ host.style.display = "none";+ whenShown?.(() => runs.push("first"));+ whenShown?.(() => runs.push("last"));+ expect(observers).toHaveLength(1);+ observers[0].callback();+ expect(runs).toEqual([ "shown" ]);++ host.style.display = "";+ observers[0].callback();+ observers[0].callback();+ expect(runs).toEqual([ "shown", "last" ]);+ expect(observers[0].disconnect).toHaveBeenCalled();++ // Unmounted while a callback waits, it stops watching.+ host.style.display = "none";+ whenShown?.(() => runs.push("unmounted"));+ await act(async () => { render(null, host); });+ expect(observers[1].disconnect).toHaveBeenCalled();+ expect(runs).toEqual([ "shown", "last" ]);+ } finally {+ globalThis.ResizeObserver = ResizeObserver;+ host.remove();+ }+ });+});diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 7ea9e7cc0f..c6dd0b9852 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -114,7 +114,10 @@ export interface SavedData {}[];}-export function useEditorSpacedUpdate({ note, noteType, noteContext, getData, onContentChange, dataSaved, updateInterval, block }: {+export function useEditorSpacedUpdate({+ note, noteType, noteContext, getData, onContentChange, dataSaved, updateInterval, block,+ shownRef+}: {noteType: NoteType;note: FNote | null | undefined,noteContext: NoteContext | null | undefined,@@ -128,6 +131,8 @@ export function useEditorSpacedUpdate({ note, noteType, noteContext, getData, on* replaces.*/block?: string;+ /** The element of the editor. Content saved elsewhere loads into it once it is shown. */+ shownRef?: RefObject<Element | null>;}) {const parentComponent = useContext(ParentComponent);const blockRef = useRef(block ?? "");@@ -135,7 +140,8 @@ export function useEditorSpacedUpdate({ note, noteType, noteContext, getData, onreportLoadStateTo: noteContext,load: block === undefined? undefined- : (loadedNote) => loadBlocks(loadedNote, blockRef.current)+ : (loadedNote) => loadBlocks(loadedNote, blockRef.current),+ shownRef});// The note whose content is currently loaded in the editor. Editor instances are reused@@ -837,10 +843,16 @@ export function useNoteBlob<T extends { content: string } = FBlob>(note: FNote |refreshOnShow?: boolean;/** Reads the content instead of the blob of the note, resolving to `null` when it fails. */load?: (note: FNote) => Promise<T | null>;+ /**+ * The element that displays the content. While it is hidden, content saved elsewhere loads at+ * once, and the blob changes once the element is shown (`useWhenShown()`).+ */+ shownRef?: RefObject<Element | null>;}): T | null | undefined {const [ blob, setBlob ] = useState<T | null>();const requestIdRef = useRef(0);const missedContentChangeRef = useRef(false);+ const whenShown = useWhenShown(opts?.shownRef);function reportLoadState(state: "loading" | "loaded" | "error") {if (opts?.isVisible === false) {@@ -849,9 +861,11 @@ export function useNoteBlob<T extends { content: string } = FBlob>(note: FNote |opts?.reportLoadStateTo?.setContextData("contentLoad", { state, retry: () => refresh() });}- async function refresh() {+ /** Loads the blob. With `isSavedElsewhere`, it applies once `shownRef` is shown. */+ async function refresh(isSavedElsewhere = false) {const requestId = ++requestIdRef.current;- if (note) {+ const isWaiting = isSavedElsewhere && !!opts?.shownRef;+ if (note && !isWaiting) {reportLoadState("loading");}const newBlob = note && opts?.load@@ -859,12 +873,19 @@ export function useNoteBlob<T extends { content: string } = FBlob>(note: FNote |: await note?.getBlob() as T | null | undefined;// Only update if this is the latest request.- if (requestId === requestIdRef.current) {+ if (requestId !== requestIdRef.current) return;+ const update = () => {+ if (requestId !== requestIdRef.current) return;setBlob(newBlob);if (note) {// froca.getBlob() resolves to null when the fetch failed.reportLoadState(newBlob ? "loaded" : "error");}+ };+ if (isWaiting) {+ whenShown(update);+ } else {+ update();}}@@ -880,7 +901,7 @@ export function useNoteBlob<T extends { content: string } = FBlob>(note: FNote |}if (loadResults.isNoteContentReloaded(note.noteId, componentId)) {- refresh();+ refresh(true);} else if (opts?.refreshOnShow && loadResults.isNoteContentReloaded(note.noteId)) {// The change came from our own component, so the refetch was skipped; remember it so// the content is brought up to date when the widget is displayed again.@@ -1021,6 +1042,47 @@ export function useElementSize(ref: RefObject<HTMLElement | null>) {return size;}+/**+ * Returns a function that runs a callback at once while the element of `ref` is shown, and+ * otherwise once it is shown again, for example when its tab is selected. While the element is+ * hidden, only the last callback runs. Without an element, or with one that is not in the page+ * yet, the callback runs at once.+ */+export function useWhenShown(ref: RefObject<Element | null> | undefined) {+ const waitingRef =+ useRef<{ callback: () => void; observer: ResizeObserver } | undefined>(undefined);+ useEffect(() => () => waitingRef.current?.observer.disconnect(), []);++ return useCallback((callback: () => void) => {+ const element = ref?.current;+ const waiting = waitingRef.current;+ if (!element || isShown(element)) {+ waiting?.observer.disconnect();+ waitingRef.current = undefined;+ callback();+ } else if (waiting) {+ waiting.callback = callback;+ } else {+ // `display: none` gives the element no size, so showing it resizes it.+ const observer = new ResizeObserver(() => {+ const current = waitingRef.current;+ if (current && isShown(element)) {+ observer.disconnect();+ waitingRef.current = undefined;+ current.callback();+ }+ });+ waitingRef.current = { callback, observer };+ observer.observe(element);+ }+ }, [ ref ]);+}++/** Whether `element` is laid out, or not in the page yet. */+function isShown(element: Element) {+ return !element.isConnected || element.checkVisibility();+}+/*** Whether an element has the screen to itself, and the way to give it or take it back.*diff --git a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx b/apps/client/src/widgets/type_widgets/Attachment.spec.tsxindex ab43f70af8..764cd5368d 100644--- a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.spec.tsx@@ -10,10 +10,9 @@ import { renderInto } from "../../test/render";import { ParentComponent } from "../react/react_utils";const CANVAS_MIME = "application/vnd.excalidraw+json";-const { getRenderedContent, drawingMounts, drawingProps } = vi.hoisted(() => ({+const { getRenderedContent, drawingMounts } = vi.hoisted(() => ({getRenderedContent: vi.fn(async () => ({ $renderedContent: [] })),- drawingMounts: vi.fn(),- drawingProps: vi.fn()+ drawingMounts: vi.fn()}));// Nothing initialises i18next in a unit test, and an uninitialised one answers every lookup with an@@ -35,8 +34,7 @@ vi.mock("../../services/content_renderer", () => ({}));// Counts the mounts of the drawing editor, which loads a change saved elsewhere in place.vi.mock("./canvas/CanvasDrawing", () => ({- CanvasDrawingDetail: (props: { revision?: number }) => {- drawingProps(props);+ CanvasDrawingDetail: () => {useEffect(() => drawingMounts(), []);return <div className="drawing-detail-stub" />;}@@ -131,9 +129,7 @@ describe("AttachmentList", () => {});describe("AttachmentDetail", () => {- it("edits a canvas drawing, loading again only what another component saved", async () => {- const getRevision = () =>- (drawingProps.mock.lastCall?.[0] as { revision?: number }).revision;+ it("edits a canvas drawing in an editor that stays mounted through every save", async () => {const drawing = { ...role("file"), mime: CANVAS_MIME } as FAttachment;vi.spyOn(froca, "getAttachment").mockResolvedValue(drawing);getRenderedContent.mockClear();@@ -154,16 +150,12 @@ describe("AttachmentDetail", () => {expect(container.querySelector(".attachment-canvas-drawing .drawing-detail-stub")).not.toBeNull();expect(getRenderedContent).not.toHaveBeenCalled();- // Its own save, and a change to another attachment, keep the editor as it is.+ // The drawing shows a save made elsewhere in place.await reloadAttachment(component, drawing.attachmentId, component.componentId);- await reloadAttachment(component, "otherAttachment", "otherComponent");- expect(drawingMounts).toHaveBeenCalledOnce();- expect(getRevision()).toBe(0);-- // The detail stays mounted, so that its unsaved changes carry over to the drawing.await reloadAttachment(component, drawing.attachmentId, "otherComponent");- await vi.waitFor(() => expect(getRevision()).toBe(1));+ await reloadAttachment(component, "otherAttachment", "otherComponent");expect(drawingMounts).toHaveBeenCalledOnce();+ expect(getRenderedContent).not.toHaveBeenCalled();});});diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsxindex ece910312d..d275e352dc 100644--- a/apps/client/src/widgets/type_widgets/Attachment.tsx+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx@@ -207,7 +207,6 @@ export function AttachmentDetail({ note, viewScope, noteContext }: TypeWidgetPro}function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, viewScope }: { attachment: FAttachment, isFullDetail?: boolean, ownerNote?: FNote, noteContext?: NoteContext, viewScope?: ViewScope }) {- const parentComponent = useContext(ParentComponent);const contentWrapper = useRef<HTMLDivElement>(null);const imageViewerWrapper = useRef<HTMLDivElement>(null);const [ title, setTitle ] = useState(attachment.title);@@ -219,8 +218,6 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view// Same reason, for the content itself: replacing an attachment changes neither its id nor its title, so// without this nothing here re-renders and the viewer/player would keep showing what it first loaded.const [ modified, setModified ] = useState(attachment.utcDateModified);- // Counts the changes that another component saved, which the drawing editor loads again.- const [ externalRevision, setExternalRevision ] = useState(0);// "importSource" attachments (e.g. OneNote debug source) behave like ordinary files for// preview, OCR and link-copying purposes.const isFileLike = isFileLikeAttachment(attachment);@@ -274,12 +271,9 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view}, [ attachment, rendersItself ]);useTriliumEvent("entitiesReloaded", ({ loadResults }) => {const isThis = (row: AttachmentRow) => row.attachmentId === attachment.attachmentId;- if (!loadResults.getAttachmentRows().some(isThis)) return;-- if (loadResults.getAttachmentRows(parentComponent?.componentId).some(isThis)) {- setExternalRevision((revision) => revision + 1);+ if (loadResults.getAttachmentRows().some(isThis)) {+ refresh();}- refresh();});// Electron right-click menu (copy image / reference) for the interactive image viewer.@@ -360,7 +354,6 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view<Suspense fallback={null}><CanvasDrawingDetailkey={attachment.attachmentId}- revision={externalRevision}attachment={attachment}note={ownerNote}noteContext={noteContext}diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsxindex d3abdb870c..2d0cdaf0eb 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx@@ -43,19 +43,13 @@ vi.mock("./persistence", () => ({loadDrawingFiles: async () => ({ files: {}, referencedFileIds: new Set() }),exportDrawingPicture}));-const { detailEditor, attachmentEditorMounts, canvasEditorMounts, exportDrawingPicture } =- vi.hoisted(() => ({- detailEditor: { canEdit: () => true, release: vi.fn() },- attachmentEditorMounts: vi.fn(),- canvasEditorMounts: vi.fn(),- exportDrawingPicture: vi.fn(async (_scene: unknown, _files: unknown, _isDark: boolean) =>- document.createElementNS("http://www.w3.org/2000/svg", "svg"))- }));+const { detailEditor, exportDrawingPicture } = vi.hoisted(() => ({+ detailEditor: { canEdit: () => true, release: vi.fn() },+ exportDrawingPicture: vi.fn(async (_scene: unknown, _files: unknown, _isDark: boolean) =>+ document.createElementNS("http://www.w3.org/2000/svg", "svg"))+}));vi.mock("../text/attachment_saves", () => ({- useAttachmentEditor: () => {- useEffect(() => attachmentEditorMounts(), []);- return detailEditor;- }+ useAttachmentEditor: () => detailEditor}));const canvasEditorProps = vi.fn();@@ -81,7 +75,6 @@ function MockCanvasEditor(props: MockCanvasEditorProps) {canvasEditorProps(props);const [ isLoaded, setIsLoaded ] = useState(false);useEffect(() => setIsLoaded(true), []);- useEffect(() => canvasEditorMounts(), []);return isLoaded ? <div className="excalidraw" tabIndex={0} /> : <span className="loading" />;}@@ -873,23 +866,6 @@ describe("CanvasDrawing", () => {editable.remove();});- it("loads the drawing again for each revision, keeping its detail editor", async () => {- const note = buildNote({ title: "Owner" });- const mountDetail = (revision: number) => act(async () => {- render(- <CanvasDrawingDetail attachment={ATTACHMENT} note={note} revision={revision} />,- box- );- });- attachmentEditorMounts.mockClear();- canvasEditorMounts.mockClear();-- await mountDetail(0);- await mountDetail(1);- expect(canvasEditorMounts).toHaveBeenCalledTimes(2);- expect(attachmentEditorMounts).toHaveBeenCalledOnce();- });-it("edits the drawing in the detail of its attachment unless its note is read-only", async () => {async function mountDetail(title: string, labels: Record<string, string> = {}) {const note = buildNote({ title, ...labels });diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsxindex 83b45a64f1..4e49358c6f 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx@@ -15,7 +15,7 @@ import options from "../../../services/options";import { getEffectiveThemeStyle } from "../../../services/theme";import { isDesktop } from "../../../services/utils";import {- useColorScheme, useEffectiveReadOnly, useElementSize, useTriliumEvent+ useColorScheme, useEffectiveReadOnly, useElementSize, useTriliumEvent, useWhenShown} from "../../react/hooks";import { useAttachmentEditor } from "../text/attachment_saves";import { useContentEmbedEvent } from "../text/content_embed_tools";@@ -179,8 +179,8 @@ interface DrawingPictureProps {/*** The drawing as a picture, at the view of `viewRef`, else at the saved view, else fit in the box.- * It renders again when the drawing saves, and centers the drawing once fullscreen changes its- * size, as Excalidraw does.+ * It renders again after each save, once `rootRef` is shown, and centers the drawing once+ * fullscreen changes its size, as Excalidraw does.*/function DrawingPicture({attachment, editor, colorScheme, rootRef, viewRef, onDrawn@@ -191,11 +191,12 @@ function DrawingPicture({const [ recenterings, setRecenterings ] = useState(0);const renderedKeyRef = useRef<string | null>(null);const size = useElementSize(layerRef);+ const whenShown = useWhenShown(rootRef);useTriliumEvent("entitiesReloaded", ({ loadResults }) => {const rows = loadResults.getAttachmentRows();if (rows.some((row) => row.attachmentId === attachment.attachmentId)) {- setRevision((count) => count + 1);+ whenShown(() => setRevision((count) => count + 1));}});@@ -366,43 +367,20 @@ interface CanvasDrawingDetailProps {/** The note that owns the attachment. */note: FNote;noteContext?: NoteContext;- /** Counts the changes to the drawing saved elsewhere, each of which loads it again. */- revision?: number;}/*** A canvas drawing saved in an attachment, edited in the full detail of the attachment. It is* read-only while its note is.*/-export function CanvasDrawingDetail({- attachment, note, noteContext, revision-}: CanvasDrawingDetailProps) {- // Outlives the drawing loaded again for each revision, which starts from its unsaved changes.+export function CanvasDrawingDetail({ attachment, note, noteContext }: CanvasDrawingDetailProps) {+ const apiRef = useRef<ExcalidrawImperativeAPI>(null);+ const colorScheme = useColorScheme();const attachmentEditor = useAttachmentEditor(note, noteContext);const editor = useMemo(() => bindAttachmentEditor(attachmentEditor, attachment),[ attachmentEditor, attachment ]);-- return (- <DetailDrawing- key={revision}- attachment={attachment}- note={note}- noteContext={noteContext}- editor={editor}- />- );-}--interface DetailDrawingProps extends Omit<CanvasDrawingDetailProps, "revision"> {- editor: ContentEditor;-}--/** The drawing of `CanvasDrawingDetail`, saved by `editor`. */-function DetailDrawing({ attachment, note, noteContext, editor }: DetailDrawingProps) {- const apiRef = useRef<ExcalidrawImperativeAPI>(null);- const colorScheme = useColorScheme();const isNoteReadOnly = useEffectiveReadOnly(note, noteContext);const canEdit = editor.canEdit() && !isNoteReadOnly&& !options.is("databaseReadonly");diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsxindex d7d7dcea39..7c398bfc6a 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_history.tsx@@ -38,6 +38,11 @@ export interface DrawingMount {attach(api: ExcalidrawImperativeAPI, root: HTMLElement | null, store: DrawingStore): void;/** Records the change of the drawing that Excalidraw ended, if any. */record(): void;+ /**+ * Shows `scene`, saved elsewhere, outside the undo history. Before `attach()`, it shows nothing+ * and returns `false`.+ */+ show(scene: DrawingScene): boolean;/** Stops recording, before Excalidraw unmounts. */detach(): void;}@@ -119,6 +124,11 @@ export default class DrawingHistory {record: () => {if (drawing) this.record(drawing);},+ show: (scene) => {+ if (!drawing) return false;+ showScene(drawing, scene);+ return true;+ },detach: () => {if (drawing) this.detach(drawing);drawing = undefined;@@ -417,16 +427,30 @@ function watchInput() {/** Shows `scene` in `drawing`, outside the undo history. */function showScene(drawing: AttachedDrawing, scene: DrawingScene) {- const { api } = drawing;const background = scene.appState.viewBackgroundColor ?? drawing.background;+ showDrawingScene(drawing.api, {+ ...scene,+ appState: { ...scene.appState, viewBackgroundColor: background }+ });+ commitElements(drawing);+ drawing.background = background;+}++/**+ * Shows `scene` in `api`, outside Excalidraw's undo history, with the background and the grid that+ * the scene sets, if any. The view stays where it is.+ */+export function showDrawingScene(api: ExcalidrawImperativeAPI, scene: DrawingScene) {+ const shown = api.getAppState();addMissingFiles(api, scene.files);api.updateScene({elements: scene.elements,- appState: { viewBackgroundColor: background },+ appState: {+ viewBackgroundColor: scene.appState.viewBackgroundColor ?? shown.viewBackgroundColor,+ gridModeEnabled: scene.appState.gridModeEnabled ?? shown.gridModeEnabled+ },captureUpdate: CaptureUpdateAction.NEVER});- commitElements(drawing);- drawing.background = background;}function applyToScene(scene: DrawingScene, step: DrawingStep, isUndo: boolean): DrawingScene {diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsxindex c5ae41f8a3..d851583399 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.spec.tsx@@ -10,18 +10,21 @@* still-empty initializing scene as a change worth saving. Only subsequent loads (note* switches on an already-initialized instance) may use `updateScene`.*/-import { exportToSvg } from "@excalidraw/excalidraw";+import { exportToSvg, restoreElements } from "@excalidraw/excalidraw";import { ExcalidrawImperativeAPI, ExcalidrawInitialDataState } from "@excalidraw/excalidraw/types";import type { HostHistoryStep } from "@triliumnext/codemirror";import { type RefObject, render } from "preact";import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import Component from "../../../components/component";import type FAttachment from "../../../entities/fattachment";import type FNote from "../../../entities/fnote";import type { ContentEditor } from "../../../services/content_renderer";+import LoadResults from "../../../services/load_results";import server from "../../../services/server";import { buildNote } from "../../../test/easy-froca";+import { ParentComponent } from "../../react/react_utils";import type { EmbedHostHistory } from "../text/embed_history";import useCanvasPersistence, {saveDrawingPicture, useCanvasDrawingPersistence@@ -31,6 +34,8 @@ interface FakeElement {id: string;type: string;version: number;+ text?: string;+ isDeleted?: boolean;}interface PlacedElement {@@ -45,6 +50,12 @@ vi.mock("@excalidraw/excalidraw", () => ({// Mirrors the real behavior closely enough for change tracking: the version of a scene// is derived from its elements, and an empty scene is always version 0.getSceneVersion: (elements: FakeElement[]) => elements.reduce((sum, el) => sum + (el.version ?? 0), 0),+ // As Excalidraw restores a loaded scene: it deletes a text element without text, which bumps+ // its version.+ restoreElements: (elements: FakeElement[]) => elements.map((element) =>+ element.type === "text" && !element.text && !element.isDeleted+ ? { ...element, isDeleted: true, version: element.version + 1 }+ : element),exportToSvg: vi.fn(async () => ({ outerHTML: "<svg/>" })),getCommonBounds: (elements: PlacedElement[]) => [Math.min(...elements.map(({ x }) => x)),@@ -188,6 +199,35 @@ describe("useCanvasPersistence content loading (#10279)", () => {expect(saved.elements.map((el) => el.id)).toEqual([ "a1", "a2" ]);});+ it("saves nothing for the content as Excalidraw restores it", async () => {+ const note = buildNote({+ title: "Canvas",+ type: "canvas",+ content: JSON.stringify({+ type: "excalidraw", version: 2, files: {}, appState: {},+ elements: [+ { id: "a1", type: "rectangle", version: 10 },+ { id: "t1", type: "text", version: 2, text: "" }+ ]+ })+ });+ note.getAttachmentsByRole = async () => [];+ let sceneElements: FakeElement[] = [];+ const { api } = buildApi(() => sceneElements);+ const apiRef = { current: api } as RefObject<ExcalidrawImperativeAPI | null>;++ await mount(note, apiRef);+ const initialData = await resolvedInitialData();+ // Excalidraw restores what `initialData` holds once more as it shows it.+ sceneElements =+ restoreElements(initialData?.elements ?? [], null) as unknown as FakeElement[];+ props?.onChange?.([], {} as never, {} as never);+ await act(async () => {+ await vi.advanceTimersByTimeAsync(1500);+ });+ expect(puts).toEqual([]);+ });+it("loads a subsequent note via updateScene once initialData is consumed", async () => {const { note: noteA } = buildCanvasNote([ "a1" ]);const { note: noteB } = buildCanvasNote([ "b1" ]);@@ -237,6 +277,7 @@ describe("useCanvasDrawingPersistence", () => {let sceneFiles: Record<string, unknown>;let appState: Record<string, unknown>;let updateScene: ReturnType<typeof vi.fn>;+ let parent: Component;function DrawingProbe({ attachment, editor, apiRef, options }: {attachment: FAttachment;@@ -253,6 +294,7 @@ describe("useCanvasDrawingPersistence", () => {sceneElements = [];sceneFiles = { f1: IMAGE, f2: UNUSED_IMAGE };updateScene = vi.fn();+ parent = new Component();appState = {scrollX: 1, scrollY: 2, zoom: { value: 1 }, gridModeEnabled: false,viewBackgroundColor: "transparent", theme: "light"@@ -299,12 +341,14 @@ describe("useCanvasDrawingPersistence", () => {const apiRef = { current: api } as RefObject<ExcalidrawImperativeAPI | null>;await act(async () => {render(- <DrawingProbe- attachment={attachment}- editor={editor}- apiRef={apiRef}- options={options}- />,+ <ParentComponent.Provider value={parent}>+ <DrawingProbe+ attachment={attachment}+ editor={editor}+ apiRef={apiRef}+ options={options}+ />+ </ParentComponent.Provider>,container);});@@ -315,6 +359,23 @@ describe("useCanvasDrawingPersistence", () => {drawingProps?.onChange?.([], appState as never, {});}+ /** Reports a save of the attachment `a1` made elsewhere, and waits until it shows. */+ async function reportSave(row: object = { attachmentId: "a1" }) {+ const loadResults = new LoadResults([]);+ loadResults.addAttachmentRow(row as never, "otherComponent");+ await act(async () => {+ await parent.handleEvent("entitiesReloaded", { loadResults });+ await new Promise((resolve) => setTimeout(resolve));+ });+ }++ /** Has the mocked Excalidraw show the elements that `updateScene()` replaces. */+ function showUpdatedElements() {+ updateScene.mockImplementation((data: { elements?: FakeElement[] }) => {+ if (data.elements) sceneElements = data.elements;+ });+ }+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: {} });@@ -389,6 +450,29 @@ describe("useCanvasDrawingPersistence", () => {expect(editor.release).toHaveBeenCalledOnce();});+ it("saves nothing for the content as Excalidraw restores it, only the edits", async () => {+ // A text element placed with nothing typed into it yet, as another view saved it.+ const saved = [+ { id: "r1", type: "rectangle", version: 3 },+ { id: "t1", type: "text", version: 2, text: "" }+ ];+ const { attachment } = buildAttachment({ elements: saved });+ const editor = buildEditor();+ const initialData = await mount(attachment, editor);++ // Excalidraw restores what `initialData` holds once more as it shows it.+ change();+ sceneElements =+ restoreElements(initialData.elements ?? [], null) as unknown as FakeElement[];+ change();+ expect(editor.scheduleSave).not.toHaveBeenCalled();+ expect(initialData.elements).toMatchObject([ { id: "r1" }, { id: "t1", isDeleted: true } ]);++ sceneElements = [ { ...saved[0], version: 4 }, sceneElements[1] ];+ change();+ expect(editor.scheduleSave).toHaveBeenCalledOnce();+ });+it("keeps its changes on the undo stack of the note, also once Excalidraw is gone", async () => {const loaded = [ { id: "r1", type: "rectangle", version: 3, x: 0 } ];const { attachment, getBlob } = buildAttachment({ elements: loaded });@@ -428,6 +512,149 @@ describe("useCanvasDrawingPersistence", () => {.toMatchObject([ { id: "r1", x: 0 }, { id: "e1" } ]);});+ it("shows a save made elsewhere in place, which it neither saves nor undoes", async () => {+ const loaded = [ { id: "r1", type: "rectangle", version: 3, versionNonce: 1 } ];+ const { attachment, getBlob } = buildAttachment({ elements: loaded });+ const steps: HostHistoryStep[] = [];+ const host: EmbedHostHistory = {+ push: (step) => {+ steps.push(step);+ return { isLatest: () => true };+ },+ undo: vi.fn(),+ redo: vi.fn(),+ isEnabled: () => false,+ subscribe: () => () => {}+ };+ const editor = { ...buildEditor(), history: { host, write: vi.fn() } };+ showUpdatedElements();+ await mount(attachment, editor, { rootRef: { current: document.createElement("div") } });+ change();+ sceneElements = loaded;+ change();++ const saved = [+ { ...loaded[0], version: 4, versionNonce: 2 },+ { id: "e1", type: "ellipse", version: 1, versionNonce: 3 }+ ];+ getBlob.mockResolvedValue({ content: JSON.stringify({+ elements: saved, appState: { viewBackgroundColor: "#ffc9c9", zoom: { value: 3 } }+ }) });+ await reportSave();+ expect(updateScene).toHaveBeenCalledExactlyOnceWith({+ elements: saved,+ appState: { viewBackgroundColor: "#ffc9c9", gridModeEnabled: false },+ captureUpdate: "NEVER"+ });+ appState = { ...appState, viewBackgroundColor: "#ffc9c9" };+ change();+ expect(editor.scheduleSave).not.toHaveBeenCalled();+ expect(steps).toEqual([]);++ // The scene that shows already stays, through a deletion too, and so do the changes that+ // wait for a save here.+ updateScene.mockClear();+ await reportSave();+ getBlob.mockResolvedValue({ content: JSON.stringify({ elements: [] }) });+ await reportSave({ attachmentId: "a1", isDeleted: true });+ editor.getUnsavedContent.mockReturnValue(JSON.stringify({ elements: loaded }));+ await reportSave();+ expect(updateScene).not.toHaveBeenCalled();+ });++ it("shows a save made elsewhere once it shows, as in a tab in the background", async () => {+ const resizeCallbacks: (() => void)[] = [];+ const { ResizeObserver } = globalThis;+ globalThis.ResizeObserver = class {+ constructor(callback: () => void) {+ resizeCallbacks.push(callback);+ }+ observe() {}+ disconnect() {}+ } as unknown as typeof ResizeObserver;+ try {+ const loaded = [ { id: "r1", type: "rectangle", version: 3, versionNonce: 1 } ];+ const { attachment, getBlob } = buildAttachment({ elements: loaded });+ const root = container.appendChild(document.createElement("div"));+ showUpdatedElements();+ await mount(attachment, buildEditor(), { rootRef: { current: root } });+ change();+ sceneElements = loaded;+ change();++ container.style.display = "none";+ const saved = [ { ...loaded[0], version: 4, versionNonce: 2 } ];+ getBlob.mockResolvedValue({ content: JSON.stringify({ elements: saved }) });+ await reportSave();+ expect(getBlob).toHaveBeenCalledTimes(2);+ expect(updateScene).not.toHaveBeenCalled();++ container.style.display = "";+ await act(async () => {+ for (const callback of resizeCallbacks) callback();+ await new Promise((resolve) => setTimeout(resolve));+ });+ expect(updateScene).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({+ elements: saved+ }));+ } finally {+ globalThis.ResizeObserver = ResizeObserver;+ }+ });++ it("keeps the text that the user types, and shows a save made meanwhile after", async () => {+ const loaded = [ { id: "r1", type: "rectangle", version: 3, versionNonce: 1 } ];+ const { attachment, getBlob } = buildAttachment({ elements: loaded });+ showUpdatedElements();+ await mount(attachment, buildEditor());+ change();+ sceneElements = loaded;+ change();++ // Its own save of a text element with nothing typed into it yet.+ const text = { id: "t1", type: "text", version: 1, versionNonce: 5, text: "" };+ sceneElements = [ ...loaded, text ];+ getBlob.mockResolvedValue({ content: JSON.stringify({ elements: sceneElements }) });+ await reportSave();+ expect(updateScene).not.toHaveBeenCalled();++ appState = { ...appState, editingTextElement: text };+ const saved = [ { ...loaded[0], version: 4, versionNonce: 2 } ];+ getBlob.mockResolvedValue({ content: JSON.stringify({ elements: saved }) });+ await reportSave();+ expect(updateScene).not.toHaveBeenCalled();++ appState = { ...appState, editingTextElement: null };+ await act(async () => {+ change();+ await new Promise((resolve) => setTimeout(resolve));+ });+ expect(updateScene).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({+ elements: saved+ }));+ });++ it("shows a save that lands while the drawing loads once Excalidraw shows it", async () => {+ const loaded = [ { id: "r1", type: "rectangle", version: 3, versionNonce: 1 } ];+ const { attachment, getBlob } = buildAttachment({ elements: loaded });+ showUpdatedElements();+ await mount(attachment, undefined);++ const saved = [ { ...loaded[0], version: 4, versionNonce: 2 } ];+ getBlob.mockResolvedValue({ content: JSON.stringify({ elements: saved }) });+ await reportSave();+ expect(updateScene).not.toHaveBeenCalled();++ sceneElements = loaded;+ await act(async () => {+ change();+ await new Promise((resolve) => setTimeout(resolve));+ });+ expect(updateScene).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({+ elements: saved+ }));+ });+it("loads the images it refers to from its note and saves them by reference", async () => {const owner = buildNote({ title: "Owner" });const getAttachmentsByRole = vi.fn(async () => [diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.ts b/apps/client/src/widgets/type_widgets/canvas/persistence.tsindex 4f524a199e..9f6a7e61b0 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.ts+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.ts@@ -1,4 +1,6 @@-import { CaptureUpdateAction, exportToSvg, getSceneVersion } from "@excalidraw/excalidraw";+import {+ CaptureUpdateAction, exportToSvg, getSceneVersion, restoreElements+} from "@excalidraw/excalidraw";import { ExcalidrawElement, NonDeletedExcalidrawElement } from "@excalidraw/excalidraw/element/types";import {AppState, BinaryFileData, BinaryFiles, ExcalidrawImperativeAPI, ExcalidrawInitialDataState,@@ -14,8 +16,12 @@ import FNote from "../../../entities/fnote";import type { ContentEditor } from "../../../services/content_renderer";import froca from "../../../services/froca";import server from "../../../services/server";-import { SavedData, useEditorSpacedUpdate } from "../../react/hooks";-import { type DrawingScene, getDrawingHistory, type SavedAppState } from "./drawing_history";+import {+ SavedData, useEditorSpacedUpdate, useTriliumEvent, useWhenShown+} from "../../react/hooks";+import {+ type DrawingScene, getDrawingHistory, isInteracting, type SavedAppState, showDrawingScene+} from "./drawing_history";import { type DrawingView, getBoundedView, getFittedView } from "./drawing_view";import {buildNewImageAttachments, CANVAS_EXPORT_TITLE, IMAGE_ROLE, loadImageAttachments, loadImageFiles@@ -93,6 +99,7 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteConteactiveNoteIdRef.current = loadedNoteId;const content = parseContent(newContent, note);+ const elements = restoreLoadedElements(content.elements);// Legacy notes carry inline images in `content.files` (a fileId-keyed record);// current notes store them as attachments. Both go into `initialData.files`.@@ -113,13 +120,13 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteContelibraryCache.current = libraryResult.libraryItems;attachmentMetadata.current = libraryResult.metadata;- const expectedSceneVersion = getSceneVersion(content.elements ?? []);+ const expectedSceneVersion = getSceneVersion(elements);currentSceneVersion.current = expectedSceneVersion;initialSceneAppliedRef.current = expectedSceneVersion === 0;initialDataResolvedRef.current = true;initialData.resolve({- elements: content.elements ?? [],+ elements,appState: { ...content.appState, theme },files,libraryItems: libraryResult.libraryItems@@ -131,7 +138,7 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteConteinitialSceneAppliedRef.current = true;// Resolve anyway so Excalidraw does not stay on its loading screen forever.initialData.resolve({- elements: content.elements ?? [],+ elements,appState: { ...content.appState, theme },files});@@ -357,7 +364,10 @@ interface DrawingPersistenceOptions {initialView?: DrawingView | null;/** Called once Excalidraw shows the loaded scene. */onSceneShown?: () => void;- /** The element that holds Excalidraw, in which the undo history of the note watches input. */+ /**+ * The element that holds Excalidraw. The undo history of the note watches input in it, and a+ * save made elsewhere shows once it is shown.+ */rootRef?: RefObject<HTMLElement | null>;}@@ -392,7 +402,7 @@ export function useCanvasDrawingPersistence(const loaded = await loadDrawingFiles(attachment, content);referencedFileIds.current = loaded.referencedFileIds;- const elements = content.elements ?? [];+ const elements = restoreLoadedElements(content.elements);currentSceneVersion.current = getSceneVersion(elements);initialSceneAppliedRef.current = currentSceneVersion.current === 0;isLoadedSceneEmptyRef.current = !elements.some((element) => !element.isDeleted);@@ -417,6 +427,84 @@ export function useCanvasDrawingPersistence(useLayoutEffect(() => () => drawingMount?.detach(), [ drawingMount ]);useLayoutEffect(() => () => editor?.release(), [ editor ]);+ /**+ * Whether a save of the drawing waits until Excalidraw shows the loaded scene, or until the+ * user ends a change.+ */+ const isSaveWaitingRef = useRef(false);+ const showRequestRef = useRef(0);+ const whenShown = useWhenShown(rootRef);++ /** Reads the saved drawing now, and shows it once `rootRef` is shown. */+ async function loadSavedDrawing() {+ if (!apiRef.current || !isSceneShownRef.current) {+ isSaveWaitingRef.current = true;+ return;+ }+ isSaveWaitingRef.current = false;+ const request = ++showRequestRef.current;+ const blob = await attachment.getBlob();+ if (blob && request === showRequestRef.current) {+ whenShown(() => void showSavedDrawing(blob.content ?? "", request));+ }+ }++ /**+ * Shows `savedContent`, the saved drawing, in place. Does nothing while `editor` has unsaved+ * changes of the drawing: they replace the saved drawing when they save.+ */+ async function showSavedDrawing(savedContent: string, request: number) {+ const api = apiRef.current;+ const isCurrent = () => request === showRequestRef.current && apiRef.current === api+ && editor?.getUnsavedContent() === undefined;+ if (!api || !isCurrent()) return;+ if (isInteracting(api.getAppState())) {+ isSaveWaitingRef.current = true;+ return;+ }++ const content = parseContent(savedContent, attachment);+ const elements = restoreLoadedElements(content.elements);+ const appState: ImportantAppState = {+ gridModeEnabled: content.appState?.gridModeEnabled ?? false,+ viewBackgroundColor: content.appState?.viewBackgroundColor ?? "transparent"+ };+ const shownAppState = getImportantAppState(api.getAppState());+ // A restore deletes an empty text element, so the shown elements are restored as well.+ if (isSameScene(restoreLoadedElements(api.getSceneElements()), elements)+ && appState.gridModeEnabled === shownAppState.gridModeEnabled+ && appState.viewBackgroundColor === shownAppState.viewBackgroundColor) {+ return;+ }++ const shownFiles = api.getFiles();+ const loaded = await loadDrawingFiles(attachment, {+ ...content,+ elements: elements.filter((element) =>+ "fileId" in element && element.fileId && !shownFiles[element.fileId])+ });+ if (!isCurrent()) return;++ referencedFileIds.current = new Set([+ ...referencedFileIds.current, ...loaded.referencedFileIds+ ]);+ const scene = { elements, appState, files: loaded.files };+ if (!drawingMount?.show(scene)) {+ showDrawingScene(api, scene);+ }+ // Excalidraw replaces the elements at once and renders the app state later.+ currentSceneVersion.current = getSceneVersion(api.getSceneElements());+ appStateToCompare.current = appState;+ }++ useTriliumEvent("entitiesReloaded", ({ loadResults }) => {+ const isSaved = loadResults.getAttachmentRows().some((row) =>+ row.attachmentId === attachment.attachmentId && !row.isDeleted);+ if (isSaved) {+ void loadSavedDrawing();+ }+ });+return {initialData,excalidrawAPI: (api) => {@@ -444,6 +532,10 @@ export function useCanvasDrawingPersistence(});onSceneShown?.();}+ if (isSaveWaitingRef.current && isSceneShownRef.current+ && !isInteracting(api.getAppState())) {+ void loadSavedDrawing();+ }if (!editor) return;if (!initialSceneAppliedRef.current) {@@ -669,6 +761,26 @@ function serializeDrawing(scene: DrawingScene, referencedFileIds: ReadonlySet<streturn JSON.stringify(buildSceneContent(elements, scene.appState, files));}+/**+ * Loaded `elements` as Excalidraw restores them when it loads a scene: a text element without text+ * is deleted, with a new version, and elements without a size are dropped. A second restore+ * changes nothing, so the scene that Excalidraw shows has the version of the result.+ */+function restoreLoadedElements(elements: readonly ExcalidrawElement[] | undefined) {+ return restoreElements(elements ?? [], null, { repairBindings: true });+}++/** Whether `shown` and `saved` hold the same elements, deleted ones aside, at the same versions. */+function isSameScene(shown: readonly ExcalidrawElement[], saved: readonly ExcalidrawElement[]) {+ const getVersion = (element: ExcalidrawElement) => `${element.version}:${element.versionNonce}`;+ const shownVersions = new Map(shown+ .filter((element) => !element.isDeleted)+ .map((element) => [ element.id, getVersion(element) ]));+ const savedElements = saved.filter((element) => !element.isDeleted);+ return savedElements.length === shownVersions.size+ && savedElements.every((element) => shownVersions.get(element.id) === getVersion(element));+}+export function parseContent(newContent: string, entity: FNote | FAttachment): CanvasContent {let content: CanvasContent = {elements: [],diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsxindex 0a29dbacb4..13b4846ea0 100644--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx@@ -16,7 +16,9 @@ import {useNoteCodeSettings, useTabWidthMenu} from "../../layout/code_menus";import { SaveStateBadge } from "../../layout/NoteBadges";-import { useNoteBlob, useNoteLabelOptionalBool, useTriliumEvent } from "../../react/hooks";+import {+ useNoteBlob, useNoteLabelOptionalBool, useTriliumEvent, useWhenShown+} from "../../react/hooks";import { useContentEmbedMenus } from "../text/content_embed_menus";import { useContentEmbedAttribute, useContentEmbedFlag } from "../text/content_embed_tools";import {@@ -67,7 +69,7 @@ export default function CodeEmbed({});const [ content, setContent ] = useState(initialContent);const mime = settings.mime ?? initialMime;- useSavedContent(entity, editor, setContent);+ useSavedContent(entity, editor, setContent, rootRef);// The editor while the content is edited, otherwise the content with the unsaved changes of any// embed of it, which a change saves.const getCodeText = (): CodeText => (isEditing ? viewRef.current : null) ?? {@@ -253,17 +255,20 @@ export function SaveStatus({ rootRef, editor, isShown }: SaveStatusProps) {}/**- * Shows the content of `entity` each time it saves, also when another embed of it saved it.- * Unsaved changes made in the embed are kept, and replace that content when they save.+ * Shows the content of `entity` after each save, also one made by another embed of it, once+ * `shownRef` is shown. Unsaved changes made in the embed are kept, and replace that content when+ * they save.*/export function useSavedContent(entity: FNote | FAttachment,editor: ContentEditor | undefined,- setContent: (content: string) => void+ setContent: (content: string) => void,+ shownRef: RefObject<Element | null>) {- const noteBlob = useNoteBlob(entity instanceof FNote ? entity : null);+ const noteBlob = useNoteBlob(entity instanceof FNote ? entity : null, undefined, { shownRef });const [ attachmentContent, setAttachmentContent ] = useState<string>();const requestIdRef = useRef(0);+ const whenShown = useWhenShown(shownRef);const content = entity instanceof FNote ? noteBlob?.content : attachmentContent;useTriliumEvent("entitiesReloaded", ({ loadResults }) => {@@ -271,11 +276,11 @@ export function useSavedContent(const rows = loadResults.getAttachmentRows();if (rows.some((row) => row.attachmentId === entity.attachmentId)) {const requestId = ++requestIdRef.current;- void entity.getBlob().then((blob) => {+ void entity.getBlob().then((blob) => whenShown(() => {if (requestId === requestIdRef.current) {setAttachmentContent(blob?.content);}- });+ }));}});diff --git a/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.tsx b/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.tsxindex d9f49a96d0..158e06ca8f 100644--- a/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.tsx@@ -60,7 +60,7 @@ export default function MarkdownEmbed({const { tabWidth, useTabs } = useNoteCodeSettings(note);const [ content, setContent ] = useState(initialContent);- useSavedContent(entity, editor, setContent);+ useSavedContent(entity, editor, setContent, rootRef);const renderedContent = useDebouncedValue(content, PREVIEW_DELAY);const shownPreview = useEmbedPreview(preview, renderedContent, () => renderPreview(renderedContent)diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts b/apps/client/src/widgets/type_widgets/text/EditableText.spec.tsindex 9f08e9859b..8e3be60087 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts+++ b/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts@@ -9,6 +9,8 @@import type { CKEditorError, CKTextEditor, FileUploadData } from "@triliumnext/ckeditor5";import { beforeEach, describe, expect, it, vi } from "vitest";+import type FAttachment from "../../../entities/fattachment";+import froca from "../../../services/froca";import type LoadResults from "../../../services/load_results";const showError = vi.hoisted(() => vi.fn());@@ -30,6 +32,13 @@ vi.mock("../../../services/dialog", () => ({ default: { info: showInfoDialog } }// Imported by EditableText for editor types and content styles; irrelevant (and heavy) here.vi.mock("@triliumnext/ckeditor5", () => ({}));+const showsAttachmentSave =+ vi.hoisted(() => vi.fn((_root: HTMLElement, _attachment: object) => false));+vi.mock("./utils", async (importOriginal) => ({+ ...await importOriginal<typeof import("./utils")>(),+ showsAttachmentSave+}));+const {applyPendingAttachmentChanges,getAddLinkOpts,@@ -209,8 +218,10 @@ describe("notifyAttachmentChanges", () => {type Row = { attachmentId?: string; isDeleted?: boolean };const updateAttachmentLinks = vi.fn();+ const root = document.createElement("div");const editor = {- plugins: { get: () => ({ updateAttachmentLinks }) }+ plugins: { get: () => ({ updateAttachmentLinks }) },+ editing: { view: { getDomRoot: () => root } }} as unknown as CKTextEditor;function loadResultsOf(rows: Row[], isContentReloaded = false) {@@ -247,6 +258,29 @@ describe("notifyAttachmentChanges", () => {]);});+ it("leaves out the saves that every embed of the attachment shows by itself", () => {+ const drawing = { attachmentId: "drawing" } as FAttachment;+ const renamed = { attachmentId: "renamed" } as FAttachment;+ froca.attachments.drawing = drawing;+ froca.attachments.renamed = renamed;+ showsAttachmentSave.mockImplementation((_root, attachment) => attachment === drawing);++ notify(loadResultsOf([+ { attachmentId: "drawing" },+ { attachmentId: "renamed" },+ { attachmentId: "drawing", isDeleted: true }+ ]), { current: undefined });++ expect(showsAttachmentSave).toHaveBeenCalledWith(root, drawing);+ expect(updateAttachmentLinks).toHaveBeenCalledExactlyOnceWith([+ { attachmentId: "renamed", isDeleted: false },+ { attachmentId: "drawing", isDeleted: true }+ ]);+ showsAttachmentSave.mockReset().mockReturnValue(false);+ delete froca.attachments.drawing;+ delete froca.attachments.renamed;+ });+it("leaves the links alone when no attachment changed", () => {const pending: PendingRef = { current: undefined };diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex 0770d6339e..7e3b62a7a1 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -22,6 +22,7 @@ import {import { consumeBookmark } from "../../../services/bookmark_jump";import { getUploadBoxSize } from "../../../services/content_renderer";import dialog from "../../../services/dialog";+import froca from "../../../services/froca";import { t } from "../../../services/i18n";import link, { parseNavigationStateFromUrl, type ViewScope } from "../../../services/link";import type LoadResults from "../../../services/load_results";@@ -57,6 +58,7 @@ import {openContentEmbedMenu,refreshEmbeddedNote,setupImageOpening,+ showsAttachmentSave,watchContentEmbeds} from "./utils";@@ -117,6 +119,7 @@ export default function EditableText({noteContext,noteType: "text",block,+ shownRef: containerRef,getData() {const editor = editorRef.current;if (!editor) {@@ -951,7 +954,8 @@ interface PendingAttachmentChanges {/*** Passes the attachments changed in `loadResults` to the editor's reference links, so a link shows* the new title of its attachment or goes away with it. Changes that this editor saved, such as a- * canvas drawing, are skipped: redrawing an embed remounts what it shows.+ * canvas drawing, are skipped: redrawing an embed remounts what it shows. So are the saves that+ * every embed of the attachment shows by itself (`showsAttachmentSave()`).** When `loadResults` also reloads the note's content, the changes wait in `pending` until* `applyPendingAttachmentChanges()` runs on the reloaded content.@@ -965,10 +969,15 @@ export function notifyAttachmentChanges(componentId: string | undefined,pending: { current: PendingAttachmentChanges | undefined }) {+ const root = editor.editing.view.getDomRoot();const rows = loadResults.getAttachmentRows(componentId);- const changes = rows.flatMap(({ attachmentId, isDeleted }) =>- attachmentId ? [ { attachmentId, isDeleted: !!isDeleted } ] : []- );+ const changes = rows.flatMap(({ attachmentId, isDeleted }) => {+ if (!attachmentId) return [];+ const attachment = froca.attachments[attachmentId];+ const isShown = !isDeleted && !!root && !!attachment+ && showsAttachmentSave(root, attachment);+ return isShown ? [] : [ { attachmentId, isDeleted: !!isDeleted } ];+ });if (!changes.length) {return;}diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsxindex e72baa8e30..0e63f7e52f 100644--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx@@ -177,6 +177,40 @@ describe("ReadOnlyText reacting to content changes (#10575)", () => {expect(harness.contentLoadStates).not.toContain("loading");});+ it("shows a change saved elsewhere once its tab shows, having loaded it at once", async () => {+ const resizeCallbacks: (() => void)[] = [];+ const { ResizeObserver } = globalThis;+ globalThis.ResizeObserver = class {+ constructor(callback: () => void) {+ resizeCallbacks.push(callback);+ }+ observe() {}+ disconnect() {}+ } as unknown as typeof ResizeObserver;+ try {+ const harness = setupHarness({ isVisible: true });+ cleanupContainer = harness.container;+ await harness.mount();+ harness.contentLoadStates.length = 0;++ // A tab in the background, hidden as `SplitNoteContainer` hides one.+ harness.container.style.display = "none";+ harness.getBlobSpy.mockResolvedValue({ content: "<p>changed</p>" } as never);+ await harness.fireContentChange("ReactWrappedWidget-remote");+ expect(harness.getBlobSpy).toHaveBeenCalledTimes(2);+ expect(harness.container.textContent).toContain("hello");++ harness.container.style.display = "";+ await act(async () => {+ for (const callback of resizeCallbacks) callback();+ });+ expect(harness.container.textContent).toContain("changed");+ expect(harness.contentLoadStates).toEqual([ "loaded" ]);+ } finally {+ globalThis.ResizeObserver = ResizeObserver;+ }+ });+it("catches up on skipped own-component changes when displayed again, but only then", async () => {const harness = setupHarness({ isVisible: false });cleanupContainer = harness.container;diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsxindex 157f2bcbb5..0a55f646ff 100644--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx@@ -51,13 +51,14 @@ export default function ReadOnlyText({ note, noteContext, ntxId, parentComponent// hidden while the user temporarily edits an auto-read-only note — would refetch the whole blob// after every save and flash the loading overlay over the editor (#10575). Skipped own-component// changes are caught up on via refreshOnShow when this view is displayed again.+ const readOnlyContentRef = usePreactRef<HTMLDivElement>(null);const blob = useNoteBlob(note, parentComponent?.componentId, {reportLoadStateTo: noteContext,isVisible,- refreshOnShow: true+ refreshOnShow: true,+ shownRef: readOnlyContentRef});const { isRtl } = useNoteLanguage(note);- const readOnlyContentRef = usePreactRef<HTMLDivElement>(null);const renderedNoteIdRef = usePreactRef<string | undefined>(undefined);// Scroll to the bookmark or the blocks a link points at. The blob gate skips the mount run,diff --git a/apps/client/src/widgets/type_widgets/text/SourceEmbed.tsx b/apps/client/src/widgets/type_widgets/text/SourceEmbed.tsxindex eb43bbf6e2..68df1f5b9b 100644--- a/apps/client/src/widgets/type_widgets/text/SourceEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/text/SourceEmbed.tsx@@ -1,5 +1,5 @@import type { InlineEmbedSource } from "@triliumnext/ckeditor5";-import type { ComponentType } from "preact";+import type { ComponentType, RefObject } from "preact";import { useSyncExternalStore } from "preact/compat";import { useEffect, useMemo, useState } from "preact/hooks";@@ -17,14 +17,16 @@ interface AttachmentSourceEmbedProps {/** The source shown first. */content: string;component: ComponentType<SourceEmbedProps>;+ /** The element that holds the embed. A save shows once it is shown. */+ containerRef: RefObject<HTMLElement | null>;}/** The component of a source embed type, over a source kept in a note or an attachment. */export default function AttachmentSourceEmbed({- entity, editor, content: initialContent, component: Component+ entity, editor, content: initialContent, component: Component, containerRef}: AttachmentSourceEmbedProps) {const [ content, setContent ] = useState(initialContent);- useSavedContent(entity, editor, setContent);+ useSavedContent(entity, editor, setContent, containerRef);return <Component content={content} editor={editor} />;}diff --git a/apps/client/src/widgets/type_widgets/text/TextEmbed.tsx b/apps/client/src/widgets/type_widgets/text/TextEmbed.tsxindex c964159df8..651ef7ad82 100644--- a/apps/client/src/widgets/type_widgets/text/TextEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/text/TextEmbed.tsx@@ -1,10 +1,11 @@+import type { RefObject } from "preact";import { lazy } from "preact/compat";import { useCallback, useEffect, useRef, useState } from "preact/hooks";import type FNote from "../../../entities/fnote";import type { ContentEditor } from "../../../services/content_renderer";import froca from "../../../services/froca";-import { useTriliumEvent } from "../../react/hooks";+import { useTriliumEvent, useWhenShown } from "../../react/hooks";import {EditableEmbedContent, hasFixedToolbarAround, useEditableEmbed, useEmbedPreview} from "./editable_embed";@@ -41,7 +42,7 @@ export default function TextEmbed({note,focusTarget: ".ck-editor__editable"});- const [ content, onEditorClose ] = useShownContent(note, initialContent);+ const [ content, onEditorClose ] = useShownContent(note, initialContent, rootRef);const [ block, setBlock ] = useState(initialBlock);const onEditorBlockChange = useCallback((editorBlock: string) => {setBlock(editorBlock);@@ -72,23 +73,36 @@ export default function TextEmbed({/*** The content of `note` to preview: the content of the editor of the embed as it closed, until its* last save lands, and otherwise the latest content that a save of the note stores. After a failed- * save, the content of the editor stays until the next save of the note. Returns that content,- * and the callback that takes the content of the closing editor and its save.+ * save, the content of the editor stays until the next save of the note. A save shows once+ * `rootRef` is shown. Returns that content, and the callback that takes the content of the closing+ * editor and its save.*/-function useShownContent(note: FNote, initialContent: string) {+function useShownContent(+ note: FNote,+ initialContent: string,+ rootRef: RefObject<HTMLElement | null>+) {const [ content, setContent ] = useState(initialContent);const requestIdRef = useRef(0);const closingSaveRef = useRef<Promise<boolean> | undefined>(undefined);+ const whenShown = useWhenShown(rootRef);// Only the latest read applies, and none while the last save of the editor runs: an earlier// save that lands meanwhile stores older content.- const refresh = useCallback(async () => {+ const refresh = useCallback(async (isSavedElsewhere = false) => {const requestId = ++requestIdRef.current;const blob = await note.getBlob();- if (blob && requestId === requestIdRef.current && !closingSaveRef.current) {- setContent(blob.content);+ const update = () => {+ if (blob && requestId === requestIdRef.current && !closingSaveRef.current) {+ setContent(blob.content);+ }+ };+ if (isSavedElsewhere) {+ whenShown(update);+ } else {+ update();}- }, [ note ]);+ }, [ note, whenShown ]);// Reads once on mount too, for a save that landed after the host read the content.useEffect(() => {@@ -96,7 +110,7 @@ function useShownContent(note: FNote, initialContent: string) {}, [ refresh ]);useTriliumEvent("entitiesReloaded", ({ loadResults }) => {if (loadResults.isNoteContentReloaded(note.noteId)) {- void refresh();+ void refresh(true);}});diff --git a/apps/client/src/widgets/type_widgets/text/utils.spec.ts b/apps/client/src/widgets/type_widgets/text/utils.spec.tsindex a93ba0de5e..4f164e16f4 100644--- a/apps/client/src/widgets/type_widgets/text/utils.spec.ts+++ b/apps/client/src/widgets/type_widgets/text/utils.spec.ts@@ -15,6 +15,8 @@ vi.mock("../../../services/link", () => ({vi.mock("../../../services/content_renderer", () => ({default: {getRenderedContent: vi.fn(),+ getRenderingType: vi.fn(),+ isSelfUpdatingType: (type: string) => type === "canvasDrawing",disposeInteractiveContent: vi.fn(),mountInteractiveWidget: vi.fn()}@@ -55,6 +57,7 @@ import {loadEmbeddedNote,openContentEmbedMenu,refreshEmbeddedNote,+ showsAttachmentSave,watchContentEmbeds} from "./utils";@@ -285,6 +288,48 @@ describe("loadEmbeddedAttachment", () => {expect(getLastOptions()).not.toHaveProperty("isCodeWrapped");});+ it("tells when every box of an attachment shows its saves by itself", async () => {+ const drawing = { ...attachment, title: "Canvas.excalidraw" } as FAttachment;+ vi.mocked(froca.getAttachment).mockResolvedValue(drawing);+ vi.mocked(content_renderer.getRenderingType).mockReturnValue("canvasDrawing");+ const container = inPage(document.createElement("div"));+ const addBox = () => {+ const embed = document.createElement("figure");+ embed.className = "include-note";+ embed.dataset.attachmentId = "att1";+ const wrapper = document.createElement("div");+ wrapper.className = "include-note-wrapper";+ embed.append(wrapper);+ container.append(embed);+ return wrapper;+ };+ const draw = async (wrapper: HTMLElement, type: string) => {+ vi.mocked(content_renderer.getRenderedContent)+ .mockResolvedValueOnce({ $renderedContent: $(content), type } as never);+ await loadEmbeddedAttachment("att1", $(wrapper), "medium");+ };+ expect(showsAttachmentSave(container, drawing)).toBe(false);++ const first = addBox();+ expect(showsAttachmentSave(container, drawing)).toBe(false);+ await draw(first, "canvasDrawing");+ expect(showsAttachmentSave(container, drawing)).toBe(true);++ // A new title, or another type of content, draws the box again.+ expect(showsAttachmentSave(container, { ...drawing, title: "Renamed" } as FAttachment))+ .toBe(false);+ vi.mocked(content_renderer.getRenderingType).mockReturnValueOnce("file");+ expect(showsAttachmentSave(container, drawing)).toBe(false);++ // So does every box, once one of them shows content that does not follow the saves.+ const second = addBox();+ await draw(second, "pdf");+ expect(showsAttachmentSave(container, drawing)).toBe(false);+ await draw(second, "canvasDrawing");+ await draw(first, "pdf");+ expect(showsAttachmentSave(container, drawing)).toBe(false);+ });+it("leaves the box alone for a deleted attachment", async () => {vi.mocked(froca.getAttachment).mockResolvedValue(null);diff --git a/apps/client/src/widgets/type_widgets/text/utils.ts b/apps/client/src/widgets/type_widgets/text/utils.tsindex df294d5e7e..0fcdc310a2 100644--- a/apps/client/src/widgets/type_widgets/text/utils.ts+++ b/apps/client/src/widgets/type_widgets/text/utils.ts@@ -3,6 +3,7 @@ import { attachmentIcon } from "@triliumnext/commons";import { h, type JSX } from "preact";import appContext from "../../../components/app_context";+import type FAttachment from "../../../entities/fattachment";import linkContextMenu from "../../../menus/link_context_menu";import content_renderer, {type AttachmentEditor, CONTENT_RENDERERS, type NoteEditor@@ -28,6 +29,12 @@ interface EmbeddedNoteOptions {onBlockChange?: (block: string) => void;}+/**+ * The title and content type that an attachment box was rendered with, by its wrapper. Only boxes+ * whose content reloads itself after each save are kept.+ */+const selfUpdatingBoxes = new WeakMap<HTMLElement, { title: string; type: string }>();+/*** Fills an embed box with a note. Without a box size of its own, the box takes the one of its* embed.@@ -117,6 +124,7 @@ export async function loadEmbeddedAttachment(if (!attachment) return;const el = $el[0];+ selfUpdatingBoxes.delete(el);const size = boxSize ?? getEmbedBoxSize(el);const viewScope: ViewScope = { viewMode: "attachments", attachmentId };if (size === "tiny") {@@ -150,6 +158,9 @@ export async function loadEmbeddedAttachment(histories,...getEmbedCodeView(el.closest(".include-note"))});+ if (content_renderer.isSelfUpdatingType(type)) {+ selfUpdatingBoxes.set(el, { title: attachment.title, type });+ }const box = h(ContentEmbed, {boxSize: size,@@ -163,6 +174,21 @@ export async function loadEmbeddedAttachment(await mountEmbedBox(el, box, $renderedContent);}+/**+ * Whether every box of `attachment` in `container` shows its latest save without a redraw: the+ * content reloads itself, and the title and content type are unchanged. `false` when `container`+ * has no box of the attachment.+ */+export function showsAttachmentSave(container: HTMLElement, attachment: FAttachment) {+ const boxes = container.querySelectorAll<HTMLElement>(+ `.include-note[data-attachment-id="${attachment.attachmentId}"] > .include-note-wrapper`);+ const type = content_renderer.getRenderingType(attachment);+ return boxes.length > 0 && [ ...boxes ].every((box) => {+ const shown = selfUpdatingBoxes.get(box);+ return shown?.title === attachment.title && shown.type === type;+ });+}+interface InlineEmbedOptions {/** Whether the content takes the focus once the box is mounted. */isFocused: boolean;