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

+23 -6
View File
@@ -13,6 +13,7 @@ import CircleStyle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import { createEmpty, extend } from 'ol/extent';
import { click } from 'ol/events/condition';
import type { GeoJson, Road, WorkbenchState } from '../types/state';
import { createLayers, updateJunctionHotspots, updateLayers, updateSelectedRoad, type LayerName } from '../map/layers';
@@ -71,6 +72,8 @@ interface Props {
/** Last solve came back degraded; hold the ghost instead of clearing it. */
ghostPending?: boolean;
editMode?: boolean;
/** When set, fit the map to one junction instead of the whole imported area. */
focusJunctionId?: string | null;
junctionHotspots?: boolean;
onJunctionHotspot?: (ref: JunctionRef, coordinate: Coordinate, descriptor: JunctionDescriptor) => void;
}
@@ -105,6 +108,7 @@ export function MapCanvas({
onRangeEnd,
ghostPending,
editMode = false,
focusJunctionId = null,
junctionHotspots = true,
onJunctionHotspot,
}: Props) {
@@ -424,14 +428,27 @@ export function MapCanvas({
const layers = layersRef.current;
const map = mapRef.current;
if (!layers || !map) return;
updateLayers(layers, state, selected);
updateLayers(layers, state, selected, Boolean(focusJunctionId));
updateJunctionHotspots(layers, junctionDescriptors(state), junctionHotspots);
const extent = layers.osm.getSource()!.getExtent();
if (fittedAreaIdRef.current !== state.areaId && extent && Number.isFinite(extent[0])) {
map.getView().fit(extent, { padding: [48, 48, 48, 48], maxZoom: 19 });
fittedAreaIdRef.current = state.areaId;
const junctionSource = layers.native.getSource();
const focusExtent = focusJunctionId ? createEmpty() : null;
if (focusExtent && junctionSource) {
junctionSource.getFeatures().forEach((feature) => {
const nativeId = feature.get('native_id');
if (nativeId === `junction:node/${focusJunctionId}`)
extend(focusExtent, feature.getGeometry()?.getExtent() ?? focusExtent);
});
}
}, [state, junctionHotspots]);
const extent = focusExtent && Number.isFinite(focusExtent[0]) ? focusExtent : layers.osm.getSource()!.getExtent();
const fitKey = focusJunctionId ? `${state.areaId}:junction:${focusJunctionId}` : state.areaId;
if (fittedAreaIdRef.current !== fitKey && extent && Number.isFinite(extent[0])) {
map.getView().fit(extent, {
padding: focusJunctionId ? [80, 470, 80, 80] : [48, 48, 48, 48],
maxZoom: focusJunctionId ? 20 : 19,
});
fittedAreaIdRef.current = fitKey;
}
}, [focusJunctionId, state, junctionHotspots]);
useEffect(() => {
const layers = layersRef.current;
if (!layers) return;
@@ -24,6 +24,7 @@
// unnoticed. Same invariant, two devices, two honest ways to say "no".
import { useImperativeHandle, useLayoutEffect, useRef, useState, type Ref } from 'react';
import { Minus, Plus } from 'lucide-react';
import {
formatBoundInward,
formatParameterValue,
@@ -63,6 +64,9 @@ interface RowCallbacks {
onAbandon: () => void;
/** Withdraw the constraint and return the row to the compiler's baseline. */
onReset: (handle: EditHandle) => void;
labelForHandle?: (handle: EditHandle) => string;
onDraftGroup?: (handles: EditHandle[], value: number) => void;
onSettleGroup?: (handles: EditHandle[], value: number) => void;
}
function ParameterRow({
@@ -72,6 +76,7 @@ function ParameterRow({
onSettle,
onAbandon,
onReset,
labelForHandle: resolveLabel,
}: RowCallbacks & {
handle: EditHandle;
register: (handleId: string, node: HTMLInputElement | null) => void;
@@ -92,6 +97,8 @@ function ParameterRow({
// previewable nor an error, so it must not light the field up red.
const verdict = parsed === null ? 'ok' : rangeVerdictOf(parsed, min, max);
const rejected = verdict !== 'ok';
const step = 0.1;
const displayLabel = resolveLabel?.(handle) ?? labelForHandle(handle);
const draft = (next: string) => {
setText(next);
@@ -101,6 +108,12 @@ function ParameterRow({
onDraft(handle, value);
};
const nudge = (direction: -1 | 1) => (event: React.MouseEvent) => {
event.preventDefault();
const next = Math.min(max, Math.max(min, (parsed ?? current) + direction * step));
draft(formatParameterValue(next));
};
const settle = () => {
const value = text === null ? null : parseParameterInput(text);
const touched = text !== null && text !== seeded.current;
@@ -140,7 +153,7 @@ function ParameterRow({
return (
<li className={reason ? 'parameter disabled' : 'parameter'}>
<span className="parameter-label">
{labelForHandle(handle)}
{displayLabel}
{isModified(handle) ? (
<>
<b className="parameter-dot" title="已修改" />
@@ -168,6 +181,12 @@ function ParameterRow({
onChange={(event) => draft(event.target.value)}
onBlur={settle}
onKeyDown={(event) => {
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
event.preventDefault();
const direction = event.key === 'ArrowUp' ? 1 : -1;
const next = Math.min(max, Math.max(min, (parsed ?? current) + direction * step));
draft(formatParameterValue(next));
}
if (event.key === 'Enter') event.currentTarget.blur();
if (event.key === 'Escape') {
setText(null);
@@ -177,6 +196,14 @@ function ParameterRow({
}}
/>{' '}
<em>m</em>
<span className="parameter-stepper" aria-label={`${displayLabel}步进控制`}>
<button type="button" onMouseDown={nudge(-1)} disabled={Boolean(reason) || current <= min} title="减小">
<Minus size={13} />
</button>
<button type="button" onMouseDown={nudge(1)} disabled={Boolean(reason) || current >= max} title="增大">
<Plus size={13} />
</button>
</span>
</span>
<span className={rejected ? 'parameter-range rejected' : 'parameter-range'}>
{reason ?? (
@@ -200,11 +227,13 @@ function ParameterRow({
export function ParameterPanel({
handles,
hasSelection,
grouped = false,
ref,
...callbacks
}: RowCallbacks & {
handles: EditHandle[];
hasSelection: boolean;
grouped?: boolean;
ref?: Ref<ParameterPanelHandle>;
}) {
const nodes = useRef(new Map<string, HTMLInputElement>());
@@ -264,18 +293,113 @@ export function ParameterPanel({
}
});
const groupedHandles = new Map<string, EditHandle[]>();
for (const handle of handles) {
const list = groupedHandles.get(handle.kind) ?? [];
list.push(handle);
groupedHandles.set(handle.kind, list);
}
return (
<section className="parameters">
<h1>道路参数</h1>
<h1>{grouped ? '路口参数' : '道路参数'}</h1>
{handles.length ? (
<ul>
{handles.map((handle) => (
<ParameterRow handle={handle} register={register} key={handle.handleId} {...callbacks} />
))}
</ul>
grouped ? (
<GroupedParameterList groups={[...groupedHandles.entries()]} register={register} {...callbacks} />
) : (
<ul>
{handles.map((handle) => (
<ParameterRow handle={handle} register={register} key={handle.handleId} {...callbacks} />
))}
</ul>
)
) : (
<p className="parameter-empty">{hasSelection ? '当前道路无可编辑参数' : '选中道路以显示参数'}</p>
)}
</section>
);
}
function GroupedParameterList({
groups,
register,
...callbacks
}: RowCallbacks & {
groups: Array<[string, EditHandle[]]>;
register: (handleId: string, node: HTMLInputElement | null) => void;
}) {
const [expanded, setExpanded] = useState<Record<string, boolean>>({});
return (
<ul className="parameter-groups">
{groups.map(([kind, items]) => {
const representative = items[0];
const dominant = items
.map((item) => item.value.current)
.sort(
(a, b) =>
items.filter((candidate) => formatParameterValue(candidate.value.current) === formatParameterValue(b))
.length -
items.filter((candidate) => formatParameterValue(candidate.value.current) === formatParameterValue(a))
.length,
)[0];
const globalHandle: EditHandle = {
...representative,
handleId: `group:${kind}`,
constraintId: undefined,
value: {
...representative.value,
current: dominant,
min: Math.max(...items.map((item) => item.value.min)),
max: Math.min(...items.map((item) => item.value.max)),
},
};
const expandedGroup = Boolean(expanded[kind]);
const globalDraft =
callbacks.onDraftGroup ?? ((group: EditHandle[], value: number) => callbacks.onDraft?.(group[0], value));
const globalSettle =
callbacks.onSettleGroup ?? ((group: EditHandle[], value: number) => callbacks.onSettle?.(group[0], value));
return (
<li className="parameter-group" key={kind}>
<div className="parameter-group__header">
<span>
<strong>{groupLabel(kind, representative)}</strong>
<small>统一设置 · {items.length} 项</small>
</span>
<button
type="button"
className="parameter-group__toggle"
onClick={() => setExpanded((current) => ({ ...current, [kind]: !expandedGroup }))}
>
{expandedGroup ? '收起单独定制' : '展开单独定制'}
</button>
</div>
<ParameterRow
handle={globalHandle}
register={register}
{...callbacks}
onDraft={(handle, value) => globalDraft(items, value)}
onSettle={(handle, value) => globalSettle(items, value)}
labelForHandle={() => '统一值'}
/>
{expandedGroup ? (
<ul className="parameter-group__details">
{items.map((handle) => (
<ParameterRow handle={handle} register={register} key={handle.handleId} {...callbacks} />
))}
</ul>
) : null}
</li>
);
})}
</ul>
);
}
function groupLabel(kind: string, handle: EditHandle): string {
if (kind === 'junction-approach-width') return '进口宽度';
if (kind === 'junction-cutback') return '进口 cutback';
if (kind === 'junction-corner-radius') return '角部圆角';
if (kind === 'junction-crosswalk-inset') return '斑马线退让';
if (kind === 'junction-stop-line-offset') return '停止线退距';
return labelForHandle(handle);
}