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
fix(text): don't flash the loading overlay on the user's own saves, closes #10575
Elian Doran3a5b661bf6d0
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex a5d32b8b02..c0871e5286 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -783,11 +783,25 @@ export function useNoteBlob(note: FNote | null | undefined, componentId?: string* only be set by widgets whose main content display is gated on this blob. (Passed* explicitly because NoteContextContext is only provided in dialogs, not the main window.) */reportLoadStateTo?: NoteContext | null;+ /** Whether the consuming widget is currently displayed. When explicitly `false`, load states+ * are not published — a cached/hidden type widget must not drive the note detail's loading+ * overlay over the widget the user is actually looking at (#10575). Leave undefined when the+ * consumer has no cached-hidden state. */+ isVisible?: boolean;+ /** Refetch on becoming visible if a content change was skipped while hidden because it came+ * from this widget's own component (`componentId`). For widgets that display saved content+ * produced by a sibling under the same parent component (e.g. the read-only text view behind+ * the editor), own-component changes are somebody else's edits that must eventually show. */+ refreshOnShow?: boolean;}): FBlob | null | undefined {const [ blob, setBlob ] = useState<FBlob | null>();const requestIdRef = useRef(0);+ const missedContentChangeRef = useRef(false);function reportLoadState(state: "loading" | "loaded" | "error") {+ if (opts?.isVisible === false) {+ return;+ }opts?.reportLoadStateTo?.setContextData("contentLoad", { state, retry: () => refresh() });}@@ -821,9 +835,20 @@ export function useNoteBlob(note: FNote | null | undefined, componentId?: stringif (loadResults.isNoteContentReloaded(note.noteId, componentId)) {refresh();+ } 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.+ missedContentChangeRef.current = true;}});+ useEffect(() => {+ if (opts?.refreshOnShow && opts?.isVisible && missedContentChangeRef.current) {+ missedContentChangeRef.current = false;+ refresh();+ }+ }, [ opts?.isVisible ]); // eslint-disable-line react-hooks/exhaustive-deps+useDebugValue(note?.noteId);return blob;diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsxnew file mode 100644index 0000000000..e4638867d6--- /dev/null+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx@@ -0,0 +1,173 @@+/**+ * Regression tests for https://github.com/TriliumNext/Trilium/issues/10575+ * ("on bigger notes. The content of the note tends to 'refresh'").+ *+ * When a big text note opens in auto-read-only mode and the user temporarily enables+ * editing, the `ReadOnlyText` widget stays mounted (hidden) behind the editor. It reads+ * the note content via `useNoteBlob(note, undefined, { reportLoadStateTo: noteContext })`:+ * because no componentId is passed, the WS echo of the user's *own* spaced-update save+ * (which carries the saving component's id) is misclassified as a foreign change. The+ * hidden widget then refetches the whole blob after every save and publishes+ * `contentLoad: "loading"` to the shared note context, which makes the note-detail+ * loading overlay cover the visible editor — the content "disappears and reappears"+ * while typing.+ *+ * These tests assert the *correct* behavior, so they are red while the bug exists and+ * become the regression suite once it is fixed:+ * - an entity change originating from the widget's own component must trigger neither+ * a blob refetch nor a `contentLoad` publish;+ * - a foreign change may refetch, but a hidden widget must never publish `contentLoad`+ * (the overlay would cover the editor the user is typing into).+ */+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, describe, expect, it, vi } from "vitest";++import Component from "../../../components/component";+import NoteContext from "../../../components/note_context";+import LoadResults from "../../../services/load_results";+import { buildNote } from "../../../test/easy-froca";+import { ParentComponent } from "../../react/react_utils";+import ReadOnlyText from "./ReadOnlyText";++// Imported by ReadOnlyText only for its content styles; irrelevant (and heavy) in happy-dom.+vi.mock("@triliumnext/ckeditor5", () => ({}));++vi.stubGlobal("logError", vi.fn());+vi.stubGlobal("logInfo", vi.fn());++function setupHarness({ isVisible: initialIsVisible }: { isVisible: boolean }) {+ const note = buildNote({ title: "Big note", type: "text", content: "<p>hello</p>" });++ // Count blob fetches without changing what they resolve to.+ const originalGetBlob = note.getBlob.bind(note);+ const getBlobSpy = vi.fn(originalGetBlob);+ note.getBlob = getBlobSpy;++ const parent = new Component();+ const noteContext = new NoteContext("ro-ntx");++ // Record every contentLoad state published to the shared note context.+ const contentLoadStates: string[] = [];+ const originalSetContextData = noteContext.setContextData.bind(noteContext);+ noteContext.setContextData = ((key, value) => {+ if (key === "contentLoad" && value && typeof value === "object" && "state" in value) {+ contentLoadStates.push(String(value.state));+ }+ return originalSetContextData(key, value);+ }) as typeof noteContext.setContextData;++ const container = document.createElement("div");+ document.body.appendChild(container);++ async function mount(isVisible = initialIsVisible) {+ await act(async () => {+ render(+ <ParentComponent.Provider value={parent}>+ <ReadOnlyText+ note={note}+ noteContext={noteContext}+ ntxId={noteContext.ntxId}+ parentComponent={parent}+ viewScope={undefined}+ isVisible={isVisible}+ />+ </ParentComponent.Provider>,+ container+ );+ });+ // The blob resolves on a microtask after the first effect flush; a second act+ // cycle lets useNoteBlob's state update land and deliver the content.+ await act(async () => {});+ }++ async function fireContentChange(componentId: string) {+ const loadResults = new LoadResults([]);+ loadResults.addNoteContent(note.noteId, componentId);+ await act(async () => {+ await parent.handleEvent("entitiesReloaded", { loadResults });+ });+ await act(async () => {});+ }++ return { note, parent, noteContext, contentLoadStates, getBlobSpy, container, mount, fireContentChange };+}++describe("ReadOnlyText reacting to content changes (#10575)", () => {+ let cleanupContainer: HTMLElement | undefined;++ afterEach(() => {+ if (cleanupContainer) {+ render(null, cleanupContainer);+ cleanupContainer.remove();+ cleanupContainer = undefined;+ }+ });++ it("loads and renders the content on mount, reporting the load state", async () => {+ const harness = setupHarness({ isVisible: true });+ cleanupContainer = harness.container;++ await harness.mount();++ expect(harness.container.textContent).toContain("hello");+ expect(harness.getBlobSpy).toHaveBeenCalledTimes(1);+ // The initial fetch is exactly what the loading overlay exists for.+ expect(harness.contentLoadStates).toEqual([ "loading", "loaded" ]);+ });++ it("ignores the echo of a save made by its own component (no refetch, no overlay)", async () => {+ const harness = setupHarness({ isVisible: false });+ cleanupContainer = harness.container;++ await harness.mount();+ expect(harness.getBlobSpy).toHaveBeenCalledTimes(1); // sanity: initial load only++ harness.contentLoadStates.length = 0;++ // The user types into EditableText in the same split; the WS echo of the spaced-update+ // save carries the shared parent component's id.+ await harness.fireContentChange(harness.parent.componentId);++ // The change originated here — refetching the whole blob is pure waste...+ expect(harness.getBlobSpy).toHaveBeenCalledTimes(1);+ // ...and publishing "loading" makes the note-detail overlay cover the editor mid-typing.+ expect(harness.contentLoadStates).toEqual([]);+ });++ it("does not publish a load state while hidden, even for foreign changes", async () => {+ const harness = setupHarness({ isVisible: false });+ cleanupContainer = harness.container;++ await harness.mount();+ harness.contentLoadStates.length = 0;++ // A change from elsewhere (another split, another device via sync). A hidden widget+ // may refresh its content, but it must not drive the shared loading overlay.+ await harness.fireContentChange("ReactWrappedWidget-remote");++ expect(harness.contentLoadStates).not.toContain("loading");+ });++ it("catches up on skipped own-component changes when displayed again, but only then", async () => {+ const harness = setupHarness({ isVisible: false });+ cleanupContainer = harness.container;++ await harness.mount();+ expect(harness.getBlobSpy).toHaveBeenCalledTimes(1);++ // Becoming visible without having missed anything must not refetch (the user just+ // toggles between read-only and editable without editing).+ await harness.mount(true);+ expect(harness.getBlobSpy).toHaveBeenCalledTimes(1);++ // The user edits in the editable view (own-component echo while hidden)...+ await harness.mount(false);+ await harness.fireContentChange(harness.parent.componentId);+ expect(harness.getBlobSpy).toHaveBeenCalledTimes(1); // still skipped++ // ...and re-locks the note: the read-only view must not show stale content.+ await harness.mount(true);+ expect(harness.getBlobSpy).toHaveBeenCalledTimes(2);+ });+});diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsxindex 7f040d2f82..1d7446a012 100644--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx@@ -22,8 +22,17 @@ import { TypeWidgetProps } from "../type_widget";import { applyReferenceLinks } from "./read_only_helper";import { loadIncludedNote, refreshIncludedNote, setupImageOpening } from "./utils";-export default function ReadOnlyText({ note, noteContext, ntxId }: TypeWidgetProps) {- const blob = useNoteBlob(note, undefined, { reportLoadStateTo: noteContext });+export default function ReadOnlyText({ note, noteContext, ntxId, parentComponent, isVisible }: TypeWidgetProps) {+ // The componentId matters: the WS echo of a save made by the editable-text editor in the same+ // split carries the shared parent component's id. Without it, this widget — kept mounted but+ // 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 blob = useNoteBlob(note, parentComponent?.componentId, {+ reportLoadStateTo: noteContext,+ isVisible,+ refreshOnShow: true+ });const { isRtl } = useNoteLanguage(note);const readOnlyContentRef = usePreactRef<HTMLDivElement>(null);