feat: select a crosswalk group by clicking any of its stripes

A crosswalk renders as six stripe features but is edited as one thing. Clicking
one now resolves the whole group, and clicking anything else drops the selection
so a box cannot outlive what it was drawn around.

The group id needed no compiler change: crosswalk:node/<crossingId> is already
stable, because compileControlMarkings() emits exactly one group per crossing
from a single candidate road. Verified against real output — 48 stripes, 8
groups of 6, nothing ungrouped, and none of the 8 stop lines mistaken for one,
since both share controlFeature() and are told apart by provenance. Grouping
therefore reads a published identifier rather than clustering on proximity,
which prd.md forbids.

Two supporting fixes in MapCanvas: the select callbacks join the existing
callback ref, and an empty click now reports background instead of returning
early. The listener is registered once, so it had been holding the first
render's closures — harmless while they only called stable setters, and not
survivable now that one of them reads current layers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
que01andClaude Opus 5 committed 2026-08-31 13:35:59 +08:00
1 parent 623481fd2c
commit 2694cad619
4 files changed
+267 -9

No files matched your search

+17 -4
View File
@@ -20,6 +20,8 @@ interface Props {
scene: boolean;
onSelectRoad: (road: Road) => void;
onFeature: (properties: Record<string, unknown>) => void;
/** A click that selected nothing, so element selection can be cleared. */
onBackground?: () => void;
/** Already filtered to the selected road. Ignored unless `directEdit` is on. */
handles?: EditHandle[];
/** The two ends of the affected interval, drawn as squares. */
@@ -44,6 +46,7 @@ export function MapCanvas({
scene,
onSelectRoad,
onFeature,
onBackground,
handles,
ranges,
preview,
@@ -63,9 +66,14 @@ export function MapCanvas({
const visibleRef = useRef(visible);
visibleRef.current = visible;
// Callbacks live in refs so the map is built once and never rebuilt when a
// parent re-renders with new closures.
// parent re-renders with new closures. The select callbacks belong here too:
// the listener is registered once, so without a ref it would keep calling the
// first render's closure — harmless while those only called stable setters,
// but not once one of them reads current state.
const dragRef = useRef({ onHandleDrag, onHandleDragEnd, onHandleBlocked, onRangeDrag, onRangeEnd, ghostPending });
dragRef.current = { onHandleDrag, onHandleDragEnd, onHandleBlocked, onRangeDrag, onRangeEnd, ghostPending };
const selectRef = useRef({ onSelectRoad, onFeature, onBackground });
selectRef.current = { onSelectRoad, onFeature, onBackground };
const selectedRef = useRef<Road | null>(selected);
const stateRef = useRef(state);
const sceneRef = useRef(scene);
@@ -130,7 +138,12 @@ export function MapCanvas({
}
const listener = ({ selected: values }: { selected: import('ol/Feature').default[] }) => {
const properties = values[0]?.getProperties();
if (!properties) return;
// An empty click deselects. The road inspector deliberately keeps showing
// the last feature, but an element selection box must not outlive it.
if (!properties) {
selectRef.current.onBackground?.();
return;
}
const roadId =
properties.road_id ||
properties.from_road_id ||
@@ -138,8 +151,8 @@ export function MapCanvas({
? properties.from_lane_id.slice(5, properties.from_lane_id.lastIndexOf(':'))
: undefined);
const road = stateRef.current.compiled.model.roads.find((item) => item.id === roadId);
if (road) onSelectRoad(road);
onFeature(properties);
if (road) selectRef.current.onSelectRoad(road);
selectRef.current.onFeature(properties);
};
select.on('select', listener);
return () => {