Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): let an inherited icon reach a new marker

Elian Doran742b68f32fcf
diff --git a/apps/client/src/entities/fnote.spec.ts b/apps/client/src/entities/fnote.spec.ts
index 97e956a3ff..5f5d2b1ff3 100644
--- a/apps/client/src/entities/fnote.spec.ts
+++ b/apps/client/src/entities/fnote.spec.ts
@@ -956,6 +956,20 @@ describe("FNote icon, color, folder & css", () => {
expect(noIcon.getIcon()).toContain("tn-icon");
});
+ it("draws a note carrying a location as a pin, behind an icon of its own", () => {
+ const here = buildNote({ title: "here", "#geolocation": "48.85,2.36" });
+ expect(here.getIcon()).toBe("tn-icon bx bx-pin");
+
+ const own = buildNote({
+ title: "own", "#geolocation": "48.85,2.36", "#iconClass": "bx bx-store" });
+ expect(own.getIcon()).toBe("tn-icon bx bx-store");
+
+ // Taking a marker off the map empties the label rather than removing it (see moveMarker in
+ // the geo map's api), and a note that stands nowhere is a plain note again.
+ const gone = buildNote({ title: "gone", "#geolocation": "" });
+ expect(gone.getIcon()).toBe("tn-icon bx bx-note");
+ });
+
it("isFolder reflects subtreeHidden, search type and filtered children", () => {
const hiddenSubtree = buildNote({ title: "hs", "#subtreeHidden": "" });
expect(hiddenSubtree.isFolder()).toBe(false);
diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.ts
index 78353e12cd..45f4f52689 100644
--- a/apps/client/src/entities/fnote.ts
+++ b/apps/client/src/entities/fnote.ts
@@ -588,7 +588,8 @@ export default class FNote {
mime: this.mime,
iconClass: iconClassLabels.length > 0 ? iconClassLabels[0].value : undefined,
workspaceIconClass,
- isFolder: this.isFolder.bind(this)
+ isFolder: this.isFolder.bind(this),
+ getLabelValue: this.getLabelValue.bind(this)
});
return `tn-icon ${icon}`;
}
diff --git a/apps/client/src/widgets/collections/geomap/EditToolbar.tsx b/apps/client/src/widgets/collections/geomap/EditToolbar.tsx
index 45a8ee3a73..37284bce1c 100644
--- a/apps/client/src/widgets/collections/geomap/EditToolbar.tsx
+++ b/apps/client/src/widgets/collections/geomap/EditToolbar.tsx
@@ -43,10 +43,10 @@ export default function EditToolbar({ isReadOnly, placing, onTogglePlacement, on
return (
<OverlayControlGroup className="geo-edit-toolbar" placement="bottom-center" overCanvas>
- {/* The pin a note dropped on the map wears (see CHILD_NOTE_ICON in api.ts) — the button
- shows the very thing it drops, which is also the ghost that will follow the pointer
- once armed. The words beside it are what names the button; the tooltip says at more
- length what it does. */}
+ {/* The pin a note dropped on the map wears (see GEO_MARKER_ICON in commons) — the
+ button shows the very thing it drops, which is also the ghost that will follow the
+ pointer once armed. The words beside it are what names the button; the tooltip says
+ at more length what it does. */}
<OverlayControlButton
title={placing ? t("geo-map.create-child-note-cancel") : t("geo-map.create-child-note-title")}
icon="bx-pin"
diff --git a/apps/client/src/widgets/collections/geomap/GhostPin.tsx b/apps/client/src/widgets/collections/geomap/GhostPin.tsx
index 123d47f1bd..6ba6649863 100644
--- a/apps/client/src/widgets/collections/geomap/GhostPin.tsx
+++ b/apps/client/src/widgets/collections/geomap/GhostPin.tsx
@@ -1,10 +1,10 @@
import "./GhostPin.css";
+import { GEO_MARKER_ICON } from "@triliumnext/commons";
import type { MapMouseEvent } from "maplibre-gl";
import { useContext, useEffect, useRef } from "preact/hooks";
import FNote from "../../../entities/fnote";
-import { CHILD_NOTE_ICON } from "./api";
import { ParentMap } from "./map";
import { DEFAULT_MARKER_COLOR, drawMarkerImage } from "./Markers";
@@ -14,8 +14,8 @@ import { DEFAULT_MARKER_COLOR, drawMarkerImage } from "./Markers";
* what will stand on it.
*
* Drawn with the very image the symbol layer would stamp there (see {@link drawMarkerImage}) — the
- * note's own colour and icon for a marker being moved, the pin a new note is given for one being
- * created — and translucent, so it reads as an offer rather than as a marker already standing.
+ * note's own colour and icon for a marker being moved, the pin a located note is drawn under for
+ * one being created — and translucent, so it reads as an offer rather than as a marker standing.
*
* Positioned by hand off the map's `mousemove` rather than as a MapLibre marker: the ghost follows
* the pointer, not a coordinate, so there is no lngLat for a marker to be held at.
@@ -28,7 +28,7 @@ export default function GhostPin({ note }: {
const elementRef = useRef<HTMLDivElement>(null);
const color = note?.getLabelValue("color") ?? DEFAULT_MARKER_COLOR;
- const iconClass = note?.getIcon() ?? CHILD_NOTE_ICON;
+ const iconClass = note?.getIcon() ?? GEO_MARKER_ICON;
// The pin image, drawn fresh rather than through the layer's cache: the cache keeps one <img>
// element per look, and an element can only stand in one place — two maps split side by side,
diff --git a/apps/client/src/widgets/collections/geomap/Markers.tsx b/apps/client/src/widgets/collections/geomap/Markers.tsx
index 1207f7b83b..5f1c475b58 100644
--- a/apps/client/src/widgets/collections/geomap/Markers.tsx
+++ b/apps/client/src/widgets/collections/geomap/Markers.tsx
@@ -1,3 +1,4 @@
+import { GEO_LOCATION_ATTRIBUTE } from "@triliumnext/commons";
import { AddLayerObject, type CircleLayerSpecification, type ExpressionSpecification, type GeoJSONSource, type MapGeoJSONFeature, type Map as MapLibreGLMap, type MapMouseEvent, type SymbolLayerSpecification } from "maplibre-gl";
import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";
@@ -9,7 +10,7 @@ import { useTriliumEvent } from "../../react/hooks";
import { CLUSTER_LAYERS, CLUSTER_SOURCE_OPTIONS, installClusterLayers, UNCLUSTERED_ONLY, useClusterExpansion } from "./clusters";
import { ParentMap } from "./map";
-export const LOCATION_ATTRIBUTE = "geolocation";
+export { GEO_LOCATION_ATTRIBUTE as LOCATION_ATTRIBUTE } from "@triliumnext/commons";
export const MARKER_LAYER = "points-layer";
export const MARKER_SOURCE = "points";
/** The glow put under the selected marker, drawn from the same source beneath the pins. */
@@ -479,7 +480,7 @@ async function buildMarkerData(notes: FNote[]) {
const wanted = new Map<string, { color: string, iconClass: string }>();
for (const note of notes) {
- const latLng = parseLocation(note.getLabelValue(LOCATION_ATTRIBUTE));
+ const latLng = parseLocation(note.getLabelValue(GEO_LOCATION_ATTRIBUTE));
if (!latLng) continue;
const color = note.getLabelValue("color") ?? DEFAULT_MARKER_COLOR;
diff --git a/apps/client/src/widgets/collections/geomap/api.spec.ts b/apps/client/src/widgets/collections/geomap/api.spec.ts
index 76f4803ec6..3b16fef25b 100644
--- a/apps/client/src/widgets/collections/geomap/api.spec.ts
+++ b/apps/client/src/widgets/collections/geomap/api.spec.ts
@@ -49,10 +49,12 @@ describe("geo map api", () => {
title: undefined,
// Nothing is asked first — the name is typed over in the pane, not answered in a modal.
activate: false,
- attributes: expect.arrayContaining([
- { type: "label", name: "geolocation", value: "48.85,2.36" },
- { type: "label", name: "iconClass", value: "bx bx-pin" }
- ])
+ // The location and nothing else. No icon is written onto the marker: getNoteIcon draws
+ // a note carrying a location as a pin, so an icon the map hands down through
+ // `#child:iconClass` or a template is what the marker wears instead.
+ attributes: [
+ { type: "label", name: "geolocation", value: "48.85,2.36" }
+ ]
}));
});
@@ -77,16 +79,17 @@ describe("geo map api", () => {
}));
});
- it("gives a place of no stated kind the icon a placed marker wears", async () => {
+ it("writes no icon for a place of no stated kind, which leaves it drawn as a pin", async () => {
const parent = buildNote({ title: "The map" });
- // Neither the geocoder nor the tile said what kind of place it is (see placeIcon).
+ // Neither the geocoder nor the tile said what kind of place it is (see placeIcon), so there
+ // is nothing to keep the marker under and getNoteIcon draws it as a pin.
await createNoteForPlace(parent, { name: "Somewhere", lat: 45.796, lng: 24.147 });
expect(createNote).toHaveBeenCalledWith(parent.noteId, expect.objectContaining({
- attributes: expect.arrayContaining([
- { type: "label", name: "iconClass", value: "bx bx-pin" }
- ])
+ attributes: [
+ { type: "label", name: "geolocation", value: "45.796,24.147" }
+ ]
}));
});
diff --git a/apps/client/src/widgets/collections/geomap/api.ts b/apps/client/src/widgets/collections/geomap/api.ts
index 976b0de0aa..21297442a1 100644
--- a/apps/client/src/widgets/collections/geomap/api.ts
+++ b/apps/client/src/widgets/collections/geomap/api.ts
@@ -1,3 +1,5 @@
+import { AttributeRow } from "@triliumnext/commons";
+
import FNote from "../../../entities/fnote";
import attributes from "../../../services/attributes";
import dialog from "../../../services/dialog";
@@ -8,9 +10,6 @@ import { GPX_MIME } from "./GpxTrack";
import type { GeoMouseEvent } from "./map";
import { LOCATION_ATTRIBUTE } from "./Markers";
-/** The icon a note created on the map is given, and so what the ghost pin previews for one. */
-export const CHILD_NOTE_ICON = "bx bx-pin";
-
export async function moveMarker(noteId: string, latLng: { lat: number; lng: number } | null) {
const value = latLng ? [latLng.lat, latLng.lng].join(",") : "";
await attributes.setLabel(noteId, LOCATION_ATTRIBUTE, value);
@@ -140,20 +139,26 @@ interface PlaceToKeep {
*
* `icon` is what the place is already drawn under — a cart for a supermarket, a flag for a country
* (see placeIcon in osm_icons) — so the marker keeps the icon the panel offered it under. A note
- * dropped by clicking the map has no such place behind it and wears CHILD_NOTE_ICON.
+ * dropped by clicking the map has no such place behind it and is given no icon at all:
+ * `getNoteIcon` draws a note carrying a location as a pin, which leaves an icon the map hands down
+ * through `#child:iconClass` or a template to apply instead.
*/
async function createNoteAt(
parentNote: FNote, [ lat, lng ]: [number, number], title?: string, icon?: string) {
+ const noteAttributes: Omit<AttributeRow, "noteId" | "attributeId">[] = [
+ { type: "label", name: LOCATION_ATTRIBUTE, value: [ lat, lng ].join(",") }
+ ];
+ if (icon) {
+ noteAttributes.push({ type: "label", name: "iconClass", value: icon });
+ }
+
const { note } = await note_create.createNote(parentNote.noteId, {
title,
content: "",
type: "text",
activate: false,
isProtected: parentNote.isProtected,
- attributes: [
- { type: "label", name: LOCATION_ATTRIBUTE, value: [ lat, lng ].join(",") },
- { type: "label", name: "iconClass", value: icon ?? CHILD_NOTE_ICON }
- ]
+ attributes: noteAttributes
});
return note;
diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts
index a7a96ab347..15baa86a30 100644
--- a/apps/client/src/widgets/note_tree.ts
+++ b/apps/client/src/widgets/note_tree.ts
@@ -5,6 +5,8 @@ import "jquery.fancytree/dist/modules/jquery.fancytree.filter.js";
import "../stylesheets/tree.css";
import "./note_tree.css";
+import { GEO_LOCATION_ATTRIBUTE } from "@triliumnext/commons";
+
import appContext, { type CommandListenerData, type EventData } from "../components/app_context.js";
import type { SetNoteOpts } from "../components/note_context.js";
import type FBranch from "../entities/fbranch.js";
@@ -1315,7 +1317,9 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
#processAttributeRows(attributeRows: AttributeRow[], refreshCtx: RefreshContext) {
for (const attrRow of attributeRows) {
- const dirtyingLabels = ["iconClass", "cssClass", "workspace", "workspaceIconClass", "color"];
+ // `geolocation` decides the icon of a note that has none of its own (see getNoteIcon).
+ const dirtyingLabels = ["iconClass", "cssClass", "workspace", "workspaceIconClass",
+ "color", GEO_LOCATION_ATTRIBUTE];
if (attrRow.type === "label" && dirtyingLabels.includes(attrRow.name ?? "") && attrRow.noteId) {
if (attrRow.isInheritable) {
diff --git a/apps/server/src/share/shaca/entities/snote.ts b/apps/server/src/share/shaca/entities/snote.ts
index d32b7d8df1..790824fd27 100644
--- a/apps/server/src/share/shaca/entities/snote.ts
+++ b/apps/server/src/share/shaca/entities/snote.ts
@@ -540,7 +540,8 @@ class SNote extends AbstractShacaEntity {
mime: this.mime,
workspaceIconClass: undefined,
iconClass: iconClassLabels.length > 0 ? iconClassLabels[0].value : undefined,
- isFolder: this.isFolder.bind(this)
+ isFolder: this.isFolder.bind(this),
+ getLabelValue: this.getLabelValue.bind(this)
});
return `tn-icon ${icon}`;
diff --git a/packages/commons/src/lib/attachment_roles.spec.ts b/packages/commons/src/lib/attachment_roles.spec.ts
index 2c94e38924..306ceb26d5 100644
--- a/packages/commons/src/lib/attachment_roles.spec.ts
+++ b/packages/commons/src/lib/attachment_roles.spec.ts
@@ -130,7 +130,8 @@ describe("attachmentIcon", () => {
// The point of routing both through one table: a PDF is a PDF however it got here.
for (const [ type, mime ] of [ [ "file", "application/pdf" ], [ "file", "video/mp4" ], [ "image", "image/gif" ] ] as const) {
expect(attachmentIcon(type, mime), mime).toBe(getNoteIcon({
- noteId: "abc123", type, mime, iconClass: undefined, workspaceIconClass: undefined, isFolder: () => false
+ noteId: "abc123", type, mime, iconClass: undefined, workspaceIconClass: undefined,
+ isFolder: () => false, getLabelValue: () => null
}));
}
});
diff --git a/packages/commons/src/lib/notes.spec.ts b/packages/commons/src/lib/notes.spec.ts
index c2323d3953..f2a8307a4a 100644
--- a/packages/commons/src/lib/notes.spec.ts
+++ b/packages/commons/src/lib/notes.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { getImageAttachmentTitle, getMimeIcon, getNoteIcon, NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS, parseMindMapNoteLink } from "./notes.js";
+import { GEO_LOCATION_ATTRIBUTE, getImageAttachmentTitle, getMimeIcon, getNoteIcon, NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS, parseMindMapNoteLink } from "./notes.js";
import { NoteType } from "./rows.js";
function buildArgs(overrides: {
@@ -10,6 +10,7 @@ function buildArgs(overrides: {
iconClass?: string | undefined;
workspaceIconClass?: string | undefined;
isFolder?: () => boolean;
+ getLabelValue?: (name: string) => string | null;
}) {
return {
noteId: "abc123",
@@ -18,6 +19,7 @@ function buildArgs(overrides: {
iconClass: undefined,
workspaceIconClass: undefined,
isFolder: () => false,
+ getLabelValue: () => null,
...overrides
};
}
@@ -84,6 +86,26 @@ describe("getNoteIcon", () => {
expect(icon).toBe("bx bx-folder");
});
+ it("draws a located text note as a pin, ahead of the folder icon but behind its own", () => {
+ // The pin is what a marker wears without one being written onto it, so an icon the geo map
+ // hands down through `#child:iconClass` or a template still applies (see the map's api).
+ const located = (value = "48.85,2.36") =>
+ (name: string) => name === GEO_LOCATION_ATTRIBUTE ? value : null;
+
+ expect(getNoteIcon(buildArgs({ getLabelValue: located() }))).toBe("bx bx-pin");
+ expect(getNoteIcon(buildArgs({ getLabelValue: located(), isFolder: () => true })))
+ .toBe("bx bx-pin");
+ expect(getNoteIcon(buildArgs({ getLabelValue: located(), iconClass: "bx bx-store" })))
+ .toBe("bx bx-store");
+ // Taking a marker off the map empties the label rather than removing it (see moveMarker in
+ // the geo map's api), and a note that stands nowhere is a plain note again.
+ expect(getNoteIcon(buildArgs({ getLabelValue: located("") }))).toBe("bx bx-note");
+ // Only the generic note icon is displaced: a file put on the map still says what it holds.
+ expect(getNoteIcon(buildArgs({
+ type: "file", mime: "application/gpx+xml", getLabelValue: located()
+ }))).toBe("bx bx-trip");
+ });
+
it("returns the note icon for a text note that is not a folder", () => {
const icon = getNoteIcon(buildArgs({ type: "text", isFolder: () => false }));
expect(icon).toBe("bx bx-note");
diff --git a/packages/commons/src/lib/notes.ts b/packages/commons/src/lib/notes.ts
index 1b1eb3dfb3..c9dfbb8a16 100644
--- a/packages/commons/src/lib/notes.ts
+++ b/packages/commons/src/lib/notes.ts
@@ -89,13 +89,32 @@ const IMAGE_MIME_MAPPINGS: Record<string, string> = {
"image/gif": "bx bxs-file-gif",
};
-export function getNoteIcon({ noteId, type, mime, iconClass, workspaceIconClass, isFolder }: {
+/** The label a note carries to stand on a geo map, read by {@link getNoteIcon}. */
+export const GEO_LOCATION_ATTRIBUTE = "geolocation";
+
+/** The icon a note on a geo map is drawn under where it has none of its own. */
+export const GEO_MARKER_ICON = "bx bx-pin";
+
+/**
+ * The icon a note is drawn under: its own `#iconClass` where it has one, and a default read off
+ * what the note is otherwise.
+ *
+ * A note carrying a non-empty `#geolocation` is drawn as a pin where it has nothing more specific,
+ * so the geo map writes no `#iconClass` onto a marker it creates and an icon the map hands down
+ * through `#child:iconClass` or a template still applies. `iconClass` stays an argument rather than
+ * being read here because the share tree narrows it to the prefixes an icon pack supplies.
+ */
+export function getNoteIcon({
+ noteId, type, mime, iconClass, workspaceIconClass, isFolder, getLabelValue
+}: {
noteId: string;
type: NoteType;
mime: string;
iconClass: string | undefined;
workspaceIconClass: string | undefined;
isFolder: () => boolean;
+ /** One of the note's labels, inherited ones included, as every note entity resolves them. */
+ getLabelValue: (name: string) => string | null;
}) {
if (iconClass) {
return iconClass;
@@ -107,6 +126,11 @@ export function getNoteIcon({ noteId, type, mime, iconClass, workspaceIconClass,
if (noteId === "_share") {
return "bx bx-share-alt";
} else if (type === "text") {
+ // A location is written onto a note deliberately, so it outranks the folder icon the note
+ // picks up from having children.
+ if (getLabelValue(GEO_LOCATION_ATTRIBUTE)) {
+ return GEO_MARKER_ICON;
+ }
if (isFolder()) {
return "bx bx-folder";
}
diff --git a/packages/trilium-core/src/becca/entities/bnote.ts b/packages/trilium-core/src/becca/entities/bnote.ts
index e1beb36804..04b721935f 100644
--- a/packages/trilium-core/src/becca/entities/bnote.ts
+++ b/packages/trilium-core/src/becca/entities/bnote.ts
@@ -1775,7 +1775,8 @@ class BNote extends AbstractBeccaEntity<BNote> {
mime: this.mime,
iconClass: iconClassLabels.length > 0 ? iconClassLabels[0].value : undefined,
workspaceIconClass: undefined,
- isFolder: this.isFolder.bind(this)
+ isFolder: this.isFolder.bind(this),
+ getLabelValue: this.getLabelValue.bind(this)
});
return `tn-icon ${icon}`;