Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): name and icon a drawn shape as a placed marker is

Elian Doran6db44a7be1db
diff --git a/apps/client/src/widgets/collections/geomap/api.spec.ts b/apps/client/src/widgets/collections/geomap/api.spec.ts
index 7329a4f6a9..313c46fe81 100644
--- a/apps/client/src/widgets/collections/geomap/api.spec.ts
+++ b/apps/client/src/widgets/collections/geomap/api.spec.ts
@@ -111,7 +111,7 @@ describe("geo map api", () => {
}));
});
- it("creates a shape note under the stock name, its geometry in the label and its icon the tool's", async () => {
+ it("leaves a drawn shape to be named as a placed marker is, geometry in the label", async () => {
const parent = buildNote({ title: "The map" });
const created = await createShapeNote(parent, {
@@ -122,11 +122,14 @@ describe("geo map api", () => {
expect(created).toEqual({ noteId: "created" });
expect(createNote).toHaveBeenCalledWith(parent.noteId, expect.objectContaining({
type: "text",
+ // Named and iconed by exactly the rules a placed marker is named and iconed by: no
+ // title, so a `#titleTemplate` on the map applies, and no icon, so getNoteIcon draws
+ // the shape the label names and `#child:iconClass` or a template can override it.
+ title: undefined,
activate: false,
- attributes: expect.arrayContaining([
- { type: "label", name: "geoShape", value: "polygon:48.85,2.29 48.86,2.35 48.9,2.3" },
- { type: "label", name: "iconClass", value: "bx bx-shape-polygon" }
- ])
+ attributes: [
+ { type: "label", name: "geoShape", value: "polygon:48.85,2.29 48.86,2.35 48.9,2.3" }
+ ]
}));
});
diff --git a/apps/client/src/widgets/collections/geomap/api.ts b/apps/client/src/widgets/collections/geomap/api.ts
index 3a614d31a3..b224afbe12 100644
--- a/apps/client/src/widgets/collections/geomap/api.ts
+++ b/apps/client/src/widgets/collections/geomap/api.ts
@@ -14,13 +14,6 @@ import { type GeoShape, serializeGeoShape, SHAPE_ATTRIBUTE } from "./shapes";
/** The type a note put on the map is created as, and so what a template handed to it must match. */
export const MARKER_NOTE_TYPE: NoteType = "text";
-/** The icon a shape note wears, by the kind of shape its label spells. */
-export const SHAPE_NOTE_ICONS = {
- line: "bx bx-vector",
- polygon: "bx bx-shape-polygon",
- circle: "bx bx-shape-circle"
-} as const satisfies Record<GeoShape["type"], string>;
-
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);
@@ -167,37 +160,39 @@ async function createNoteAt(
noteAttributes.push({ type: "label", name: "iconClass", value: icon });
}
- const { note } = await note_create.createNote(parentNote.noteId, {
- title,
- content: "",
- type: MARKER_NOTE_TYPE,
- activate: false,
- isProtected: parentNote.isProtected,
- attributes: noteAttributes
- });
-
- return note;
+ return createMapNote(parentNote, noteAttributes, title);
}
/**
* Makes a note of a shape drawn on the map, and hands it back for the pane to open on.
*
- * A marker note in every way but the label: an ordinary text note, stock-named for the pane to
- * offer the title for typing over, whose place on the map is written in an attribute — the whole
- * shape in `#geoShape` where a marker keeps its point in `#geolocation` (see shapes.ts). The note
- * is the shape the way a marker note is its pin; the content stays the user's to write.
+ * A marker note in every way but the label: the whole shape in `#geoShape` where a marker keeps its
+ * point in `#geolocation` (see shapes.ts). The note is the shape the way a marker note is its pin,
+ * so it is named and iconed by the same rules, and the content stays the user's to write.
*/
export async function createShapeNote(parentNote: FNote, shape: GeoShape) {
+ return createMapNote(parentNote, [
+ { type: "label", name: SHAPE_ATTRIBUTE, value: serializeGeoShape(shape) }
+ ]);
+}
+
+/**
+ * Creates a note of the map's own, carrying whatever puts it on the map.
+ *
+ * No title is sent unless the caller has one worth keeping, which leaves the naming where every
+ * other new note's is: the server's, and so a `#titleTemplate` on the map's if it carries one. No
+ * `#iconClass` either, `getNoteIcon` drawing a note by the geo label it carries (see commons), so
+ * an icon the map hands down through `#child:iconClass` or a template still applies.
+ */
+async function createMapNote(
+ parentNote: FNote, attributes: Omit<AttributeRow, "noteId" | "attributeId">[], title?: string) {
const { note } = await note_create.createNote(parentNote.noteId, {
- title: t("relation_map.default_new_note_title"),
+ title,
content: "",
- type: "text",
+ type: MARKER_NOTE_TYPE,
activate: false,
isProtected: parentNote.isProtected,
- attributes: [
- { type: "label", name: SHAPE_ATTRIBUTE, value: serializeGeoShape(shape) },
- { type: "label", name: "iconClass", value: SHAPE_NOTE_ICONS[shape.type] }
- ]
+ attributes
});
return note;
diff --git a/apps/client/src/widgets/collections/geomap/shapes.ts b/apps/client/src/widgets/collections/geomap/shapes.ts
index 46f1b26ac0..5fc3203989 100644
--- a/apps/client/src/widgets/collections/geomap/shapes.ts
+++ b/apps/client/src/widgets/collections/geomap/shapes.ts
@@ -17,8 +17,9 @@
import { type Bounds, boundsOf } from "./coordinates";
-/** The label a shape note carries its geometry in. */
-export const SHAPE_ATTRIBUTE = "geoShape";
+/** The label a shape note carries its geometry in. Named in commons because `getNoteIcon` reads it
+ * to draw a shape note under the shape it draws, as `GEO_LOCATION_ATTRIBUTE` is. */
+export { GEO_SHAPE_ATTRIBUTE as SHAPE_ATTRIBUTE } from "@triliumnext/commons";
/** How many decimal places a coordinate keeps: six is about a tenth of a metre, which is already
* more than a hand-placed vertex means. */
diff --git a/packages/commons/src/lib/notes.spec.ts b/packages/commons/src/lib/notes.spec.ts
index 987ad9a82b..5c2c11465a 100644
--- a/packages/commons/src/lib/notes.spec.ts
+++ b/packages/commons/src/lib/notes.spec.ts
@@ -1,6 +1,9 @@
import { describe, expect, it } from "vitest";
-import { GEO_LOCATION_ATTRIBUTE, getImageAttachmentTitle, getMimeIcon, getNoteIcon, NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS, parseMindMapNoteLink } from "./notes.js";
+import {
+ GEO_LOCATION_ATTRIBUTE, GEO_SHAPE_ATTRIBUTE, getImageAttachmentTitle, getMimeIcon, getNoteIcon,
+ NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS, parseMindMapNoteLink
+} from "./notes.js";
import { NoteType } from "./rows.js";
function buildArgs(overrides: {
@@ -106,6 +109,27 @@ describe("getNoteIcon", () => {
}))).toBe("bx bx-trip");
});
+ it("draws a shape note as the shape its label names, by the same rules as a pin", () => {
+ const drawn = (value: string) =>
+ (name: string) => name === GEO_SHAPE_ATTRIBUTE ? value : null;
+
+ expect(getNoteIcon(buildArgs({ getLabelValue: drawn("line:48.85,2.29 48.86,2.35") })))
+ .toBe("bx bx-vector");
+ expect(getNoteIcon(buildArgs({ getLabelValue: drawn("polygon:48.85,2.29 48.9,2.3") })))
+ .toBe("bx bx-shape-polygon");
+ expect(getNoteIcon(buildArgs({ getLabelValue: drawn("circle:48.85,2.29 500") })))
+ .toBe("bx bx-shape-circle");
+ // Nothing is written onto a shape note either, so the map's own icon still wins — and the
+ // icon follows a shape redrawn as another kind rather than staying what it was created as.
+ expect(getNoteIcon(buildArgs({
+ getLabelValue: drawn("circle:48.85,2.29 500"), iconClass: "bx bx-store"
+ }))).toBe("bx bx-store");
+ // A label nobody can read draws no shape on the map, but the note is still one that carries
+ // a shape; the line's icon is the least it could be.
+ expect(getNoteIcon(buildArgs({ getLabelValue: drawn("nonsense") }))).toBe("bx bx-vector");
+ expect(getNoteIcon(buildArgs({ getLabelValue: drawn("") }))).toBe("bx bx-note");
+ });
+
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 cc48c77978..c9b75d13c8 100644
--- a/packages/commons/src/lib/notes.ts
+++ b/packages/commons/src/lib/notes.ts
@@ -95,20 +95,34 @@ const IMAGE_MIME_MAPPINGS: Record<string, string> = {
"image/gif": "bx bxs-file-gif",
};
-/** The label a note carries to stand on a geo map, read by {@link getNoteIcon}. */
+/** The labels a note carries to stand on a geo map, read by {@link getNoteIcon}. */
export const GEO_LOCATION_ATTRIBUTE = "geolocation";
+export const GEO_SHAPE_ATTRIBUTE = "geoShape";
/** 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 drawn shape is worn under, by the kind its `#geoShape` label names (see the client's
+ * geomap/shapes.ts for the format). An unreadable label falls back to the line's icon, a shape
+ * being a line before it is anything else.
+ */
+export const GEO_SHAPE_ICONS: Record<string, string> = {
+ line: "bx bx-vector",
+ polygon: "bx bx-shape-polygon",
+ circle: "bx bx-shape-circle"
+};
+
/**
* 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.
+ * A note carrying a non-empty `#geolocation` is drawn as a pin, and one carrying a `#geoShape` as
+ * the shape it draws, where either has nothing more specific. So the geo map writes no `#iconClass`
+ * onto a marker or a shape it creates: an icon the map hands down through `#child:iconClass` or a
+ * template still applies, and redrawing a shape as another kind changes the icon with it rather
+ * than leaving the one it was created under. `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
@@ -132,11 +146,15 @@ export function getNoteIcon({
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.
+ // A place on a map 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;
}
+ const shape = getLabelValue(GEO_SHAPE_ATTRIBUTE);
+ if (shape) {
+ return GEO_SHAPE_ICONS[shape.split(":", 1)[0]] ?? GEO_SHAPE_ICONS.line;
+ }
if (isFolder()) {
return "bx bx-folder";
}