From 1f439d98de11d67ec3fd2fe13fce509d96ecd1d0 Mon Sep 17 00:00:00 2001 From: que01 Date: Fri, 4 Sep 2026 14:23:27 +0800 Subject: [PATCH] fix(workbench): show junction traffic signals --- .../ui-prototype.html | 2 +- test/e2e/workbench.spec.ts | 69 +++++++++++- workbench/client/src/App.tsx | 93 ++++++++++++---- .../client/src/components/ElementEditors.tsx | 40 ++++--- workbench/client/src/components/MapCanvas.tsx | 2 +- .../client/src/components/ParameterPanel.tsx | 36 ++++++- .../src/components/WorkbenchInspector.tsx | 6 +- workbench/client/src/edit/parameter.ts | 2 +- .../client/src/hooks/useTrafficSignalDraft.ts | 12 ++- .../client/src/junction/JunctionWorkspace.tsx | 101 +++++++++++++++++- .../client/src/junction/facilities.test.ts | 79 ++++++++++++++ workbench/client/src/junction/facilities.ts | 62 +++++++++++ workbench/client/src/junction/labels.test.ts | 2 +- workbench/client/src/junction/local-state.ts | 30 +++++- workbench/client/src/styles.css | 53 +++++++++ 15 files changed, 529 insertions(+), 60 deletions(-) create mode 100644 workbench/client/src/junction/facilities.test.ts create mode 100644 workbench/client/src/junction/facilities.ts diff --git a/.trellis/tasks/08-26-direct-edit-junction-tools/ui-prototype.html b/.trellis/tasks/08-26-direct-edit-junction-tools/ui-prototype.html index bdb15f7..f0b5bf4 100644 --- a/.trellis/tasks/08-26-direct-edit-junction-tools/ui-prototype.html +++ b/.trellis/tasks/08-26-direct-edit-junction-tools/ui-prototype.html @@ -114,7 +114,7 @@

修改后立即在画布显示服务端预览

