diff --git a/workbench/client/src/App.tsx b/workbench/client/src/App.tsx index b379d14..7bb0163 100644 --- a/workbench/client/src/App.tsx +++ b/workbench/client/src/App.tsx @@ -4,10 +4,12 @@ import { api, isVersionConflict } from './lib/api'; import { MapCanvas } from './components/MapCanvas'; import { ParameterPanel, type ParameterPanelHandle } from './components/ParameterPanel'; import { - crosswalkHandleFor, elementSelectionFor, groupMembers, + handleForElement, orientedBoundsOf, + specFor, + specOfKind, type ElementSelection, } from './edit/element'; import { directEditEnabled, intervalEditingSupported } from './edit/flag'; @@ -186,14 +188,15 @@ function App() { */ const elementOutline = useMemo(() => { if (!directEditEnabled || !element || !state) return null; - const handle = crosswalkHandleFor(element, state.compiled.model.roads, editHandles); + const handle = handleForElement(element, state.compiled.model.roads, editHandles); if (!handle?.editable) return null; // Read the preview when there is one. `state.layers` is the last *compiled* // output and does not move during editing, so measuring the box there left it - // behind at the baseline while the zebra itself — drawn from the preview — + // behind at the baseline while the marking itself — drawn from the preview — // slid out from under it. A null preview layer means "back to baseline", // which is what the fallback expresses. - const drawn = preview?.crosswalks ?? state.layers.crosswalks; + const layer = specOfKind(element.kind).layer; + const drawn = preview?.[layer] ?? state.layers[layer]; const bounds = orientedBoundsOf(groupMembers(drawn?.features ?? [], element.elementId)); return bounds ? { handleId: handle.handleId, bounds } : null; }, [element, editHandles, state, preview]); @@ -495,11 +498,16 @@ function App() { ); const handleFeature = (properties: Record) => { setMarking(properties); - // A crosswalk is six features but one editable thing. Clicking any stripe - // selects the group; clicking anything else drops the element selection, - // because a box left around a crosswalk the user navigated away from reads - // as a stuck UI. - if (directEditEnabled) setElement(elementSelectionFor(properties, state.layers.crosswalks?.features ?? [])); + // A crosswalk is six features but one editable thing; a stop line is one. + // `provenance` picks which, and therefore which layer holds its siblings — + // the two share `crossing_node_id`, so keying on that alone would merge a + // zebra with the stop line protecting it. Clicking anything else drops the + // selection, because a box left around what the user navigated away from + // reads as a stuck UI. + if (directEditEnabled) { + const spec = specFor(properties); + setElement(spec ? elementSelectionFor(properties, state.layers[spec.layer]?.features ?? []) : null); + } const uid = properties.signal_uid; if (typeof uid === 'string') setStatus(`已选中原生红绿灯 ${uid}`); else if (properties.effective_style) setStatus('已选中标线,可在右侧暂存样式'); @@ -517,7 +525,7 @@ function App() { item.severity === 'error') ? 'dirty' : ''}> {editDiagnostics.find((item) => item.severity === 'error')?.message || (element - ? `已选中斑马线 ${element.crossingNodeId}(${element.featureIds.length} 条)` + ? `已选中${specOfKind(element.kind).label} ${element.crossingNodeId}(${element.featureIds.length} 条)` : editHandles.length ? `手柄 ${editHandles.length} 个` : selected diff --git a/workbench/client/src/edit/element.test.ts b/workbench/client/src/edit/element.test.ts index a77876b..1cea68a 100644 --- a/workbench/client/src/edit/element.test.ts +++ b/workbench/client/src/edit/element.test.ts @@ -3,8 +3,8 @@ import { azimuthBetween, haversineMeters, offsetCoordinate } from './meters'; import { CROSSWALK_PROVENANCE, STOP_LINE_PROVENANCE, - crosswalkElementIdOf, - crosswalkHandleFor, + elementIdOf, + handleForElement, elementSelectionFor, groupMembers, isSameElement, @@ -32,44 +32,51 @@ const features: StripeFeature[] = [ ...[1, 2, 3, 4, 5, 6].map((part) => stripe(-26458, part, 'road:way/-3250:forward')), ]; -describe('crosswalkElementIdOf', () => { +describe('elementIdOf', () => { it('reads the group off a stripe', () => { - expect(crosswalkElementIdOf(stripe(-26452, 3).properties)).toBe('crosswalk:node/-26452'); + expect(elementIdOf(stripe(-26452, 3).properties)).toBe('crosswalk:node/-26452'); }); it('gives every stripe of a group the same id', () => { - const ids = new Set([1, 2, 3, 4, 5, 6].map((part) => crosswalkElementIdOf(stripe(-26452, part).properties))); + const ids = new Set([1, 2, 3, 4, 5, 6].map((part) => elementIdOf(stripe(-26452, part).properties))); expect([...ids]).toEqual(['crosswalk:node/-26452']); }); it('does not share an id between crossings', () => { - expect(crosswalkElementIdOf(stripe(-26452, 1).properties)).not.toBe( - crosswalkElementIdOf(stripe(-26458, 1).properties), - ); + expect(elementIdOf(stripe(-26452, 1).properties)).not.toBe(elementIdOf(stripe(-26458, 1).properties)); }); it('accepts a numeric or string node id', () => { // Real extracts carry numbers, JOSM-authored data carries negative numbers, // and either can arrive as a string through JSON. - expect(crosswalkElementIdOf(stripe(8024512147, 1).properties)).toBe('crosswalk:node/8024512147'); - expect(crosswalkElementIdOf(stripe('-26452', 1).properties)).toBe('crosswalk:node/-26452'); + expect(elementIdOf(stripe(8024512147, 1).properties)).toBe('crosswalk:node/8024512147'); + expect(elementIdOf(stripe('-26452', 1).properties)).toBe('crosswalk:node/-26452'); }); - it('rejects a stop line, which shares the same feature builder', () => { + it('never merges a stop line with the zebra it protects', () => { + // Both come from controlFeature() and shared crossing_node_id on all eight + // measured groups, so keying on that alone would make them one element. + // provenance has to pick the type before the key is read. const stopLine = { properties: { ...stripe(-26452, 1).properties, provenance: STOP_LINE_PROVENANCE } }; - expect(crosswalkElementIdOf(stopLine.properties)).toBeNull(); + expect(elementIdOf(stopLine.properties)).toBe('stop-line:node/-26452'); + expect(elementIdOf(stripe(-26452, 1).properties)).toBe('crosswalk:node/-26452'); + expect(elementIdOf(stopLine.properties)).not.toBe(elementIdOf(stripe(-26452, 1).properties)); + }); + + it('rejects a marking that has no spec, such as a lane arrow', () => { + expect(elementIdOf({ provenance: 'native-road-turn-arrow/v1', lane_id: 'lane:x' })).toBeNull(); }); it('rejects anything else the user may click', () => { - expect(crosswalkElementIdOf({ road_id: 'road:way/-3246:forward' })).toBeNull(); - expect(crosswalkElementIdOf({})).toBeNull(); - expect(crosswalkElementIdOf(null)).toBeNull(); - expect(crosswalkElementIdOf(undefined)).toBeNull(); + expect(elementIdOf({ road_id: 'road:way/-3246:forward' })).toBeNull(); + expect(elementIdOf({})).toBeNull(); + expect(elementIdOf(null)).toBeNull(); + expect(elementIdOf(undefined)).toBeNull(); }); it('rejects a crosswalk with no crossing node to group by', () => { - expect(crosswalkElementIdOf({ provenance: CROSSWALK_PROVENANCE })).toBeNull(); - expect(crosswalkElementIdOf({ provenance: CROSSWALK_PROVENANCE, crossing_node_id: '' })).toBeNull(); + expect(elementIdOf({ provenance: CROSSWALK_PROVENANCE })).toBeNull(); + expect(elementIdOf({ provenance: CROSSWALK_PROVENANCE, crossing_node_id: '' })).toBeNull(); }); }); @@ -115,7 +122,7 @@ describe('elementSelectionFor', () => { }); }); -describe('crosswalkHandleFor', () => { +describe('handleForElement', () => { const roads = [ { id: 'road:way/-3256:backward', segmentId: 'segment:way/-3256/0', sourceNodeIds: ['9', '1140799725'] }, { id: 'road:way/-3256:forward', segmentId: 'segment:way/-3256/0', sourceNodeIds: ['9', '1140799919'] }, @@ -127,7 +134,7 @@ describe('crosswalkHandleFor', () => { { kind: 'junction-stop-line-offset', anchor: { segmentId: 'segment:way/-3256/0', nodeId: '1140799725' } }, ]; const select = (roadId: string) => - crosswalkHandleFor( + handleForElement( { kind: 'crosswalk', elementId: 'e', crossingNodeId: 'c', roadId, featureIds: [] }, roads, handles, @@ -151,7 +158,7 @@ describe('crosswalkHandleFor', () => { it('returns null when the road or handle is missing', () => { expect(select('road:way/-9999:forward')).toBeNull(); expect( - crosswalkHandleFor( + handleForElement( { kind: 'crosswalk', elementId: 'e', crossingNodeId: 'c', roadId: roads[0].id, featureIds: [] }, roads, [], @@ -163,7 +170,7 @@ describe('crosswalkHandleFor', () => { const numeric = [{ kind: 'junction-crosswalk-inset', anchor: { segmentId: 'segment:s', nodeId: 1140799725 } }]; const road = [{ id: 'r', segmentId: 'segment:s', sourceNodeIds: ['1140799725'] }]; expect( - crosswalkHandleFor( + handleForElement( { kind: 'crosswalk', elementId: 'e', crossingNodeId: 'c', roadId: 'r', featureIds: [] }, road, numeric, @@ -228,6 +235,59 @@ describe('orientedBoundsOf', () => { }); }); +describe('stop lines', () => { + /** A stop line: same builder as a stripe, one feature per crossing. */ + function stopLine(crossingNodeId: number, roadId = 'road:way/-3246:forward'): StripeFeature { + return { + properties: { + native_id: `stop-line:node/${crossingNodeId}:1`, + crossing_node_id: crossingNodeId, + road_id: roadId, + provenance: STOP_LINE_PROVENANCE, + }, + }; + } + + it('selects a group of one, so grouping does not rely on there being parts', () => { + // Crosswalks come in sixes. A stop line is a single feature, which is what + // makes it the useful second case: it proves the registry is not quietly + // shaped around multi-part groups. + const lines = [stopLine(-26452), stopLine(-26458, 'road:way/-3250:forward')]; + const selection = elementSelectionFor(lines[0].properties, lines); + expect(selection).toEqual({ + kind: 'stop-line', + elementId: 'stop-line:node/-26452', + crossingNodeId: '-26452', + roadId: 'road:way/-3246:forward', + featureIds: ['stop-line:node/-26452:1'], + }); + }); + + it('does not pull in the crosswalk sharing its crossing node', () => { + // Both layers are never passed together in practice, but if they were, the + // group must still be one type only. + const mixed = [...features, stopLine(-26452)]; + expect(groupMembers(mixed, 'stop-line:node/-26452')).toHaveLength(1); + expect(groupMembers(mixed, 'crosswalk:node/-26452')).toHaveLength(6); + }); + + it('resolves the stop-line handle, not the crosswalk one at the same junction', () => { + const roads = [{ id: 'r', segmentId: 'seg', sourceNodeIds: ['a', '9725'] }]; + const handles = [ + { kind: 'junction-crosswalk-inset', anchor: { segmentId: 'seg', nodeId: '9725' } }, + { kind: 'junction-stop-line-offset', anchor: { segmentId: 'seg', nodeId: '9725' } }, + ]; + const selection = { + kind: 'stop-line' as const, + elementId: 'stop-line:node/-26452', + crossingNodeId: '-26452', + roadId: 'r', + featureIds: [], + }; + expect(handleForElement(selection, roads, handles)?.kind).toBe('junction-stop-line-offset'); + }); +}); + describe('isSameElement', () => { it('compares by group, not by identity', () => { const a = elementSelectionFor(stripe(-26452, 1).properties, features); diff --git a/workbench/client/src/edit/element.ts b/workbench/client/src/edit/element.ts index 00e95f0..3aa825d 100644 --- a/workbench/client/src/edit/element.ts +++ b/workbench/client/src/edit/element.ts @@ -26,16 +26,77 @@ export const CROSSWALK_PROVENANCE = 'native-road-crosswalk/v1'; /** Stop lines share `controlFeature()`; kept so the two are never confused. */ export const STOP_LINE_PROVENANCE = 'native-road-stop-line/v1'; -export type ElementKind = 'crosswalk'; +export type ElementKind = 'crosswalk' | 'stop-line'; + +/** + * What makes one kind of marking an editable element. + * + * A registry rather than a general element protocol, because that is all the + * evidence supports. Measured across the marking layers, group keys come in + * single-field, two-field and derived forms, group sizes run from 1 to 230, and + * two layers have no constraint at all. The one claim that held everywhere is + * "provenance picks the type, then a key picks the group" — which is exactly + * what this promises and no more. + */ +export interface ElementSpec { + kind: ElementKind; + provenance: string; + /** Prefix of the group id, matching the compiler's `native_id`. */ + idPrefix: string; + /** Layer the members live in, in both `state.layers` and a preview. */ + layer: 'crosswalks' | 'vehicleStopLines'; + /** The constraint that moves this element. */ + constraintKind: 'junction-crosswalk-inset' | 'junction-stop-line-offset'; + label: string; + /** The property whose value groups members together. */ + groupProperty: string; +} + +/** + * Crosswalks and stop lines share `crossing_node_id` on every group measured — + * eight out of eight — because both come from `controlFeature()`. Keying on that + * property alone would merge a zebra with the stop line protecting it, so + * `provenance` selects the spec *before* the key is ever read. + */ +export const ELEMENT_SPECS: readonly ElementSpec[] = [ + { + kind: 'crosswalk', + provenance: CROSSWALK_PROVENANCE, + idPrefix: 'crosswalk', + layer: 'crosswalks', + constraintKind: 'junction-crosswalk-inset', + label: '斑马线', + groupProperty: 'crossing_node_id', + }, + { + kind: 'stop-line', + provenance: STOP_LINE_PROVENANCE, + idPrefix: 'stop-line', + layer: 'vehicleStopLines', + constraintKind: 'junction-stop-line-offset', + label: '停止线', + groupProperty: 'crossing_node_id', + }, +]; + +export function specFor(properties: Record | null | undefined): ElementSpec | null { + if (!properties) return null; + return ELEMENT_SPECS.find((spec) => spec.provenance === properties.provenance) ?? null; +} + +export function specOfKind(kind: ElementKind): ElementSpec { + // Every kind in the union has a spec, so this cannot miss. + return ELEMENT_SPECS.find((spec) => spec.kind === kind)!; +} export interface ElementSelection { kind: ElementKind; - /** `crosswalk:node/` — the group, not a stripe. */ + /** `:node/` — the group, not one of its features. */ elementId: string; crossingNodeId: string; /** The directional road the group was placed against. */ roadId: string; - /** `native_id` of every stripe in the group, for highlight and preview scoping. */ + /** `native_id` of every member, for highlight and preview scoping. */ featureIds: string[]; } @@ -61,44 +122,46 @@ function text(value: unknown): string | null { } /** - * The group id for a clicked feature, or `null` when it is not a crosswalk - * stripe — a road surface, a stop line, or anything else the user may hit. + * The group id for a clicked feature, or `null` when it is not an editable + * element — a road surface, a lane arrow, or anything else the user may hit. */ -export function crosswalkElementIdOf(properties: Record | null | undefined): string | null { - if (!properties || properties.provenance !== CROSSWALK_PROVENANCE) return null; - const crossingNodeId = text(properties.crossing_node_id); - return crossingNodeId ? `crosswalk:node/${crossingNodeId}` : null; +export function elementIdOf(properties: Record | null | undefined): string | null { + const spec = specFor(properties); + if (!spec) return null; + const key = text(properties![spec.groupProperty]); + return key ? `${spec.idPrefix}:node/${key}` : null; } -/** Every stripe belonging to one group, in the order the compiler emitted them. */ +/** Every feature belonging to one group, in the order the compiler emitted them. */ export function groupMembers(features: readonly StripeFeature[], elementId: string): StripeFeature[] { - return features.filter((feature) => crosswalkElementIdOf(feature.properties) === elementId); + return features.filter((feature) => elementIdOf(feature.properties) === elementId); } /** * What a click selects. * - * Returns `null` for a click that is not on a crosswalk, which the caller treats - * as "clear the element selection" — clicking a road must not leave a stale box - * around the crosswalk that was selected before. + * Returns `null` for a click that is not on an editable element, which the caller + * treats as "clear the element selection" — clicking a road must not leave a + * stale box around whatever was selected before. */ export function elementSelectionFor( properties: Record | null | undefined, features: readonly StripeFeature[], ): ElementSelection | null { - const elementId = crosswalkElementIdOf(properties); - if (!elementId) return null; + const spec = specFor(properties); + const elementId = elementIdOf(properties); + if (!spec || !elementId) return null; const members = groupMembers(features, elementId); - // The clicked stripe is itself a member, so an empty result means the feature - // collection is not the one the click came from. Selecting nothing is safer - // than selecting a group whose extent we cannot draw. + // The clicked feature is itself a member, so an empty result means the + // collection is not the one the click came from — the caller passed the wrong + // layer. Selecting nothing is safer than selecting a group we cannot draw. if (!members.length) return null; const first = members[0].properties ?? {}; const roadId = text(first.road_id); const crossingNodeId = text(first.crossing_node_id); if (!roadId || !crossingNodeId) return null; return { - kind: 'crosswalk', + kind: spec.kind, elementId, crossingNodeId, roadId, @@ -112,30 +175,33 @@ export function isSameElement(current: ElementSelection | null, next: ElementSel } /** - * The `junction-crosswalk-inset` handle that moves this group. + * The handle that moves this element. * * Segment alone is not enough: 15 of 23 segments in a measured area carry two - * such handles, one per junction at either end, and one segment carried two - * crosswalk groups. Picking by segment would move the wrong zebra half the time. + * control handles of a kind, one per junction at either end, and one segment + * carried two crosswalk groups. Picking by segment would move the wrong object + * half the time. * * The junction is the road's last source node — which is not a guess but the * rule the compiler itself applies, in `crossingJunctionInset()`: * `candidate.road.sourceNodeIds.at(-1)`. Verified to resolve 8/8 groups * uniquely on a live area, including the segment carrying two groups, where the - * directional road tells them apart. + * directional road tells them apart. The solver now applies the same rule when + * it computes that handle's ceiling; see `direct-edit-api.md` §11. */ -export function crosswalkHandleFor }>( +export function handleForElement }>( element: ElementSelection, roads: readonly RoadRef[], handles: readonly T[], ): T | null { + const spec = specOfKind(element.kind); const road = roads.find((item) => item.id === element.roadId); const nodeId = road?.sourceNodeIds.at(-1); if (!road || nodeId === undefined) return null; return ( handles.find( (handle) => - handle.kind === 'junction-crosswalk-inset' && + handle.kind === spec.constraintKind && handle.anchor.segmentId === road.segmentId && String(handle.anchor.nodeId) === String(nodeId), ) ?? null