refactor(workbench): split editor state and components
This commit is contained in:
1 parent
caa3cb843f
commit
834470cfa1
23 files changed
+1219
-878
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { api } from '../lib/api';
|
||||
import { EditSession } from '../edit/session';
|
||||
import type { EditDiagnostic, HandleManifest, PreviewLayerName } from '../edit/types';
|
||||
import type { GeoJson, WorkbenchState } from '../types/state';
|
||||
|
||||
/**
|
||||
* Owns the stable v2 session and the server state that surrounds it. Gesture
|
||||
* construction and map interaction stay with the composition root for now.
|
||||
*/
|
||||
export function useDirectEditSession({
|
||||
editMode,
|
||||
state,
|
||||
selectedSegmentId,
|
||||
setStatus,
|
||||
}: {
|
||||
editMode: boolean;
|
||||
state?: WorkbenchState;
|
||||
selectedSegmentId?: string;
|
||||
setStatus: (status: string) => void;
|
||||
}) {
|
||||
const [manifest, setManifest] = useState<HandleManifest | null>(null);
|
||||
const [preview, setPreview] = useState<Partial<Record<PreviewLayerName, GeoJson | null>> | null>(null);
|
||||
const [diagnostics, setDiagnostics] = useState<EditDiagnostic[]>([]);
|
||||
const [revision, setRevision] = useState(0);
|
||||
const sessionRef = useRef<EditSession | null>(null);
|
||||
const loadedDocument = useRef<string | null>(null);
|
||||
if (editMode && !sessionRef.current) sessionRef.current = new EditSession();
|
||||
const touch = () => setRevision((value) => value + 1);
|
||||
|
||||
useEffect(() => {
|
||||
// Flag off: no manifest request at all, so the network trace matches browse mode.
|
||||
if (!editMode) return;
|
||||
api
|
||||
.editState(selectedSegmentId ? { segment: selectedSegmentId } : undefined)
|
||||
.then((value) => {
|
||||
if (!('handles' in value)) return;
|
||||
setManifest(value.handles);
|
||||
const key = `${value.areaId}:${value.documentVersion}`;
|
||||
if (loadedDocument.current === key) return;
|
||||
loadedDocument.current = key;
|
||||
sessionRef.current?.load(value.document.constraints, value.document.operations, value.documentVersion);
|
||||
touch();
|
||||
})
|
||||
.catch((error: Error) => setStatus(`直接编辑手柄不可用:${error.message}`));
|
||||
}, [editMode, selectedSegmentId, setStatus, state]);
|
||||
|
||||
return {
|
||||
session: sessionRef.current,
|
||||
manifest,
|
||||
setManifest,
|
||||
preview,
|
||||
setPreview,
|
||||
diagnostics,
|
||||
setDiagnostics,
|
||||
revision,
|
||||
touch,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
import { api } from '../lib/api';
|
||||
import type { Override, WorkbenchState } from '../types/state';
|
||||
|
||||
export function useOverrideDraft({
|
||||
state,
|
||||
setState,
|
||||
setStatus,
|
||||
}: {
|
||||
state?: WorkbenchState;
|
||||
setState: (state: WorkbenchState) => void;
|
||||
setStatus: (status: string) => void;
|
||||
}) {
|
||||
const [staged, setStaged] = useState<Override[]>([]);
|
||||
const stage = useCallback((change: Override) => {
|
||||
setStaged((current) => [...current.filter((item) => item.id !== change.id), change]);
|
||||
}, []);
|
||||
const save = useCallback(async () => {
|
||||
if (!state || !staged.length) return true;
|
||||
try {
|
||||
const overrides = [
|
||||
...state.overrides.overrides.filter((item) => !staged.some((change) => change.id === item.id)),
|
||||
...staged,
|
||||
];
|
||||
const result = await api.overrides(overrides);
|
||||
setState({ ...state, overrides: result.overrides });
|
||||
setStaged([]);
|
||||
setStatus('已保存,点击“保存并重新生成”写入几何');
|
||||
return true;
|
||||
} catch (error) {
|
||||
setStatus((error as Error).message);
|
||||
return false;
|
||||
}
|
||||
}, [setState, setStatus, staged, state]);
|
||||
const clear = useCallback(() => setStaged([]), []);
|
||||
return { staged, stage, save, clear };
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
import { useCallback, useRef, useState, type Dispatch, type SetStateAction } from 'react';
|
||||
import { haversineMeters, type Coordinate } from '../edit/meters';
|
||||
import type { GeoFeature, WorkbenchState } from '../types/state';
|
||||
|
||||
const limitPole = (feature: GeoFeature | undefined, coordinate: Coordinate): Coordinate => {
|
||||
const stop: Coordinate | null = feature
|
||||
? [Number(feature.properties?.stop_lon), Number(feature.properties?.stop_lat)]
|
||||
: null;
|
||||
if (!stop || !stop.every(Number.isFinite)) return coordinate;
|
||||
const distance = haversineMeters(stop, coordinate);
|
||||
if (distance <= 40) return coordinate;
|
||||
const ratio = 40 / distance;
|
||||
return [stop[0] + (coordinate[0] - stop[0]) * ratio, stop[1] + (coordinate[1] - stop[1]) * ratio];
|
||||
};
|
||||
|
||||
const rotateFeatures = (features: GeoFeature[], uid: string, mastHeading: number): GeoFeature[] => {
|
||||
const heading = ((mastHeading % 360) + 360) % 360;
|
||||
return features.map((feature) =>
|
||||
feature.properties?.signal_uid === uid
|
||||
? {
|
||||
...feature,
|
||||
properties: {
|
||||
...feature.properties,
|
||||
mast_heading_deg: heading,
|
||||
face_heading_deg: (((heading - 90) % 360) + 360) % 360,
|
||||
},
|
||||
}
|
||||
: feature,
|
||||
);
|
||||
};
|
||||
|
||||
export function useTrafficSignalDraft({
|
||||
state,
|
||||
setState,
|
||||
}: {
|
||||
state?: WorkbenchState;
|
||||
setState: Dispatch<SetStateAction<WorkbenchState | undefined>>;
|
||||
}) {
|
||||
const [dirty, setDirty] = useState(false);
|
||||
const draftRef = useRef<GeoFeature[] | null>(null);
|
||||
const undoRef = useRef<GeoFeature[][]>([]);
|
||||
const redoRef = useRef<GeoFeature[][]>([]);
|
||||
const savedRef = useRef<GeoFeature[] | null>(null);
|
||||
const moveActiveRef = useRef(false);
|
||||
const rotateActiveRef = useRef(false);
|
||||
|
||||
const update = useCallback(
|
||||
async (features: GeoFeature[]) => {
|
||||
if (state) undoRef.current.push(state.trafficSignals.assemblies.features);
|
||||
redoRef.current = [];
|
||||
draftRef.current = features;
|
||||
setDirty(true);
|
||||
setState((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
trafficSignals: {
|
||||
...current.trafficSignals,
|
||||
assemblies: { ...current.trafficSignals.assemblies, features },
|
||||
},
|
||||
}
|
||||
: current,
|
||||
);
|
||||
},
|
||||
[setState, state],
|
||||
);
|
||||
|
||||
const move = useCallback(
|
||||
(uid: string, coordinate: Coordinate) => {
|
||||
setState((current) => {
|
||||
if (!current) return current;
|
||||
if (!moveActiveRef.current) {
|
||||
undoRef.current.push(current.trafficSignals.assemblies.features);
|
||||
redoRef.current = [];
|
||||
moveActiveRef.current = true;
|
||||
}
|
||||
const source = current.trafficSignals.assemblies.features.find(
|
||||
(feature) => feature.properties?.signal_uid === uid,
|
||||
);
|
||||
const nextCoordinate = limitPole(source, coordinate);
|
||||
return {
|
||||
...current,
|
||||
trafficSignals: {
|
||||
...current.trafficSignals,
|
||||
assemblies: {
|
||||
...current.trafficSignals.assemblies,
|
||||
features: current.trafficSignals.assemblies.features.map((feature) =>
|
||||
feature.properties?.signal_uid === uid
|
||||
? { ...feature, geometry: { type: 'Point', coordinates: nextCoordinate } }
|
||||
: feature,
|
||||
),
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
setDirty(true);
|
||||
},
|
||||
[setState],
|
||||
);
|
||||
|
||||
const finishMove = useCallback(
|
||||
(uid: string, coordinate: Coordinate) => {
|
||||
if (!state) return;
|
||||
const source = state.trafficSignals.assemblies.features.find((feature) => feature.properties?.signal_uid === uid);
|
||||
const nextCoordinate = limitPole(source, coordinate);
|
||||
draftRef.current = state.trafficSignals.assemblies.features.map((feature) =>
|
||||
feature.properties?.signal_uid === uid
|
||||
? { ...feature, geometry: { type: 'Point', coordinates: nextCoordinate } }
|
||||
: feature,
|
||||
);
|
||||
moveActiveRef.current = false;
|
||||
setDirty(true);
|
||||
},
|
||||
[state],
|
||||
);
|
||||
|
||||
const rotate = useCallback(
|
||||
(uid: string, mastHeading: number) => {
|
||||
setState((current) => {
|
||||
if (!current) return current;
|
||||
if (!rotateActiveRef.current) {
|
||||
undoRef.current.push(current.trafficSignals.assemblies.features);
|
||||
redoRef.current = [];
|
||||
rotateActiveRef.current = true;
|
||||
}
|
||||
return {
|
||||
...current,
|
||||
trafficSignals: {
|
||||
...current.trafficSignals,
|
||||
assemblies: {
|
||||
...current.trafficSignals.assemblies,
|
||||
features: rotateFeatures(current.trafficSignals.assemblies.features, uid, mastHeading),
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
setDirty(true);
|
||||
},
|
||||
[setState],
|
||||
);
|
||||
|
||||
const finishRotate = useCallback(
|
||||
(uid: string, mastHeading: number) => {
|
||||
if (!state) return;
|
||||
draftRef.current = rotateFeatures(state.trafficSignals.assemblies.features, uid, mastHeading);
|
||||
rotateActiveRef.current = false;
|
||||
setDirty(true);
|
||||
},
|
||||
[state],
|
||||
);
|
||||
|
||||
const reset = useCallback(() => {
|
||||
draftRef.current = null;
|
||||
undoRef.current = [];
|
||||
redoRef.current = [];
|
||||
moveActiveRef.current = false;
|
||||
rotateActiveRef.current = false;
|
||||
setDirty(false);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
dirty,
|
||||
setDirty,
|
||||
draftRef,
|
||||
undoRef,
|
||||
redoRef,
|
||||
savedRef,
|
||||
update,
|
||||
move,
|
||||
finishMove,
|
||||
rotate,
|
||||
finishRotate,
|
||||
reset,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user