import { useEffect, useMemo, useState } from 'react'; import { candidateForJunction, type JunctionDescriptor } from './hotspots'; import { canEditJunction, type JunctionSession } from './workspace'; import { Button } from '../ui/button'; import { ParameterPanel } from '../components/ParameterPanel'; import type { EditHandle } from '../edit/types'; import type { ElementSelection } from '../edit/element'; import type { Road } from '../types/state'; import type { WorkbenchState } from '../types/state'; import type { Coordinate } from '../edit/meters'; import { junctionLabelForHandle, junctionRoadName } from './labels'; import { junctionFacilities } from './facilities'; import { SignalEditor } from '../components/ElementEditors'; interface Props { descriptor: JunctionDescriptor; session: JunctionSession; dirty?: boolean; diagnostics?: Array<{ id: string; message: string; rule: string; severity?: string }>; selectedObjectKey?: string | null; onSelectObject?: (key: string) => void; facilitySummary?: { crosswalks: number; stopLines: number; signals: number }; state?: WorkbenchState; onClose: () => void; onApply: () => void; onDiscard: () => void; onSave: () => void; editMode?: boolean; onEnableEdit?: () => void; onEnableComplex?: (nodeIds: string[]) => void; onUpdateComplexNodes?: (nodeIds: string[]) => void; onCandidateMarkersChange?: (markers: Array<{ coordinate: Coordinate; label: string; selected: boolean }>) => void; handles?: EditHandle[]; onDraft?: (handle: EditHandle, value: number) => void; onDraftGroup?: (handles: EditHandle[], value: number) => void; onSettle?: (handle: EditHandle, value: number) => void; onSettleGroup?: (handles: EditHandle[], value: number) => void; onAbandon?: () => void; onReset?: (handle: EditHandle) => void; roads?: Road[]; junctionCoordinate?: Coordinate; selectedFacility?: ElementSelection | null; onSelectFacility?: (facility: { id: string; kind: 'crosswalk' | 'stop-line' | 'signal' }) => void; signalUids?: readonly string[]; selectedSignalUid?: string | null; onSignalStatus?: (message: string) => void; onUpdateSignal?: (features: import('../types/state').GeoFeature[]) => Promise; } export function JunctionWorkspace({ descriptor, session, dirty = session.dirty, onClose, onApply, onDiscard, onSave, editMode = false, onEnableEdit, onEnableComplex, onUpdateComplexNodes, onCandidateMarkersChange, diagnostics = [], selectedObjectKey = null, onSelectObject, facilitySummary = { crosswalks: 0, stopLines: 0, signals: 0 }, state, handles = [], onDraft, onDraftGroup, onSettle, onSettleGroup, onAbandon, onReset, roads = [], junctionCoordinate = descriptor.coordinate, selectedFacility, onSelectFacility, signalUids = [], selectedSignalUid, onSignalStatus, onUpdateSignal, }: Props) { const editable = useMemo(() => canEditJunction(descriptor), [descriptor]); const complexEnabled = useMemo(() => { const nodes = new Set(descriptor.nodeIds.map(String)); return Boolean( state?.debug?.junctionClusters?.some( (cluster) => (descriptor.ref.type === 'cluster' && cluster.id === descriptor.ref.id) || (cluster.nodeIds.length === nodes.size && cluster.nodeIds.every((nodeId) => nodes.has(String(nodeId)))), ), ); }, [descriptor, state?.debug?.junctionClusters]); const shapeSummary = useMemo( () => state?.debug?.junctionShapes?.find((shape) => shape.clusterId === descriptor.ref.id), [descriptor.ref.id, state?.debug?.junctionShapes], ); const candidate = useMemo(() => { return candidateForJunction(state, descriptor); }, [descriptor, state]); const configuredNodeIds = useMemo( () => state?.debug?.junctionClusters?.find((cluster) => cluster.id === descriptor.ref.id)?.nodeIds.map(String) || [], [descriptor.ref.id, state?.debug?.junctionClusters], ); const [selectedCandidateNodes, setSelectedCandidateNodes] = useState([]); const [candidateNodePickerOpen, setCandidateNodePickerOpen] = useState(false); const candidateNodes = useMemo( () => (candidate?.nodeIds?.length ? candidate.nodeIds : descriptor.nodeIds.length ? descriptor.nodeIds : configuredNodeIds ).map(String), [candidate?.nodeIds, configuredNodeIds, descriptor.nodeIds], ); const activeCandidateNodes = selectedCandidateNodes.filter((nodeId) => candidateNodes.includes(nodeId)).length ? selectedCandidateNodes.filter((nodeId) => candidateNodes.includes(nodeId)) : candidateNodes; const candidateRoadNames = useMemo(() => { if (!state) return []; const nodeIds = new Set((candidate?.nodeIds || descriptor.nodeIds).map(String)); return [ ...new Set( state.compiled.model.roads .filter((road) => road.sourceNodeIds.some((nodeId) => nodeIds.has(String(nodeId)))) .map((road) => junctionRoadName(road, state.compiled.model.roads)), ), ]; }, [candidate, descriptor.nodeIds, state]); const candidateNodeLabels = useMemo(() => { if (!state) return []; return candidateNodes.map((nodeId, index) => { const names = [ ...new Set( state.compiled.model.roads .filter((road) => road.sourceNodeIds.some((item) => String(item) === nodeId)) .map((road) => junctionRoadName(road, state.compiled.model.roads)), ), ]; return { nodeId, label: `路口点 ${index + 1}${names.length ? ` · ${names.join('、')}` : ''}` }; }); }, [candidate, candidateNodes, state]); useEffect(() => { if (!onCandidateMarkersChange || !state || !candidateNodePickerOpen) { onCandidateMarkersChange?.([]); return; } const selectedIds = new Set(activeCandidateNodes); onCandidateMarkersChange( candidateNodes.flatMap((nodeId, index) => { const points = state.compiled.model.endpoints .filter((endpoint) => String(endpoint.nodeId) === nodeId) .map((endpoint) => endpoint.coordinate); return points.length ? [ { coordinate: [ points.reduce((sum, point) => sum + point[0], 0) / points.length, points.reduce((sum, point) => sum + point[1], 0) / points.length, ] as Coordinate, label: String(index + 1), selected: selectedIds.has(nodeId), }, ] : []; }), ); return () => onCandidateMarkersChange([]); }, [activeCandidateNodes, candidateNodePickerOpen, candidateNodes, onCandidateMarkersChange, state]); const facilities = useMemo( () => (state ? junctionFacilities(state, descriptor.nodeIds, roads) : []), [descriptor.nodeIds, roads, state], ); const structure = state?.junctionStructure; const standardDiagnostics = state?.standardsDiagnostics || []; return (
JunctionTools

{complexEnabled ? '当前按复杂路口渲染' : descriptor.status === 'candidate' ? '当前按普通路口渲染,可切换为复杂路口' : editable ? editMode ? '可编辑路口几何参数' : '当前为浏览模式,开启编辑后可调整路口几何参数' : '只读上下文:当前路口类型暂不支持编辑'}

{editable && !editMode ? : null}
{shapeSummary ? ( <>
路口形态
{shapeSummary.label} · {shapeSummary.armCount} 条有效道路臂
) : null}
组成节点
{descriptor.nodeIds.length ? `${descriptor.nodeIds.length} 个路口节点` : '—'}
进口道路
{roads.length ? roads.map((road) => junctionRoadName(road, roads)).join('、') : '—'}
诊断
{descriptor.diagnosticIds.length ? `${descriptor.diagnosticIds.length} 条` : '无'}
状态
{session.status} {dirty ? '(有未应用草稿)' : ''}
{structure ? (

结构与标准

{state?.standardsProfile?.label || '项目默认标准'} · {structure.shape.label} ·{' '} {structure.shape.effectiveArmCount} 条有效道路臂

{state?.junctionReference ? (

参考层:{state.junctionReference.coordinateSystem || 'GCJ-02'} → WGS84 ·{' '} {state.junctionReference.source || '已加载参考文件'}

) : null}
道路臂
{structure.arms.length ? ( {structure.arms.map((arm) => ( ))} ) : ( '无' )}
角部
{structure.corners.length ? structure.corners.map((corner) => corner.label).join('、') : '无'}
设施
{structure.facilities.length ? structure.facilities.map((facility) => facility.label).join('、') : '无'}
{standardDiagnostics.length ? (
    {standardDiagnostics.map((item) => (
  • ))}
) : null}
) : null} {candidate || complexEnabled ? (
{complexEnabled ? '当前复杂路口组成' : '附近可合并节点'} {candidate ? ( 已根据当前路口附近路网动态检测到 {candidate.nodeCount} 个相邻节点,范围约 {candidate.diameterMeters}{' '} 米。 {candidateRoadNames.length ? ` 关联道路:${candidateRoadNames.join('、')}。` : ''} 建议核心半径{' '} {candidate.coreRadiusMeters} 米。 ) : ( 当前复杂路口包含 {candidateNodes.length} 个路口点。 )} {candidateNodes.length > 2 ? ( <> {candidateNodePickerOpen ? (
参与合并的路口点 {candidateNodeLabels.map(({ nodeId, label }, index) => ( ))}
) : null} ) : null} {!complexEnabled && onEnableComplex ? ( ) : null} {complexEnabled && onUpdateComplexNodes ? ( ) : null}
) : null}

设施与验证

斑马线 {facilitySummary.crosswalks} · 停止线 {facilitySummary.stopLines} · 信号灯 {facilitySummary.signals}

{facilities.length ? (
    {facilities.map((facility) => (
  • ))}
) : null} {selectedFacility && selectedFacility.kind !== 'lane-maneuver' ? (

当前选中:{selectedFacility.kind === 'crosswalk' ? '斑马线' : '停止线'}。可在下方参数中调整位置。

) : null} {selectedSignalUid ? (

当前选中:信号灯。可在下方参数中调整。

) : null} {diagnostics.length ? (
    {diagnostics.map((item) => (
  • {item.severity === 'error' ? '阻塞' : '提示'} {item.message}
  • ))}
) : (

当前路口没有诊断问题。

)}
{editable && editMode ? ( <> undefined)} onDraftGroup={onDraftGroup} onSettle={onSettle ?? (() => undefined)} onSettleGroup={onSettleGroup} onAbandon={onAbandon ?? (() => undefined)} onReset={onReset ?? (() => undefined)} labelForHandle={(handle) => junctionLabelForHandle(handle, roads, junctionCoordinate)} highlightedHandleId={ selectedFacility && selectedFacility.kind !== 'lane-maneuver' ? handles.find( (handle) => handle.anchor.type === 'junction-crossing' && String(handle.anchor.crossingId) === String(selectedFacility.crossingNodeId) && handle.kind === (selectedFacility.kind === 'crosswalk' ? 'junction-crosswalk-inset' : 'junction-stop-line-offset'), )?.handleId : undefined } /> {onUpdateSignal ? ( String(item.properties?.signal_uid) === selectedSignalUid, ) : undefined } signalUids={signalUids} allowDelete={false} status={onSignalStatus ?? (() => undefined)} update={onUpdateSignal} /> ) : null}
) : editable ? (

开启编辑模式后,这里会显示进口宽度、进口收口距离和角部圆角控件。

) : complexEnabled ? (

复杂路口几何已启用;首版仅支持查看,不支持参数拖拽编辑。

) : null}
); }