Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): avoid nesting a redundant include-note-wrapper

Elian Doran26e439a0cf5a
diff --git a/apps/client/src/widgets/type_widgets/text/utils.spec.ts b/apps/client/src/widgets/type_widgets/text/utils.spec.ts
new file mode 100644
index 0000000000..9eafb1d3de
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/text/utils.spec.ts
@@ -0,0 +1,61 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import type FNote from "../../../entities/fnote";
+
+vi.mock("../../../services/froca", () => ({
+ default: { getNote: vi.fn() }
+}));
+vi.mock("../../../services/link", () => ({
+ default: { createLink: vi.fn() }
+}));
+vi.mock("../../../services/content_renderer", () => ({
+ default: { getRenderedContent: vi.fn(), disposeInteractiveContent: vi.fn() }
+}));
+
+import content_renderer from "../../../services/content_renderer";
+import froca from "../../../services/froca";
+import link from "../../../services/link";
+import { loadIncludedNote } from "./utils";
+
+const note = { noteId: "noteY" } as unknown as FNote;
+
+describe("loadIncludedNote", () => {
+ beforeEach(() => {
+ vi.mocked(froca.getNote).mockResolvedValue(note);
+ vi.mocked(link.createLink).mockResolvedValue($('<span class="link"><a href="#">noteY</a></span>'));
+ vi.mocked(content_renderer.getRenderedContent).mockResolvedValue({ $renderedContent: $("<p>body</p>"), type: "text" } as never);
+ vi.mocked(content_renderer.disposeInteractiveContent).mockReset();
+ });
+
+ it("reuses the wrapper element without nesting a second one (editing-view path)", async () => {
+ // The editing-view downcast hands us the `.include-note-wrapper` element itself.
+ const $el = $('<div class="include-note-wrapper">');
+
+ await loadIncludedNote("noteY", $el, "small");
+
+ const wrappers = $el.find(".include-note-wrapper");
+ expect(wrappers.length).toBe(0);
+ expect($el.children(".include-note-title").length).toBe(1);
+ expect($el.children(".include-note-content").length).toBe(1);
+ });
+
+ it("builds a single wrapper inside the section (read-only / refresh path)", async () => {
+ // The read-only and refresh paths hand us the outer `section.include-note`.
+ const $el = $('<section class="include-note" data-note-id="noteY">');
+
+ await loadIncludedNote("noteY", $el, "small");
+
+ const wrappers = $el.find(".include-note-wrapper");
+ expect(wrappers.length).toBe(1);
+ expect(wrappers.children(".include-note-title").length).toBe(1);
+ expect(wrappers.children(".include-note-content").length).toBe(1);
+ });
+
+ it("disposes interactive content of a previous render before replacing it", async () => {
+ const $el = $('<div class="include-note-wrapper">');
+
+ await loadIncludedNote("noteY", $el, "small");
+
+ expect(content_renderer.disposeInteractiveContent).toHaveBeenCalledWith($el);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/text/utils.ts b/apps/client/src/widgets/type_widgets/text/utils.ts
index c1041bd48e..67bfb3468f 100644
--- a/apps/client/src/widgets/type_widgets/text/utils.ts
+++ b/apps/client/src/widgets/type_widgets/text/utils.ts
@@ -13,6 +13,12 @@ export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>,
const effectiveBoxSize = boxSize ?? $el.closest('section.include-note').attr('data-box-size');
const isExpandable = effectiveBoxSize === 'expandable';
+ // The editing-view downcast passes the `.include-note-wrapper` element itself as $el, whereas the
+ // read-only and refresh paths pass the outer `section.include-note`. Build the content in a
+ // detached wrapper either way (so the old content stays visible during the async render — no
+ // flicker), then swap it in; when $el is already the wrapper we move the built children straight
+ // into it instead of nesting a redundant second `.include-note-wrapper`.
+ const isWrapper = $el.hasClass('include-note-wrapper');
const $wrapper = $('<div class="include-note-wrapper">');
const $link = await link.createLink(note.noteId, {
showTooltip: false,
@@ -52,7 +58,7 @@ export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>,
// change or refreshIncludedNote) before $el.empty() discards their DOM — otherwise their
// standalone Preact roots (collections, web views) would leak.
content_renderer.disposeInteractiveContent($el);
- $el.empty().append($wrapper);
+ $el.empty().append(isWrapper ? $wrapper.children() : $wrapper);
}
export function refreshIncludedNote(container: HTMLDivElement, noteId: string) {