route editor: shortest-path insert by default + one undo step per action #44

Merged
jmz merged 2 commits from feat/dwim-insert into deploy 2026-11-01 20:49:47 +00:00
Member

What

Route editor point-adding, per Jörg's 2026-11-01 spec. Based on deploy @ 584644d7 (includes #43 BRouter sign-in gate, 9b3bc183). Do not merge from a bot; Jörg merges in the UI. No image build / no homelab-flux change in this PR.

  1. DWIM insert (default = shortest path). A map click goes where the resulting path is shortest (chooseInsertionIndex, unchanged: added length, ties -> append, prepend, n=0/1 special-cased). Two modes: shortest-path (default) and append-only. The existing toggle switches them; Alt / ⌥ / ⌘-click does the other mode for that click only (default mode: modifier-click appends once; append-only mode: modifier-click inserts once). Shift (one-off off-road/track) still composes.
    i18n: append-to-end, insert-at-nearest, insert-modifier-hint replaced by append-only-mode ("Appending to end of route"), shortest-path-mode ("Shortest-path insertion"), mode-modifier-hint ("Hold Alt / ⌘ while clicking to use the other mode once."), in en, de, fr (the only locales that had the old keys).
  2. Click on the drawn line (dot-in-circle cursor) still inserts at that exact spot, now through the same undo path (handleSegmentClick, and line-drag via handleSegmentDragEnd).
  3. Undo/redo = one step per user action, exact order. Root cause of the live bug (A, B, then on-line C: one Undo removed B): the on-line insert is addAnchor + splitSegment = editRoute + insertIntoRoute = two undo entries, neither with anchor snapshots, so undo fell back to re-deriving anchors from the route geometry and dropped the wrong point. Only the toggle/modifier insert path was tagged (tagLastRouteUndo); drag, line-click, line-drag, list-delete and segment-mode flip were not.
    Fix: recordUserAction(fn) in valhalla_store.svelte.ts snapshots anchors + undo depth, runs the whole action, then folds every undo entry it pushed into one entry (changesets concatenated in order; reverse in reverse order) carrying anchorsBefore/After; an action that only changes anchors (first point) becomes an anchor-only entry. tagLastRouteUndo is removed. All mutation paths go through it: append, shortest-path insert/prepend, modifier click, first anchor, on-line click, on-line drag, marker drag (pre-drag snapshot taken at dragstart; markers are moved back on undo), anchor-list/marker delete, segment-mode flip, reset. Anchor snapshots are copies so a later drag cannot rewrite history. Not changed: moveAnchor (already single-entry + tagged), loop-generator accept (documented segment-by-segment), reverse.

Loop trade-off (decision)

The chooser alone inserts a click near the start A next to A (the last point is far away). Resolved (Jörg-approved): in shortest-path mode a plain click within 20 screen px of the first anchor, on a route with >= 3 anchors, appends (closes the loop). Implemented at the click wiring with a pure closesLoop helper (pixel projection via map.project), so chooseInsertionIndex stays pure; the chooser test still pins the raw behaviour. Modifier-click, append-only and on-line clicks are unchanged.

Judged the suggested rule "prefer append when the click is closer to the last point than to any interior segment": not adopted. (a) It does not touch the loop case at all, where the click is near the first point and far from the last. (b) For clicks near the last point the chooser already appends unless the click lies inside the C-D leg's own detour circle, where inserting is the shorter path by a few metres; forcing append there only lengthens the path and breaks the "most logical path" contract. The start-snap rule above is what addresses loops. Test never adds more length than appending would covers the optimality claim over a grid.

Tests

  • route_insertion.test.ts: loop-closing case, click beside last point, optimality grid.
  • valhalla_undo.test.ts (new): the reported bug (A, B, on-line C -> one undo gives A,B), a six-step mixed history (append, shortest insert, prepend, on-line) stepped back and forth in exact order, marker drag (old position restored), list delete, last-anchor removal and reset, redo cleared by new action.
  • Ran locally: vitest run on those two files (17 pass). Full suite and svelte-check: Forgejo CI.

