feat: drag lane turn arrows along their lanes

This commit is contained in:
que01 committed 2026-09-01 10:24:59 +08:00
1 parent 5457f960ff
commit 9cb715a592
12 files changed
+335 -24

No files matched your search

+95 -9
View File
@@ -4,6 +4,7 @@ import Map from 'ol/Map';
import Polygon from 'ol/geom/Polygon';
import View from 'ol/View';
import Select from 'ol/interaction/Select';
import PointerInteraction from 'ol/interaction/Pointer';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import Fill from 'ol/style/Fill';
@@ -17,7 +18,7 @@ import { createHandleDragInteraction } from '../edit/drag-interaction';
import { EditGhostLayer } from '../edit/ghost-layer';
import { EditHandleLayer, type ElementOutline } from '../edit/handle-layer';
import { EditPreviewLayer } from '../edit/preview-layer';
import { fromLonLat, type Coordinate } from '../edit/meters';
import { fromLonLat, offsetCoordinate, signedMetersAlongAxis, toLonLat, type Coordinate } from '../edit/meters';
import type { IntervalRangeHandle } from '../edit/selection';
import type { EditHandle, PreviewLayerName, RoadIntervalAnchor } from '../edit/types';
@@ -38,6 +39,10 @@ interface Props {
element?: ElementOutline | null;
/** A display-only outline for the selected v1 turn arrow. */
laneSelectionBounds?: Coordinate[] | null;
/** Polygon parts copied to the drag feedback layer; baseline parts are hidden by id. */
laneSelectionFeatures?: GeoJson['features'];
laneSelectionAxisAzimuth?: number | null;
onLaneSelectionDragEnd?: (from: Coordinate, to: Coordinate) => void;
/** 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. */
@@ -63,6 +68,9 @@ export function MapCanvas({
ranges,
element,
laneSelectionBounds,
laneSelectionFeatures,
laneSelectionAxisAzimuth,
onLaneSelectionDragEnd,
preview,
onHandleDrag,
onHandleDragEnd,
@@ -78,6 +86,8 @@ export function MapCanvas({
const ghostRef = useRef<EditGhostLayer | null>(null);
const previewRef = useRef<EditPreviewLayer | null>(null);
const laneSelectionSourceRef = useRef<VectorSource | null>(null);
const laneSelectionFeaturesRef = useRef<Feature[]>([]);
const hiddenMarkingIdsRef = useRef<ReadonlySet<string>>(new Set());
const visibleRef = useRef(visible);
visibleRef.current = visible;
// Callbacks live in refs so the map is built once and never rebuilt when a
@@ -85,8 +95,26 @@ export function MapCanvas({
// 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 dragRef = useRef({
onHandleDrag,
onHandleDragEnd,
onHandleBlocked,
onRangeDrag,
onRangeEnd,
onLaneSelectionDragEnd,
laneSelectionAxisAzimuth,
ghostPending,
});
dragRef.current = {
onHandleDrag,
onHandleDragEnd,
onHandleBlocked,
onRangeDrag,
onRangeEnd,
onLaneSelectionDragEnd,
laneSelectionAxisAzimuth,
ghostPending,
};
const selectRef = useRef({ onSelectRoad, onFeature, onBackground });
selectRef.current = { onSelectRoad, onFeature, onBackground };
const selectedRef = useRef<Road | null>(selected);
@@ -95,11 +123,17 @@ export function MapCanvas({
selectedRef.current = selected;
stateRef.current = state;
sceneRef.current = scene;
hiddenMarkingIdsRef.current = new Set(
(laneSelectionFeatures ?? [])
.map((feature) => feature.properties?.native_id)
.filter((id): id is string => typeof id === 'string'),
);
useEffect(() => {
if (!target.current) return;
const layers = createLayers(
() => selectedRef.current,
() => sceneRef.current,
() => hiddenMarkingIdsRef.current,
);
layersRef.current = layers;
const laneSelectionSource = new VectorSource();
@@ -108,7 +142,7 @@ export function MapCanvas({
zIndex: 90,
style: new Style({
stroke: new Stroke({ color: '#d49318', width: 2, lineDash: [6, 4] }),
fill: new Fill({ color: 'rgba(212, 147, 24, 0.12)' }),
fill: new Fill({ color: '#f5f6ee' }),
}),
});
laneSelectionSourceRef.current = laneSelectionSource;
@@ -146,6 +180,47 @@ export function MapCanvas({
style: null,
});
map.addInteraction(select);
let laneDragOrigin: Coordinate | null = null;
let laneDragFeatures: Feature[] = [];
map.addInteraction(
new PointerInteraction({
handleDownEvent: (event) => {
const hit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
layerFilter: (layer) => layer === laneSelectionLayer,
hitTolerance: 10,
});
if (!hit) return false;
laneDragOrigin = event.coordinate as Coordinate;
laneDragFeatures = laneSelectionFeaturesRef.current.map((feature) => feature.clone());
return true;
},
handleDragEvent: (event) => {
const axis = dragRef.current.laneSelectionAxisAzimuth;
if (!laneDragOrigin || axis === null || axis === undefined || !laneDragFeatures.length) return;
const origin = fromLonLat(toLonLat(laneDragOrigin));
const moved = fromLonLat(
offsetCoordinate(
toLonLat(laneDragOrigin),
axis,
signedMetersAlongAxis(laneDragOrigin, event.coordinate as Coordinate, axis),
),
);
const dx = moved[0] - origin[0];
const dy = moved[1] - origin[1];
laneSelectionFeaturesRef.current.forEach((feature, index) => {
const geometry = laneDragFeatures[index]?.getGeometry()?.clone();
geometry?.translate(dx, dy);
feature.setGeometry(geometry);
});
},
handleUpEvent: (event) => {
if (laneDragOrigin) dragRef.current.onLaneSelectionDragEnd?.(laneDragOrigin, event.coordinate as Coordinate);
laneDragOrigin = null;
laneDragFeatures = [];
return false;
},
}),
);
if (handleLayer && ghost) {
map.addInteraction(
createHandleDragInteraction({
@@ -211,13 +286,24 @@ export function MapCanvas({
const source = laneSelectionSourceRef.current;
if (!source) return;
source.clear();
if (laneSelectionBounds && laneSelectionBounds.length >= 3)
const parts = (laneSelectionFeatures ?? []).flatMap((feature) => {
const coordinates = feature.geometry?.coordinates;
if (feature.geometry?.type !== 'Polygon' || !Array.isArray(coordinates) || !Array.isArray(coordinates[0]))
return [];
const ring = coordinates[0] as Coordinate[];
return [new Feature({ geometry: new Polygon([ring.map(fromLonLat)]) })];
});
if (parts.length) source.addFeatures(parts);
else if (laneSelectionBounds && laneSelectionBounds.length >= 3)
source.addFeature(
new Feature({
geometry: new Polygon([[...laneSelectionBounds, laneSelectionBounds[0]].map(fromLonLat)]),
}),
new Feature({ geometry: new Polygon([[...laneSelectionBounds, laneSelectionBounds[0]].map(fromLonLat)]) }),
);
}, [laneSelectionBounds]);
laneSelectionFeaturesRef.current = parts;
}, [laneSelectionBounds, laneSelectionFeatures]);
useEffect(() => {
// Selection changes only affect presentation; compiler GeoJSON stays untouched.
layersRef.current?.markings.changed();
}, [laneSelectionFeatures]);
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.