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
feat(geomap): follow a link to another marker without leaving the map
Elian Doran1c42361585cf
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsxindex 33f93e44b6..3a558eace2 100644--- a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx@@ -470,6 +470,89 @@ describe("DetailPane", () => {});});+ /**+ * Following a link inside the pane. One that points at another marker of this map moves the+ * selection — the pane switches, the map pans along — instead of navigating the whole tab away+ * from the map; every other link, and every click asking for more than a plain navigation,+ * keeps meaning what it means anywhere in the app.+ */+ describe("following a link inside the pane", () => {+ function twoMarkersAndAStray() {+ buildNote({ id: "root", title: "root", children: [+ { id: "fromhere", title: "From here", "#geolocation": "1,2" },+ { id: "tothere", title: "To there", "#geolocation": "3,4" },+ { id: "offmap", title: "Off the map" }+ ] });+ return [ froca.notes["fromhere"], froca.notes["tothere"], froca.notes["offmap"] ];+ }++ async function openPaneOn(note: FNote, notes: FNote[], map: ReturnType<typeof fakeMap>) {+ await mount(notes, map);+ map.setUnderPointer([ markerFeature(note) ]);+ await act(async () => map.click());+ await settle();+ openInCurrentNoteContext.mockClear();+ }++ /** A link as the note's content carries one, clicked as the browser reports it. */+ function clickLink(noteId: string, init: MouseEventInit = {}) {+ const anchor = document.createElement("a");+ anchor.setAttribute("href", `#root/${noteId}`);+ pane()?.querySelector(".geo-detail-pane-body")?.appendChild(anchor);++ const event = new MouseEvent("click", { bubbles: true, cancelable: true, ...init });+ // A real left click says `which: 1`, which is what the app's own link handler goes by;+ // the DOM stand-in says 0 for every button, which it reads as no click at all.+ Object.defineProperty(event, "which", { value: 1 });+ return anchor.dispatchEvent(event);+ }++ it("follows a link to another marker by switching the pane, and pans to it", async () => {+ const notes = twoMarkersAndAStray();+ const map = fakeMap();+ await openPaneOn(notes[0], notes, map);+ expect(pane()?.textContent).toContain("From here");+ const easedBefore = map.eased.length;++ let defaultKept = true;+ await act(async () => { defaultKept = clickLink("tothere"); });+ await settle();++ // The pane switched and the map went along; the tab underneath heard nothing of it.+ expect(defaultKept).toBe(false);+ expect(pane()?.textContent).toContain("To there");+ expect(map.eased.length).toBeGreaterThan(easedBefore);+ expect(openInCurrentNoteContext).not.toHaveBeenCalled();+ });++ it("leaves a link alone when its note does not stand on this map", async () => {+ const notes = twoMarkersAndAStray();+ const map = fakeMap();+ await openPaneOn(notes[0], notes, map);++ await act(async () => { clickLink("offmap"); });+ await settle();++ // The click fell through to the app's own handler, and the pane stayed where it was.+ expect(openInCurrentNoteContext).toHaveBeenCalledWith(expect.anything(), "root/offmap", expect.anything());+ expect(pane()?.textContent).toContain("From here");+ });++ it("leaves a modified click to mean a new tab, marker or not", async () => {+ const notes = twoMarkersAndAStray();+ const map = fakeMap();+ const openTabWithNoteWithHoisting = vi.fn();+ (appContext.tabManager as unknown as Record<string, unknown>).openTabWithNoteWithHoisting = openTabWithNoteWithHoisting;+ await openPaneOn(notes[0], notes, map);++ await act(async () => { clickLink("tothere", { ctrlKey: true }); });+ await settle();++ expect(openTabWithNoteWithHoisting).toHaveBeenCalledWith("root/tothere", expect.anything());+ expect(pane()?.textContent).toContain("From here");+ });+ });+describe("the note itself", () => {async function openPane(map: ReturnType<typeof fakeMap>) {buildNote({ id: "root", title: "root", children: [ { id: "somewhere", title: "Somewhere", "#geolocation": "1,2" } ] });diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.tsxindex 0db8d7085c..95707ab870 100644--- a/apps/client/src/widgets/collections/geomap/DetailPane.tsx+++ b/apps/client/src/widgets/collections/geomap/DetailPane.tsx@@ -96,6 +96,20 @@ export default function DetailPane({ notes, placing, isReadOnly, selection, onSeonRelocate(note.noteId);}, [ note, closePane, onRelocate ]);+ /**+ * Follows a link inside the pane whose note stands on this map: the pane switches to it — the+ * map panning along, through the easing effect below — the same as if its marker had been+ * clicked. Answers whether the note stands on the map at all, a link to anything else keeping+ * its ordinary meaning (see the interception in MarkerDetails).+ */+ const followLink = useCallback((noteId: string) => {+ const target = notes.find((n) => n.noteId === noteId);+ if (!target || !parseLocation(target.getLabelValue(LOCATION_ATTRIBUTE))) return false;++ onSelect({ noteId });+ return true;+ }, [ notes, onSelect ]);+// A note no longer on the map takes the pane with it. Its location may merely have been cleared// — which is all "remove from map" does — so the note being gone is not the only case.useEffect(() => {@@ -161,7 +175,7 @@ export default function DetailPane({ notes, placing, isReadOnly, selection, onSe// TitleRow), and answers to the pane's own component rather than the map's (see below).<ParentComponent.Provider value={paneComponent}><NoteContextContext.Provider value={noteContext}>- <MarkerDetails note={note} isReadOnly={isReadOnly} onClose={closePane} onRelocate={relocate} />+ <MarkerDetails note={note} isReadOnly={isReadOnly} onClose={closePane} onRelocate={relocate} onFollowLink={followLink} />{selection?.isNew && <SelectTitleOnFirstOpen />}</NoteContextContext.Provider></ParentComponent.Provider>@@ -223,7 +237,14 @@ function paneOffset(map: MapLibreGLMap): [number, number] {}/** The pane itself, for a marker there is one to draw. */-function MarkerDetails({ note, isReadOnly, onClose, onRelocate }: { note: FNote; isReadOnly: boolean; onClose(): void; onRelocate(): void }) {+function MarkerDetails({ note, isReadOnly, onClose, onRelocate, onFollowLink }: {+ note: FNote;+ isReadOnly: boolean;+ onClose(): void;+ onRelocate(): void;+ /** Offered a link's note; answers whether the map took the navigation over. */+ onFollowLink(noteId: string): boolean;+}) {// The marker's own colour, which is what dresses its icon (see DetailPane.css) — as the quick// editor's wrapper carries it. Without it the pane would inherit the hue of the note split it// stands in, which is the map's colour and not the marker's.@@ -240,6 +261,42 @@ function MarkerDetails({ note, isReadOnly, onClose, onRelocate }: { note: FNote;const paneRef = useRef<HTMLDivElement>(null);useLegacyComponentElement(paneRef);+ /*+ * Links followed within the pane. One that points at another marker of this map switches the+ * pane to it — the map panning along — instead of navigating the whole tab away from the map.+ * Every other link keeps meaning what it means anywhere else, as does every way of asking for+ * more than a plain navigation.+ *+ * Captured on the pane's own element, so it goes ahead of the document-level handler every+ * link click otherwise lands in (see the delegated listeners in link.ts) — and of the editor's.+ */+ useEffect(() => {+ const pane = paneRef.current;+ if (!pane) return;++ const onClick = (e: MouseEvent) => {+ // A modified click asks for a new tab or window, neither of which the pane answers.+ if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;++ const anchor = e.target instanceof Element ? e.target.closest("a") : null;+ const href = anchor?.getAttribute("href") ?? anchor?.getAttribute("data-href");+ if (!href) return;++ // A link that says how it wants to be opened — in a popup, at an attachment, at a+ // bookmark, in a named tab — is left to say it; only the plain "go to this note" is+ // the pane's to take, and only for a note the map can go to.+ const { noteId, ntxId, viewScope, openInPopup } = link.parseNavigationStateFromUrl(href);+ if (!noteId || ntxId || openInPopup || viewScope?.viewMode !== "default"+ || viewScope.attachmentId || viewScope.bookmark || !onFollowLink(noteId)) return;++ e.preventDefault();+ e.stopPropagation();+ };++ pane.addEventListener("click", onClick, true);+ return () => pane.removeEventListener("click", onClick, true);+ }, [ onFollowLink ]);+return (<OverlayPanelcontainerRef={paneRef}