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/ui/card: fix some issues
Adorian Dorancfe7ce0e2ad7
diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsxindex 2dd0fb6df6..2c16a45bf2 100644--- a/apps/client/src/widgets/collections/board/card.spec.tsx+++ b/apps/client/src/widgets/collections/board/card.spec.tsx@@ -139,27 +139,39 @@ describe("Board card", () => {const link = card(first).querySelector<HTMLElement>(".relation-link");if (!link) throw new Error("expected the relation link");+ const reachedPage = vi.fn();+ document.addEventListener("click", reachedPage);const press = new MouseEvent("click", { bubbles: true, cancelable: true, detail: 1 });await act(async () => { link.dispatchEvent(press); });+ document.removeEventListener("click", reachedPage);expect(opened).toHaveBeenCalledTimes(1);expect(opened).toHaveBeenCalledWith("openInPopup", { noteIdOrPath: "root/targetNote" });- // Taken here, so the page's own handler never sees it and no tab is opened.+ // Taken here, so `goToLink` never sees it and opens no tab for it.expect(press.defaultPrevented).toBe(true);- expect(press.cancelBubble).toBe(true);+ expect(reachedPage).not.toHaveBeenCalled();});- /** A link out of Trilium is the browser's to follow, and opens no card over it. */- it("leaves a link that names no note alone", async () => {+ /**+ * A link naming no note is `goToLink`'s to open, and the card neither opens itself over it nor+ * takes the press away from the handler that would follow it.+ */+ it("leaves a link that names no note to the page's own handler", async () => {const { first } = await renderBoard();const opened = vi.spyOn(appContext, "triggerCommand").mockReturnValue(undefined);opened.mockClear();+ const link = card(first).querySelector<HTMLElement>(".outside-link");+ if (!link) throw new Error("expected the outside link");- await act(async () => {- card(first).querySelector<HTMLElement>(".outside-link")?.click();- });+ const reachedPage = vi.fn();+ document.addEventListener("click", reachedPage);+ const press = new MouseEvent("click", { bubbles: true, cancelable: true, detail: 1 });+ await act(async () => { link.dispatchEvent(press); });+ document.removeEventListener("click", reachedPage);expect(opened).not.toHaveBeenCalled();+ // Reaches `goToLink`, which is what follows an address of its own.+ expect(reachedPage).toHaveBeenCalled();});/**diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsxindex 6bc37d140c..cb481aee2c 100644--- a/apps/client/src/widgets/collections/board/card.tsx+++ b/apps/client/src/widgets/collections/board/card.tsx@@ -91,17 +91,18 @@ function Card({// popup already standing is taken as the one to stack on, and closing that leaves neither.if (e.detail > 1) return;- // A link the card draws, such as the target of a relation, names a note of its own. It is- // opened in the popup like any other card, and the press is taken here so that neither the- // page's own link handler (which would open a tab) nor the card below it acts on it too.+ // A link to a note, such as a relation's target, opens in the popup. Cancelled here so that+ // `goToLink` does not open a tab for it as well; a link naming no note is left alone.const link = (e.target as HTMLElement | null)?.closest<HTMLAnchorElement>("a[href]");if (link) {const { notePath } = parseNavigationStateFromUrl(link.getAttribute("href") ?? undefined);+ if (!notePath) {+ return;+ }+e.preventDefault();e.stopPropagation();- if (notePath) {- api.openNote(notePath);- }+ api.openNote(notePath);return;}