Manual browser test (adapted to the new behaviour)

  1. New trail -> Draw a route. Under the button the toggle reads "Shortest-path insertion"; hint "Hold Alt / ⌘ while clicking to use the other mode once."
  2. Click A, then B about 2 km apart. Then click C beside the middle of the A-B line. Expected: order A, C, B (inserted; route A->C->B as two legs).
  3. Click D well beyond B. Expected: D is appended (A, C, B, D).
  4. Alt-click (Mac: ⌥ or ⌘) beside the C-B leg. Expected: the point is appended (end of the list), once. A following plain click is shortest-path again.
  5. Turn the toggle on ("Appending to end of route"). Click beside the middle of A-C. Expected: appended. Then Alt/⌘-click beside A-C: inserted between A and C once.
  6. Click directly on the drawn line (dot-in-circle cursor). Expected: a point is inserted exactly there.
  7. Undo, step by step, from the end: each press removes exactly the last action (the on-line point first, not the end point; then the Alt-insert, ...), marker and legs together. Redo brings them back in the same order, markers included.
  8. Drag a marker, then Undo. Expected: one undo moves it back and restores both adjacent legs; Redo moves it again. Same for dragging the line to create a point (one undo removes the point).
  9. Keep pressing Undo until the map is empty (first anchor included; Undo greys out). Redo everything: same order, all markers back.
  10. Loop check: with at least 3 anchors in shortest-path mode, a plain click within ~20 screen px of A closes the loop. Expected: the point is appended (end of the list). A plain click farther from A is shortest-path as usual; Alt/⌘-click and the append-only toggle are unchanged.
  11. Delete a middle anchor and then the last one from the anchor list: legs/totals update; each delete is one Undo step. Save, reopen the editor, click once more: anchors come back from the saved GPX and insertion still works.

Watch: on Linux desktops Alt+click may be grabbed by the window manager (window move); the toggle covers that. On Windows, ⌘ is the Windows key.

🤖 Generated with Claude Code

