diff --git a/.trellis/tasks/09-02-junction-tools-workspace/implement.md b/.trellis/tasks/09-02-junction-tools-workspace/implement.md new file mode 100644 index 0000000..73bb96c --- /dev/null +++ b/.trellis/tasks/09-02-junction-tools-workspace/implement.md @@ -0,0 +1,15 @@ +# Implementation + +依次实现入口/上下文、session 四态、互斥守卫、返回恢复;用状态机单测和最小浏览器流程验证后再接几何编辑。 + +## Progress + +- [x] 独立 `/junction/:type/:id` 路由与浏览器返回监听 +- [x] 路口上下文摘要和只读/可编辑状态 +- [x] draft / applied / saved / cancelled 状态机 +- [x] 未处理草稿离开守卫与版本冲突检查 +- [x] 客户端类型检查、单元测试、格式检查和生产构建 + +## Deferred + +- 几何约束控件和真实 preview/apply/save 数据流由 geometry-editor 子任务接入。 diff --git a/workbench/client/src/App.tsx b/workbench/client/src/App.tsx index e538c7f..887c057 100644 --- a/workbench/client/src/App.tsx +++ b/workbench/client/src/App.tsx @@ -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 | null>(null); + const [junctionRoute, setJunctionRoute] = useState(() => { + 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(null); const editHistoryRef = useRef({ 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) => { 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() { + {junctionRoute && junctionDescriptor && junctionSession ? ( + 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} ); } diff --git a/workbench/client/src/junction/JunctionWorkspace.tsx b/workbench/client/src/junction/JunctionWorkspace.tsx new file mode 100644 index 0000000..d288cd5 --- /dev/null +++ b/workbench/client/src/junction/JunctionWorkspace.tsx @@ -0,0 +1,59 @@ +import { useMemo } from 'react'; +import type { JunctionDescriptor } from './hotspots'; +import { canEditJunction, type JunctionSession } from './workspace'; +import { Button } from '../ui/button'; + +interface Props { + descriptor: JunctionDescriptor; + session: JunctionSession; + onClose: () => void; + onApply: () => void; + onDiscard: () => void; + onSave: () => void; +} + +export function JunctionWorkspace({ descriptor, session, onClose, onApply, onDiscard, onSave }: Props) { + const editable = useMemo(() => canEditJunction(descriptor), [descriptor]); + return ( +
+
+
+ JunctionTools +

+ {descriptor.ref.type === 'node' ? '路口节点' : '复杂路口上下文'} {descriptor.ref.id} +

+

{editable ? '可编辑路口几何参数' : '只读上下文:当前路口类型暂不支持编辑'}

