refactor(workbench): use persistent mode switch

This commit is contained in:
que01 committed 2026-09-01 17:20:16 +08:00
1 parent c804a373ca
commit 00d5f7650f
5 files changed
+20 -41

No files matched your search

+16 -2
View File
@@ -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<Record<string, unknown> | 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()
@@ -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(
-33
View File
@@ -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.
*
+1 -2
View File
@@ -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';
+1 -2
View File
@@ -72,8 +72,7 @@ export const api = {
acceptJunctionCandidate: (index: number) =>
request<WorkbenchState & { added: { id: string } }>('/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<string, string>)}` : '';
return request<EditStateResponse | { active: false }>(`/api/edit-state${query}`);