feat(workbench): add junction tools workspace shell

This commit is contained in:
que01 committed 2026-09-03 11:20:37 +08:00
1 parent 96833d3a63
commit 2504e8342f
6 files changed
+277

No files matched your search

+65
View File
@@ -54,6 +54,15 @@ import { Button } from './ui/button';
import type { GeoFeature, GeoJson, Road, WorkbenchState } from './types/state';
import type { LayerName } from './map/layers';
import type { JunctionRef } from './junction/hotspots';
import { junctionDescriptors, hotspotAt } from './junction/hotspots';
import { JunctionWorkspace } from './junction/JunctionWorkspace';
import {
applyJunctionDraft,
cancelJunctionSession,
createJunctionSession,
saveJunctionSession,
type JunctionSession,
} from './junction/workspace';
const layerLabels: Array<[LayerName, string]> = [
['osm', '方向道路参考线'],
@@ -84,6 +93,12 @@ function App() {
const [status, setStatus] = useState('正在加载工作台...');
const [filter, setFilter] = useState<'all' | 'candidates' | 'other'>('all');
const [marking, setMarking] = useState<Record<string, unknown> | null>(null);
const [junctionRoute, setJunctionRoute] = useState<JunctionRef | null>(() => {
if (typeof window === 'undefined') return null;
const match = window.location.pathname.match(/^\/junction\/(node|cluster)\/([^/]+)$/);
return match ? { type: match[1] as JunctionRef['type'], id: decodeURIComponent(match[2]) } : null;
});
const [junctionSession, setJunctionSession] = useState<JunctionSession | null>(null);
const editHistoryRef = useRef<EditHistory>({ undo: [], redo: [] });
const recordSignalEdit = useMemo(
() => () => {
@@ -122,6 +137,14 @@ function App() {
// Storage may be unavailable in private browsing; the switch still works for this page.
}
}, [editMode]);
useEffect(() => {
const onPopState = () => {
const match = window.location.pathname.match(/^\/junction\/(node|cluster)\/([^/]+)$/);
setJunctionRoute(match ? { type: match[1] as JunctionRef['type'], id: decodeURIComponent(match[2]) } : null);
};
window.addEventListener('popstate', onPopState);
return () => window.removeEventListener('popstate', onPopState);
}, []);
useEffect(() => {
api
.state()
@@ -151,6 +174,17 @@ function App() {
revision: editRevision,
touch,
} = directEdit;
const junctionDescriptor = useMemo(
() => (state && junctionRoute ? hotspotAt(junctionDescriptors(state), junctionRoute) : null),
[junctionRoute, state],
);
useEffect(() => {
if (!junctionRoute) {
setJunctionSession(null);
return;
}
setJunctionSession(createJunctionSession(junctionRoute, editRevision));
}, [editRevision, junctionRoute]);
const editHandles = useMemo(() => {
if (!editMode || !state) return [];
// Handle anchors carry a directional road id while reserves are keyed by
@@ -665,6 +699,19 @@ function App() {
(filter === 'all' ||
(filter === 'candidates' ? Boolean(item.manualCandidates?.length) : !item.manualCandidates?.length)),
);
const openJunction = (ref: JunctionRef) => {
window.history.pushState({}, '', `/junction/${ref.type}/${encodeURIComponent(ref.id)}`);
setJunctionRoute(ref);
};
const closeJunction = () => {
if (junctionSession?.dirty) {
const choice = window.confirm('路口有未应用草稿。确定放弃并返回主地图吗?');
if (!choice) return;
setJunctionSession(cancelJunctionSession(junctionSession));
}
window.history.pushState({}, '', '/');
setJunctionRoute(null);
};
const handleFeature = (properties: Record<string, unknown>) => {
setMarking(properties);
// A crosswalk is six features but one editable thing; a stop line is one.
@@ -807,6 +854,7 @@ function App() {
scene={scene}
junctionHotspots={junctionHotspots}
onJunctionHotspot={(ref: JunctionRef) => {
openJunction(ref);
setStatus(ref.type === 'node' ? `已定位路口节点 ${ref.id}(可编辑)` : `已定位路口 ${ref.id}(只读)`);
}}
onSelectRoad={setSelected}
@@ -918,6 +966,23 @@ function App() {
</WorkbenchInspector>
</aside>
</main>
{junctionRoute && junctionDescriptor && junctionSession ? (
<JunctionWorkspace
descriptor={junctionDescriptor}
session={junctionSession}
onClose={closeJunction}
onApply={() => setJunctionSession((current) => (current ? applyJunctionDraft(current) : current))}
onDiscard={() => setJunctionSession((current) => (current ? cancelJunctionSession(current) : current))}
onSave={() => {
try {
setJunctionSession((current) => (current ? saveJunctionSession(current, editRevision) : current));
setStatus('路口草稿已保存');
} catch (error) {
setStatus((error as Error).message);
}
}}
/>
) : null}
</>
);
}