m
-
m
+
m
m
diff --git a/test/e2e/workbench.spec.ts b/test/e2e/workbench.spec.ts index c289677..c4f9a7e 100644 --- a/test/e2e/workbench.spec.ts +++ b/test/e2e/workbench.spec.ts @@ -117,11 +117,14 @@ test.describe('workbench editing shell', () => { await expect(page.getByLabel('路口编辑工作区')).toBeVisible(); await expect(page.getByLabel('道路地图')).toBeVisible(); expect((await page.getByLabel('道路地图').boundingBox())?.width).toBeGreaterThan(0); - await expect(page.getByText(`路口节点 ${nodeId}`)).toBeVisible(); + await expect(page.getByText('JunctionTools', { exact: true })).toBeVisible(); + await expect(page.getByText(nodeId, { exact: true })).toHaveCount(0); await expect(page.getByText('设施与验证', { exact: true })).toBeVisible(); await expect(page.getByText('当前为浏览模式,开启编辑后可调整路口几何参数')).toBeVisible(); await page.getByRole('button', { name: '开启编辑模式' }).click(); - await expect(page.getByLabel('路口编辑工作区').getByText('道路参数', { exact: true })).toBeVisible(); + await expect(page.getByLabel('路口编辑工作区').getByText('路口参数', { exact: true })).toBeVisible(); + await expect(page.getByLabel('路口编辑工作区').getByText('展开单独定制', { exact: true }).first()).toBeVisible(); + await expect(page.getByLabel('路口编辑工作区').getByText('进口收口距离', { exact: true })).toBeVisible(); await expect( page .getByLabel('路口编辑工作区') @@ -129,4 +132,66 @@ test.describe('workbench editing shell', () => { .first(), ).toBeVisible(); }); + + test('discards junction drafts and lists each approach road once', async ({ page }) => { + const state = await page.request.get('/api/state').then((response) => response.json()); + const nodeCounts = new Map(); + for (const endpoint of state.compiled.model.endpoints) { + const nodeId = String(endpoint.nodeId); + nodeCounts.set(nodeId, (nodeCounts.get(nodeId) || 0) + 1); + } + const nodeId = [...nodeCounts].find(([, count]) => count >= 2)?.[0]; + expect(nodeId).toBeTruthy(); + await page.goto(`/junction/node/${encodeURIComponent(nodeId!)}`); + await page.getByRole('button', { name: '开启编辑模式' }).click(); + + const roadsText = await page.locator('.junction-workspace__body dd').nth(1).innerText(); + const roads = roadsText + .split('、') + .map((road) => road.trim()) + .filter(Boolean); + expect(new Set(roads).size).toBe(roads.length); + + const input = page.getByLabel('路口编辑工作区').locator('input').first(); + const original = Number(await input.inputValue()); + await input.fill(String(original + 0.1)); + await expect(page.getByRole('button', { name: '放弃草稿' })).toBeEnabled(); + await page.getByRole('button', { name: '放弃草稿' }).click(); + await expect(input).toHaveValue(String(original)); + + await page.getByRole('button', { name: '展开单独定制' }).first().click(); + const detailRows = page.locator('.parameter-group__details .parameter'); + await expect(detailRows.first()).toBeVisible(); + await expect(detailRows.first().locator('.parameter-label')).toContainText(/进口/); + }); + + test('opens a junction signal from the local facility list without exposing delete', async ({ page }) => { + const state = await page.request.get('/api/state').then((response) => response.json()); + const nodeCounts = new Map(); + for (const endpoint of state.compiled.model.endpoints) { + const nodeId = String(endpoint.nodeId); + nodeCounts.set(nodeId, (nodeCounts.get(nodeId) || 0) + 1); + } + const nodeId = [...nodeCounts].find(([, count]) => count >= 2)?.[0]; + expect(nodeId).toBeTruthy(); + await page.goto(`/junction/node/${encodeURIComponent(nodeId!)}`); + await page.getByRole('button', { name: '开启编辑模式' }).click(); + + const signalItem = page + .getByLabel('已有设施') + .getByRole('button', { name: /信号灯/ }) + .first(); + if ((await signalItem.count()) === 0) return; + await signalItem.click(); + const signalSelect = page.getByLabel('检查信号灯'); + await expect(signalSelect).toHaveValue(/.+/); + await expect(page.getByRole('button', { name: '删除信号灯' })).toHaveCount(0); + await expect(page.getByText('当前选中:信号灯。可在下方参数中调整。')).toBeVisible(); + const heading = page.getByLabel('横杆方向(度)'); + const original = await heading.inputValue(); + await heading.fill(String((Number(original) + 15) % 360)); + await expect(page.getByRole('button', { name: '放弃草稿' })).toBeEnabled(); + await page.getByRole('button', { name: '放弃草稿' }).click(); + await expect(heading).toHaveValue(original); + }); }); diff --git a/workbench/client/src/App.tsx b/workbench/client/src/App.tsx index 8f9000f..38999b6 100644 --- a/workbench/client/src/App.tsx +++ b/workbench/client/src/App.tsx @@ -265,7 +265,9 @@ function App() { nodeIds.has(String(feature.properties?.crossing_node_id)), ).length; const signals = state.trafficSignals.assemblies.features.filter((feature) => - nodeIds.has(String(feature.properties?.node_id)), + [feature.properties?.node_id, feature.properties?.control_id].some( + (value) => value !== undefined && value !== null && nodeIds.has(String(value)), + ), ).length; return { crosswalks: crossingIds.size, stopLines, signals }; }, [junctionDescriptor, state]); @@ -610,11 +612,27 @@ function App() { previewSession(); }; const discardJunctionDraft = () => { - if (!session) return; - session.discard(); + session?.discard(); + if (signalDirty && signalSavedRef.current) { + setState((current) => + current + ? { + ...current, + trafficSignals: { + ...current.trafficSignals, + assemblies: { ...current.trafficSignals.assemblies, features: signalSavedRef.current! }, + }, + } + : current, + ); + signalDraftRef.current = null; + signalUndoRef.current = []; + signalRedoRef.current = []; + setSignalDirty(false); + } gesture.current = null; panelRef.current?.clearLive(); - previewSession(); + if (session) previewSession(); setJunctionSession((current) => (current ? cancelJunctionSession(current) : current)); }; /** A settled value from either channel: one undoable command, or nothing. */ @@ -796,9 +814,9 @@ function App() { } return true; }; - const compile = async () => { - if (!(await save())) return; - if (!(await saveDirectEdits(false))) return; + const compile = async (): Promise => { + if (!(await save())) return false; + if (!(await saveDirectEdits(false))) return false; if (signalDirty && signalDraftRef.current && state) { try { const result = await api.signals({ @@ -811,7 +829,7 @@ function App() { setSignalDirty(false); } catch (error) { setStatus(`红绿灯保存失败:${(error as Error).message}`); - return; + return false; } } try { @@ -822,8 +840,10 @@ function App() { setSelected((current) => next.compiled.model.roads.find((road) => road.id === current?.id) || null); setPreview(null); setStatus('已保存并重新生成'); + return true; } catch (error) { setStatus((error as Error).message); + return false; } }; if (!state) @@ -858,11 +878,16 @@ function App() { setJunctionRoute(ref); }; const closeJunction = () => { - if (junctionSession?.dirty) { + if (junctionSession?.dirty || signalDirty) { const choice = window.prompt('路口有未应用草稿:输入 1 应用并返回,2 放弃并返回,3 留在工作区', '3'); if (choice === '3' || !choice) return; - if (choice === '1') setJunctionSession(applyJunctionDraft(junctionSession)); - else if (choice === '2') { + if (choice === '1') { + if (signalDirty) { + setStatus('信号灯草稿仍未保存,请在工作区内完成保存或放弃。'); + return; + } + if (junctionSession) setJunctionSession(applyJunctionDraft(junctionSession)); + } else if (choice === '2') { discardJunctionDraft(); } else return; } @@ -1021,7 +1046,7 @@ function App() { }} onSelectRoad={setSelected} onFeature={handleFeature} - signalSelectionUid={typeof marking?.signal_uid === 'string' ? marking.signal_uid : null} + signalSelectionUid={marking?.signal_uid == null ? null : String(marking.signal_uid)} onBackground={() => { setElement(null); setMarking(null); @@ -1132,22 +1157,18 @@ function App() { setJunctionSession((current) => (current ? applyJunctionDraft(current) : current))} onDiscard={discardJunctionDraft} - onSave={() => { - try { - void saveDirectEdits().then((saved) => { - if (!saved) return; - setJunctionSession((current) => (current ? saveJunctionSession(current, editRevision) : current)); - setStatus('路口草稿已保存并重新生成'); - }); - } catch (error) { - setStatus((error as Error).message); - } - }} + onSave={() => + void compile().then((ok) => { + if (ok) setJunctionSession((current) => (current ? saveJunctionSession(current, editRevision) : current)); + }) + } editMode={editMode} onEnableEdit={() => setEditMode(true)} handles={junctionHandles} @@ -1172,6 +1193,32 @@ function App() { }} roads={junctionRoads} junctionCoordinate={junctionDescriptor.coordinate} + selectedFacility={element} + selectedSignalUid={marking?.signal_uid == null ? null : String(marking.signal_uid)} + signalUids={state.trafficSignals.assemblies.features + .filter((item) => + [item.properties?.node_id, item.properties?.control_id].some( + (value) => value !== undefined && value !== null && junctionDescriptor.nodeIds.includes(String(value)), + ), + ) + .map((item) => String(item.properties?.signal_uid ?? '')) + .filter(Boolean)} + onSignalStatus={setStatus} + onUpdateSignal={updateSignal} + onSelectFacility={(facility) => { + const source = + facility.kind === 'crosswalk' + ? state.layers.crosswalks + : facility.kind === 'stop-line' + ? state.layers.vehicleStopLines + : state.trafficSignals.assemblies; + const feature = source?.features.find((item) => { + if (facility.kind === 'signal') + return String(item.properties?.signal_uid) === facility.id.slice('signal:'.length); + return String(item.properties?.crossing_node_id) === facility.id.split(':').slice(1).join(':'); + }); + if (feature) handleFeature(feature.properties ?? {}); + }} /> ) : null} diff --git a/workbench/client/src/components/ElementEditors.tsx b/workbench/client/src/components/ElementEditors.tsx index e2c9bcc..cee6d46 100644 --- a/workbench/client/src/components/ElementEditors.tsx +++ b/workbench/client/src/components/ElementEditors.tsx @@ -131,17 +131,31 @@ export function MarkingEditor({ export function SignalEditor({ state, signal, + signalUids, + allowDelete = true, status, update, }: { state: WorkbenchState; signal?: GeoFeature; + signalUids?: readonly string[]; + allowDelete?: boolean; status: (text: string) => void; update: (features: GeoFeature[]) => Promise; }) { + const allowed = signalUids ? new Set(signalUids) : null; + const signals = state.trafficSignals.assemblies.features.filter((item) => { + const uid = String(item.properties?.signal_uid ?? ''); + return !allowed || allowed.has(uid); + }); const [selected, setSelected] = useState(''); useEffect(() => setSelected(String(signal?.properties?.signal_uid || '')), [signal]); - const feature = state.trafficSignals.assemblies.features.find((item) => item.properties?.signal_uid === selected); + useEffect(() => { + if (selected && !signals.some((item) => String(item.properties?.signal_uid) === selected)) setSelected(''); + }, [selected, signals]); + const feature = state.trafficSignals.assemblies.features.find( + (item) => String(item.properties?.signal_uid ?? '') === selected, + ); const coordinates = feature?.geometry?.type === 'Point' && Array.isArray(feature.geometry.coordinates) ? (feature.geometry.coordinates as [number, number]) @@ -183,7 +197,7 @@ export function SignalEditor({ 检查信号灯