From 00d5f7650fc2f391a0786dc5e206117b51597a2b Mon Sep 17 00:00:00 2001 From: que01 Date: Tue, 1 Sep 2026 17:20:16 +0800 Subject: [PATCH] refactor(workbench): use persistent mode switch --- workbench/client/src/App.tsx | 18 ++++++++-- workbench/client/src/components/MapCanvas.tsx | 4 +-- workbench/client/src/edit/flag.ts | 33 ------------------- workbench/client/src/edit/handle-layer.ts | 3 +- workbench/client/src/lib/api.ts | 3 +- 5 files changed, 20 insertions(+), 41 deletions(-) diff --git a/workbench/client/src/App.tsx b/workbench/client/src/App.tsx index 911d48e..c4d67e2 100644 --- a/workbench/client/src/App.tsx +++ b/workbench/client/src/App.tsx @@ -14,7 +14,7 @@ import { specOfKind, type ElementSelection, } from './edit/element'; -import { directEditEnabled, intervalEditingSupported } from './edit/flag'; +import { intervalEditingSupported } from './edit/flag'; import type { ElementOutline } from './edit/handle-layer'; import { azimuthBetween, @@ -115,7 +115,21 @@ function App() { const [status, setStatus] = useState('正在加载工作台...'); const [filter, setFilter] = useState<'all' | 'candidates' | 'other'>('all'); const [marking, setMarking] = useState | null>(null); - const [editMode, setEditMode] = useState(directEditEnabled); + const [editMode, setEditMode] = useState(() => { + if (typeof window === 'undefined') return false; + try { + return window.localStorage.getItem('workbench-mode') === 'edit'; + } catch { + return false; + } + }); + useEffect(() => { + try { + window.localStorage.setItem('workbench-mode', editMode ? 'edit' : 'browse'); + } catch { + // Storage may be unavailable in private browsing; the switch still works for this page. + } + }, [editMode]); useEffect(() => { api .state() diff --git a/workbench/client/src/components/MapCanvas.tsx b/workbench/client/src/components/MapCanvas.tsx index 1d8827b..1b7ff3f 100644 --- a/workbench/client/src/components/MapCanvas.tsx +++ b/workbench/client/src/components/MapCanvas.tsx @@ -169,8 +169,8 @@ export function MapCanvas({ }); laneSelectionSourceRef.current = laneSelectionSource; laneSelectionLayerRef.current = laneSelectionLayer; - // The v2 direct-edit sources below remain absent with the flag off. The lane - // selection layer above is a regular v1 override affordance, not an edit source. + // Direct-edit sources are always installed; visibility and hit testing follow + // the in-page browser/edit switch. const handleLayer = new EditHandleLayer(); const ghost = new EditGhostLayer(); const previewLayer = new EditPreviewLayer( diff --git a/workbench/client/src/edit/flag.ts b/workbench/client/src/edit/flag.ts index 83e306c..f0fc0ed 100644 --- a/workbench/client/src/edit/flag.ts +++ b/workbench/client/src/edit/flag.ts @@ -1,36 +1,3 @@ -// The `directEdit` kill switch. -// -// Default off, per design.md's rollout shape: with it off the workbench must -// behave exactly like main — no edit interaction registered, no manifest -// request, and none of the three edit sources created. Callers check this once -// and skip the whole subsystem, rather than guarding individual call sites. -// -// Opt in per browser session with `?directEdit=1`, or persistently with -// `localStorage.setItem('directEdit', '1')`. A query parameter of `0` wins over -// stored state so a tab can always be forced back to the shipped behaviour. - -const STORAGE_KEY = 'directEdit'; - -function readFlag(): boolean { - if (typeof window === 'undefined') return false; - const parameter = new URLSearchParams(window.location.search).get(STORAGE_KEY); - if (parameter === '1' || parameter === 'true') return true; - if (parameter === '0' || parameter === 'false') return false; - try { - return window.localStorage.getItem(STORAGE_KEY) === '1'; - } catch { - // Private mode can throw on storage access; treat it as "off". - return false; - } -} - -/** - * Resolved once at module load so a single render pass cannot see the flag flip - * halfway through and build a half-wired map. - */ -/** Initial mode for compatibility with the old URL opt-in. The UI may switch it at runtime. */ -export const directEditEnabled = readFlag(); - /** * Whether a `road-interval` constraint actually applies to its interval. * diff --git a/workbench/client/src/edit/handle-layer.ts b/workbench/client/src/edit/handle-layer.ts index 2e3d9c5..3c016bc 100644 --- a/workbench/client/src/edit/handle-layer.ts +++ b/workbench/client/src/edit/handle-layer.ts @@ -5,8 +5,7 @@ // never keeps a second copy of the constraint model. This class owns both the // source and that lookup index, which is why they cannot drift apart. // -// Baseline layers are untouched. This source is additive and is only created -// when the `directEdit` flag is on. +// Baseline layers are untouched. This source is additive and follows edit mode. import Feature from 'ol/Feature'; import Point from 'ol/geom/Point'; diff --git a/workbench/client/src/lib/api.ts b/workbench/client/src/lib/api.ts index 50c5b0d..4b7cb33 100644 --- a/workbench/client/src/lib/api.ts +++ b/workbench/client/src/lib/api.ts @@ -72,8 +72,7 @@ export const api = { acceptJunctionCandidate: (index: number) => request('/api/junction-clusters', json({ index })), - // Direct edit. Only reached when the `directEdit` flag is on; with it off the - // workbench never touches these routes, so behaviour matches main exactly. + // Direct-edit endpoints used by the in-page browser/edit switch. editState: (selection?: EditSelection) => { const query = selection ? `?${new URLSearchParams(selection as Record)}` : ''; return request(`/api/edit-state${query}`);