Files
road-compiler/workbench/client/src/edit/drag-interaction.ts
T
que01andClaude Opus 5 5292c00926 feat: scope the handle manifest and re-aim the ghost
Two consequences of the preview dropping from ~1535 ms to ~142 ms.

The manifest is now scoped to the object being edited. It was 844 KB on a
41-road workspace — 239 handles, 89% of the bytes being `affects` id lists at 46
ids per junction handle — shipped on every preview while the map rendered six.
A segment selection returns 12.2 KB, a 69x reduction, and the cost no longer
multiplies with each handle kind we are about to add. Omitting the selection
keeps the full manifest for the compiler and existing callers, and the tests
assert that scoping is a filter of the full manifest rather than a second
derivation.

The ghost is re-aimed from "estimated geometry" to "what you asked for". Its
guide line existed to mark the origin through a long wait that no longer happens,
so it is gone; what remains is what the preview cannot say — the numeric delta
and whether the drag has hit its clamp. The translucent outline
research/joint-solver.md asked for is deliberately not built: drawing it
accurately means recomputing the road surface in the browser, which the design
forbids, and drawing it crudely would be wrong exactly at transitions, junction
boundaries and clamps. A preview that lies is worse than none.

`degraded` finally has a consumer. EditSession has tracked it since the session
work but nothing read it; a slow solve now dims the ghost in place with a pending
label instead of clearing it and letting the geometry flicker, as design.md
requires.

Selection also fixed a latent hazard: the manifest effect reloads on every
selection change, and it used to call session.load() each time, which would have
discarded unsaved edits the moment the user clicked another road. It now adopts a
document only when the compiled document actually changed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-28 12:26:15 +08:00

141 lines
5.9 KiB
TypeScript

// 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;
/**
* True when the last solve came back `degraded`. design.md requires the ghost to
* stay with a pending state rather than clearing and letting the geometry
* flicker while a slow answer is still on the way.
*/
holdGhost?: () => boolean;
}
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));
if (options.holdGhost?.()) options.ghost.markPending();
else options.ghost.clear();
return false;
},
});
}