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 [state, setState] = useState<WorkbenchState>();
|
||||||
const [selected, setSelected] = useState<Road | null>(null);
|
const [selected, setSelected] = useState<Road | null>(null);
|
||||||
const [staged, setStaged] = useState<Override[]>([]);
|
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 [layers, setLayers] = useState(defaultLayers);
|
||||||
const [scene, setScene] = useState(false);
|
const [scene, setScene] = useState(false);
|
||||||
const [status, setStatus] = useState('正在加载工作台...');
|
const [status, setStatus] = useState('正在加载工作台...');
|
||||||
@@ -216,6 +220,11 @@ function App() {
|
|||||||
const laneSelectionFeatures = useMemo(() => {
|
const laneSelectionFeatures = useMemo(() => {
|
||||||
if (!element || element.kind !== 'lane-maneuver' || !state) return [];
|
if (!element || element.kind !== 'lane-maneuver' || !state) return [];
|
||||||
const ids = new Set(element.featureIds);
|
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 =
|
const existing =
|
||||||
state.layers.turnArrows?.features.filter((feature) => {
|
state.layers.turnArrows?.features.filter((feature) => {
|
||||||
const nativeId = feature.properties?.native_id;
|
const nativeId = feature.properties?.native_id;
|
||||||
@@ -238,7 +247,7 @@ function App() {
|
|||||||
const distance = placementDistanceMeters ?? 6;
|
const distance = placementDistanceMeters ?? 6;
|
||||||
if (!line || line.length < 2 || !maneuver) return [];
|
if (!line || line.length < 2 || !maneuver) return [];
|
||||||
return previewArrowFeatures(element.laneId, maneuver, line, distance);
|
return previewArrowFeatures(element.laneId, maneuver, line, distance);
|
||||||
}, [element, staged, state]);
|
}, [element, preview, staged, state]);
|
||||||
const laneSelectionAxisAzimuth = useMemo(() => {
|
const laneSelectionAxisAzimuth = useMemo(() => {
|
||||||
if (!element || element.kind !== 'lane-maneuver' || !state) return null;
|
if (!element || element.kind !== 'lane-maneuver' || !state) return null;
|
||||||
const road = state.compiled.model.roads.find((item) => item.id === element.roadId);
|
const road = state.compiled.model.roads.find((item) => item.id === element.roadId);
|
||||||
@@ -442,6 +451,14 @@ function App() {
|
|||||||
const undoEdit = () => {
|
const undoEdit = () => {
|
||||||
// A saved gesture cannot be un-applied — persisted history is append-only — so
|
// 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.
|
// 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({
|
const reversed = session?.undo({
|
||||||
operationId: `operation:undo:${Date.now()}`,
|
operationId: `operation:undo:${Date.now()}`,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
@@ -451,6 +468,14 @@ function App() {
|
|||||||
previewSession();
|
previewSession();
|
||||||
};
|
};
|
||||||
const redoEdit = () => {
|
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;
|
if (!session?.redo()) return;
|
||||||
touch();
|
touch();
|
||||||
previewSession();
|
previewSession();
|
||||||
@@ -462,6 +487,9 @@ function App() {
|
|||||||
overridePreviewRef.current = null;
|
overridePreviewRef.current = null;
|
||||||
session.discard();
|
session.discard();
|
||||||
setStaged([]);
|
setStaged([]);
|
||||||
|
stagedRef.current = [];
|
||||||
|
overrideUndoRef.current = [];
|
||||||
|
overrideRedoRef.current = [];
|
||||||
gesture.current = null;
|
gesture.current = null;
|
||||||
touch();
|
touch();
|
||||||
setPreview(null);
|
setPreview(null);
|
||||||
@@ -547,13 +575,19 @@ function App() {
|
|||||||
const stage = (change: Override) =>
|
const stage = (change: Override) =>
|
||||||
setStaged((current) => {
|
setStaged((current) => {
|
||||||
const next = [...current.filter((item) => item.id !== change.id), change];
|
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;
|
return next;
|
||||||
});
|
});
|
||||||
const save = async () => {
|
const save = async () => {
|
||||||
if (!state || !staged.length) return true;
|
if (!state || !staged.length) return true;
|
||||||
overridePreviewRef.current?.abort();
|
overridePreviewRef.current?.abort();
|
||||||
overridePreviewRef.current = null;
|
overridePreviewRef.current = null;
|
||||||
|
overrideUndoRef.current = [];
|
||||||
|
overrideRedoRef.current = [];
|
||||||
try {
|
try {
|
||||||
const overrides = [
|
const overrides = [
|
||||||
...state.overrides.overrides.filter((item) => !staged.some((change) => change.id === item.id)),
|
...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}>
|
<Button onClick={() => void saveDirectEdits()} disabled={!session?.dirty}>
|
||||||
<Save size={15} /> 保存直接编辑
|
<Save size={15} /> 保存直接编辑
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={undoEdit} disabled={!session?.canUndo}>
|
<Button onClick={undoEdit} disabled={!session?.canUndo && !overrideUndoRef.current.length}>
|
||||||
撤销
|
撤销
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={redoEdit} disabled={!session?.canRedo}>
|
<Button onClick={redoEdit} disabled={!session?.canRedo && !overrideRedoRef.current.length}>
|
||||||
重做
|
重做
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={discardEdits} disabled={!session?.dirty && !staged.length}>
|
<Button onClick={discardEdits} disabled={!session?.dirty && !staged.length}>
|
||||||
@@ -745,6 +779,28 @@ function App() {
|
|||||||
laneSelectionHiddenIds={element?.kind === 'lane-maneuver' ? element.featureIds : []}
|
laneSelectionHiddenIds={element?.kind === 'lane-maneuver' ? element.featureIds : []}
|
||||||
laneSelectionAnchor={laneSelectionAnchor}
|
laneSelectionAnchor={laneSelectionAnchor}
|
||||||
laneSelectionAxisAzimuth={laneSelectionAxisAzimuth}
|
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) => {
|
onLaneSelectionDragEnd={(from, to) => {
|
||||||
if (!element || element.kind !== 'lane-maneuver') return;
|
if (!element || element.kind !== 'lane-maneuver') return;
|
||||||
const lane = state.layers.laneCenterlines?.features.find(
|
const lane = state.layers.laneCenterlines?.features.find(
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ interface Props {
|
|||||||
laneSelectionAnchor?: Coordinate | null;
|
laneSelectionAnchor?: Coordinate | null;
|
||||||
laneSelectionAxisAzimuth?: number | null;
|
laneSelectionAxisAzimuth?: number | null;
|
||||||
/** Receives the axis-projected final map coordinate. */
|
/** Receives the axis-projected final map coordinate. */
|
||||||
|
onLaneSelectionDrag?: (from: Coordinate, to: Coordinate) => void;
|
||||||
onLaneSelectionDragEnd?: (from: Coordinate, to: Coordinate) => void;
|
onLaneSelectionDragEnd?: (from: Coordinate, to: Coordinate) => void;
|
||||||
/** Authoritative preview geometry; null returns the map to the baseline. */
|
/** Authoritative preview geometry; null returns the map to the baseline. */
|
||||||
preview?: Partial<Record<PreviewLayerName, GeoJson | null>> | null;
|
preview?: Partial<Record<PreviewLayerName, GeoJson | null>> | null;
|
||||||
@@ -76,6 +77,7 @@ export function MapCanvas({
|
|||||||
laneSelectionHiddenIds,
|
laneSelectionHiddenIds,
|
||||||
laneSelectionAnchor,
|
laneSelectionAnchor,
|
||||||
laneSelectionAxisAzimuth,
|
laneSelectionAxisAzimuth,
|
||||||
|
onLaneSelectionDrag,
|
||||||
onLaneSelectionDragEnd,
|
onLaneSelectionDragEnd,
|
||||||
preview,
|
preview,
|
||||||
onHandleDrag,
|
onHandleDrag,
|
||||||
@@ -111,6 +113,7 @@ export function MapCanvas({
|
|||||||
onRangeDrag,
|
onRangeDrag,
|
||||||
onRangeEnd,
|
onRangeEnd,
|
||||||
onLaneSelectionDragEnd,
|
onLaneSelectionDragEnd,
|
||||||
|
onLaneSelectionDrag,
|
||||||
laneSelectionAxisAzimuth,
|
laneSelectionAxisAzimuth,
|
||||||
ghostPending,
|
ghostPending,
|
||||||
});
|
});
|
||||||
@@ -121,6 +124,7 @@ export function MapCanvas({
|
|||||||
onRangeDrag,
|
onRangeDrag,
|
||||||
onRangeEnd,
|
onRangeEnd,
|
||||||
onLaneSelectionDragEnd,
|
onLaneSelectionDragEnd,
|
||||||
|
onLaneSelectionDrag,
|
||||||
laneSelectionAxisAzimuth,
|
laneSelectionAxisAzimuth,
|
||||||
ghostPending,
|
ghostPending,
|
||||||
};
|
};
|
||||||
@@ -228,6 +232,8 @@ export function MapCanvas({
|
|||||||
geometry?.translate(dx, dy);
|
geometry?.translate(dx, dy);
|
||||||
feature.setGeometry(geometry);
|
feature.setGeometry(geometry);
|
||||||
});
|
});
|
||||||
|
if (laneDragAnchor)
|
||||||
|
dragRef.current.onLaneSelectionDrag?.(laneDragOrigin, [laneDragAnchor[0] + dx, laneDragAnchor[1] + dy]);
|
||||||
},
|
},
|
||||||
handleUpEvent: (event) => {
|
handleUpEvent: (event) => {
|
||||||
const axis = dragRef.current.laneSelectionAxisAzimuth;
|
const axis = dragRef.current.laneSelectionAxisAzimuth;
|
||||||
|
|||||||
Reference in new issue
Block a user