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): offer the ways of opening a note from its marker's pane
Elian Doran4b833dcdaa55
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 151f0d5241..66338d2fbf 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2750,7 +2750,8 @@"zoom-out": "Zoom out","fullscreen": "Fullscreen","exit-fullscreen": "Exit fullscreen",- "close-details": "Close"+ "close-details": "Close",+ "open-note": "Open note"},"geo-map-context": {"open-location": "Open location",diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.css b/apps/client/src/widgets/collections/geomap/DetailPane.cssindex 13a94f7881..bef92b2bf6 100644--- a/apps/client/src/widgets/collections/geomap/DetailPane.css+++ b/apps/client/src/widgets/collections/geomap/DetailPane.css@@ -42,6 +42,21 @@z-index: 3;}+/*+ * The ways of opening the note, side by side at the head of the pane — where the first thing to be+ * done with a place is offered on any map that shows you one.+ *+ * Named by their tooltips rather than beside their icons, as every other row of actions in a panel+ * is (see the picture's row in NodePanel): a label apiece would have four of them wrapping onto two+ * lines in a pane this narrow, and these are the same four actions, under the same four icons, that+ * a note is opened by everywhere else in Trilium.+ */+.geo-map-container .geo-detail-pane .geo-detail-pane-actions {+ display: flex;+ align-items: center;+ gap: 4px;+}+/** The pane is dressed in the app's own colors rather than the map's, which is what the bar beside it* does (see MapToolbar.css): what stands here is a note, and a note belongs to the app — the samediff --git a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsxindex d614bd3960..8c311d2698 100644--- a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx@@ -2,14 +2,23 @@ import { render } from "preact";import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import appContext from "../../../components/app_context";import Component from "../../../components/component";import type FNote from "../../../entities/fnote";+import link from "../../../services/link";import { buildNote } from "../../../test/easy-froca";import { ParentComponent } from "../../react/react_utils";import DetailPane from "./DetailPane";import { ParentMap } from "./map";import { MARKER_LAYER } from "./Markers";+/** Navigating the tab the map is in, which is a module function rather than a command. */+const openInCurrentNoteContext = vi.fn();+vi.mock("../../../components/note_context", async (importOriginal) => ({+ ...(await importOriginal<object>()),+ openInCurrentNoteContext: (...args: unknown[]) => openInCurrentNoteContext(...args)+}));+/** What a marker click hands the handler, and what the pane reads the note out of. */type Listener = (e?: unknown) => void;@@ -64,9 +73,18 @@ describe("DetailPane", () => {beforeEach(() => {container = document.createElement("div");document.body.appendChild(container);++ // The pane asks where the reader is hoisted, which the app has settled by the time a map is+ // ever drawn and a test has not: `tabManager` is only built when the app starts.+ (appContext as unknown as { tabManager: unknown }).tabManager = {+ getActiveContext: () => undefined,+ openContextWithNote: async () => undefined+ };});afterEach(() => {+ (appContext as unknown as { tabManager: unknown }).tabManager = undefined;+if (container) {render(null, container);container.remove();@@ -220,6 +238,58 @@ describe("DetailPane", () => {expect(pane()).toBeNull();});+ describe("the ways of opening it", () => {+ /** Opens the pane on a note that hangs somewhere, so that there is a path to open. */+ async function openPaneFor(note: FNote, map: ReturnType<typeof fakeMap>) {+ await mount([ note ], map);+ map.setUnderPointer([ markerFeature(note) ]);+ await act(async () => map.click());+ }++ function press(icon: string) {+ container?.querySelector<HTMLButtonElement>(`.geo-detail-pane-actions button.${icon}`)?.click();+ }++ it("opens the note where every note in Trilium is opened", async () => {+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });+ const map = fakeMap();++ // What each of these opens is a path rather than an id, a note being reachable by more+ // than one. Which path that is belongs to the tree, not to the pane, so it is said here+ // instead of built: nothing in a froca raised for a test hangs under the root the real+ // one is searched from.+ const notePath = "root/places/somewhere";+ const bestNotePath = vi.spyOn(note, "getBestNotePathString").mockReturnValue(notePath);+ const openContextWithNote = vi.spyOn(appContext.tabManager, "openContextWithNote").mockResolvedValue(undefined as never);+ const triggerCommand = vi.spyOn(appContext, "triggerCommand").mockResolvedValue(undefined);+ const goToLinkExt = vi.spyOn(link, "goToLinkExt").mockReturnValue(true);++ try {+ await openPaneFor(note, map);++ press("bx-log-in");+ expect(openInCurrentNoteContext).toHaveBeenCalledWith(expect.anything(), notePath);++ press("bx-link-external");+ expect(openContextWithNote).toHaveBeenCalledWith(notePath, { hoistedNoteId: undefined });++ press("bx-window-open");+ expect(triggerCommand).toHaveBeenCalledWith("openInWindow", { notePath, hoistedNoteId: undefined });++ // Latitude first, as a geo URI is written and as the map's own menu hands one over —+ // the note stores it that way round, and the features the map draws do not.+ press("bx-map-alt");+ expect(goToLinkExt).toHaveBeenCalledWith(null, "geo:1,2");+ } finally {+ bestNotePath.mockRestore();+ openContextWithNote.mockRestore();+ triggerCommand.mockRestore();+ goToLinkExt.mockRestore();+ openInCurrentNoteContext.mockClear();+ }+ });+ });+it("keeps clicks and key presses from reaching the map underneath", async () => {const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });const map = fakeMap();diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.tsxindex 75886b3f63..2c3370e58f 100644--- a/apps/client/src/widgets/collections/geomap/DetailPane.tsx+++ b/apps/client/src/widgets/collections/geomap/DetailPane.tsx@@ -3,8 +3,12 @@ import "./DetailPane.css";import type { Map as MapLibreGLMap, MapMouseEvent } from "maplibre-gl";import { useContext, useEffect, useState } from "preact/hooks";+import appContext from "../../../components/app_context";+import { openInCurrentNoteContext } from "../../../components/note_context";import FNote from "../../../entities/fnote";import { t } from "../../../services/i18n";+import link from "../../../services/link";+import ActionButton from "../../react/ActionButton";import { useNoteIcon, useNoteLabel, useNoteProperty } from "../../react/hooks";import OverlayPanel, { OverlayPanelBody, OverlayPanelTitle } from "../../react/OverlayPanel";import { ParentMap } from "./map";@@ -143,9 +147,63 @@ function MarkerDetails({ note, onClose }: { note: FNote; onClose(): void }) {header={<OverlayPanelTitle icon={icon ?? note.getIcon()} text={title ?? note.title} />}close={{ text: t("geo-map.close-details"), onClick: onClose }}>- {/* Nothing is made of the note yet — what stands here is the wiring, and the pane is- filled in a step of its own. */}- <OverlayPanelBody>Hello world</OverlayPanelBody>+ <OverlayPanelBody>+ <MarkerActions note={note} />+ </OverlayPanelBody></OverlayPanel>);}++/**+ * What can be done with the note behind the marker: the ways of opening it, and the way of opening+ * where it is.+ *+ * These are the ways every note in Trilium is opened, which is why they are named as they are named+ * everywhere else — the same strings the link menu offers them under. What is not among them is+ * the quick editor: it used to be what a click on a marker raised, and the pane took that click+ * over, so offering it here would only put a modal back over the pane that replaced it.+ */+function MarkerActions({ note }: { note: FNote }) {+ const [ location ] = useNoteLabel(note, LOCATION_ATTRIBUTE);+ const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId;+ // The path the note is best reached by from wherever the reader is hoisted, rather than its id:+ // a note may hang in several places, and every one of these opens a path.+ const notePath = note.getBestNotePathString(hoistedNoteId);+ const latLng = parseLocation(location);++ return (+ <div className="geo-detail-pane-actions">+ <ActionButton+ icon="bx bx-log-in"+ text={t("geo-map.open-note")}+ // Handed the event so that the note lands in the pane's own tab, which is the one+ // the map is in — `openInCurrentNoteContext` reads it off the element clicked.+ onClick={(e) => notePath && openInCurrentNoteContext(e as MouseEvent, notePath)}+ disabled={!notePath}+ />++ <ActionButton+ icon="bx bx-link-external"+ text={t("link_context_menu.open_note_in_new_tab")}+ onClick={() => notePath && appContext.tabManager.openContextWithNote(notePath, { hoistedNoteId })}+ disabled={!notePath}+ />++ <ActionButton+ icon="bx bx-window-open"+ text={t("link_context_menu.open_note_in_new_window")}+ onClick={() => notePath && appContext.triggerCommand("openInWindow", { notePath, hoistedNoteId })}+ disabled={!notePath}+ />++ <ActionButton+ icon="bx bx-map-alt"+ text={t("geo-map-context.open-location")}+ // Where the note is, handed to whatever the system opens a place with, as the map's+ // own right-click menu does with the point that was clicked.+ onClick={() => latLng && link.goToLinkExt(null, `geo:${latLng[1]},${latLng[0]}`)}+ disabled={!latLng}+ />+ </div>+ );+}