543 lines
23 KiB
TypeScript
543 lines
23 KiB
TypeScript
import { useEffect, useRef } from 'react';
|
|
import Feature from 'ol/Feature';
|
|
import LineString from 'ol/geom/LineString';
|
|
import Map from 'ol/Map';
|
|
import Point from 'ol/geom/Point';
|
|
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 CircleStyle from 'ol/style/Circle';
|
|
import Fill from 'ol/style/Fill';
|
|
import Stroke from 'ol/style/Stroke';
|
|
import Style from 'ol/style/Style';
|
|
import { click } from 'ol/events/condition';
|
|
import type { GeoJson, Road, WorkbenchState } from '../types/state';
|
|
import { createLayers, updateJunctionHotspots, updateLayers, updateSelectedRoad, type LayerName } from '../map/layers';
|
|
import { junctionDescriptors, type JunctionDescriptor, type JunctionRef } from '../junction/hotspots';
|
|
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, offsetCoordinate, signedMetersAlongAxis, toLonLat, type Coordinate } from '../edit/meters';
|
|
import { azimuthBetween } from '../edit/meters';
|
|
import type { IntervalRangeHandle } from '../edit/selection';
|
|
import type { EditHandle, PreviewLayerName, RoadIntervalAnchor } from '../edit/types';
|
|
|
|
interface Props {
|
|
state: WorkbenchState;
|
|
selected: Road | null;
|
|
visible: Partial<Record<LayerName, boolean>>;
|
|
scene: boolean;
|
|
onSelectRoad: (road: Road) => void;
|
|
onFeature: (properties: Record<string, unknown>) => void;
|
|
/** A click that selected nothing, so element selection can be cleared. */
|
|
onBackground?: () => 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[];
|
|
/** The selected element's box, grabbable as the control that moves it. */
|
|
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'];
|
|
laneSelectionHiddenIds?: string[];
|
|
/** The lane centerline point that anchors every part of the selected arrow. */
|
|
laneSelectionAnchor?: Coordinate | null;
|
|
laneSelectionAxisAzimuth?: number | null;
|
|
/** Receives the axis-projected final map coordinate. */
|
|
onLaneSelectionDrag?: (from: Coordinate, to: Coordinate) => void;
|
|
onLaneSelectionDragEnd?: (from: Coordinate, to: Coordinate) => void;
|
|
onSignalDrag?: (signalUid: string, coordinate: Coordinate) => void;
|
|
onSignalDragEnd?: (signalUid: string, coordinate: Coordinate) => void;
|
|
/** The selected signal gets a temporary pivot marker and rotate handle. */
|
|
signalSelectionUid?: string | null;
|
|
onSignalRotate?: (signalUid: string, mastHeading: number) => void;
|
|
onSignalRotateEnd?: (signalUid: string, mastHeading: number) => 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. */
|
|
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;
|
|
/** Last solve came back degraded; hold the ghost instead of clearing it. */
|
|
ghostPending?: boolean;
|
|
editMode?: boolean;
|
|
junctionHotspots?: boolean;
|
|
onJunctionHotspot?: (ref: JunctionRef, coordinate: Coordinate, descriptor: JunctionDescriptor) => void;
|
|
}
|
|
export function MapCanvas({
|
|
state,
|
|
selected,
|
|
visible,
|
|
scene,
|
|
onSelectRoad,
|
|
onFeature,
|
|
onBackground,
|
|
handles,
|
|
ranges,
|
|
element,
|
|
laneSelectionBounds,
|
|
laneSelectionFeatures,
|
|
laneSelectionHiddenIds,
|
|
laneSelectionAnchor,
|
|
laneSelectionAxisAzimuth,
|
|
onLaneSelectionDrag,
|
|
onLaneSelectionDragEnd,
|
|
onSignalDrag,
|
|
onSignalDragEnd,
|
|
signalSelectionUid,
|
|
onSignalRotate,
|
|
onSignalRotateEnd,
|
|
preview,
|
|
onHandleDrag,
|
|
onHandleDragEnd,
|
|
onHandleBlocked,
|
|
onRangeDrag,
|
|
onRangeEnd,
|
|
ghostPending,
|
|
editMode = false,
|
|
junctionHotspots = true,
|
|
onJunctionHotspot,
|
|
}: 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);
|
|
/** Auto-fit is for a newly loaded area, never for a recompile of the same one. */
|
|
const fittedAreaIdRef = useRef<string | null>(null);
|
|
const laneSelectionSourceRef = useRef<VectorSource | null>(null);
|
|
const laneSelectionLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
|
const signalTransformSourceRef = useRef<VectorSource | null>(null);
|
|
const signalTransformLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
|
|
const laneSelectionFeaturesRef = useRef<Feature[]>([]);
|
|
const laneSelectionAnchorRef = useRef<Coordinate | null>(null);
|
|
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
|
|
// parent re-renders with new closures. The select callbacks belong here too:
|
|
// 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,
|
|
onLaneSelectionDragEnd,
|
|
onLaneSelectionDrag,
|
|
onSignalDrag,
|
|
onSignalDragEnd,
|
|
onSignalRotate,
|
|
onSignalRotateEnd,
|
|
laneSelectionAxisAzimuth,
|
|
ghostPending,
|
|
});
|
|
dragRef.current = {
|
|
onHandleDrag,
|
|
onHandleDragEnd,
|
|
onHandleBlocked,
|
|
onRangeDrag,
|
|
onRangeEnd,
|
|
onLaneSelectionDragEnd,
|
|
onLaneSelectionDrag,
|
|
onSignalDrag,
|
|
onSignalDragEnd,
|
|
onSignalRotate,
|
|
onSignalRotateEnd,
|
|
laneSelectionAxisAzimuth,
|
|
ghostPending,
|
|
};
|
|
const selectRef = useRef({ onSelectRoad, onFeature, onBackground, onJunctionHotspot });
|
|
selectRef.current = { onSelectRoad, onFeature, onBackground, onJunctionHotspot };
|
|
const selectedRef = useRef<Road | null>(selected);
|
|
const stateRef = useRef(state);
|
|
const sceneRef = useRef(scene);
|
|
const editModeRef = useRef(editMode);
|
|
selectedRef.current = selected;
|
|
stateRef.current = state;
|
|
sceneRef.current = scene;
|
|
editModeRef.current = editMode;
|
|
hiddenMarkingIdsRef.current = new Set(
|
|
laneSelectionHiddenIds ??
|
|
(laneSelectionFeatures ?? [])
|
|
.map((feature) => feature.properties?.native_id)
|
|
.filter((id): id is string => typeof id === 'string'),
|
|
);
|
|
laneSelectionAnchorRef.current = laneSelectionAnchor ?? null;
|
|
useEffect(() => {
|
|
if (!target.current) return;
|
|
// StrictMode intentionally tears this effect down and creates a fresh map.
|
|
// A fresh view starts at [0, 0], so it must receive its own initial fit.
|
|
fittedAreaIdRef.current = null;
|
|
const layers = createLayers(
|
|
() => selectedRef.current,
|
|
() => sceneRef.current,
|
|
() => hiddenMarkingIdsRef.current,
|
|
);
|
|
layersRef.current = layers;
|
|
const laneSelectionSource = new VectorSource();
|
|
const laneSelectionLayer = new VectorLayer({
|
|
source: laneSelectionSource,
|
|
zIndex: 90,
|
|
style: new Style({
|
|
stroke: new Stroke({ color: '#d49318', width: 2, lineDash: [6, 4] }),
|
|
fill: new Fill({ color: '#f5f6ee' }),
|
|
}),
|
|
});
|
|
laneSelectionSourceRef.current = laneSelectionSource;
|
|
laneSelectionLayerRef.current = laneSelectionLayer;
|
|
const signalTransformSource = new VectorSource();
|
|
const signalTransformLayer = new VectorLayer({
|
|
source: signalTransformSource,
|
|
zIndex: 95,
|
|
style: (feature) => {
|
|
const component = feature.get('signal_transform');
|
|
if (component === 'crosshair') return new Style({ stroke: new Stroke({ color: '#0e7490', width: 2 }) });
|
|
if (component === 'rotate-leader')
|
|
return new Style({ stroke: new Stroke({ color: '#2563eb', width: 1.5, lineDash: [4, 3] }) });
|
|
return new Style({
|
|
image: new CircleStyle({
|
|
radius: 8,
|
|
fill: new Fill({ color: '#2563eb' }),
|
|
stroke: new Stroke({ color: '#ffffff', width: 2 }),
|
|
}),
|
|
});
|
|
},
|
|
});
|
|
signalTransformSourceRef.current = signalTransformSource;
|
|
signalTransformLayerRef.current = signalTransformLayer;
|
|
// Direct-edit sources are always installed; visibility and hit testing follow
|
|
// the in-page browser/edit switch.
|
|
const handleLayer = new EditHandleLayer();
|
|
const ghost = new EditGhostLayer();
|
|
const previewLayer = new EditPreviewLayer(
|
|
() => selectedRef.current,
|
|
() => sceneRef.current,
|
|
);
|
|
handleLayerRef.current = handleLayer;
|
|
ghostRef.current = ghost;
|
|
previewRef.current = previewLayer;
|
|
const map = new Map({
|
|
target: target.current,
|
|
layers: [
|
|
...Object.values(layers),
|
|
...(previewLayer ? previewLayer.all() : []),
|
|
laneSelectionLayer,
|
|
signalTransformLayer,
|
|
...(handleLayer ? [handleLayer.layer] : []),
|
|
...(ghost ? [ghost.layer] : []),
|
|
],
|
|
view: new View({ center: [0, 0], zoom: 2 }),
|
|
});
|
|
mapRef.current = map;
|
|
// Non-visual probe for browser regression tests; production interactions still
|
|
// go through OpenLayers and the normal pointer handlers below.
|
|
(target.current as HTMLDivElement & { __roadWorkbenchMap?: Map }).__roadWorkbenchMap = map;
|
|
const select = new Select({
|
|
condition: click,
|
|
// Handles are not selectable features: clicking one must not re-target the
|
|
// road inspector, and dragging is owned by the edit interaction.
|
|
layers: (layer) =>
|
|
layer !== layers.selectedRoad &&
|
|
layer !== handleLayer?.layer &&
|
|
layer !== laneSelectionLayer &&
|
|
layer !== signalTransformLayer &&
|
|
layer !== layers.junctionHotspots,
|
|
hitTolerance: 12,
|
|
style: null,
|
|
});
|
|
map.addInteraction(select);
|
|
map.on('singleclick', (event) => {
|
|
const hit = map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
|
layerFilter: (layer) => layer === layers.junctionHotspots,
|
|
hitTolerance: 12,
|
|
});
|
|
const ref = hit?.get('junction_ref');
|
|
const descriptor = hit?.get('junction_descriptor');
|
|
if (!ref || !descriptor) return;
|
|
selectRef.current.onJunctionHotspot?.(ref, toLonLat(event.coordinate as Coordinate), descriptor);
|
|
});
|
|
let signalUid: string | null = null;
|
|
let rotatePivot: Coordinate | null = null;
|
|
map.addInteraction(
|
|
new PointerInteraction({
|
|
handleDownEvent: (event) => {
|
|
if (!editModeRef.current) return false;
|
|
const rotateHit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
|
layerFilter: (layer) => layer === signalTransformLayer,
|
|
hitTolerance: 12,
|
|
});
|
|
if (rotateHit?.get('signal_transform') === 'rotate-handle') {
|
|
const uid = rotateHit.get('signal_uid');
|
|
const pivot = rotateHit.get('signal_pivot');
|
|
if (typeof uid === 'string' && Array.isArray(pivot) && pivot.length === 2) {
|
|
signalUid = uid;
|
|
rotatePivot = pivot as Coordinate;
|
|
event.map.getViewport().style.cursor = 'grabbing';
|
|
return true;
|
|
}
|
|
}
|
|
const hit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
|
layerFilter: (layer) => layer === layers.signals,
|
|
hitTolerance: 10,
|
|
});
|
|
const uid = hit?.get('signal_uid');
|
|
if (!hit || typeof uid !== 'string' || hit.get('signal_component')) return false;
|
|
signalUid = uid;
|
|
selectRef.current.onFeature(hit.getProperties());
|
|
event.map.getViewport().style.cursor = 'grabbing';
|
|
return true;
|
|
},
|
|
handleDragEvent: (event) => {
|
|
if (signalUid) {
|
|
const target = toLonLat(event.coordinate as Coordinate);
|
|
if (rotatePivot) dragRef.current.onSignalRotate?.(signalUid, azimuthBetween(rotatePivot, target));
|
|
else dragRef.current.onSignalDrag?.(signalUid, target);
|
|
}
|
|
},
|
|
handleUpEvent: (event) => {
|
|
if (signalUid) {
|
|
const target = toLonLat(event.coordinate as Coordinate);
|
|
if (rotatePivot) dragRef.current.onSignalRotateEnd?.(signalUid, azimuthBetween(rotatePivot, target));
|
|
else dragRef.current.onSignalDragEnd?.(signalUid, target);
|
|
}
|
|
signalUid = null;
|
|
rotatePivot = null;
|
|
event.map.getViewport().style.cursor = '';
|
|
return false;
|
|
},
|
|
}),
|
|
);
|
|
let laneDragOrigin: Coordinate | null = null;
|
|
let laneDragAnchor: Coordinate | null = null;
|
|
let laneDragFeatures: Feature[] = [];
|
|
map.addInteraction(
|
|
new PointerInteraction({
|
|
handleDownEvent: (event) => {
|
|
if (!editModeRef.current) return false;
|
|
const hit = event.map.forEachFeatureAtPixel(event.pixel, (feature) => feature, {
|
|
layerFilter: (layer) => layer === laneSelectionLayer,
|
|
hitTolerance: 10,
|
|
});
|
|
if (!hit) return false;
|
|
laneDragOrigin = event.coordinate as Coordinate;
|
|
laneDragAnchor = laneSelectionAnchorRef.current ? fromLonLat(laneSelectionAnchorRef.current) : null;
|
|
laneDragFeatures = laneSelectionFeaturesRef.current.map((feature) => feature.clone());
|
|
return Boolean(laneDragAnchor);
|
|
},
|
|
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);
|
|
});
|
|
if (laneDragAnchor)
|
|
dragRef.current.onLaneSelectionDrag?.(laneDragOrigin, [laneDragAnchor[0] + dx, laneDragAnchor[1] + dy]);
|
|
},
|
|
handleUpEvent: (event) => {
|
|
const axis = dragRef.current.laneSelectionAxisAzimuth;
|
|
if (laneDragOrigin && laneDragAnchor && axis !== null && axis !== undefined) {
|
|
const to = event.coordinate as Coordinate;
|
|
const projectedPointer = fromLonLat(
|
|
offsetCoordinate(toLonLat(laneDragOrigin), axis, signedMetersAlongAxis(laneDragOrigin, to, axis)),
|
|
);
|
|
const dx = projectedPointer[0] - laneDragOrigin[0];
|
|
const dy = projectedPointer[1] - laneDragOrigin[1];
|
|
dragRef.current.onLaneSelectionDragEnd?.(laneDragOrigin, [laneDragAnchor[0] + dx, laneDragAnchor[1] + dy]);
|
|
}
|
|
laneDragOrigin = null;
|
|
laneDragAnchor = null;
|
|
laneDragFeatures = [];
|
|
return false;
|
|
},
|
|
}),
|
|
);
|
|
if (handleLayer && ghost) {
|
|
map.addInteraction(
|
|
createHandleDragInteraction({
|
|
layer: handleLayer.layer,
|
|
ghost,
|
|
handleAt: (handleId) => (editModeRef.current ? handleLayer.handle(handleId) : undefined),
|
|
rangeAt: (handleId) => (editModeRef.current ? handleLayer.range(handleId) : undefined),
|
|
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),
|
|
holdGhost: () => dragRef.current.ghostPending === true,
|
|
}),
|
|
);
|
|
}
|
|
const listener = ({ selected: values }: { selected: import('ol/Feature').default[] }) => {
|
|
const properties = values[0]?.getProperties();
|
|
// An empty click deselects. The road inspector deliberately keeps showing
|
|
// the last feature, but an element selection box must not outlive it.
|
|
if (!properties) {
|
|
selectRef.current.onBackground?.();
|
|
return;
|
|
}
|
|
const roadId =
|
|
properties.road_id ||
|
|
properties.from_road_id ||
|
|
(typeof properties.from_lane_id === 'string'
|
|
? properties.from_lane_id.slice(5, properties.from_lane_id.lastIndexOf(':'))
|
|
: undefined);
|
|
const road = stateRef.current.compiled.model.roads.find((item) => item.id === roadId);
|
|
if (road) selectRef.current.onSelectRoad(road);
|
|
selectRef.current.onFeature(properties);
|
|
};
|
|
select.on('select', listener);
|
|
return () => {
|
|
select.un('select', listener);
|
|
map.setTarget(undefined);
|
|
delete (target.current as HTMLDivElement & { __roadWorkbenchMap?: Map })?.__roadWorkbenchMap;
|
|
};
|
|
// Map construction must be once per mounted canvas; state changes update sources below.
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
useEffect(() => {
|
|
const layers = layersRef.current;
|
|
const map = mapRef.current;
|
|
if (!layers || !map) return;
|
|
updateLayers(layers, state, selected);
|
|
updateJunctionHotspots(layers, junctionDescriptors(state), junctionHotspots);
|
|
const extent = layers.osm.getSource()!.getExtent();
|
|
if (fittedAreaIdRef.current !== state.areaId && extent && Number.isFinite(extent[0])) {
|
|
map.getView().fit(extent, { padding: [48, 48, 48, 48], maxZoom: 19 });
|
|
fittedAreaIdRef.current = state.areaId;
|
|
}
|
|
}, [state, junctionHotspots]);
|
|
useEffect(() => {
|
|
const layers = layersRef.current;
|
|
if (!layers) return;
|
|
updateSelectedRoad(layers, selected);
|
|
layers.osm.changed();
|
|
}, [selected]);
|
|
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 ?? [], ranges ?? [], element ?? null);
|
|
}, [handles, ranges, element]);
|
|
useEffect(() => {
|
|
const source = laneSelectionSourceRef.current;
|
|
if (!source) return;
|
|
laneSelectionLayerRef.current?.setVisible(editMode);
|
|
source.clear();
|
|
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)]) }),
|
|
);
|
|
laneSelectionFeaturesRef.current = parts;
|
|
}, [laneSelectionBounds, laneSelectionFeatures]);
|
|
useEffect(() => {
|
|
const source = signalTransformSourceRef.current;
|
|
const layer = signalTransformLayerRef.current;
|
|
if (!source || !layer) return;
|
|
source.clear();
|
|
layer.setVisible(Boolean(editMode && signalSelectionUid));
|
|
if (!editMode || !signalSelectionUid) return;
|
|
const signal = state.trafficSignals.assemblies.features.find(
|
|
(feature) => feature.properties?.signal_uid === signalSelectionUid,
|
|
);
|
|
const pivot =
|
|
signal?.geometry?.type === 'Point' && Array.isArray(signal.geometry.coordinates)
|
|
? (signal.geometry.coordinates as Coordinate)
|
|
: null;
|
|
const mastHeading = Number(signal?.properties?.mast_heading_deg);
|
|
const reach = Number(signal?.properties?.mast_reach_m);
|
|
if (!pivot || !Number.isFinite(mastHeading) || !Number.isFinite(reach)) return;
|
|
const mastEnd = offsetCoordinate(pivot, mastHeading, reach);
|
|
const rotateHandle = offsetCoordinate(pivot, mastHeading, reach + 1.2);
|
|
const crosshairRadius = 0.45;
|
|
source.addFeatures([
|
|
new Feature({
|
|
geometry: new LineString([
|
|
fromLonLat(offsetCoordinate(pivot, 270, crosshairRadius)),
|
|
fromLonLat(offsetCoordinate(pivot, 90, crosshairRadius)),
|
|
]),
|
|
signal_transform: 'crosshair',
|
|
}),
|
|
new Feature({
|
|
geometry: new LineString([
|
|
fromLonLat(offsetCoordinate(pivot, 180, crosshairRadius)),
|
|
fromLonLat(offsetCoordinate(pivot, 0, crosshairRadius)),
|
|
]),
|
|
signal_transform: 'crosshair',
|
|
}),
|
|
new Feature({
|
|
geometry: new LineString([fromLonLat(mastEnd), fromLonLat(rotateHandle)]),
|
|
signal_transform: 'rotate-leader',
|
|
}),
|
|
new Feature({
|
|
geometry: new Point(fromLonLat(rotateHandle)),
|
|
signal_transform: 'rotate-handle',
|
|
signal_uid: signalSelectionUid,
|
|
signal_pivot: pivot,
|
|
}),
|
|
]);
|
|
}, [editMode, signalSelectionUid, state.trafficSignals.assemblies.features]);
|
|
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.
|
|
if (!ghostPending) ghostRef.current?.clear();
|
|
}, [ghostPending]);
|
|
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;
|
|
Object.entries(visible).forEach(([name, enabled]) =>
|
|
layers[name as LayerName]?.setVisible(
|
|
!scene || !['osm', 'lanes', 'connectors'].includes(name) ? enabled !== false : false,
|
|
),
|
|
);
|
|
layers.native.changed();
|
|
layers.sidewalks.changed();
|
|
}, [visible, scene]);
|
|
return <div className="map" ref={target} aria-label="道路地图" />;
|
|
}
|