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>
213 lines
9.0 KiB
TypeScript
213 lines
9.0 KiB
TypeScript
import { useEffect, useRef } from 'react';
|
|
import Map from 'ol/Map';
|
|
import View from 'ol/View';
|
|
import Select from 'ol/interaction/Select';
|
|
import { click } from 'ol/events/condition';
|
|
import type { GeoJson, Road, WorkbenchState } from '../types/state';
|
|
import { createLayers, updateLayers, updateSelectedRoad, type LayerName } from '../map/layers';
|
|
import { directEditEnabled } from '../edit/flag';
|
|
import { createHandleDragInteraction } from '../edit/drag-interaction';
|
|
import { EditGhostLayer } from '../edit/ghost-layer';
|
|
import { EditHandleLayer } from '../edit/handle-layer';
|
|
import { EditPreviewLayer } from '../edit/preview-layer';
|
|
import type { IntervalRangeHandle } from '../edit/selection';
|
|
import type { EditHandle, PreviewLayerName, RoadIntervalAnchor } from '../edit/types';
|
|
|
|
interface Props {
|
|
state: WorkbenchState;
|
|
selected: Road | null;
|
|
visible: Partial<Record<LayerName, boolean>>;
|
|
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. */
|
|
ranges?: IntervalRangeHandle[];
|
|
/** Authoritative preview geometry; null returns the map to the baseline. */
|
|
preview?: Partial<Record<PreviewLayerName, GeoJson | null>> | null;
|
|
/** Live value during a drag — the caller debounces the preview request. */
|
|
onHandleDrag?: (handle: EditHandle, value: number) => void;
|
|
/** Final value on pointerup — the caller sends it without debouncing. */
|
|
onHandleDragEnd?: (handle: EditHandle, value: number) => void;
|
|
onHandleBlocked?: (handle: EditHandle, reason: string) => void;
|
|
/** Live interval while a range end is dragged. */
|
|
onRangeDrag?: (range: IntervalRangeHandle, anchor: RoadIntervalAnchor) => void;
|
|
onRangeEnd?: (range: IntervalRangeHandle, anchor: RoadIntervalAnchor) => void;
|
|
/** Last solve came back degraded; hold the ghost instead of clearing it. */
|
|
ghostPending?: boolean;
|
|
}
|
|
export function MapCanvas({
|
|
state,
|
|
selected,
|
|
visible,
|
|
scene,
|
|
onSelectRoad,
|
|
onFeature,
|
|
onBackground,
|
|
handles,
|
|
ranges,
|
|
preview,
|
|
onHandleDrag,
|
|
onHandleDragEnd,
|
|
onHandleBlocked,
|
|
onRangeDrag,
|
|
onRangeEnd,
|
|
ghostPending,
|
|
}: Props) {
|
|
const target = useRef<HTMLDivElement>(null);
|
|
const mapRef = useRef<Map | null>(null);
|
|
const layersRef = useRef<ReturnType<typeof createLayers> | null>(null);
|
|
const handleLayerRef = useRef<EditHandleLayer | null>(null);
|
|
const ghostRef = useRef<EditGhostLayer | null>(null);
|
|
const previewRef = useRef<EditPreviewLayer | null>(null);
|
|
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. 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);
|
|
selectedRef.current = selected;
|
|
stateRef.current = state;
|
|
sceneRef.current = scene;
|
|
useEffect(() => {
|
|
if (!target.current) return;
|
|
const layers = createLayers(
|
|
() => selectedRef.current,
|
|
() => sceneRef.current,
|
|
);
|
|
layersRef.current = layers;
|
|
// With `directEdit` off nothing below exists: no extra source, no layer, no
|
|
// interaction — the canvas is byte-for-byte the shipped behaviour.
|
|
const handleLayer = directEditEnabled ? new EditHandleLayer() : null;
|
|
const ghost = directEditEnabled ? new EditGhostLayer() : null;
|
|
const previewLayer = directEditEnabled
|
|
? new EditPreviewLayer(
|
|
() => selectedRef.current,
|
|
() => sceneRef.current,
|
|
)
|
|
: null;
|
|
handleLayerRef.current = handleLayer;
|
|
ghostRef.current = ghost;
|
|
previewRef.current = previewLayer;
|
|
const map = new Map({
|
|
target: target.current,
|
|
layers: [
|
|
...Object.values(layers),
|
|
...(previewLayer ? previewLayer.all() : []),
|
|
...(handleLayer ? [handleLayer.layer] : []),
|
|
...(ghost ? [ghost.layer] : []),
|
|
],
|
|
view: new View({ center: [0, 0], zoom: 2 }),
|
|
});
|
|
mapRef.current = map;
|
|
const select = new Select({
|
|
condition: click,
|
|
// Handles are not selectable features: clicking one must not re-target the
|
|
// road inspector, and dragging is owned by the edit interaction.
|
|
layers: (layer) => layer !== layers.selectedRoad && layer !== handleLayer?.layer,
|
|
hitTolerance: 12,
|
|
style: null,
|
|
});
|
|
map.addInteraction(select);
|
|
if (handleLayer && ghost) {
|
|
map.addInteraction(
|
|
createHandleDragInteraction({
|
|
layer: handleLayer.layer,
|
|
ghost,
|
|
handleAt: (handleId) => handleLayer.handle(handleId),
|
|
rangeAt: (handleId) => handleLayer.range(handleId),
|
|
onDrag: (handle, value) => dragRef.current.onHandleDrag?.(handle, value),
|
|
onEnd: (handle, value) => dragRef.current.onHandleDragEnd?.(handle, value),
|
|
onRangeDrag: (range, anchor) => dragRef.current.onRangeDrag?.(range, anchor),
|
|
onRangeEnd: (range, anchor) => dragRef.current.onRangeEnd?.(range, anchor),
|
|
onBlocked: (handle, reason) => dragRef.current.onHandleBlocked?.(handle, reason),
|
|
holdGhost: () => dragRef.current.ghostPending === true,
|
|
}),
|
|
);
|
|
}
|
|
const listener = ({ selected: values }: { selected: import('ol/Feature').default[] }) => {
|
|
const properties = values[0]?.getProperties();
|
|
// 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 ||
|
|
(typeof properties.from_lane_id === 'string'
|
|
? 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) selectRef.current.onSelectRoad(road);
|
|
selectRef.current.onFeature(properties);
|
|
};
|
|
select.on('select', listener);
|
|
return () => {
|
|
select.un('select', listener);
|
|
map.setTarget(undefined);
|
|
};
|
|
// Map construction must be once per mounted canvas; state changes update sources below.
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
useEffect(() => {
|
|
const layers = layersRef.current;
|
|
const map = mapRef.current;
|
|
if (!layers || !map) return;
|
|
updateLayers(layers, state, selected);
|
|
const extent = layers.osm.getSource()!.getExtent();
|
|
if (extent && Number.isFinite(extent[0])) map.getView().fit(extent, { padding: [48, 48, 48, 48], maxZoom: 19 });
|
|
}, [state]);
|
|
useEffect(() => {
|
|
const layers = layersRef.current;
|
|
if (!layers) return;
|
|
updateSelectedRoad(layers, selected);
|
|
layers.osm.changed();
|
|
}, [selected]);
|
|
useEffect(() => {
|
|
// Null when the flag is off, so this whole path is inert on main.
|
|
// Only the editHandles source is replaced; baseline layers are never touched.
|
|
handleLayerRef.current?.render(handles ?? [], ranges ?? []);
|
|
}, [handles, ranges]);
|
|
useEffect(() => {
|
|
// A non-degraded answer means the pending ghost has been superseded by real
|
|
// geometry. Clearing an empty ghost is a no-op, so this is safe mid-drag.
|
|
if (!ghostPending) ghostRef.current?.clear();
|
|
}, [ghostPending]);
|
|
useEffect(() => {
|
|
const previewLayer = previewRef.current;
|
|
const baseline = layersRef.current;
|
|
if (!previewLayer || !baseline) return;
|
|
// Showing hides the baseline layers it supersedes; clearing gives them back
|
|
// the visibility the layer switches ask for, never a hardcoded default.
|
|
if (preview) previewLayer.show(preview, baseline);
|
|
else previewLayer.clear(baseline, visibleRef.current);
|
|
}, [preview]);
|
|
useEffect(() => {
|
|
const layers = layersRef.current;
|
|
if (!layers) return;
|
|
Object.entries(visible).forEach(([name, enabled]) =>
|
|
layers[name as LayerName]?.setVisible(
|
|
!scene || !['osm', 'lanes', 'reference', 'gaodeReference', 'connectors'].includes(name)
|
|
? enabled !== false
|
|
: false,
|
|
),
|
|
);
|
|
layers.native.changed();
|
|
layers.sidewalks.changed();
|
|
}, [visible, scene]);
|
|
return <div className="map" ref={target} aria-label="道路地图" />;
|
|
}
|