fix(workbench): preview lane arrow drag
This commit is contained in:
1 parent
fe73d993a9
commit
118dc8e9aa
2 files changed
+66
-4
No files matched your search
@@ -104,6 +104,10 @@ function App() {
|
||||
const [state, setState] = useState<WorkbenchState>();
|
||||
const [selected, setSelected] = useState<Road | null>(null);
|
||||
const [staged, setStaged] = useState<Override[]>([]);
|
||||
const stagedRef = useRef<Override[]>([]);
|
||||
const overrideUndoRef = useRef<Override[][]>([]);
|
||||
const overrideRedoRef = useRef<Override[][]>([]);
|
||||
stagedRef.current = staged;
|
||||
const [layers, setLayers] = useState(defaultLayers);
|
||||
const [scene, setScene] = useState(false);
|
||||
const [status, setStatus] = useState('正在加载工作台...');
|
||||
@@ -216,6 +220,11 @@ function App() {
|
||||
const laneSelectionFeatures = useMemo(() => {
|
||||
if (!element || element.kind !== 'lane-maneuver' || !state) return [];
|
||||
const ids = new Set(element.featureIds);
|
||||
const previewFeatures = preview?.turnArrows?.features.filter((feature) => {
|
||||
const nativeId = feature.properties?.native_id;
|
||||
return typeof nativeId === 'string' && ids.has(nativeId);
|
||||
});
|
||||
if (previewFeatures?.length) return previewFeatures;
|
||||
const existing =
|
||||
state.layers.turnArrows?.features.filter((feature) => {
|
||||
const nativeId = feature.properties?.native_id;
|
||||
@@ -238,7 +247,7 @@ function App() {
|
||||
const distance = placementDistanceMeters ?? 6;
|
||||
if (!line || line.length < 2 || !maneuver) return [];
|
||||
return previewArrowFeatures(element.laneId, maneuver, line, distance);
|
||||
}, [element, staged, state]);
|
||||
}, [element, preview, staged, state]);
|
||||
const laneSelectionAxisAzimuth = useMemo(() => {
|
||||
if (!element || element.kind !== 'lane-maneuver' || !state) return null;
|
||||
const road = state.compiled.model.roads.find((item) => item.id === element.roadId);
|
||||
@@ -442,6 +451,14 @@ function App() {
|
||||
const undoEdit = () => {
|
||||
// A saved gesture cannot be un-applied — persisted history is append-only — so
|
||||
// it is reversed by an inverse operation, which needs its own identity.
|
||||
if (!session?.canUndo && overrideUndoRef.current.length) {
|
||||
const previous = overrideUndoRef.current.pop() || [];
|
||||
overrideRedoRef.current.push([...stagedRef.current]);
|
||||
setStaged(previous);
|
||||
previewOverrides([...state!.overrides.overrides, ...previous]);
|
||||
touch();
|
||||
return;
|
||||
}
|
||||
const reversed = session?.undo({
|
||||
operationId: `operation:undo:${Date.now()}`,
|
||||
createdAt: new Date().toISOString(),
|
||||
@@ -451,6 +468,14 @@ function App() {
|
||||
previewSession();
|
||||
};
|
||||
const redoEdit = () => {
|
||||
if (!session?.canRedo && overrideRedoRef.current.length) {
|
||||
const next = overrideRedoRef.current.pop() || [];
|
||||
overrideUndoRef.current.push([...stagedRef.current]);
|
||||
setStaged(next);
|
||||
previewOverrides([...state!.overrides.overrides, ...next]);
|
||||
touch();
|
||||
return;
|
||||
}
|
||||
if (!session?.redo()) return;
|
||||
touch();
|
||||
previewSession();
|
||||
@@ -462,6 +487,9 @@ function App() {
|
||||
overridePreviewRef.current = null;
|
||||
session.discard();
|
||||
setStaged([]);
|
||||
stagedRef.current = [];
|
||||
overrideUndoRef.current = [];
|
||||
overrideRedoRef.current = [];
|
||||
gesture.current = null;
|
||||
touch();
|
||||
setPreview(null);
|
||||
@@ -547,13 +575,19 @@ function App() {
|
||||
const stage = (change: Override) =>
|
||||
setStaged((current) => {
|
||||
const next = [...current.filter((item) => item.id !== change.id), change];
|
||||
if (change.kind === 'lane-maneuver' && state) previewOverrides([...state.overrides.overrides, ...next]);
|
||||
if (change.kind === 'lane-maneuver') {
|
||||
overrideUndoRef.current.push([...current.filter((item) => item.kind === 'lane-maneuver')]);
|
||||
overrideRedoRef.current = [];
|
||||
if (state) previewOverrides([...state.overrides.overrides, ...next]);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
const save = async () => {
|
||||
if (!state || !staged.length) return true;
|
||||
overridePreviewRef.current?.abort();
|
||||
overridePreviewRef.current = null;
|
||||
overrideUndoRef.current = [];
|
||||
overrideRedoRef.current = [];
|
||||
try {
|
||||
const overrides = [
|
||||
...state.overrides.overrides.filter((item) => !staged.some((change) => change.id === item.id)),
|
||||
@@ -659,10 +693,10 @@ function App() {
|
||||
<Button onClick={() => void saveDirectEdits()} disabled={!session?.dirty}>
|
||||
<Save size={15} /> 保存直接编辑
|
||||
</Button>
|
||||
<Button onClick={undoEdit} disabled={!session?.canUndo}>
|
||||
<Button onClick={undoEdit} disabled={!session?.canUndo && !overrideUndoRef.current.length}>
|
||||
撤销
|
||||
</Button>
|
||||
<Button onClick={redoEdit} disabled={!session?.canRedo}>
|
||||
<Button onClick={redoEdit} disabled={!session?.canRedo && !overrideRedoRef.current.length}>
|
||||
重做
|
||||
</Button>
|
||||
<Button onClick={discardEdits} disabled={!session?.dirty && !staged.length}>
|
||||
@@ -745,6 +779,28 @@ function App() {
|
||||
laneSelectionHiddenIds={element?.kind === 'lane-maneuver' ? element.featureIds : []}
|
||||
laneSelectionAnchor={laneSelectionAnchor}
|
||||
laneSelectionAxisAzimuth={laneSelectionAxisAzimuth}
|
||||
onLaneSelectionDrag={(from, to) => {
|
||||
if (!element || element.kind !== 'lane-maneuver') return;
|
||||
const lane = state.layers.laneCenterlines?.features.find(
|
||||
(feature) => feature.properties?.native_id === element.laneId,
|
||||
);
|
||||
const line = lane?.geometry?.type === 'LineString' ? (lane.geometry.coordinates as Coordinate[]) : null;
|
||||
if (!line || line.length < 2) return;
|
||||
const along = metersAlongPolyline(line, toLonLat(to));
|
||||
const length = polylineLengthMeters(line);
|
||||
if (along === null || !(length > 0)) return;
|
||||
const existing = [...state.overrides.overrides, ...staged]
|
||||
.reverse()
|
||||
.find((item) => item.kind === 'lane-maneuver' && item.laneId === element.laneId);
|
||||
const next: Override = {
|
||||
id: existing?.id || `车道转向:${element.laneId}`,
|
||||
kind: 'lane-maneuver',
|
||||
laneId: element.laneId,
|
||||
maneuver: typeof existing?.maneuver === 'string' ? existing.maneuver : element.maneuver,
|
||||
placementDistanceMeters: Math.max(0, length - along),
|
||||
};
|
||||
previewOverrides([...state.overrides.overrides, ...staged.filter((item) => item.id !== next.id), next]);
|
||||
}}
|
||||
onLaneSelectionDragEnd={(from, to) => {
|
||||
if (!element || element.kind !== 'lane-maneuver') return;
|
||||
const lane = state.layers.laneCenterlines?.features.find(
|
||||
|
||||
@@ -46,6 +46,7 @@ interface Props {
|
||||
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;
|
||||
/** Authoritative preview geometry; null returns the map to the baseline. */
|
||||
preview?: Partial<Record<PreviewLayerName, GeoJson | null>> | null;
|
||||
@@ -76,6 +77,7 @@ export function MapCanvas({
|
||||
laneSelectionHiddenIds,
|
||||
laneSelectionAnchor,
|
||||
laneSelectionAxisAzimuth,
|
||||
onLaneSelectionDrag,
|
||||
onLaneSelectionDragEnd,
|
||||
preview,
|
||||
onHandleDrag,
|
||||
@@ -111,6 +113,7 @@ export function MapCanvas({
|
||||
onRangeDrag,
|
||||
onRangeEnd,
|
||||
onLaneSelectionDragEnd,
|
||||
onLaneSelectionDrag,
|
||||
laneSelectionAxisAzimuth,
|
||||
ghostPending,
|
||||
});
|
||||
@@ -121,6 +124,7 @@ export function MapCanvas({
|
||||
onRangeDrag,
|
||||
onRangeEnd,
|
||||
onLaneSelectionDragEnd,
|
||||
onLaneSelectionDrag,
|
||||
laneSelectionAxisAzimuth,
|
||||
ghostPending,
|
||||
};
|
||||
@@ -228,6 +232,8 @@ export function MapCanvas({
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user