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
fix(geomap): preview the marker the map would actually drop
Elian Doran8596bb8d05da
diff --git a/apps/client/src/entities/fnote.spec.ts b/apps/client/src/entities/fnote.spec.tsindex 5f5d2b1ff3..f988446c0e 100644--- a/apps/client/src/entities/fnote.spec.ts+++ b/apps/client/src/entities/fnote.spec.ts@@ -970,6 +970,45 @@ describe("FNote icon, color, folder & css", () => {expect(gone.getIcon()).toBe("tn-icon bx bx-note");});+ it("answers what a new child would be given, nearest source first", () => {+ const template = buildNote({ title: "Place", "#iconClass": "bx bx-map" });++ // All three at once: the copy beats the inheritable label, which beats the template.+ const dressed = buildNote({+ title: "The map",+ "#child:iconClass": "bx bx-store",+ "#iconClass(inheritable)": "bx bx-star",+ "~child:template": template.noteId+ });+ expect(dressed.getLabelValueForNewChild("iconClass")).toBe("bx bx-store");++ const inheritable = buildNote({ title: "map2", "#iconClass(inheritable)": "bx bx-star" });+ expect(inheritable.getLabelValueForNewChild("iconClass")).toBe("bx bx-star");++ const templated = buildNote({ title: "map3", "~child:template": template.noteId });+ expect(templated.getLabelValueForNewChild("iconClass")).toBe("bx bx-map");++ // An inheritable `~template` reaches the child down the tree rather than by being copied.+ const handedDown = buildNote({ title: "map4", "~template(inheritable)": template.noteId });+ expect(handedDown.getLabelValueForNewChild("iconClass")).toBe("bx bx-map");++ // An icon the map wears itself is the map's, and says nothing about its children.+ const own = buildNote({ title: "map5", "#iconClass": "bx bx-star" });+ expect(own.getLabelValueForNewChild("iconClass")).toBeNull();+ expect(buildNote({ title: "map6" }).getLabelValueForNewChild("iconClass")).toBeNull();+ });++ it("skips a child template of another type, which core would not apply either", () => {+ // The type is the caller's explicit choice, so core leaves such a template alone (#3015);+ // left unsaid, the template is taken to apply.+ const canvas = buildNote({ title: "Sketch", type: "canvas", "#iconClass": "bx bx-pen" });+ const map = buildNote({ title: "The map", "~child:template": canvas.noteId });++ expect(map.getLabelValueForNewChild("iconClass", "text")).toBeNull();+ expect(map.getLabelValueForNewChild("iconClass", "canvas")).toBe("bx bx-pen");+ expect(map.getLabelValueForNewChild("iconClass")).toBe("bx bx-pen");+ });+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.tsindex 45f4f52689..6556674495 100644--- a/apps/client/src/entities/fnote.ts+++ b/apps/client/src/entities/fnote.ts@@ -860,6 +860,79 @@ export default class FNote {return relations.map((rel) => this.froca.notes[rel.value]);}+ /**+ * The value a note created under this one would be given for the attribute without asking for+ * it, or `null` where it would be given none: a `child:`-prefixed attribute copied onto every+ * new child, an inheritable attribute handed down the tree, or one lent by a template the child+ * is given. Checked in that order, which is the order `getAttributes()` lists them in on the+ * child itself.+ *+ * Mirrors `copyChildAttributes()` in trilium-core, which is where the copying happens. Two+ * uses: previewing a note that does not exist yet (see GhostPin in the geo map), and telling a+ * default apart from a value the user chose — what a note would be given anyway is not worth+ * writing onto it, and writing it defeats the setting that would have given it.+ *+ * `childType` is the type the caller is about to create. A `~child:template` of another type is+ * skipped by core when the type was chosen explicitly (#3015) and lends such a child nothing;+ * left out, the template is taken to apply. The title is not answered for: `#titleTemplate` is+ * evaluated by the server against values the client does not hold.+ */+ getAttributeValueForNewChild(type: AttributeType, name: string, childType?: NoteType) {+ const copied = this.getAttributeValue(type, `child:${name}`);+ if (copied !== null) {+ return copied;+ }++ const inherited = this.getAttributes(type, name).find((attr) => attr.isInheritable);+ if (inherited) {+ return inherited.value;+ }++ for (const template of this.__getTemplatesForNewChild(childType)) {+ const value = template.getAttributeValue(type, name);+ if (value !== null) {+ return value;+ }+ }++ return null;+ }++ /**+ * @param name - label name+ * @returns the label value a note created under this one would be given, or null+ */+ getLabelValueForNewChild(name: string, childType?: NoteType) {+ return this.getAttributeValueForNewChild(LABEL, name, childType);+ }++ /**+ * The templates a note created under this one would be given: the target of a `~child:template`+ * copied onto it, and the targets of the inheritable `~template` / `~inherit` it inherits.+ *+ * @private+ */+ __getTemplatesForNewChild(childType?: NoteType) {+ const templates: FNote[] = [];++ for (const attr of this.getAttributes(RELATION)) {+ const isCopied = attr.name === "child:template";+ const isHandedDown = attr.isInheritable && ["template", "inherit"].includes(attr.name);+ if (!isCopied && !isHandedDown) {+ continue;+ }++ const template = this.froca.notes[attr.value];+ if (!template || (isCopied && childType && template.type !== childType)) {+ continue;+ }++ templates.push(template);+ }++ return templates;+ }+getPromotedDefinitionAttributes() {if (this.isLabelTruthy("hidePromotedAttributes")) {return [];diff --git a/apps/client/src/widgets/collections/geomap/GhostPin.spec.tsx b/apps/client/src/widgets/collections/geomap/GhostPin.spec.tsxindex aced8ccaec..d5fe1dccc9 100644--- a/apps/client/src/widgets/collections/geomap/GhostPin.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/GhostPin.spec.tsx@@ -81,11 +81,13 @@ describe("GhostPin", () => {vi.unstubAllGlobals();});- function mount(map: ReturnType<typeof fakeMap>, note?: FNote) {+ function mount(map: ReturnType<typeof fakeMap>, note?: FNote, parentNote?: FNote) {return act(async () => {render(<ParentMap.Provider value={map as never}>- <GhostPin note={note} />+ <GhostPin+ note={note}+ parentNote={parentNote ?? buildNote({ title: "The map" })} /></ParentMap.Provider>,container as HTMLElement);@@ -130,6 +132,21 @@ describe("GhostPin", () => {expect(await lastSvgBlob?.text()).toContain(`fill="${DEFAULT_MARKER_COLOR}"`);});+ it("wears what the map would give a new note, ahead of the pin", async () => {+ const { renderIconImage } = await import("../../../services/icon_glyphs");+ // Copied onto every note created under the map (see copyChildAttributes in core), so the+ // marker the click drops is what the ghost has to offer.+ const parent = buildNote({+ title: "The map", "#child:iconClass": "bx bx-store", "#child:color": "green" });+ const map = fakeMap();++ await mount(map, undefined, parent);+ await act(async () => { await settle(); });++ expect(renderIconImage).toHaveBeenCalledWith("bx bx-store", expect.anything());+ expect(await lastSvgBlob?.text()).toContain(`fill="green"`);+ });+it("wears the moved note's own colour and icon", async () => {const { renderIconImage } = await import("../../../services/icon_glyphs");const note = buildNote({ title: "Somewhere", "#geolocation": "1,2", "#color": "red", "#iconClass": "bx bx-star" });diff --git a/apps/client/src/widgets/collections/geomap/GhostPin.tsx b/apps/client/src/widgets/collections/geomap/GhostPin.tsxindex 6ba6649863..c8ba3b3357 100644--- a/apps/client/src/widgets/collections/geomap/GhostPin.tsx+++ b/apps/client/src/widgets/collections/geomap/GhostPin.tsx@@ -5,6 +5,7 @@ import type { MapMouseEvent } from "maplibre-gl";import { useContext, useEffect, useRef } from "preact/hooks";import FNote from "../../../entities/fnote";+import { MARKER_NOTE_TYPE } from "./api";import { ParentMap } from "./map";import { DEFAULT_MARKER_COLOR, drawMarkerImage } from "./Markers";@@ -13,22 +14,32 @@ import { DEFAULT_MARKER_COLOR, drawMarkerImage } from "./Markers";* preceded by a picture of what it will do: the crosshair says a place is wanted, and this says* 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 located note is drawn under for- * one being created — and translucent, so it reads as an offer rather than as a marker standing.+ * Drawn with the very image the symbol layer would stamp there (see {@link drawMarkerImage}) and+ * translucent, so it reads as an offer rather than as a marker already standing.+ *+ * A marker being moved wears its own colour and icon. One being created wears what the map would+ * give it — a `#child:iconClass`, a template, an inheritable label (see+ * {@link FNote.getLabelValueForNewChild}) — and the pin a located note is drawn under otherwise,+ * so a map that dresses its markers offers the marker it will actually drop.** 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.*/-export default function GhostPin({ note }: {+export default function GhostPin({ note, parentNote }: {/** The note being moved, whose pin the ghost wears — or none, for a note yet to be created. */note?: FNote;+ /** The map, which a note yet to be created takes its look from. */+ parentNote: FNote;}) {const map = useContext(ParentMap);const elementRef = useRef<HTMLDivElement>(null);- const color = note?.getLabelValue("color") ?? DEFAULT_MARKER_COLOR;- const iconClass = note?.getIcon() ?? GEO_MARKER_ICON;+ const color = note?.getLabelValue("color")+ ?? parentNote.getLabelValueForNewChild("color", MARKER_NOTE_TYPE)+ ?? DEFAULT_MARKER_COLOR;+ const iconClass = note?.getIcon()+ ?? parentNote.getLabelValueForNewChild("iconClass", MARKER_NOTE_TYPE)+ ?? 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/api.ts b/apps/client/src/widgets/collections/geomap/api.tsindex 21297442a1..10d9b69efc 100644--- a/apps/client/src/widgets/collections/geomap/api.ts+++ b/apps/client/src/widgets/collections/geomap/api.ts@@ -1,6 +1,6 @@import { AttributeRow } from "@triliumnext/commons";-import FNote from "../../../entities/fnote";+import FNote, { type NoteType } from "../../../entities/fnote";import attributes from "../../../services/attributes";import dialog from "../../../services/dialog";import { t } from "../../../services/i18n";@@ -10,6 +10,9 @@ import { GPX_MIME } from "./GpxTrack";import type { GeoMouseEvent } from "./map";import { LOCATION_ATTRIBUTE } from "./Markers";+/** 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";+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);@@ -155,7 +158,7 @@ async function createNoteAt(const { note } = await note_create.createNote(parentNote.noteId, {title,content: "",- type: "text",+ type: MARKER_NOTE_TYPE,activate: false,isProtected: parentNote.isProtected,attributes: noteAttributesdiff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsxindex 9399460003..d630ed99b1 100644--- a/apps/client/src/widgets/collections/geomap/index.tsx+++ b/apps/client/src/widgets/collections/geomap/index.tsx@@ -395,9 +395,14 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM/><Tooltips selectedNoteId={selection?.noteId ?? null} paneMaximized={paneMaximized} />{/* The preview under the pointer while a click is armed to mean a place — the note- being moved wearing its own pin, a note to be created wearing the pin it will be- given (see api.ts). */}- {placement && <GhostPin note={placement.mode === "move" ? notes.find((n) => n.noteId === placement.noteId) : undefined} />}+ being moved wearing its own pin, a note to be created wearing the one the map+ would give it (see GhostPin). */}+ {placement && <GhostPin+ parentNote={note}+ note={placement.mode === "move"+ ? notes.find((n) => n.noteId === placement.noteId)+ : undefined}+ />}<DetailPanenotes={notes} parentNote={note} placing={!!placement} isReadOnly={isReadOnly}selection={selection} onSelect={selectNote} onRelocate={startMarkerRelocation}