refactor(workbench): split editor state and components

This commit is contained in:
que01 committed 2026-09-02 15:04:58 +08:00
1 parent caa3cb843f
commit 834470cfa1
23 files changed
+1219 -878

No files matched your search

@@ -0,0 +1,273 @@
import { useEffect, useState, type ReactNode } from 'react';
import { Candidates, LaneManeuverEditor, MarkingEditor, SignalEditor } from './ElementEditors';
import { InspectorSection } from './InspectorSection';
import type { EditSession } from '../edit/session';
import type { ElementSelection } from '../edit/element';
import type { Override, GeoFeature, Road, WorkbenchState } from '../types/state';
import { Button } from '../ui/button';
const laneIndex = (id: string) => Number(id.split(':').at(-1));
const direction = (road: Road) => (road.direction === 'forward' ? '沿 OSM 方向' : '逆 OSM 方向');
const label = (road: Road) => road.tags.name || `${road.highway}(OSM ${road.osmWayIds.join(', ')})`;
export function WorkbenchInspector({
road,
state,
staged,
marking,
element,
stage,
status,
session,
onLaneManeuverChange,
updateSignal,
onAcceptCandidate,
children,
}: {
road: Road | null;
state: WorkbenchState;
staged: Override[];
marking: Record<string, unknown> | null;
element: ElementSelection | null;
stage: (item: Override) => void;
status: (value: string) => void;
session: EditSession | null;
onLaneManeuverChange: (maneuver: string, placementDistanceMeters: number) => void;
updateSignal: (features: GeoFeature[]) => Promise<void>;
onAcceptCandidate: (index: number) => void;
children?: ReactNode;
}) {
const [width, setWidth] = useState(0);
const [lanes, setLanes] = useState(1);
const [left, setLeft] = useState(false);
const [right, setRight] = useState(false);
useEffect(() => {
if (road) {
setWidth(road.widthMeters);
setLanes(road.laneCount);
setLeft(road.sidewalkLeft);
setRight(road.sidewalkRight);
}
}, [road]);
const endpoint = state.compiled.model.endpoints.find((item) => item.roadId === road?.id && item.side === 'end');
const movements = state.compiled.movements?.filter((item) => item.fromRoadId === road?.id) || [];
const effective = (id: string, fallback: boolean) =>
((staged.find((item) => item.id === id) || state.overrides.overrides.find((item) => item.id === id))?.enabled as
boolean | undefined) ?? fallback;
const signalUid = typeof marking?.signal_uid === 'string' ? marking.signal_uid : '';
const signal = state.trafficSignals.assemblies.features.find((item) => item.properties?.signal_uid === signalUid);
const [open, setOpen] = useState<'road' | 'element' | 'connections' | 'advanced' | null>('road');
useEffect(() => {
if (element || marking) setOpen('element');
else if (road) setOpen('road');
}, [element, marking, road]);
const section = (name: NonNullable<typeof open>) => () => setOpen((current) => (current === name ? null : name));
const selectedLabel =
element?.kind === 'lane-maneuver'
? `第 ${laneIndex(element.laneId)} 车道`
: marking
? '已选标线或设施'
: road
? label(road)
: '未选择对象';
return (
<section className="workbench-inspector">
<div className="inspector-summary">
<span>编辑工作台</span>
<strong>{selectedLabel}</strong>
<small>{staged.length ? `已暂存 ${staged.length} 项修改` : '没有待保存的覆盖修改'}</small>
</div>
<InspectorSection
title="道路"
summary={road ? `${road.laneCount} 车道` : '选择道路'}
open={open === 'road'}
onToggle={section('road')}
>
{road ? (
<form
onSubmit={(event) => {
event.preventDefault();
stage({
id: `道路:${road.id}`,
kind: 'road',
roadId: road.id,
widthMeters: width,
laneCount: lanes,
sidewalkLeft: left,
sidewalkRight: right,
});
const opposite = state.compiled.model.roads.find(
(item) => item.id !== road.id && item.segmentId === road.segmentId,
);
if (opposite)
stage({
id: `道路:${opposite.id}`,
kind: 'road',
roadId: opposite.id,
sidewalkLeft: right,
sidewalkRight: left,
});
status('有未保存修改');
}}
>
<label>道路</label>
<output>
{label(road)}({direction(road)})
</output>
<output>
{movements.length
? `已识别 ${movements.length} 个行驶动作,${movements.filter((item) => item.geometryPublished).length} 条已绘制路径`
: '当前方向没有已识别的行驶动作'}
</output>
<label>
本方向道路宽度(米)
<input
type="number"
min="1"
step="0.01"
value={width}
onChange={(event) => setWidth(Number(event.target.value))}
/>
</label>
<label>
本方向车道数
<input
type="number"
min="1"
step="1"
value={lanes}
onChange={(event) => setLanes(Number(event.target.value))}
/>
</label>
<label>
<input type="checkbox" checked={left} onChange={(event) => setLeft(event.target.checked)} />{' '}
左侧有路缘与步行带
</label>
<label>
<input type="checkbox" checked={right} onChange={(event) => setRight(event.target.checked)} />{' '}
右侧有路缘与步行带
</label>
<Button type="submit">暂存本道路修改</Button>
</form>
) : (
<p>点击道路、车道、标线、转向路径或路口面以查看详情。</p>
)}
{children}
</InspectorSection>
<InspectorSection
title="元素"
summary={element ? selectedLabel : '箭头、标线与信号'}
open={open === 'element'}
onToggle={section('element')}
>
<LaneManeuverEditor
element={element}
value={
element?.kind === 'lane-maneuver'
? (session
?.constraints()
.find(
(item) =>
item.kind === 'lane-maneuver' &&
item.anchor.type === 'lane' &&
item.anchor.laneId === element.laneId,
)?.value as { maneuver?: string; placementDistanceMeters?: number } | undefined)
: undefined
}
onChange={onLaneManeuverChange}
/>
<MarkingEditor marking={marking} stage={stage} />
<SignalEditor state={state} signal={signal} status={status} update={updateSignal} />
</InspectorSection>
<InspectorSection
title="连接"
summary={road ? `${movements.length} 个行驶动作` : '选择道路'}
open={open === 'connections'}
onToggle={section('connections')}
>
{road ? (
<div className="connections-list">
{state.compiled.model.connections
.filter((connection) => connection.fromEndpointId === endpoint?.id)
.map((connection) => (
<label key={connection.id}>
<input
type="checkbox"
checked={effective(`连接:${connection.id}`, connection.enabled)}
onChange={(event) =>
stage({
id: `连接:${connection.id}`,
kind: 'junction-connection',
fromEndpointId: connection.fromEndpointId,
toEndpointId: connection.toEndpointId,
enabled: event.target.checked,
})
}
/>{' '}
{connection.id}
</label>
))}
{movements.map((movement) => (
<label key={movement.id}>
<input
type="checkbox"
checked={effective(`车道连接:${movement.fromLaneId}->${movement.toLaneId}`, true)}
onChange={(event) =>
stage({
id: `车道连接:${movement.fromLaneId}->${movement.toLaneId}`,
kind: 'lane-connection',
fromLaneId: movement.fromLaneId,
toLaneId: movement.toLaneId,
enabled: event.target.checked,
})
}
/>{' '}
{movement.turn}:第 {laneIndex(movement.fromLaneId)} 车道 → 第 {laneIndex(movement.toLaneId)} 车道(
{movement.geometryStatus || 'connector'})
</label>
))}
</div>
) : (
<p className="form-context">选择一条道路以管理其驶入路口的连接。</p>
)}
{endpoint &&
state.compiled.diagnostics
.find((item) => item.endpointId === endpoint.id)
?.manualCandidates?.map((candidate) => {
const target = state.compiled.model.roads.find((item) => item.id === candidate.roadId);
const targetEndpoint = state.compiled.model.endpoints.find(
(item) => item.roadId === candidate.roadId && item.side === 'start',
);
return target && targetEndpoint ? (
<Button
key={candidate.roadId}
onClick={() => {
stage({
id: `连接:connection:${endpoint.id}:${targetEndpoint.id}`,
kind: 'junction-connection',
fromEndpointId: endpoint.id,
toEndpointId: targetEndpoint.id,
enabled: true,
});
status('手工连接已暂存;保存并重新生成后会出现转向路径');
}}
>
新增:{label(target)}({candidate.distanceMeters} 米)
</Button>
) : null;
})}
</InspectorSection>
<InspectorSection title="高级" summary="候选与来源" open={open === 'advanced'} onToggle={section('advanced')}>
<Candidates state={state} onAccept={onAcceptCandidate} />
<details>
<summary>技术详情与来源</summary>
<pre>
{JSON.stringify(
road ? { OSM道路: road.osmWayIds, 当前方向节点顺序: road.sourceNodeIds, 原始标签: road.tags } : marking,
null,
2,
)}
</pre>
</details>
</InspectorSection>
</section>
);
}