Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/include notes: fix some issues

Adorian Doran1e82ef0bd049
diff --git a/apps/client/src/menus/link_context_menu.spec.ts b/apps/client/src/menus/link_context_menu.spec.ts
index 66e1bc7762..40c4dd5db3 100644
--- a/apps/client/src/menus/link_context_menu.spec.ts
+++ b/apps/client/src/menus/link_context_menu.spec.ts
@@ -209,6 +209,19 @@ describe("handleLinkContextMenuItem", () => {
});
});
+describe("getOriginBelow", () => {
+ it("opens a menu below the anchor, from the anchor or from another element", () => {
+ const anchor = document.createElement("button");
+ anchor.getBoundingClientRect = () => ({ left: 40, bottom: 70 }) as DOMRect;
+ const include = document.createElement("figure");
+ const below = { pageX: 40 + window.scrollX, pageY: 70 + window.scrollY };
+
+ expect(linkContextMenu.getOriginBelow(anchor)).toEqual({ ...below, target: anchor });
+ expect(linkContextMenu.getOriginBelow(anchor, include))
+ .toEqual({ ...below, target: include });
+ });
+});
+
describe("openContextMenu", () => {
it("shows the menu at the pointer and routes the choice with the link's state", async () => {
const event = contextMenuEvent();
diff --git a/apps/client/src/menus/link_context_menu.ts b/apps/client/src/menus/link_context_menu.ts
index dfd2f52fdb..8297f5c131 100644
--- a/apps/client/src/menus/link_context_menu.ts
+++ b/apps/client/src/menus/link_context_menu.ts
@@ -67,6 +67,12 @@ function keepEditorFocused(
};
}
+/** The origin of a menu that opens below `anchor` and acts from `target`. */
+function getOriginBelow(anchor: Element, target: Element = anchor): LinkMenuOrigin {
+ const { left, bottom } = anchor.getBoundingClientRect();
+ return { pageX: left + window.scrollX, pageY: bottom + window.scrollY, target };
+}
+
function getItems(e: LinkMenuOrigin | GeoMouseEvent): MenuItem<CommandNames>[] {
return [ ...getOpenItems(e), getQuickEditItem() ];
}
@@ -250,6 +256,7 @@ function getNtxId(e: LinkMenuOrigin | GeoMouseEvent) {
export default {
getItems,
+ getOriginBelow,
getQuickEditItem,
getOpenNoteItem,
handleLinkContextMenuItem,
diff --git a/apps/client/src/widgets/type_widgets/text/IncludeNote.spec.tsx b/apps/client/src/widgets/type_widgets/text/IncludeNote.spec.tsx
index 77806cc40a..34f65c5f97 100644
--- a/apps/client/src/widgets/type_widgets/text/IncludeNote.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/IncludeNote.spec.tsx
@@ -7,11 +7,13 @@ const {
openTabWithNoteWithHoisting,
triggerCommand,
openContextMenu,
+ getOriginBelow,
disposeInteractiveContent
} = vi.hoisted(() => ({
openTabWithNoteWithHoisting: vi.fn(),
triggerCommand: vi.fn(),
openContextMenu: vi.fn(),
+ getOriginBelow: vi.fn((anchor: Element) => ({ below: anchor })),
disposeInteractiveContent: vi.fn()
}));
@@ -19,7 +21,9 @@ vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));
vi.mock("../../../components/app_context", () => ({
default: { tabManager: { openTabWithNoteWithHoisting }, triggerCommand }
}));
-vi.mock("../../../menus/link_context_menu", () => ({ default: { openContextMenu } }));
+vi.mock("../../../menus/link_context_menu", () => ({
+ default: { openContextMenu, getOriginBelow }
+}));
vi.mock("../../../services/content_renderer", () => ({ default: { disposeInteractiveContent } }));
import IncludeNote, {
@@ -207,9 +211,9 @@ describe("IncludeNote", () => {
placement: "afterCurrent"
});
- const { event, isStopped } = click(menu);
- expect(isStopped).toBe(true);
- expect(openContextMenu).toHaveBeenCalledWith("owner", event, ATTACHMENT_SCOPE);
+ expect(click(menu).isStopped).toBe(true);
+ expect(openContextMenu)
+ .toHaveBeenCalledWith("owner", { below: menu }, ATTACHMENT_SCOPE);
});
it("gives its content the screen, and takes it back from the overlay button", async () => {
@@ -292,9 +296,10 @@ describe("TinyIncludeNote", () => {
expect(click(download).isStopped).toBe(true);
expect(run).toHaveBeenCalledOnce();
- const { event, isStopped } = click(button("include-note-menu"));
- expect(isStopped).toBe(true);
- expect(openContextMenu).toHaveBeenCalledWith("owner", event, ATTACHMENT_SCOPE);
+ const menu = button("include-note-menu");
+ expect(click(menu).isStopped).toBe(true);
+ expect(openContextMenu)
+ .toHaveBeenCalledWith("owner", { below: menu }, ATTACHMENT_SCOPE);
});
it("leaves out the description line when there is none", () => {
diff --git a/apps/client/src/widgets/type_widgets/text/IncludeNote.tsx b/apps/client/src/widgets/type_widgets/text/IncludeNote.tsx
index becd16a465..022f2197c9 100644
--- a/apps/client/src/widgets/type_widgets/text/IncludeNote.tsx
+++ b/apps/client/src/widgets/type_widgets/text/IncludeNote.tsx
@@ -209,7 +209,8 @@ function MoreActionsButton({ notePath, viewScope }: { notePath: string; viewScop
text={t("common.more_actions")}
onClick={(e) => {
e.stopPropagation();
- void linkContextMenu.openContextMenu(notePath, e, viewScope);
+ const origin = linkContextMenu.getOriginBelow(e.currentTarget);
+ void linkContextMenu.openContextMenu(notePath, origin, viewScope);
}}
/>
);
diff --git a/apps/client/src/widgets/type_widgets/text/utils.spec.ts b/apps/client/src/widgets/type_widgets/text/utils.spec.ts
index 6223d11d2c..1dac1f9479 100644
--- a/apps/client/src/widgets/type_widgets/text/utils.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/utils.spec.ts
@@ -28,7 +28,10 @@ vi.mock("../../../services/attachment_actions", () => ({
getDownloadAction: vi.fn()
}));
vi.mock("../../../menus/link_context_menu", () => ({
- default: { openContextMenu: vi.fn() }
+ default: {
+ openContextMenu: vi.fn(),
+ getOriginBelow: vi.fn((anchor: Element, target: Element) => ({ anchor, target }))
+ }
}));
import linkContextMenu from "../../../menus/link_context_menu";
@@ -90,8 +93,18 @@ function lastMount() {
return { type: vnode.type, props: vnode.props, container: call[1] };
}
+afterEach(() => {
+ document.body.replaceChildren();
+});
+
+/** Puts `element` in the page, where the includes that load are. */
+function inPage<T extends HTMLElement>(element: T) {
+ document.body.append(element);
+ return element;
+}
+
function createWrapper() {
- const wrapper = document.createElement("div");
+ const wrapper = inPage(document.createElement("div"));
wrapper.className = "include-note-wrapper";
return wrapper;
}
@@ -135,7 +148,7 @@ describe("loadIncludedNote", () => {
it("mounts a tiny box with the note's path, without the note or a caption", async () => {
const actions = [ action("edit"), action("open") ];
vi.mocked(getNoteActions).mockReturnValue(actions);
- const figure = document.createElement("figure");
+ const figure = inPage(document.createElement("figure"));
figure.className = "include-note";
figure.dataset.boxSize = "tiny";
figure.innerHTML = "<figcaption>Caption</figcaption>";
@@ -227,7 +240,7 @@ describe("loadIncludedAttachment", () => {
describe("the element an include box is mounted in", () => {
it("is a wrapper created in a read-only include ahead of its caption", async () => {
- const figure = document.createElement("figure");
+ const figure = inPage(document.createElement("figure"));
figure.className = "include-note";
figure.dataset.boxSize = "expandable";
figure.innerHTML = "&nbsp;<figcaption>Caption</figcaption>";
@@ -248,7 +261,7 @@ describe("the element an include box is mounted in", () => {
});
it("is a wrapper created in a legacy <section> include", async () => {
- const section = document.createElement("section");
+ const section = inPage(document.createElement("section"));
section.className = "include-note";
section.dataset.boxSize = "medium";
@@ -259,7 +272,7 @@ describe("the element an include box is mounted in", () => {
});
it("is the wrapper an editor include holds, leaving the editor's own elements", async () => {
- const figure = document.createElement("figure");
+ const figure = inPage(document.createElement("figure"));
figure.className = "include-note ck-widget";
figure.dataset.boxSize = "full";
const wrapper = createWrapper();
@@ -275,9 +288,36 @@ describe("the element an include box is mounted in", () => {
});
});
+describe("an include that leaves the page while it loads", () => {
+ it("gets no box, and the content rendered for it is disposed", async () => {
+ const loads: [ (wrapper: HTMLElement) => Promise<void>, boolean ][] = [
+ [ (wrapper) => loadIncludedNote("noteY", $(wrapper), "medium"), true ],
+ [ (wrapper) => loadIncludedNote("noteY", $(wrapper), "tiny"), false ],
+ [ (wrapper) => loadIncludedAttachment("att1", $(wrapper), "full"), true ],
+ [ (wrapper) => loadIncludedAttachment("att1", $(wrapper), "tiny"), false ]
+ ];
+
+ for (const [ load, rendersContent ] of loads) {
+ vi.mocked(content_renderer.mountInteractiveWidget).mockClear();
+ vi.mocked(content_renderer.disposeInteractiveContent).mockClear();
+ const wrapper = createWrapper();
+ vi.mocked(link.createLink).mockImplementation(async () => {
+ wrapper.remove();
+ return $(title);
+ });
+
+ await load(wrapper);
+
+ expect(content_renderer.mountInteractiveWidget).not.toHaveBeenCalled();
+ expect(vi.mocked(content_renderer.disposeInteractiveContent).mock.calls
+ .map(([ $element ]) => $element[0])).toEqual(rendersContent ? [ content ] : []);
+ }
+ });
+});
+
describe("refreshIncludedNote", () => {
it("reloads every include of the note, of either element", async () => {
- const container = document.createElement("div");
+ const container = inPage(document.createElement("div"));
container.innerHTML = `<figure class="include-note" data-note-id="noteY"></figure>`
+ `<section class="include-note" data-note-id="noteY"></section>`
+ `<figure class="include-note" data-note-id="other"></figure>`;
@@ -348,29 +388,27 @@ describe("watchIncludedNotes", () => {
describe("openIncludeNoteMenu", () => {
it("opens the menu of the note or attachment an include shows, below the anchor", async () => {
const anchor = document.createElement("button");
- anchor.getBoundingClientRect = () => ({ left: 40, bottom: 70 }) as DOMRect;
- const origin = { pageX: 40 + window.scrollX, pageY: 70 + window.scrollY };
- const noteInclude = document.createElement("figure");
+ const noteInclude = inPage(document.createElement("figure"));
noteInclude.dataset.noteId = "noteY";
- const embed = document.createElement("figure");
+ const embed = inPage(document.createElement("figure"));
embed.dataset.attachmentId = "att1";
await openIncludeNoteMenu(noteInclude, anchor);
await openIncludeNoteMenu(embed, anchor);
expect(vi.mocked(linkContextMenu.openContextMenu).mock.calls).toEqual([
- [ "noteY", { ...origin, target: noteInclude }, {} ],
- [ "owner", { ...origin, target: embed }, ATTACHMENT_SCOPE ]
+ [ "noteY", { anchor, target: noteInclude }, {} ],
+ [ "owner", { anchor, target: embed }, ATTACHMENT_SCOPE ]
]);
});
it("opens nothing for an include of a deleted attachment, or of nothing", async () => {
vi.mocked(froca.getAttachment).mockResolvedValue(null);
- const embed = document.createElement("figure");
+ const embed = inPage(document.createElement("figure"));
embed.dataset.attachmentId = "att1";
await openIncludeNoteMenu(embed, document.createElement("button"));
- await openIncludeNoteMenu(document.createElement("figure"), document.createElement("button"));
+ await openIncludeNoteMenu(inPage(document.createElement("figure")), document.createElement("button"));
expect(linkContextMenu.openContextMenu).not.toHaveBeenCalled();
});
diff --git a/apps/client/src/widgets/type_widgets/text/utils.ts b/apps/client/src/widgets/type_widgets/text/utils.ts
index b0ccd36014..6b5f24a9eb 100644
--- a/apps/client/src/widgets/type_widgets/text/utils.ts
+++ b/apps/client/src/widgets/type_widgets/text/utils.ts
@@ -1,5 +1,5 @@
import { attachmentIcon } from "@triliumnext/commons";
-import { h } from "preact";
+import { h, type JSX } from "preact";
import appContext from "../../../components/app_context";
import linkContextMenu from "../../../menus/link_context_menu";
@@ -31,7 +31,7 @@ export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>,
notePath: note.noteId,
actions: getNoteActions(note.noteId)
});
- await content_renderer.mountInteractiveWidget(box, getWrapper(el));
+ await mountIncludeBox(el, box);
return;
}
@@ -55,7 +55,7 @@ export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>,
contentType: type,
notePath: note.noteId
});
- await content_renderer.mountInteractiveWidget(box, getWrapper(el));
+ await mountIncludeBox(el, box, $renderedContent);
}
/**
@@ -87,7 +87,7 @@ export async function loadIncludedAttachment(
viewScope,
actions: [ getOpenExternallyAction(attachment), getDownloadAction(attachment) ]
});
- await content_renderer.mountInteractiveWidget(box, getWrapper(el));
+ await mountIncludeBox(el, box);
return;
}
@@ -109,6 +109,21 @@ export async function loadIncludedAttachment(
notePath: attachment.ownerId,
viewScope
});
+ await mountIncludeBox(el, box, $renderedContent);
+}
+
+/**
+ * Mounts `box` in the include `el`. An include that left the page while it loaded gets no box,
+ * and `content` rendered for it is disposed: `watchIncludedNotes()` has already passed it.
+ */
+async function mountIncludeBox(el: HTMLElement, box: JSX.Element, content?: JQuery<HTMLElement>) {
+ if (!el.isConnected) {
+ if (content) {
+ content_renderer.disposeInteractiveContent(content);
+ }
+ return;
+ }
+
await content_renderer.mountInteractiveWidget(box, getWrapper(el));
}
@@ -144,12 +159,7 @@ function getWrapper(el: HTMLElement) {
*/
export async function openIncludeNoteMenu(include: HTMLElement, anchor: HTMLElement) {
const { noteId, attachmentId } = include.dataset;
- const { left, bottom } = anchor.getBoundingClientRect();
- const origin = {
- pageX: left + window.scrollX,
- pageY: bottom + window.scrollY,
- target: include
- };
+ const origin = linkContextMenu.getOriginBelow(anchor, include);
if (attachmentId) {
const attachment = await froca.getAttachment(attachmentId, true);