feat: drag road handles with ghost and server preview

Three things, kept in one commit because they touch overlapping hunks of the
same two files and this environment has no interactive hunk staging. Splitting
them by file would have drawn boundaries that misrepresent what changed.

1. Step 4 of the map editor. A native OpenLayers PointerInteraction turns a
   drag into a clamped constraint value, the ghost source shows it immediately,
   and the solver's answer replaces a parallel set of preview layers while the
   baseline layers are hidden rather than overwritten. Preview requests debounce
   at 80 ms, pointerup flushes without waiting, and a newer request aborts the
   one in flight; EditSession decides which answers count. Handle positions come
   from the clamped value, so a handle stops at its limit instead of following
   the cursor. Three of the four drag capabilities are live: edge offset,
   sidewalk width, lane divider.

2. Road edge handles were drawn on the wrong side. offsetLine() offsets
   counter-clockwise from the direction of travel and sidewalks use
   `heading + (side === 'left' ? -90 : 90)`, so left is `tangent - 90`;
   makeRoadHandles() placed the left handle at `tangent + 90`, over the right
   kerb. Dragging the visually-left handle moved the right edge. Fixed on both
   sides of the wire, with regression tests that name the sides geographically
   rather than by axis sign.

3. Roads the junctions geometrically fill are now read-only. The 0.45 cap per
   reserve made the existing `unavailable` branch unreachable, so a 14.5 m stub
   between two junctions was offered a 1.5 m editable band with no room for the
   transitions a road-interval constraint needs. Greying only affects the
   manifest: constraints already saved against such a road keep being solved, so
   the geometry output is unchanged and the fixture baselines do not move.

Range handles are built and unit-tested but hidden behind
`intervalEditingSupported`: compileGeometry() reads neither profile.interval nor
profile.transitions, so every edit applies to the whole road and the control
would have had no effect. Recorded in research/interval-not-applied.md, which
also blocks one PRD acceptance criterion.

The ol-ext probe stays in the tree as a manual harness; ol-ext is still not a
dependency.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
que01andClaude Opus 5 committed 2026-08-27 17:38:34 +08:00
1 parent bc4b9a9717
commit 92297270f1
25 files changed
+1826 -58

No files matched your search

+83 -6
View File
@@ -3,11 +3,15 @@ import Map from 'ol/Map';
import View from 'ol/View';
import Select from 'ol/interaction/Select';
import { click } from 'ol/events/condition';
import type { Road, WorkbenchState } from '../types/state';
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 type { EditHandle } from '../edit/types';
import { EditPreviewLayer } from '../edit/preview-layer';
import type { IntervalRangeHandle } from '../edit/selection';
import type { EditHandle, PreviewLayerName, RoadIntervalAnchor } from '../edit/types';
interface Props {
state: WorkbenchState;
@@ -18,12 +22,47 @@ interface Props {
onFeature: (properties: Record<string, unknown>) => 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;
}
export function MapCanvas({ state, selected, visible, scene, onSelectRoad, onFeature, handles }: Props) {
export function MapCanvas({
state,
selected,
visible,
scene,
onSelectRoad,
onFeature,
handles,
ranges,
preview,
onHandleDrag,
onHandleDragEnd,
onHandleBlocked,
onRangeDrag,
onRangeEnd,
}: 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.
const dragRef = useRef({ onHandleDrag, onHandleDragEnd, onHandleBlocked, onRangeDrag, onRangeEnd });
dragRef.current = { onHandleDrag, onHandleDragEnd, onHandleBlocked, onRangeDrag, onRangeEnd };
const selectedRef = useRef<Road | null>(selected);
const stateRef = useRef(state);
const sceneRef = useRef(scene);
@@ -40,10 +79,24 @@ export function MapCanvas({ state, selected, visible, scene, onSelectRoad, onFea
// 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: handleLayer ? [...Object.values(layers), handleLayer.layer] : Object.values(layers),
layers: [
...Object.values(layers),
...(previewLayer ? previewLayer.all() : []),
...(handleLayer ? [handleLayer.layer] : []),
...(ghost ? [ghost.layer] : []),
],
view: new View({ center: [0, 0], zoom: 2 }),
});
mapRef.current = map;
@@ -56,6 +109,21 @@ export function MapCanvas({ state, selected, visible, scene, onSelectRoad, onFea
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),
}),
);
}
const listener = ({ selected: values }: { selected: import('ol/Feature').default[] }) => {
const properties = values[0]?.getProperties();
if (!properties) return;
@@ -94,8 +162,17 @@ export function MapCanvas({ state, selected, visible, scene, onSelectRoad, onFea
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 ?? []);
}, [handles]);
handleLayerRef.current?.render(handles ?? [], ranges ?? []);
}, [handles, ranges]);
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;