## What Route editor point-adding, per Jörg's 2026-11-01 spec. Based on `deploy` @ 584644d7 (includes #43 BRouter sign-in gate, 9b3bc183). **Do not merge from a bot; Jörg merges in the UI. No image build / no homelab-flux change in this PR.** 1. **DWIM insert (default = shortest path).** A map click goes where the resulting path is shortest (`chooseInsertionIndex`, unchanged: added length, ties -> append, prepend, n=0/1 special-cased). Two modes: **shortest-path (default)** and **append-only**. The existing toggle switches them; Alt / ⌥ / ⌘-click does the *other* mode for that click only (default mode: modifier-click appends once; append-only mode: modifier-click inserts once). Shift (one-off off-road/track) still composes. i18n: `append-to-end`, `insert-at-nearest`, `insert-modifier-hint` replaced by `append-only-mode` ("Appending to end of route"), `shortest-path-mode` ("Shortest-path insertion"), `mode-modifier-hint` ("Hold Alt / ⌘ while clicking to use the other mode once."), in en, de, fr (the only locales that had the old keys). 2. **Click on the drawn line** (dot-in-circle cursor) still inserts at that exact spot, now through the same undo path (`handleSegmentClick`, and line-drag via `handleSegmentDragEnd`). 3. **Undo/redo = one step per user action, exact order.** Root cause of the live bug (A, B, then on-line C: one Undo removed B): the on-line insert is `addAnchor` + `splitSegment` = `editRoute` + `insertIntoRoute` = **two** undo entries, neither with anchor snapshots, so undo fell back to re-deriving anchors from the route geometry and dropped the wrong point. Only the toggle/modifier insert path was tagged (`tagLastRouteUndo`); drag, line-click, line-drag, list-delete and segment-mode flip were not. Fix: `recordUserAction(fn)` in `valhalla_store.svelte.ts` snapshots anchors + undo depth, runs the whole action, then folds every undo entry it pushed into **one** entry (changesets concatenated in order; reverse in reverse order) carrying `anchorsBefore/After`; an action that only changes anchors (first point) becomes an anchor-only entry. `tagLastRouteUndo` is removed. All mutation paths go through it: append, shortest-path insert/prepend, modifier click, first anchor, on-line click, on-line drag, marker drag (pre-drag snapshot taken at dragstart; markers are moved back on undo), anchor-list/marker delete, segment-mode flip, reset. Anchor snapshots are copies so a later drag cannot rewrite history. Not changed: `moveAnchor` (already single-entry + tagged), loop-generator accept (documented segment-by-segment), reverse. ## Loop trade-off (decision) The chooser alone inserts a click near the start A next to A (the last point is far away). **Resolved (Jörg-approved):** in shortest-path mode a plain click within 20 screen px of the first anchor, on a route with >= 3 anchors, appends (closes the loop). Implemented at the click wiring with a pure `closesLoop` helper (pixel projection via `map.project`), so `chooseInsertionIndex` stays pure; the chooser test still pins the raw behaviour. Modifier-click, append-only and on-line clicks are unchanged. Judged the suggested rule "prefer append when the click is closer to the last point than to any interior segment": **not adopted.** (a) It does not touch the loop case at all, where the click is near the *first* point and far from the last. (b) For clicks near the last point the chooser already appends unless the click lies inside the C-D leg's own detour circle, where inserting is the shorter path by a few metres; forcing append there only lengthens the path and breaks the "most logical path" contract. The start-snap rule above is what addresses loops. Test `never adds more length than appending would` covers the optimality claim over a grid. ## Tests - `route_insertion.test.ts`: loop-closing case, click beside last point, optimality grid. - `valhalla_undo.test.ts` (new): the reported bug (A, B, on-line C -> one undo gives A,B), a six-step mixed history (append, shortest insert, prepend, on-line) stepped back and forth in exact order, marker drag (old position restored), list delete, last-anchor removal and reset, redo cleared by new action. - Ran locally: `vitest run` on those two files (17 pass). Full suite and svelte-check: Forgejo CI. ## Manual browser test (adapted to the new behaviour) 1. **New trail -> Draw a route.** Under the button the toggle reads **"Shortest-path insertion"**; hint "Hold Alt / ⌘ while clicking to use the other mode once." 2. Click A, then B about 2 km apart. Then click C beside the middle of the A-B line. **Expected:** order A, C, B (inserted; route A->C->B as two legs). 3. Click D well beyond B. **Expected:** D is appended (A, C, B, D). 4. **Alt-click** (Mac: ⌥ or ⌘) beside the C-B leg. **Expected:** the point is *appended* (end of the list), once. A following plain click is shortest-path again. 5. Turn the toggle on ("Appending to end of route"). Click beside the middle of A-C. **Expected:** appended. Then Alt/⌘-click beside A-C: **inserted** between A and C once. 6. Click **directly on the drawn line** (dot-in-circle cursor). **Expected:** a point is inserted exactly there. 7. **Undo, step by step, from the end:** each press removes exactly the last action (the on-line point first, not the end point; then the Alt-insert, ...), marker and legs together. **Redo** brings them back in the same order, markers included. 8. Drag a marker, then Undo. **Expected:** one undo moves it back and restores both adjacent legs; Redo moves it again. Same for dragging the line to create a point (one undo removes the point). 9. Keep pressing Undo until the map is empty (first anchor included; Undo greys out). Redo everything: same order, all markers back. 10. Loop check: with at least 3 anchors in shortest-path mode, a plain click within ~20 screen px of A closes the loop. **Expected:** the point is appended (end of the list). A plain click farther from A is shortest-path as usual; Alt/⌘-click and the append-only toggle are unchanged. 11. Delete a middle anchor and then the last one from the anchor list: legs/totals update; each delete is one Undo step. Save, reopen the editor, click once more: anchors come back from the saved GPX and insertion still works. Watch: on Linux desktops Alt+click may be grabbed by the window manager (window move); the toggle covers that. On Windows, ⌘ is the Windows key. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
feat(route-editor): shortest-path insert by default, one undo step per action
All checks were successful
CI / web (push) Successful in 1m18s
CI / go (push) Successful in 10m24s
f9668d3465
Clicking the map now places the point where the resulting route is shortest
(chooseInsertionIndex). The toggle switches to append-only, and Alt/Cmd-click
flips whichever mode is active for that click. i18n keys append-to-end,
insert-at-nearest and insert-modifier-hint are replaced by append-only-mode,
shortest-path-mode and mode-modifier-hint (en, de, fr).

Undo/redo: every mutation path (append, shortest insert, modifier click,
on-line click and drag, marker drag, anchor-list delete, segment mode flip,
reset) now runs through recordUserAction, which folds the low-level route edits
into one undo entry carrying anchor snapshots. Fixes the on-line insert, where
split+insert pushed two untagged entries and one Undo removed the end point.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
feat(route-editor): close a loop with a plain click near the first anchor
All checks were successful
CI / web (push) Successful in 1m38s
CI / go (push) Successful in 10m52s
ba0a249aeb
In shortest-path mode a plain click within 20 screen pixels of the first
anchor, on a route with at least 3 anchors, appends instead of inserting next
to the start. Modifier-click, append-only mode and on-line clicks are
unchanged. closesLoop is a pure helper; the pixel projection happens at the
click wiring so chooseInsertionIndex stays pure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XMf4EKnnNBqMcGPkv3bzvQ
maximus changed title from WIP: route editor: shortest-path insert by default + one undo step per action to route editor: shortest-path insert by default + one undo step per action 2026-11-01 20:39:21 +00:00
jmz merged commit ba0a249aeb into deploy 2026-11-01 20:49:47 +00:00
jmz deleted branch feat/dwim-insert 2026-11-01 20:49:48 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
public/wanderer!44
No description provided.