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

+125
View File
@@ -0,0 +1,125 @@
// The `editGhost` source: client-side immediate feedback while a drag is live.
//
// Written to directly, never through React state. The ol-ext probe drove its
// readout from a setState per `translating` event and produced ~1600 renders in
// one session; React is the wrong owner of per-frame feedback. The ghost is also
// not authoritative — it shows where the handle now sits and how far it moved,
// while `editPreview` carries the geometry the server actually solved.
import Feature from 'ol/Feature';
import LineString from 'ol/geom/LineString';
import Point from 'ol/geom/Point';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import CircleStyle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import Text from 'ol/style/Text';
import { coordinateAtStation, fromLonLat, type Coordinate } from './meters';
import { handlePositionFor } from './projection';
import type { IntervalRangeHandle } from './selection';
import type { EditHandle, RoadIntervalAnchor } from './types';
const GUIDE = new Style({
stroke: new Stroke({ color: '#00a5cf', width: 2, lineDash: [6, 4] }),
});
/** The stretch of road an interval edit applies to. */
const BAND = new Style({
stroke: new Stroke({ color: '#29695699', width: 14 }),
});
/** Drawn at the bound so it is obvious the handle stopped rather than stuck. */
const CLAMPED = new Style({
stroke: new Stroke({ color: '#d49318', width: 2, lineDash: [6, 4] }),
});
function knob(label: string, clamped: boolean): Style {
return new Style({
image: new CircleStyle({
radius: 8,
fill: new Fill({ color: clamped ? '#d49318' : '#00a5cf' }),
stroke: new Stroke({ color: '#fff', width: 2 }),
}),
text: new Text({
text: label,
offsetY: -18,
font: '600 12px system-ui, sans-serif',
fill: new Fill({ color: '#1b2426' }),
stroke: new Stroke({ color: '#ffffffcc', width: 3 }),
}),
});
}
export class EditGhostLayer {
readonly layer: VectorLayer<VectorSource>;
private readonly source = new VectorSource();
constructor() {
this.layer = new VectorLayer({
source: this.source,
// Above the handles so the live knob is never occluded by the static one.
zIndex: 110,
style: (feature) => {
if (feature.get('band')) return BAND;
const label = feature.get('label');
if (typeof label === 'string') return knob(label, Boolean(feature.get('clamped')));
return feature.get('clamped') ? CLAMPED : GUIDE;
},
});
}
/**
* Shows the handle at the position implied by `value`, plus a guide line back to
* where it started. `value` is already clamped by `projectHandleValue`, so a
* ghost pinned at the bound is the correct picture: the road cannot go further.
*/
show(handle: EditHandle, value: number): void {
const origin = fromLonLat(handle.position);
const moved = fromLonLat(handlePositionFor(handle, value));
const clamped = value >= handle.value.max - 1e-6 || value <= handle.value.min + 1e-6;
const delta = value - handle.value.current;
this.source.clear();
this.source.addFeatures([
new Feature({ geometry: new LineString([origin, moved]), clamped }),
new Feature({
geometry: new Point(moved),
label: `${delta >= 0 ? '+' : ''}${delta.toFixed(2)} 米`,
clamped,
}),
]);
}
/**
* Shows the interval a range drag is resizing: a band along the road plus its
* length. A pair of dots cannot convey *where* the edit applies, which is the
* whole point of the range handles.
*/
showInterval(range: IntervalRangeHandle, anchor: RoadIntervalAnchor): void {
const span = Math.max(0, anchor.endStation - anchor.startStation);
const steps = 24;
const points: Coordinate[] = [];
for (let index = 0; index <= steps; index += 1) {
const point = coordinateAtStation(range.centerline, anchor.startStation + (span * index) / steps);
if (point) points.push(point);
}
if (points.length < 2) return;
// Pinned when the dragged end has run into the reserve or the other end.
const station = range.end === 'start' ? anchor.startStation : anchor.endStation;
const clamped = station <= range.window.minStation + 1e-6 || station >= range.window.maxStation - 1e-6;
this.source.clear();
this.source.addFeatures([
new Feature({ geometry: new LineString(points.map((point) => fromLonLat(point))), band: true }),
new Feature({
geometry: new Point(fromLonLat(points[range.end === 'start' ? 0 : points.length - 1])),
label: `${(span * range.roadLengthMeters).toFixed(1)} 米`,
clamped,
}),
]);
}
clear(): void {
this.source.clear();
}
}