Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): open the pane for a GPX track clicked like any marker

Elian Doran3e36c1da7e66
diff --git a/apps/client/src/test/easy-froca.ts b/apps/client/src/test/easy-froca.ts
index d3ae75718f..049ce07ad3 100644
--- a/apps/client/src/test/easy-froca.ts
+++ b/apps/client/src/test/easy-froca.ts
@@ -15,6 +15,7 @@ interface NoteDefinition extends AttributeDefinitions, RelationDefinitions {
id?: string | undefined;
title: string;
type?: NoteType;
+ mime?: string;
children?: NoteDefinition[];
content?: string;
}
@@ -49,7 +50,7 @@ export function buildNote(noteDef: NoteDefinition) {
noteId: noteDef.id ?? utils.randomString(12),
title: noteDef.title,
type: noteDef.type ?? "text",
- mime: "text/html",
+ mime: noteDef.mime ?? "text/html",
isProtected: false,
blobId: ""
});
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
index 3a558eace2..6304167aee 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
@@ -16,6 +16,7 @@ import { buildNote } from "../../../test/easy-froca";
import { useLegacyImperativeHandlers, useNoteContext, useTriliumEvent } from "../../react/hooks";
import { ParentComponent } from "../../react/react_utils";
import DetailPane, { OTHER_WAYS_TO_OPEN, PaneSelection } from "./DetailPane";
+import { GPX_MIME } from "./GpxTrack";
import { ParentMap } from "./map";
import { MARKER_LAYER } from "./Markers";
@@ -100,7 +101,9 @@ function fakeMap({ width = MAP_WIDTH, features = [] as unknown[] } = {}) {
return layers.includes(MARKER_LAYER) ? under : [];
},
easeTo(options: unknown) { eased.push(options); },
- getContainer: () => ({ clientWidth: width })
+ getContainer: () => ({ clientWidth: width }),
+ // Asked for by `trackHitLayers`, which reads the current GPX hit layers off the style.
+ getLayersOrder: () => [] as string[]
};
}
@@ -109,6 +112,11 @@ function markerFeature(note: FNote, coordinates: [number, number] = [ 2, 1 ]) {
return { geometry: { type: "Point", coordinates }, properties: { id: note.noteId } };
}
+/** A GPX track's hit line, as MapLibre reports one — the note carried in the feature (see GpxTrack). */
+function trackFeature(note: FNote) {
+ return { geometry: { type: "MultiLineString", coordinates: [] }, properties: { id: note.noteId } };
+}
+
describe("DetailPane", () => {
let container: HTMLElement | undefined;
@@ -212,6 +220,27 @@ describe("DetailPane", () => {
expect(pane()?.querySelector<HTMLButtonElement>(".title-row .note-icon")?.disabled).toBe(false);
});
+ /**
+ * A GPX track opens the pane the way a marker does. Its note is on the map by being drawn
+ * across it — there is no location label for the pane to read, so what this pins down is that
+ * the pane neither refuses the note nor closes itself over the label it does not find.
+ */
+ it("stands for a GPX track that was clicked, which has no location of its own", async () => {
+ buildNote({ id: "root", title: "root", children: [ { id: "hikenote", title: "A hike", mime: GPX_MIME } ] });
+ const note = froca.notes["hikenote"];
+ const map = fakeMap();
+ await mount([ note ], map);
+
+ map.setUnderPointer([ trackFeature(note) ]);
+ await act(async () => map.click());
+ await settle();
+
+ expect(pane()?.querySelector<HTMLInputElement>(".title-row input")?.value).toBe("A hike");
+ // Nothing to pan to and no place to write out, there being no location label to read.
+ expect(map.eased).toEqual([]);
+ expect(pane()?.querySelector(".geo-detail-pane-location")).toBeNull();
+ });
+
/**
* Regression test for a click on a marker whiting out the map and losing its WebGL context.
*
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.tsx
index 95707ab870..d1f4cd0b92 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.tsx
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.tsx
@@ -23,6 +23,7 @@ import { useLegacyComponentElement, useNoteColorClass, useNoteContext, useNoteLa
import OverlayPanel, { OverlayPanelBody } from "../../react/OverlayPanel";
import { NoteContextContext, ParentComponent } from "../../react/react_utils";
import { moveMarker } from "./api";
+import { GPX_MIME, trackHitLayers } from "./GpxTrack";
import { ParentMap } from "./map";
import { formatLocation, LOCATION_ATTRIBUTE, MARKER_LAYER, parseLocation } from "./Markers";
@@ -111,21 +112,24 @@ export default function DetailPane({ notes, placing, isReadOnly, selection, onSe
}, [ 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.
+ // — which is all "remove from map" does — so the note being gone is not the only case. A GPX
+ // track is on the map by being one: its place is its line, not a location label.
useEffect(() => {
- if (selection && (!note || !parseLocation(location))) {
+ if (selection && (!note || (note.mime !== GPX_MIME && !parseLocation(location)))) {
void closePane();
}
}, [ selection, note, location, closePane ]);
- // A marker selects, anywhere else clears. Read off the rendered layer rather than bound to it
- // (`map.on("click", MARKER_LAYER, ...)`) so one handler answers both, with no ordering to rely on.
+ // A marker or a GPX track selects, anywhere else clears. Read off the rendered layers rather
+ // than bound to them (`map.on("click", MARKER_LAYER, ...)`) so one handler answers all, with no
+ // ordering to rely on. The markers come ahead of the tracks for the reason the context menu
+ // puts them there: a pin standing on its own line is the smaller target, and the one aimed at.
useEffect(() => {
if (!map || placing) return;
const onClick = (e: MapMouseEvent) => {
- const feature = map.queryRenderedFeatures(e.point, { layers: [ MARKER_LAYER ] })[0];
- if (!feature || feature.geometry.type !== "Point") {
+ const feature = map.queryRenderedFeatures(e.point, { layers: [ MARKER_LAYER, ...trackHitLayers(map) ] })[0];
+ if (!feature) {
void closePane();
return;
}
diff --git a/apps/client/src/widgets/collections/geomap/GpxTrack.tsx b/apps/client/src/widgets/collections/geomap/GpxTrack.tsx
index 4cbd0e05dc..70f2d27eb8 100644
--- a/apps/client/src/widgets/collections/geomap/GpxTrack.tsx
+++ b/apps/client/src/widgets/collections/geomap/GpxTrack.tsx
@@ -21,6 +21,9 @@ export interface GpxTrackProps {
hideLabels?: boolean;
}
+/** What marks a note as a GPX track: the mime its file carries. */
+export const GPX_MIME = "application/gpx+xml";
+
/**
* What a track's layers are named with.
*
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx
index f39d111e9b..c8e7aa6863 100644
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@ -21,7 +21,7 @@ import ContextMenus from "./ContextMenus";
import DetailPane, { PaneSelection } from "./DetailPane";
import EditToolbar from "./EditToolbar";
import GhostPin from "./GhostPin";
-import { GpxTrack } from "./GpxTrack";
+import { GPX_MIME, GpxTrack } from "./GpxTrack";
import Map, { DEFAULT_ZOOM, GeoMouseEvent } from "./map";
import { DEFAULT_MAP_LAYER_NAME, MAP_LAYERS, MapLayer } from "./map_layer";
import MapToolbar from "./MapToolbar";
@@ -335,7 +335,7 @@ function ToggleReadOnlyButton({ note }: { note: FNote }) {
function NoteGpxTrackWrapper({ note, hideLabels, isDarkTheme }: { note: FNote, hideLabels: boolean, isDarkTheme: boolean }) {
const mime = useNoteProperty(note, "mime");
- if (mime !== "application/gpx+xml") {
+ if (mime !== GPX_MIME) {
return null;
}