+
+ +
+
+
+
组成节点
+
{descriptor.nodeIds.join(', ') || '—'}
+
进口道路
+
{descriptor.approachRoadIds.join(', ') || '—'}
+
诊断
+
{descriptor.diagnosticIds.length ? `${descriptor.diagnosticIds.length} 条` : '无'}
+
状态
+
+ {session.status} + {session.dirty ? '(有未应用草稿)' : ''} +
+
+ {editable ? ( +
+ + + +
+ ) : null} +
+
+ ); +} diff --git a/workbench/client/src/junction/workspace.test.ts b/workbench/client/src/junction/workspace.test.ts new file mode 100644 index 0000000..7bcfea0 --- /dev/null +++ b/workbench/client/src/junction/workspace.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest'; +import { + applyJunctionDraft, + canEditJunction, + createJunctionSession, + leaveJunctionSession, + markJunctionDraft, + saveJunctionSession, +} from './workspace'; + +const node = { ref: { type: 'node' as const, id: 'n1' }, status: 'editable' as const }; +const cluster = { ref: { type: 'cluster' as const, id: 'c1' }, status: 'readonly' as const }; + +describe('junction workspace session', () => { + it('keeps draft until apply, then emits one applied state', () => { + const initial = createJunctionSession(node.ref, 3); + expect(initial.status).toBe('draft'); + const applied = applyJunctionDraft(markJunctionDraft(initial)); + expect(applied).toMatchObject({ status: 'applied', dirty: false, appliedRevision: 3 }); + }); + + it('rejects saving against a changed document revision', () => { + const session = applyJunctionDraft(markJunctionDraft(createJunctionSession(node.ref, 3))); + expect(() => saveJunctionSession(session, 4)).toThrow(/发生变化/); + expect(saveJunctionSession(session, 3).status).toBe('saved'); + }); + + it('requires an explicit leave action for dirty sessions', () => { + const session = markJunctionDraft(createJunctionSession(node.ref)); + expect(leaveJunctionSession(session, 'stay')).toBe(session); + expect(leaveJunctionSession(session, 'discard')?.status).toBe('cancelled'); + expect(leaveJunctionSession(session, 'apply')?.status).toBe('applied'); + }); + + it('only ordinary editable nodes can be changed', () => { + expect(canEditJunction(node as never)).toBe(true); + expect(canEditJunction(cluster as never)).toBe(false); + }); +}); diff --git a/workbench/client/src/junction/workspace.ts b/workbench/client/src/junction/workspace.ts new file mode 100644 index 0000000..aca1363 --- /dev/null +++ b/workbench/client/src/junction/workspace.ts @@ -0,0 +1,51 @@ +import type { JunctionDescriptor, JunctionRef } from './hotspots'; + +export type JunctionSessionStatus = 'draft' | 'applied' | 'saved' | 'cancelled'; +export type LeaveAction = 'apply' | 'discard' | 'stay'; + +export interface JunctionSession { + ref: JunctionRef; + status: JunctionSessionStatus; + dirty: boolean; + openedRevision?: number; + appliedRevision?: number; +} + +export function createJunctionSession(ref: JunctionRef, revision?: number): JunctionSession { + return { ref, status: 'draft', dirty: false, openedRevision: revision }; +} + +export function markJunctionDraft(session: JunctionSession): JunctionSession { + return { ...session, status: 'draft', dirty: true }; +} + +export function applyJunctionDraft(session: JunctionSession): JunctionSession { + return session.dirty + ? { ...session, status: 'applied', dirty: false, appliedRevision: session.openedRevision } + : session; +} + +export function saveJunctionSession(session: JunctionSession, currentRevision?: number): JunctionSession { + if ( + session.openedRevision !== undefined && + currentRevision !== undefined && + currentRevision !== session.openedRevision + ) { + throw new Error('路口文档已发生变化,请重新加载后再保存'); + } + return { ...session, status: 'saved', dirty: false, openedRevision: currentRevision ?? session.openedRevision }; +} + +export function cancelJunctionSession(session: JunctionSession): JunctionSession { + return { ...session, status: 'cancelled', dirty: false }; +} + +export function leaveJunctionSession(session: JunctionSession, action: LeaveAction): JunctionSession | null { + if (action === 'stay') return session; + if (action === 'discard') return cancelJunctionSession(session); + return applyJunctionDraft(session); +} + +export function canEditJunction(descriptor: JunctionDescriptor): boolean { + return descriptor.ref.type === 'node' && descriptor.status === 'editable'; +} diff --git a/workbench/client/src/styles.css b/workbench/client/src/styles.css index a8f4d07..c2307b0 100644 --- a/workbench/client/src/styles.css +++ b/workbench/client/src/styles.css @@ -531,3 +531,51 @@ pre { .form-hint { color: #7b6542; } +/* JunctionTools local workspace overlay. */ +.junction-workspace { + position: fixed; + inset: 3.5rem 1rem 1rem; + z-index: 20; + overflow: auto; + background: var(--panel, #fff); + border: 1px solid #cbd5e1; + border-radius: 10px; + box-shadow: 0 14px 40px rgb(15 23 42 / 20%); + padding: 1rem 1.25rem; +} +.junction-workspace__header { + display: flex; + justify-content: space-between; + align-items: flex-start; + gap: 1rem; +} +.junction-workspace__header h2 { + margin: 0.2rem 0; +} +.junction-workspace__header p { + margin: 0; + color: #64748b; +} +.junction-workspace__eyebrow { + font-size: 0.75rem; + color: #64748b; + text-transform: uppercase; + letter-spacing: 0.08em; +} +.junction-workspace__body { + max-width: 42rem; + margin-top: 1.5rem; +} +.junction-workspace__body dl { + display: grid; + grid-template-columns: 7rem 1fr; + gap: 0.5rem 1rem; +} +.junction-workspace__body dt { + color: #64748b; +} +.junction-workspace__actions { + display: flex; + gap: 0.5rem; + margin-top: 1.5rem; +}