From c7223fe3bcef5efd92b44cd60f22d0309a13bcbf Mon Sep 17 00:00:00 2001 From: CLYEH Date: Wed, 2 Sep 2026 12:50:52 +0800 Subject: [PATCH 1/5] feat(ui): pin a note where a person clicks, and let a folded pin unfold MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two things a person could not do on their own map. A note always landed at `view.center`, so choosing a place meant panning until the place sat under an invisible crosshair; and folding a pin's bubble left a 9px anchor whose only answer was "open the card about me", which read as a note that had vanished. The place a human's note goes now comes from a click on the map while the note popover is open, held in `note-store.ts` beside the drawing draft — UI state, so no tool can see or steer an unfinished note. The agent side is untouched by construction: `add_note` keeps its toolname, description and autosubmit, and an `agentInvoked` submit still pins at `view.center` whatever a human has half-placed. A folded pin keeps a chip on screen (its source colour and the note's first word) and any click on it unfolds it and does nothing else, so the fold has a way back. `data-folded` says which state it is in. Task: T-106, T-107 --- src/app/globals.css | 70 +++++++++++++++++ src/components/AddNoteForm.tsx | 43 +++++++++-- src/components/MapCanvas.tsx | 88 +++++++++++++++++++-- src/components/Tools.tsx | 29 ++++--- src/components/annotation-marker.test.ts | 30 ++++++++ src/components/annotation-marker.ts | 97 +++++++++++++++++++++--- src/components/note-store.test.ts | 64 ++++++++++++++++ src/components/note-store.ts | 42 ++++++++++ 8 files changed, 431 insertions(+), 32 deletions(-) create mode 100644 src/components/annotation-marker.test.ts create mode 100644 src/components/note-store.test.ts create mode 100644 src/components/note-store.ts diff --git a/src/app/globals.css b/src/app/globals.css index 95cb70b..bcd9813 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -1219,6 +1219,76 @@ html:is([data-chrome="awake"]:not(:where([data-panel="closed"])), :where([data-p display: none; } +/* Folded (T-107): the bubble goes, but the note does not vanish. What is left + is a chip carrying the source's own colour and the first word — small enough + to see the map past, big enough to click, which is the whole fix: folding + used to leave a 9px anchor whose only answer was "open the card about me". */ +.pin-chip { + display: none; + align-items: center; + gap: 5px; + padding: 3px 8px 3px 6px; + border-radius: 999px; + background: var(--glass-strong); + backdrop-filter: blur(12px); + -webkit-backdrop-filter: blur(12px); + border: 1px solid rgba(45, 212, 191, 0.4); + box-shadow: 0 6px 16px rgba(3, 7, 13, 0.4); + max-width: 150px; +} +.note-pin[data-annotation-source="user"] .pin-chip { + border-color: rgba(244, 143, 177, 0.45); +} +.note-pin[data-folded="true"] .pin-chip { + display: inline-flex; +} +.pin-chip-dot { + width: 6px; + height: 6px; + border-radius: 50%; + flex-shrink: 0; + background: var(--agent); +} +.note-pin[data-annotation-source="user"] .pin-chip-dot { + background: var(--human); +} +.pin-chip-label { + font-size: 10.5px; + line-height: 1.2; + color: var(--text-mid); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +/* The provisional pin (T-106): where the note in the popover would land. A + dashed rose ring on a dashed stem — hollow because it is not a note yet, and + click-through because the next click on the map is how it moves. */ +.note-draft-pin { + display: flex; + flex-direction: column; + align-items: center; + pointer-events: none; +} +.note-draft-pin .draft-stem { + width: 1.5px; + height: 13px; + background: repeating-linear-gradient( + to bottom, + var(--human) 0 3px, + transparent 3px 6px + ); +} +.note-draft-pin .draft-anchor { + width: 11px; + height: 11px; + border-radius: 50%; + background: transparent; + border: 1.6px dashed var(--human); + box-shadow: 0 1px 6px rgba(0, 0, 0, 0.4); + transform: translateY(-2px); +} + /* ---------------------------------------------------- inspector */ .grab { display: none; diff --git a/src/components/AddNoteForm.tsx b/src/components/AddNoteForm.tsx index 6c01c19..c4ee039 100644 --- a/src/components/AddNoteForm.tsx +++ b/src/components/AddNoteForm.tsx @@ -5,6 +5,7 @@ import { ACTIVITY_NOTE_CHARS } from "@/lib/map-tools/activity"; import { truncate } from "@/lib/map-tools/shapes"; import { useMapStore } from "@/lib/store/map-store"; import { emitHumanFx } from "./fx/human-events"; +import { useNoteStore } from "./note-store"; /** ~1 m, the same precision the tools report coordinates in. */ const round5 = (n: number) => Math.round(n * 1e5) / 1e5; @@ -22,6 +23,16 @@ const round5 = (n: number) => Math.round(n * 1e5) / 1e5; * written to the store and echoed in `add-note-status`, which is also what an * agent reads back after submitting. * + * **Where the note lands depends on who submitted it** (T-106). An agent has no + * cursor, so an agent's submit pins at `view.center` — the place the tool has + * always named and the place its description still names. A person has one: + * while this popover is open, a click on the map places a provisional pin + * (`note-store.ts`, drawn by MapCanvas) and their submit pins there. Without a + * click a person gets the centre too, which is the behaviour this form shipped + * with — panning the map until the spot sits under an invisible crosshair is + * now the fallback rather than the only way. `add-note-target` reports which of + * the two is armed, and the status line names the coordinates actually used. + * * Who gets credit for the note comes from `SubmitEvent.agentInvoked`, the * spec's own discriminator. Without a WebMCP client there is no such flag and * the note is stored as `source: "user"` - the honest default for a form a @@ -43,6 +54,7 @@ const round5 = (n: number) => Math.round(n * 1e5) / 1e5; */ export function AddNoteForm({ focusable = true }: { focusable?: boolean }) { const [status, setStatus] = useState(""); + const draft = useNoteStore((s) => s.draft); const onSubmit = (event: React.FormEvent) => { event.preventDefault(); @@ -55,9 +67,16 @@ export function AddNoteForm({ focusable = true }: { focusable?: boolean }) { } const { view, addAnnotation, recordActivity } = useMapStore.getState(); const agentInvoked = (event.nativeEvent as SubmitEvent).agentInvoked === true; + // The draft is a person's answer to "where", so only a person's submit + // reads it: `add_note` promises an agent the map centre, and an agent that + // submits while a human has a pin half-placed must still get the place the + // tool description names. Read from the store rather than from the + // subscribed value, so a click that lands in the same tick as the submit + // cannot pin the note at the place before it. + const at = agentInvoked ? view.center : (useNoteStore.getState().draft ?? view.center); const stored = addAnnotation({ source: agentInvoked ? "agent" : "user", - at: view.center, + at, note, }); // An agent-submitted form is a tool call like any other, and the activity @@ -79,10 +98,11 @@ export function AddNoteForm({ focusable = true }: { focusable?: boolean }) { // drives its (teal) effect. emitHumanFx({ type: "note", annotation: stored }); } - setStatus( - `Pinned ${stored.id} at ${round5(view.center[0])}, ${round5(view.center[1])}.`, - ); + setStatus(`Pinned ${stored.id} at ${round5(at[0])}, ${round5(at[1])}.`); form.reset(); + // The place is spent with the note: the next one starts unplaced rather + // than stacking silently on the last one's pin. + useNoteStore.getState().clearDraft(); }; return ( @@ -94,7 +114,14 @@ export function AddNoteForm({ focusable = true }: { focusable?: boolean }) { onSubmit={onSubmit} className="note-form" > - + {/* The visible instruction follows the state; the `tooldescription` and + `toolparamdescription` around it do not, because they are the agent's + contract and an agent's note still goes to the centre. */} +
{status}

+ {/* Off screen: which of the two places the next human submit will use, + without reading a CSS class or measuring a marker. The draft's own + numbers are already rounded to ~1 m by the store that holds them. */} + + {draft ? `${draft[0]}, ${draft[1]}` : "centre"} + ); } diff --git a/src/components/MapCanvas.tsx b/src/components/MapCanvas.tsx index 75c2105..17fcbc7 100644 --- a/src/components/MapCanvas.tsx +++ b/src/components/MapCanvas.tsx @@ -19,7 +19,7 @@ import { type LngLat, type MapView, } from "@/lib/store/map-store"; -import { createAnnotationElement } from "./annotation-marker"; +import { createAnnotationElement, createNoteDraftElement } from "./annotation-marker"; import { createBeadImages } from "./bead-sprite"; import { useCardStore } from "./card-store"; import { @@ -42,6 +42,7 @@ import { import { useBrowseStore } from "./browse-store"; import { useDrawStore, type DrawMode } from "./draw-store"; import { setFxMap } from "./fx/map-handle"; +import { useNoteStore } from "./note-store"; import { readChromeVisible, subscribeChromeVisible } from "./panel-store"; import { DRAFT_SOURCE, @@ -245,9 +246,21 @@ export default function MapCanvas() { const store = useMapStore; const draw = useDrawStore; + const note = useNoteStore; const browse = useBrowseStore; const card = useCardStore; + /** + * A hand gesture owns the next click on the map: a polygon corner, or the + * place a note is about to be pinned to (T-106). While one is running a + * click is not a question about what is under it, so the feature, bead and + * drawing handlers defer and the cursor is a crosshair either way. + * + * Draw mode has precedence: with the note popover left open, clicking is + * still how a polygon is drawn. + */ + const handOwnsClick = () => draw.getState().mode !== "none" || note.getState().open; + /** * No map, so nothing will ever report a viewport. Compute one instead: * without it `bounds` stays null forever and every viewport tool answers @@ -789,7 +802,7 @@ export default function MapCanvas() { }; map.on("click", INTERACTIVE_LAYER_IDS, (event) => { // While drawing, a click is a vertex, not a selection. - if (draw.getState().mode !== "none") return; + if (handOwnsClick()) return; const id = event.features ?.map((f) => f.properties?.id) .find((value): value is string => typeof value === "string"); @@ -803,7 +816,7 @@ export default function MapCanvas() { // and cannot reach. const beadLayers = [...BEAD_LAYER_IDS]; map.on("click", beadLayers, (event) => { - if (draw.getState().mode !== "none") return; + if (handOwnsClick()) return; const feature = event.features?.[0]; if (!feature) return; const { id, cluster_id: cluster } = feature.properties as { @@ -840,7 +853,7 @@ export default function MapCanvas() { const drawingLayers = [...DRAWING_LAYER_IDS]; const overDrawing = [...INTERACTIVE_LAYER_IDS, ...beadLayers]; map.on("click", drawingLayers, (event) => { - if (draw.getState().mode !== "none") return; + if (handOwnsClick()) return; if (map.queryRenderedFeatures(event.point, { layers: overDrawing }).length > 0) return; const id = event.features ?.map((f) => f.properties?.id) @@ -850,11 +863,11 @@ export default function MapCanvas() { const pointerLayers = [...INTERACTIVE_LAYER_IDS, ...beadLayers, ...drawingLayers]; map.on("mouseenter", pointerLayers, () => { - if (draw.getState().mode !== "none") return; + if (handOwnsClick()) return; canvas.style.cursor = "pointer"; }); map.on("mouseleave", pointerLayers, () => { - if (draw.getState().mode !== "none") return; + if (handOwnsClick()) return; canvas.style.cursor = ""; }); @@ -876,8 +889,16 @@ export default function MapCanvas() { // arrives; the preview source is written through `setSourceData`, which is // a no-op until the style is up. + /** + * The cursor says what the next click will do, and two gestures can claim + * it: a polygon corner and a note waiting for its place. Only the polygon + * also takes the double-click away from the zoom, which is why the mode is + * still the argument — a person placing a note may double-click to zoom in + * on the spot they are aiming at, and the second click simply re-places the + * pin where they zoomed. + */ const applyDrawCursor = (mode: DrawMode) => { - map.getCanvas().style.cursor = mode === "polygon" ? "crosshair" : ""; + map.getCanvas().style.cursor = mode === "polygon" || handOwnsClick() ? "crosshair" : ""; // Otherwise the second click of "double-click to finish" zooms the map. if (mode === "polygon") map.doubleClickZoom.disable(); else map.doubleClickZoom.enable(); @@ -901,6 +922,39 @@ export default function MapCanvas() { draw.getState().finish(); }); + // --- Note draft (T-106) ----------------------------------------------- + // A person pins a note where they click. Before this, every note landed at + // `view.center`, so choosing a place meant panning the map until the place + // sat under an invisible crosshair — the map had to be moved to say + // something about it. The draft lives in `note-store.ts`, not in the map + // store: it has no id and no tool may see it. + + /** The provisional pin, while there is a place but not yet a note. */ + let draftMarker: Marker | null = null; + + const applyNoteDraft = (at: LngLat | null) => { + if (!at) { + draftMarker?.remove(); + draftMarker = null; + return; + } + if (draftMarker) draftMarker.setLngLat(at); + else { + draftMarker = new Marker({ element: createNoteDraftElement(), anchor: "bottom" }) + .setLngLat(at) + .addTo(map); + } + }; + + map.on("click", (event: MapMouseEvent) => { + // Draw mode has precedence: with the note popover left open, a click is + // still a corner (the guard above has already taken it). + if (draw.getState().mode !== "none") return; + const state = note.getState(); + if (!state.open) return; + state.place([event.lngLat.lng, event.lngLat.lat]); + }); + const unsubscribe = store.subscribe((state, previous) => { if (state.view !== previous.view && !fromMap) applyView(state.view); // Whatever took a place off the map — an agent's `select_features`, a @@ -940,7 +994,19 @@ export default function MapCanvas() { const unsubscribeDraw = draw.subscribe((state, previous) => { if (state.draft !== previous.draft) applyDraft(map, state.draft); - if (state.mode !== previous.mode) applyDrawCursor(state.mode); + if (state.mode !== previous.mode) { + applyDrawCursor(state.mode); + // Drawing takes the map's clicks over, so a note left half-placed under + // the new polygon would be a pin nothing can move any more. + if (state.mode !== "none") note.getState().clearDraft(); + } + }); + + const unsubscribeNote = note.subscribe((state, previous) => { + if (state.draft !== previous.draft) applyNoteDraft(state.draft); + // Opening the popover is what turns the map into a place-picker, so it + // changes the cursor exactly as entering draw mode does. + if (state.open !== previous.open) applyDrawCursor(draw.getState().mode); }); const unsubscribeBrowse = browse.subscribe((state, previous) => { @@ -987,16 +1053,22 @@ export default function MapCanvas() { syncAnnotationMarkers(map, markers, store.getState().annotations, tapAnnotation); applyDrawCursor(draw.getState().mode); + // The UI stores outlive this effect (a remount, or React's development + // double-mount), so a note being placed keeps its pin across one. + applyNoteDraft(note.getState().draft); return () => { window.removeEventListener("resize", onResize); setFxMap(null); unsubscribe(); unsubscribeDraw(); + unsubscribeNote(); unsubscribeBrowse(); unsubscribeChrome(); for (const marker of markers.values()) marker.remove(); markers.clear(); + draftMarker?.remove(); + draftMarker = null; try { map.remove(); } catch { diff --git a/src/components/Tools.tsx b/src/components/Tools.tsx index 2c07bef..b16bde5 100644 --- a/src/components/Tools.tsx +++ b/src/components/Tools.tsx @@ -1,8 +1,9 @@ "use client"; -import { useCallback, useRef, useState } from "react"; +import { useCallback, useRef } from "react"; import { AddNoteForm } from "./AddNoteForm"; import { DrawHint, DrawPill } from "./DrawToolbar"; +import { useNoteStore } from "./note-store"; import { ShareChip } from "./ShareChip"; /** @@ -13,18 +14,28 @@ import { ShareChip } from "./ShareChip"; * the person watching it. The row sits opposite the brand and steps aside for * the inspector lane when the agent chrome is up (globals.css). * - * Nothing here is new behaviour: the draw switch, the note form and the copy - * chip are the ones that shipped, re-skinned into pills and given a home that - * survives the agent panels being hidden. The note form especially — it is the - * declarative `add_note` WebMCP tool, and it used to live in the inspector, - * which no longer exists on a page no agent has touched. + * The draw switch, the note form and the copy chip are the ones that shipped, + * re-skinned into pills and given a home that survives the agent panels being + * hidden. The note form especially — it is the declarative `add_note` WebMCP + * tool, and it used to live in the inspector, which no longer exists on a page + * no agent has touched. + * + * Whether the note popover is open is the one piece of this row's state that is + * not this row's business alone: while it is open the map is a place-picker, + * and a click on it places the pin the note will be written at (T-106). So it + * lives in `note-store.ts` beside the drawing draft, where MapCanvas can + * subscribe to it imperatively without this component re-rendering the map. */ export function Tools() { - const [noteOpen, setNoteOpen] = useState(false); + const noteOpen = useNoteStore((s) => s.open); + const setNoteOpen = useNoteStore((s) => s.setOpen); const inputRef = useRef(null); const toggleNote = useCallback(() => { - const next = !noteOpen; + // Read through the store rather than the rendered value: `setOpen` also + // throws away any half-placed pin, and doing that against a stale answer + // would leave a pin on the map with no popover to pin it from. + const next = !useNoteStore.getState().open; setNoteOpen(next); // Focus after paint: the popover is opacity-hidden rather than unmounted // (see below), so the input exists either way — but focusing it while it is @@ -32,7 +43,7 @@ export function Tools() { if (next) { requestAnimationFrame(() => inputRef.current?.querySelector("input")?.focus()); } - }, [noteOpen]); + }, [setNoteOpen]); return ( <> diff --git a/src/components/annotation-marker.test.ts b/src/components/annotation-marker.test.ts new file mode 100644 index 0000000..abf84be --- /dev/null +++ b/src/components/annotation-marker.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vitest"; +import { foldedLabel } from "./annotation-marker"; + +/** + * A folded pin is the state T-107 exists to make survivable: before it, folding + * a note left a 9 px dot and no way back. The chip's label is the part of that + * promise a test can hold without a browser — a folded note still says which + * note it is, and it always says *something*. + */ +describe("folded pin label", () => { + it("keeps the note's first word, so a folded pin is still recognisable", () => { + expect(foldedLabel("quiet street, good light")).toBe("quiet…"); + }); + + it("clips a long first word instead of widening the chip", () => { + // A URL or a hashtag has no spaces and would otherwise print in full over + // the map it is meant to be folded out of. + expect(foldedLabel("supercalifragilistic expialidocious")).toBe("supercalifragi…"); + }); + + it("clips a note with no spaces at all by length", () => { + // 大安森林公園 is one word to a splitter and a whole note to a reader. + expect(foldedLabel("大安森林公園旁邊的長椅很安靜,午後有樹蔭")).toBe("大安森林公園旁邊的長椅很安靜…"); + }); + + it("still shows something for a note that is only whitespace", () => { + // The chip is the way back; an empty one is a fold with no handle. + expect(foldedLabel(" ")).toBe("…"); + }); +}); diff --git a/src/components/annotation-marker.ts b/src/components/annotation-marker.ts index 573fc71..f908f27 100644 --- a/src/components/annotation-marker.ts +++ b/src/components/annotation-marker.ts @@ -1,5 +1,18 @@ import type { Annotation } from "@/lib/store/map-store"; +/** How much of the note a folded pin keeps, before the ellipsis. */ +const FOLDED_LABEL_CHARS = 14; + +/** + * The note, folded down to something a person can still recognise it by: its + * first word, clipped, and an ellipsis saying there is more behind it. A note + * with no spaces (a Chinese one, say) is clipped by length alone. + */ +export function foldedLabel(note: string): string { + const first = note.trim().split(/\s+/)[0] ?? ""; + return first ? `${first.slice(0, FOLDED_LABEL_CHARS)}…` : "…"; +} + /** * The DOM of one annotation pin, used as a MapLibre marker element: a glass * card carrying who pinned it and what it says, a stem, and a white-ringed @@ -13,15 +26,24 @@ import type { Annotation } from "@/lib/store/map-store"; * * The card is visible by default because a note nobody can read is not a note; * clicking the card itself folds it away when several sit on top of each other. - * Clicking anywhere else on the pin — the stem, the anchor, or the pin by - * keyboard — asks the same question a tap on any other mark asks, and gets the - * same answer: `onTap` opens the "On the map" card, which is where a note is - * removed by hand. The two live on one element because they are two halves of - * one gesture ("let me see past this" and "what is this?"), and splitting them - * across two controls would put a second tab stop on every note on the map. + * **Folding is a two-way gesture** (T-107): a folded pin keeps a chip on screen + * — its source colour and the first word of the note — and a click anywhere on + * a folded pin unfolds it and does nothing else. Before, folding left a 9 px + * anchor whose only remaining answer was `onTap`, so a note a person folded + * read as a note that had vanished. + * + * Unfolded, the pin still answers two questions with one control: clicking the + * card folds it, and clicking anywhere else - the stem, the anchor, or the pin + * by keyboard - asks the same question a tap on any other mark asks, and gets + * the same answer: `onTap` opens the "On the map" card, which is where a note + * is removed by hand and, since T-108, where its full text is legible. The two + * live on one element because they are two halves of one gesture ("let me see + * past this" and "what is this?"), and splitting them across two controls would + * put a second tab stop on every note on the map. * * `data-testid="annotation-popup"` is on the card, `annotation-pin` on the pin - * (the FX layer projects effects onto it by id — see `fx/surfaces.ts`). + * (the FX layer projects effects onto it by id — see `fx/surfaces.ts`), and + * `data-folded` on the pin is the state, so a test never has to read a class. */ export function createAnnotationElement( annotation: Annotation, @@ -50,21 +72,48 @@ export function createAnnotationElement( card.append(source, note); + // What a folded pin leaves on the map: the source's own colour and enough of + // the note to recognise it by. Never `display: none` on its own account - + // this is the way back. + const chip = document.createElement("span"); + chip.className = "pin-chip"; + chip.dataset.testid = "annotation-chip"; + const chipDot = document.createElement("span"); + chipDot.className = "pin-chip-dot"; + const chipLabel = document.createElement("span"); + chipLabel.className = "pin-chip-label"; + chipLabel.textContent = foldedLabel(annotation.note); + chip.append(chipDot, chipLabel); + const stem = document.createElement("span"); stem.className = "pin-stem"; const anchor = document.createElement("span"); anchor.className = "pin-anchor"; - root.append(card, stem, anchor); + root.append(card, chip, stem, anchor); + + const setFolded = (folded: boolean) => { + root.dataset.folded = String(folded); + card.classList.toggle("hidden", folded); + }; + setFolded(false); root.addEventListener("click", (event) => { // Without this the click also reaches the map, which in draw mode would - // drop a polygon vertex under the pin. + // drop a polygon vertex under the pin - or, with the note popover open, + // move the pin a person is about to place. event.stopPropagation(); + // A folded pin has one thing to say, and it is "here I am again". Asking + // it anything else - including `onTap`, which the anchor answers when the + // note is open - would leave the fold with no way out. + if (root.dataset.folded === "true") { + setFolded(false); + return; + } const target = event.target; if (target instanceof Element && target.closest(".pin-card")) { - card.classList.toggle("hidden"); + setFolded(true); return; } onTap(annotation.id); @@ -72,3 +121,31 @@ export function createAnnotationElement( return root; } + +/** + * The provisional pin: where a note *would* land if it were pinned now. + * + * Rose, like every hand mark, and hollow - a dashed ring on a dashed stem - + * because it is not a note yet. It has no id, it is not in the map store and + * no tool can see it (`note-store.ts`); it exists between the click that chose + * a place and the submit that pins one. `pointer-events: none` (globals.css) + * so the second click of "actually, over there" goes through it to the map. + */ +export function createNoteDraftElement(): HTMLElement { + const root = document.createElement("span"); + root.className = "note-draft-pin"; + root.dataset.testid = "note-draft-pin"; + // Announced by the form's label and status line, not by a floating shape: a + // screen reader following the caret in the note field should not have this + // read to it as a control it can reach. + root.setAttribute("aria-hidden", "true"); + + const stem = document.createElement("span"); + stem.className = "draft-stem"; + + const anchor = document.createElement("span"); + anchor.className = "draft-anchor"; + + root.append(stem, anchor); + return root; +} diff --git a/src/components/note-store.test.ts b/src/components/note-store.test.ts new file mode 100644 index 0000000..2bfe82a --- /dev/null +++ b/src/components/note-store.test.ts @@ -0,0 +1,64 @@ +import { beforeEach, describe, expect, it } from "vitest"; +import { useMapStore } from "@/lib/store/map-store"; +import { useNoteStore } from "./note-store"; + +const note = () => useNoteStore.getState(); + +describe("note store", () => { + beforeEach(() => { + useNoteStore.setState({ open: false, draft: null }); + }); + + it("keeps an unplaced note out of the map store entirely", () => { + // The line this store exists to draw: a draft has no id, is on no map and + // is not something `get_map_state` may report. Only `addAnnotation` — the + // form's own submit — crosses it. + const before = useMapStore.getState(); + note().setOpen(true); + note().place([121.5, 25]); + + expect(useMapStore.getState().annotations).toBe(before.annotations); + expect(useMapStore.getState().activity).toBe(before.activity); + }); + + it("rounds the clicked place to ~1 m, like a drawn corner", () => { + // The pin the map draws, the coordinates the form reports and the + // annotation the store ends up with have to be the same number. + note().setOpen(true); + note().place([121.5175123456, 25.0478987654]); + expect(note().draft).toEqual([121.51751, 25.0479]); + }); + + it("moves the pin on a second click rather than collecting places", () => { + // One note, one place: unlike a polygon, a second click is a correction. + note().setOpen(true); + note().place([121.5, 25]); + note().place([121.6, 25.1]); + expect(note().draft).toEqual([121.6, 25.1]); + }); + + it("throws the draft away when the popover is toggled either way", () => { + // Closing the popover abandons the note being written, so the pin must go + // with it; re-opening starts at "centre" rather than at a place the person + // clicked in a session they left. + note().setOpen(true); + note().place([121.5, 25]); + note().setOpen(false); + expect(note().draft).toBeNull(); + + note().setOpen(true); + note().place([121.5, 25]); + note().setOpen(true); + expect(note().draft).toBeNull(); + }); + + it("clears the draft after the note is pinned", () => { + // The next note starts unplaced: leaving the old draft behind would pin it + // silently at the place of the note before it. + note().setOpen(true); + note().place([121.5, 25]); + note().clearDraft(); + expect(note().draft).toBeNull(); + expect(note().open).toBe(true); + }); +}); diff --git a/src/components/note-store.ts b/src/components/note-store.ts new file mode 100644 index 0000000..16461a7 --- /dev/null +++ b/src/components/note-store.ts @@ -0,0 +1,42 @@ +import { create } from "zustand"; +import type { LngLat } from "@/lib/store/map-store"; + +/** + * ~1 m, rounded on the way in exactly as `draw-store.ts` rounds a corner: the + * place the provisional pin is drawn at, the place the form says it will pin + * to and the place the annotation ends up at are then the same number. + */ +const round5 = (n: number) => Math.round(n * 1e5) / 1e5; + +/** + * Where the note a person is typing is going to land, deliberately *not* in + * `map-store.ts` — the same line `draw-store.ts` draws. An unfinished note is + * UI: it has no id, it is on no map, and no tool may see or steer it. Only the + * finished note crosses over, through `addAnnotation`. + * + * `open` lives here too, rather than in `Tools`'s own state, because MapCanvas + * has to know whether the next click on the map is a place for this note. + * MapCanvas subscribes imperatively (it must not re-render), the form and the + * toggle read it with the hook — the same split hand-drawing uses. + * + * The draft never survives the popover: opening or closing it starts a note + * with no place, which is the state the form calls "centre". + */ +interface NoteStore { + /** Whether the note popover is open — the toggle's own state (`Tools`). */ + open: boolean; + /** Where the next note goes, or null for "wherever the map centre is". */ + draft: LngLat | null; + setOpen: (open: boolean) => void; + /** A click on the map while the popover is open: place the pin, or move it. */ + place: (at: LngLat) => void; + clearDraft: () => void; +} + +export const useNoteStore = create((set) => ({ + open: false, + draft: null, + setOpen: (open) => set({ open, draft: null }), + place: ([lng, lat]) => set({ draft: [round5(lng), round5(lat)] }), + clearDraft: () => set({ draft: null }), +})); From 63321fae84aabdb6ab25800bfcaaa94a7b49982a Mon Sep 17 00:00:00 2001 From: CLYEH Date: Wed, 2 Sep 2026 12:51:10 +0800 Subject: [PATCH 2/5] feat(ui): put the whole note on the card a tap opens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "On the map" card was the only surface a note is fully readable on — the pin's bubble is 210px wide and a person can fold it away — and it clipped the note to 72 characters. A 200-character note therefore had an end that nothing on the page could show: fold the pin, tap it, and the answer was an ellipsis. The headline for a note is now the note, wrapped; the card already measures its own height and flips above or below the tap, so a long note makes a taller card rather than a lost one. Feature and shape headlines are unchanged. Task: T-108 --- src/app/globals.css | 10 ++++++++++ src/components/card-model.test.ts | 17 ++++++++++++----- src/components/card-model.ts | 17 +++++++++++------ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/app/globals.css b/src/app/globals.css index bcd9813..51b1818 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -2035,6 +2035,16 @@ html:is([data-chrome="awake"]:not(:where([data-panel="closed"])), :where([data-p font-weight: 650; line-height: 1.35; } +/* A note's headline is the note (T-108), so it wraps instead of being clipped: + this card is the only surface that carries every character of one, and the + card measures its own height, so a long note makes a taller card rather than + a truncated one. `anywhere` because a note may be one unbroken string — a URL, + or a language that does not space its words. */ +.otm-card[data-kind="annotation"] .otm-name { + font-weight: 600; + white-space: pre-wrap; + overflow-wrap: anywhere; +} .otm-cat { margin-top: 3px; display: flex; diff --git a/src/components/card-model.test.ts b/src/components/card-model.test.ts index 5ff3f86..9f8d586 100644 --- a/src/components/card-model.test.ts +++ b/src/components/card-model.test.ts @@ -1,11 +1,11 @@ import { describe, expect, it } from "vitest"; import type { GlassMapFeature } from "@/lib/data/schema"; +import { MAX_NOTE_CHARS } from "@/lib/map-tools/shapes"; import type { Annotation, Drawing } from "@/lib/store/map-store"; import type { MapFeature } from "@/lib/store/tier2"; import { CARD_COPY, CARD_GAP_PX, - CARD_NOTE_CHARS, CARD_TOP_MARGIN_PX, cardPlacement, cardProvenance, @@ -187,14 +187,21 @@ describe("cardView", () => { expect(view?.copy.line).toContain("you pinned it"); }); - it("clips a long note to a headline instead of growing the card", () => { - const long = "x".repeat(CARD_NOTE_CHARS * 2); + it("puts the whole of a long note on the card, however long the note is", () => { + // The card is the only surface a note is fully readable on: the pin's + // bubble is 210px wide and a person can fold it away. Clipping here (it + // used to stop at 72 characters) meant a note that `annotate` accepts in + // full — up to MAX_NOTE_CHARS, and 200 from the form — had an end that + // nothing on the page could show, and a person who folded the pin to see + // the map was left with an ellipsis and no way back to their own words. + // The card grows instead; it measures its own height and flips above or + // below the tap accordingly (`cardPlacement`, above). + const long = "x".repeat(MAX_NOTE_CHARS); const view = cardView( { kind: "annotation", id: "annotation:1", x: 1, y: 2 }, subjects({ annotations: [{ ...note, note: long }] }), ); - expect(view?.name).toHaveLength(CARD_NOTE_CHARS); - expect(view?.name.endsWith("…")).toBe(true); + expect(view?.name).toBe(long); }); it("answers for a shape with its label, its size and who drew it", () => { diff --git a/src/components/card-model.ts b/src/components/card-model.ts index 4bd23f7..1560e95 100644 --- a/src/components/card-model.ts +++ b/src/components/card-model.ts @@ -1,5 +1,4 @@ import { isFeatureCategory, type GlassMapFeature } from "@/lib/data/schema"; -import { truncate } from "@/lib/map-tools/shapes"; import type { Annotation, Drawing, SelectionSource } from "@/lib/store/map-store"; import type { MapFeature } from "@/lib/store/tier2"; import { categorySingular } from "./category-labels"; @@ -142,9 +141,6 @@ export function cardPlacement(tapY: number, cardHeight: number): CardPlace { return tapY - CARD_GAP_PX - cardHeight < CARD_TOP_MARGIN_PX ? "below" : "above"; } -/** How much of a note the card's headline shows before it trails off. */ -export const CARD_NOTE_CHARS = 72; - /** A shape with no label is named by what it is. */ export const DRAWING_KIND_WORD: Record = { circle: "Circle", @@ -163,7 +159,10 @@ function drawingWhat(drawing: Drawing): string { export interface CardView { kind: CardKind; id: string; - /** The headline: a place's name, a note's text, a shape's label. */ + /** + * The headline: a place's name, a shape's label, or a note's text — the + * whole of it, however long, because the card is where a note is read. + */ name: string; /** * The place's English name, under the headline, when the data carries one @@ -217,7 +216,13 @@ export function cardView(target: CardTarget, subjects: CardSubjects): CardView | return { kind: "annotation", id: annotation.id, - name: truncate(annotation.note, CARD_NOTE_CHARS), + // The whole note, every character of it (T-108). The card is the only + // surface a note is fully readable on — the pin's bubble is 210px wide + // and folds away — so clipping here at 72 characters left a 200-character + // note with no place on the page that carried its end. It wraps + // (globals.css) and the card re-measures its own height, so a long note + // makes a taller card rather than a lost one. + name: annotation.note, what: "Note", swatch: DRAWING_COLOR[annotation.source], sample: false, From 3c65d0cbc107fde8f7cc06d1da7d598fcb2687ca Mon Sep 17 00:00:00 2001 From: CLYEH Date: Wed, 2 Sep 2026 13:19:11 +0800 Subject: [PATCH 3/5] fix(ui): review fixes for the human note gesture (T-106/T-107/T-108) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit S1 — an agent's `add_note` no longer clears the draft pin a person is still placing. The draft is the human's answer to "where"; an agent never reads it (it pins at the centre) and must not spend it either. S2 — the note headline on the "On the map" card is bounded. `annotate` accepts 500 characters and every one of them may be a newline, which was 250 lines of card: unscrollable, and below 641px the card is fixed against the bottom bar so it grew off the top of the screen with no way back. 25vh and a scrollbar, not 40vh: measured at 1440x900, a 40vh card is 475px, fits on neither side of a tap in the middle of the map, and hangs its Remove/Keep buttons 41px below the fold. At 25vh the same worst-case card is 340px and lands wholly on screen at every tap height on both tiers. S4 — Esc leaves note mode. While the popover is open the map is a place-picker and taps on places, beads and shapes are suppressed, so a second press on the Note chip was the only way out — and the person's focus is usually out on the map by then, hence a window-level listener bound only while it is open. Focus goes back to the chip. A map click stays the placing gesture and is deliberately not also a way out. N1/N3/N6 — the CJK test literal is a place name rather than a sentence, the draw-only guard on `tapAnnotation` says why it is draw-only, and the note store test asserts whole-state identity of the map store rather than two fields. Task: T-106 Task: T-107 Task: T-108 --- src/app/globals.css | 17 ++++++++++++- src/components/AddNoteForm.tsx | 9 ++++--- src/components/MapCanvas.tsx | 6 +++++ src/components/Tools.tsx | 32 +++++++++++++++++++++++- src/components/annotation-marker.test.ts | 6 +++-- src/components/note-store.test.ts | 6 +++-- 6 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/app/globals.css b/src/app/globals.css index 51b1818..1b6bf81 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -2039,11 +2039,26 @@ html:is([data-chrome="awake"]:not(:where([data-panel="closed"])), :where([data-p this card is the only surface that carries every character of one, and the card measures its own height, so a long note makes a taller card rather than a truncated one. `anywhere` because a note may be one unbroken string — a URL, - or a language that does not space its words. */ + or a language that does not space its words; `pre-wrap` because a note may + carry its own line breaks and they are the writer's. + + Bounded, though: `annotate` accepts 500 characters and every one of them may + be a newline, which is 250 lines of card. Above 640px that would run off the + top of the map; below it the card is `position: fixed` against the bottom bar + and could not be scrolled back into view at all. A scrollbar keeps the whole + note reachable on every tier, and 25vh is what makes the card placeable: the + rest of the card measures ~115px, and `cardPlacement` can only ever guarantee + a side that fits while the whole card is shorter than half the viewport minus + its margins. Measured at 1440x900 with 500 newline characters: 225px of note, + 340px of card, which fits above a tap in the middle of the map — at 40vh the + same card was 475px, fitted on neither side, and hung its Remove/Keep buttons + 41px below the fold. */ .otm-card[data-kind="annotation"] .otm-name { font-weight: 600; white-space: pre-wrap; overflow-wrap: anywhere; + max-height: 25vh; + overflow-y: auto; } .otm-cat { margin-top: 3px; diff --git a/src/components/AddNoteForm.tsx b/src/components/AddNoteForm.tsx index c4ee039..c1f59a3 100644 --- a/src/components/AddNoteForm.tsx +++ b/src/components/AddNoteForm.tsx @@ -100,9 +100,12 @@ export function AddNoteForm({ focusable = true }: { focusable?: boolean }) { } setStatus(`Pinned ${stored.id} at ${round5(at[0])}, ${round5(at[1])}.`); form.reset(); - // The place is spent with the note: the next one starts unplaced rather - // than stacking silently on the last one's pin. - useNoteStore.getState().clearDraft(); + // The place is spent with the note a person pinned: the next one starts + // unplaced rather than stacking silently on the last one's pin. An agent's + // submit never touches it - the draft belongs to the human who clicked it, + // and a tool call arriving mid-sentence must not take their pin off the map + // (it did not use it either: an agent pins at the centre). + if (!agentInvoked) useNoteStore.getState().clearDraft(); }; return ( diff --git a/src/components/MapCanvas.tsx b/src/components/MapCanvas.tsx index 17fcbc7..59dac91 100644 --- a/src/components/MapCanvas.tsx +++ b/src/components/MapCanvas.tsx @@ -345,6 +345,12 @@ export default function MapCanvas() { * that is what the card is about — and because the pin is a tall element * whose top is nowhere near the place it points at. In draw mode a tap is * a vertex like any other, so the card stays out of the way. + * + * The guard is deliberately draw-only, and not `handOwnsClick()`: with the + * note popover open, a tap on a pin that is already on the map still opens + * its card. It cannot place a draft either way — the pin root stops the + * click from reaching the map (`annotation-marker.ts`) — so refusing here + * would only take an answer away and give nothing back. */ const tapAnnotation = (id: string) => { if (draw.getState().mode !== "none") return; diff --git a/src/components/Tools.tsx b/src/components/Tools.tsx index b16bde5..fc0717d 100644 --- a/src/components/Tools.tsx +++ b/src/components/Tools.tsx @@ -1,6 +1,6 @@ "use client"; -import { useCallback, useRef } from "react"; +import { useCallback, useEffect, useRef } from "react"; import { AddNoteForm } from "./AddNoteForm"; import { DrawHint, DrawPill } from "./DrawToolbar"; import { useNoteStore } from "./note-store"; @@ -30,6 +30,7 @@ export function Tools() { const noteOpen = useNoteStore((s) => s.open); const setNoteOpen = useNoteStore((s) => s.setOpen); const inputRef = useRef(null); + const toggleRef = useRef(null); const toggleNote = useCallback(() => { // Read through the store rather than the rendered value: `setOpen` also @@ -45,11 +46,40 @@ export function Tools() { } }, [setNoteOpen]); + /** + * Esc leaves note mode, like every other dismissible surface on this page + * (the "On the map" card, a drawing in progress). + * + * It needs to be a document-level listener rather than one on the popover: + * while the popover is open the map is a place-picker, so the person's + * pointer - and, after a click on the canvas, the focus - is out on the map + * rather than in the form. Without this the only way out of a mode that + * suppresses every tap on a place, bead or shape was a second press on the + * Note chip. Bound only while the popover is open, so it can never swallow an + * Esc meant for the drawing toolbar or the card. + * + * A click on the map is the placing gesture, so it is deliberately NOT also a + * way out: a person aiming at a spot must not close the popover by aiming. + */ + useEffect(() => { + if (!noteOpen) return; + const onKeyDown = (event: KeyboardEvent) => { + if (event.key !== "Escape") return; + setNoteOpen(false); + // Focus was in the field, which is about to be transparent and out of the + // tab order: hand it back to the chip that opened it. + toggleRef.current?.focus(); + }; + window.addEventListener("keydown", onKeyDown); + return () => window.removeEventListener("keydown", onKeyDown); + }, [noteOpen, setNoteOpen]); + return ( <>