feat(workbench): stabilize junction tools workspace

This commit is contained in:
que01 committed 2026-09-04 09:46:19 +08:00
1 parent e6b822416d
commit f10a5da325
22 files changed
+1078 -53

No files matched your search

+122 -12
View File
@@ -31,7 +31,7 @@ import {
type Coordinate,
} from './edit/meters';
import { previewArrowFeatures } from './edit/arrow-preview';
import { rendersSame } from './edit/parameter';
import { formatParameterValue, rendersSame } from './edit/parameter';
import { PreviewRequester, type PreviewDraft } from './edit/preview-request';
import { clearEditHistory, recordEdit, takeRedoDomain, takeUndoDomain, type EditHistory } from './edit/edit-history';
import {
@@ -56,6 +56,7 @@ import type { LayerName } from './map/layers';
import type { JunctionRef } from './junction/hotspots';
import { junctionDescriptors, hotspotAt } from './junction/hotspots';
import { JunctionWorkspace } from './junction/JunctionWorkspace';
import { localJunctionPreview, localJunctionState } from './junction/local-state';
import {
applyJunctionDraft,
cancelJunctionSession,
@@ -161,6 +162,7 @@ function App() {
editMode,
state,
selectedSegmentId: selected?.segmentId,
selectedJunctionId: junctionRoute?.type === 'node' ? junctionRoute.id : undefined,
setStatus,
});
const {
@@ -178,6 +180,37 @@ function App() {
() => (state && junctionRoute ? hotspotAt(junctionDescriptors(state), junctionRoute) : null),
[junctionRoute, state],
);
const junctionRoads = useMemo(() => {
if (!state || !junctionDescriptor) return [];
const ids = new Set(junctionDescriptor.approachRoadIds);
// Configured clusters may not carry approach ids yet; derive them from the
// cluster's member nodes so the workspace still stays local to the junction.
if (!ids.size) {
const nodeIds = new Set(junctionDescriptor.nodeIds.map(String));
for (const endpoint of state.compiled.model.endpoints || []) {
if (nodeIds.has(String(endpoint.nodeId))) ids.add(endpoint.roadId);
}
}
const seen = new Set<string>();
return state.compiled.model.roads.filter((road) => {
if (!(ids.has(road.id) || ids.has(road.segmentId))) return false;
const key = road.tags?.name?.trim() || `unnamed:${road.id || road.segmentId}`;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}, [junctionDescriptor, state]);
const junctionMapState = useMemo(
() => (state && junctionDescriptor && junctionRoute ? localJunctionState(state, junctionDescriptor) : state),
[junctionDescriptor, junctionRoute, state],
);
const junctionMapPreview = useMemo(
() =>
state && junctionDescriptor && junctionRoute
? localJunctionPreview(preview, junctionDescriptor, state.compiled.model.roads)
: preview,
[junctionDescriptor, junctionRoute, preview, state],
);
// A deep link starts without a map click, so it has no selected approach from
// which the existing manifest loader can scope its request. Pick the first
// descriptor-owned approach here as well as in the click path; this is a
@@ -213,7 +246,9 @@ function App() {
(handle) =>
(handle.kind === 'junction-approach-width' ||
handle.kind === 'junction-cutback' ||
handle.kind === 'junction-corner-radius') &&
handle.kind === 'junction-corner-radius' ||
handle.kind === 'junction-crosswalk-inset' ||
handle.kind === 'junction-stop-line-offset') &&
'nodeId' in handle.anchor &&
nodeIds.has(String(handle.anchor.nodeId)),
);
@@ -371,7 +406,16 @@ function App() {
// Read at send time so the requester, built once, always scopes to the current
// selection without being rebuilt.
const selectionRef = useRef<EditSelection | undefined>(undefined);
selectionRef.current = selected?.segmentId ? { segment: selected.segmentId } : undefined;
// An independent JunctionTools page owns the whole junction manifest. Never
// fall back to the approach segment here: doing so makes an accepted preview
// replace the junction manifest with a road-scoped one, which drops the
// width/cutback/radius rows immediately after the first edit.
selectionRef.current =
junctionRoute?.type === 'node'
? { junction: junctionRoute.id }
: selected?.segmentId
? { segment: selected.segmentId }
: undefined;
/**
* One gesture, one operation id. Minted on the first draft of a drag and cleared
* on release: reusing an id across gestures would put two operations with the
@@ -412,8 +456,12 @@ function App() {
// control handles silently returned null and never scheduled a preview.
const roadId = 'roadId' in handle.anchor ? handle.anchor.roadId : null;
const segmentId = 'segmentId' in handle.anchor ? handle.anchor.segmentId : null;
const cornerRoadId = handle.anchor.type === 'junction-corner' ? handle.anchor.incomingRoadId : null;
const road = state?.compiled.model.roads.find(
(item) => (roadId ? item.id === roadId : false) || (segmentId ? item.segmentId === segmentId : false),
(item) =>
(roadId ? item.id === roadId : false) ||
(segmentId ? item.segmentId === segmentId : false) ||
(cornerRoadId ? item.id === cornerRoadId || item.segmentId === cornerRoadId : false),
);
// Without a centerline there is no snapshot to record, so drop the drag rather
// than send a constraint the server would have to reject.
@@ -435,6 +483,47 @@ function App() {
operations: [...base.operations.filter((item) => item.id !== operation.id), operation],
};
};
const draftForGroup = (handles: EditHandle[], value: number): PreviewDraft | null => {
if (!handles.length || !session) return null;
const drafts = handles
.map((handle) => draftFor(handle, value))
.filter((item): item is PreviewDraft => Boolean(item));
if (!drafts.length) return null;
const base = session.fragment();
const constraints = new Map(base.constraints.map((item) => [item.id, item]));
const operations = new Map(base.operations.map((item) => [item.id, item]));
for (const draft of drafts) {
for (const constraint of draft.constraints ?? []) constraints.set(constraint.id, constraint);
for (const operation of draft.operations ?? []) operations.set(operation.id, operation);
}
return { constraints: [...constraints.values()], operations: [...operations.values()] };
};
const settleGroup = (handles: EditHandle[], value: number) => {
// Keep individually customised entries intact: the manifest does not expose
// a separate scope bit, so the dominant current value is the group's default
// and outliers are treated as local overrides.
const frequencies = new Map<string, number>();
for (const handle of handles) {
const key = formatParameterValue(handle.value.current);
frequencies.set(key, (frequencies.get(key) ?? 0) + 1);
}
const defaultKey = [...frequencies.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
const changed = handles.filter(
(handle) =>
formatParameterValue(handle.value.current) === defaultKey && !rendersSame(value, handle.value.current),
);
if (!changed.length) {
abandonGesture();
return;
}
const draft = draftForGroup(changed, value);
if (draft)
commitGesture(
draft,
changed.map((handle) => `constraint:${handle.handleId}`),
);
else abandonGesture();
};
const draftLane = (laneId: string, maneuver: string, placementDistanceMeters: number): PreviewDraft | null => {
const handle = manifest?.handles.find(
(item) => item.kind === 'lane-maneuver' && item.anchor.type === 'lane' && item.anchor.laneId === laneId,
@@ -520,6 +609,14 @@ function App() {
panelRef.current?.clearLive();
previewSession();
};
const discardJunctionDraft = () => {
if (!session) return;
session.discard();
gesture.current = null;
panelRef.current?.clearLive();
previewSession();
setJunctionSession((current) => (current ? cancelJunctionSession(current) : current));
};
/** A settled value from either channel: one undoable command, or nothing. */
const settleValue = (handle: EditHandle, value: number) => {
// A gesture that did not move the value must not become an edit. `handle` was
@@ -765,8 +862,9 @@ function App() {
const choice = window.prompt('路口有未应用草稿:输入 1 应用并返回,2 放弃并返回,3 留在工作区', '3');
if (choice === '3' || !choice) return;
if (choice === '1') setJunctionSession(applyJunctionDraft(junctionSession));
else if (choice === '2') setJunctionSession(cancelJunctionSession(junctionSession));
else return;
else if (choice === '2') {
discardJunctionDraft();
} else return;
}
window.history.pushState({}, '', '/');
setJunctionRoute(null);
@@ -790,7 +888,7 @@ function App() {
else if (properties.effective_style) setStatus('已选中标线,可在右侧暂存样式');
};
return (
<>
<div className={junctionRoute ? 'app junction-page' : 'app'}>
<header>
<strong>道路编译工作台</strong>
<label className="mode-switch">
@@ -906,8 +1004,9 @@ function App() {
</details>
</aside>
<MapCanvas
state={state}
state={junctionMapState!}
editMode={editMode}
focusJunctionId={junctionRoute?.type === 'node' ? junctionRoute.id : null}
selected={selected}
visible={layers}
scene={scene}
@@ -966,7 +1065,7 @@ function App() {
if (draft) commitGesture(draft, [`constraint:handle:lane-maneuver:${element.laneId}`]);
setStatus('箭头位置已暂存;保存并重新生成后应用。');
}}
preview={preview}
preview={junctionMapPreview}
ghostPending={session?.degraded ?? false}
onHandleDrag={(handle, value) => {
// Straight to the DOM, like the ghost writes straight to its source:
@@ -1037,7 +1136,7 @@ function App() {
facilitySummary={junctionFacilitySummary}
onClose={closeJunction}
onApply={() => setJunctionSession((current) => (current ? applyJunctionDraft(current) : current))}
onDiscard={() => setJunctionSession((current) => (current ? cancelJunctionSession(current) : current))}
onDiscard={discardJunctionDraft}
onSave={() => {
try {
void saveDirectEdits().then((saved) => {
@@ -1052,19 +1151,30 @@ function App() {
editMode={editMode}
onEnableEdit={() => setEditMode(true)}
handles={junctionHandles}
onDraft={draftValue}
onDraft={(handle, value) => {
draftValue(handle, value);
setJunctionSession((current) => (current ? { ...current, status: 'draft', dirty: true } : current));
}}
onDraftGroup={(handles, value) => {
const draft = draftForGroup(handles, value);
if (draft) requester?.schedule(draft);
setJunctionSession((current) => (current ? { ...current, status: 'draft', dirty: true } : current));
}}
onSettle={(handle, value) => {
settleValue(handle, value);
setJunctionSession((current) => (current ? { ...current, status: 'draft', dirty: true } : current));
}}
onSettleGroup={settleGroup}
onAbandon={abandonGesture}
onReset={(handle) => {
resetHandle(handle);
setJunctionSession((current) => (current ? { ...current, status: 'draft', dirty: true } : current));
}}
roads={junctionRoads}
junctionCoordinate={junctionDescriptor.coordinate}
/>
) : null}
</>
</div>
);
}
export default App;