refactor(workbench): use persistent mode switch
This commit is contained in:
1 parent
c804a373ca
commit
00d5f7650f
5 files changed
+20
-41
No files matched your search
@@ -14,7 +14,7 @@ import {
|
|||||||
specOfKind,
|
specOfKind,
|
||||||
type ElementSelection,
|
type ElementSelection,
|
||||||
} from './edit/element';
|
} from './edit/element';
|
||||||
import { directEditEnabled, intervalEditingSupported } from './edit/flag';
|
import { intervalEditingSupported } from './edit/flag';
|
||||||
import type { ElementOutline } from './edit/handle-layer';
|
import type { ElementOutline } from './edit/handle-layer';
|
||||||
import {
|
import {
|
||||||
azimuthBetween,
|
azimuthBetween,
|
||||||
@@ -115,7 +115,21 @@ function App() {
|
|||||||
const [status, setStatus] = useState('正在加载工作台...');
|
const [status, setStatus] = useState('正在加载工作台...');
|
||||||
const [filter, setFilter] = useState<'all' | 'candidates' | 'other'>('all');
|
const [filter, setFilter] = useState<'all' | 'candidates' | 'other'>('all');
|
||||||
const [marking, setMarking] = useState<Record<string, unknown> | null>(null);
|
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(() => {
|
useEffect(() => {
|
||||||
api
|
api
|
||||||
.state()
|
.state()
|
||||||
|
|||||||
@@ -169,8 +169,8 @@ export function MapCanvas({
|
|||||||
});
|
});
|
||||||
laneSelectionSourceRef.current = laneSelectionSource;
|
laneSelectionSourceRef.current = laneSelectionSource;
|
||||||
laneSelectionLayerRef.current = laneSelectionLayer;
|
laneSelectionLayerRef.current = laneSelectionLayer;
|
||||||
// The v2 direct-edit sources below remain absent with the flag off. The lane
|
// Direct-edit sources are always installed; visibility and hit testing follow
|
||||||
// selection layer above is a regular v1 override affordance, not an edit source.
|
// the in-page browser/edit switch.
|
||||||
const handleLayer = new EditHandleLayer();
|
const handleLayer = new EditHandleLayer();
|
||||||
const ghost = new EditGhostLayer();
|
const ghost = new EditGhostLayer();
|
||||||
const previewLayer = new EditPreviewLayer(
|
const previewLayer = new EditPreviewLayer(
|
||||||
|
|||||||
@@ -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.
|
* Whether a `road-interval` constraint actually applies to its interval.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -5,8 +5,7 @@
|
|||||||
// never keeps a second copy of the constraint model. This class owns both the
|
// 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.
|
// source and that lookup index, which is why they cannot drift apart.
|
||||||
//
|
//
|
||||||
// Baseline layers are untouched. This source is additive and is only created
|
// Baseline layers are untouched. This source is additive and follows edit mode.
|
||||||
// when the `directEdit` flag is on.
|
|
||||||
|
|
||||||
import Feature from 'ol/Feature';
|
import Feature from 'ol/Feature';
|
||||||
import Point from 'ol/geom/Point';
|
import Point from 'ol/geom/Point';
|
||||||
|
|||||||
@@ -72,8 +72,7 @@ export const api = {
|
|||||||
acceptJunctionCandidate: (index: number) =>
|
acceptJunctionCandidate: (index: number) =>
|
||||||
request<WorkbenchState & { added: { id: string } }>('/api/junction-clusters', json({ index })),
|
request<WorkbenchState & { added: { id: string } }>('/api/junction-clusters', json({ index })),
|
||||||
|
|
||||||
// Direct edit. Only reached when the `directEdit` flag is on; with it off the
|
// Direct-edit endpoints used by the in-page browser/edit switch.
|
||||||
// workbench never touches these routes, so behaviour matches main exactly.
|
|
||||||
editState: (selection?: EditSelection) => {
|
editState: (selection?: EditSelection) => {
|
||||||
const query = selection ? `?${new URLSearchParams(selection as Record<string, string>)}` : '';
|
const query = selection ? `?${new URLSearchParams(selection as Record<string, string>)}` : '';
|
||||||
return request<EditStateResponse | { active: false }>(`/api/edit-state${query}`);
|
return request<EditStateResponse | { active: false }>(`/api/edit-state${query}`);
|
||||||
|
|||||||
Reference in new issue
Block a user