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

@@ -0,0 +1,133 @@
// Dragging a handle, on native OpenLayers.
//
// Two things the ol-ext probe settled are baked in here:
//
// 1. The handle position is derived from the clamped constraint value, never from
// the raw pointer delta. `Transform` translated its proxy by the delta, so a
// drag reading -24.1 m left the handle 24 m out while the constraint clamped at
// -5.4 m. `EditGhostLayer.show()` takes the value, not the cursor.
// 2. Hit-testing happens once, on pointerdown. ol-ext ran `forEachFeatureAtPixel`
// from `handleMoveEvent_`, i.e. a canvas readback every mousemove. A drag only
// needs to know what it grabbed at the start.
//
// Two kinds of handle share the gesture: a value handle moves the constraint's
// number, a range end moves the interval its anchor covers. They are branched here
// rather than in two interactions so only one of them can ever own a pointer.
//
// Baseline layers are never touched: this reads the handle layer and writes only
// to the ghost source.
import PointerInteraction from 'ol/interaction/Pointer';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import type VectorLayer from 'ol/layer/Vector';
import type VectorSource from 'ol/source/Vector';
import type { EditGhostLayer } from './ghost-layer';
import { HANDLE_ID } from './handle-layer';
import type { Coordinate } from './meters';
import { projectHandleValue, projectIntervalEnd } from './projection';
import type { IntervalRangeHandle } from './selection';
import type { EditHandle, RoadIntervalAnchor } from './types';
/**
* OpenLayers hands every pointer handler the same widened event: the union covers
* keyboard and wheel because `PointerInteraction` shares one dispatch path. The
* fields used here — `map`, `pixel`, `coordinate` — exist on all of them.
*/
type MapPointerEvent = MapBrowserEvent<PointerEvent | KeyboardEvent | WheelEvent>;
type Active =
| { kind: 'value'; handle: EditHandle; origin: Coordinate }
| { kind: 'range'; range: IntervalRangeHandle; origin: Coordinate };
export interface HandleDragOptions {
/** The `editHandles` layer, the only layer hit-tested. */
layer: VectorLayer<VectorSource>;
ghost: EditGhostLayer;
/** Manifest lookup by handle id; handles carry nothing but their id. */
handleAt: (handleId: string) => EditHandle | undefined;
/** Range-end lookup, for the anchor-moving handles. */
rangeAt: (handleId: string) => IntervalRangeHandle | undefined;
/** Live value during a value drag. Callers debounce the preview request. */
onDrag: (handle: EditHandle, value: number) => void;
/** Final value on pointerup. Callers send this one without debouncing. */
onEnd: (handle: EditHandle, value: number) => void;
/** Live interval during a range drag. */
onRangeDrag: (range: IntervalRangeHandle, anchor: RoadIntervalAnchor) => void;
onRangeEnd: (range: IntervalRangeHandle, anchor: RoadIntervalAnchor) => void;
/** A reserve handle refused the drag, with the manifest's reason. */
onBlocked?: (handle: EditHandle, reason: string) => void;
}
export function createHandleDragInteraction(options: HandleDragOptions): PointerInteraction {
let active: Active | null = null;
const intervalAt = (state: Extract<Active, { kind: 'range' }>, to: Coordinate) =>
projectIntervalEnd(
state.range.anchor,
state.range.end,
state.origin,
to,
state.range.tangentAzimuth,
state.range.roadLengthMeters,
state.range.window,
);
return new PointerInteraction({
handleDownEvent: (event: MapPointerEvent) => {
const feature = event.map.forEachFeatureAtPixel(event.pixel, (candidate) => candidate, {
layerFilter: (layer) => layer === options.layer,
hitTolerance: 10,
});
if (!feature) return false;
const id = String(feature.get(HANDLE_ID));
const origin = event.coordinate as Coordinate;
const range = options.rangeAt(id);
if (range) {
active = { kind: 'range', range, origin };
options.ghost.showInterval(range, range.anchor);
return true;
}
const handle = options.handleAt(id);
if (!handle) return false;
if (!handle.editable) {
// Explain, then decline the gesture so the map still pans. Swallowing it
// would make a reserve handle feel broken rather than owned elsewhere.
options.onBlocked?.(handle, handle.disabledReason || '该手柄不可拖动。');
return false;
}
active = { kind: 'value', handle, origin };
options.ghost.show(handle, handle.value.current);
return true;
},
handleDragEvent: (event: MapPointerEvent) => {
const state = active;
if (!state) return;
const to = event.coordinate as Coordinate;
if (state.kind === 'range') {
const anchor = intervalAt(state, to);
options.ghost.showInterval(state.range, anchor);
options.onRangeDrag(state.range, anchor);
return;
}
const value = projectHandleValue(state.handle, state.origin, to);
options.ghost.show(state.handle, value);
options.onDrag(state.handle, value);
},
handleUpEvent: (event: MapPointerEvent) => {
const state = active;
active = null;
if (!state) return false;
const to = event.coordinate as Coordinate;
// The final request is not debounced: whatever the pointer settled on is the
// value the user meant, and it must not be dropped by a pending timer.
if (state.kind === 'range') options.onRangeEnd(state.range, intervalAt(state, to));
else options.onEnd(state.handle, projectHandleValue(state.handle, state.origin, to));
options.ghost.clear();
return false;
},
});
}