feat: edit lane turn arrows in workbench

This commit is contained in:
que01 committed 2026-09-01 09:17:24 +08:00
1 parent 8b84166e6f
commit e48eb15827
9 files changed
+330 -28

No files matched your search

+67 -2
View File
@@ -2,7 +2,9 @@ import { describe, expect, it } from 'vitest';
import { azimuthBetween, haversineMeters, offsetCoordinate } from './meters';
import {
CROSSWALK_PROVENANCE,
LANE_CENTERLINE_SOURCE,
STOP_LINE_PROVENANCE,
TURN_ARROW_PROVENANCE,
elementIdOf,
handleForElement,
elementSelectionFor,
@@ -26,6 +28,18 @@ function stripe(crossingNodeId: string | number, part: number, roadId = 'road:wa
};
}
function turnArrow(laneId: string, maneuver: string, part: number, roadId = 'road:way/-3246:forward'): StripeFeature {
return {
properties: {
native_id: `turn-arrow:${laneId}:${maneuver}:${part}`,
lane_id: laneId,
maneuver,
road_id: roadId,
provenance: TURN_ARROW_PROVENANCE,
},
};
}
/** Two groups of six, the arrangement a real area produces. */
const features: StripeFeature[] = [
...[1, 2, 3, 4, 5, 6].map((part) => stripe(-26452, part)),
@@ -63,8 +77,16 @@ describe('elementIdOf', () => {
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('groups a turn arrow by both lane and maneuver', () => {
expect(elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'through;left', 1).properties)).toBe(
'turn-arrow:lane:road:way/-3246:forward:1\u001fthrough;left',
);
expect(elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'through;left', 2).properties)).toBe(
elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'through;left', 1).properties),
);
expect(elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'left', 1).properties)).not.toBe(
elementIdOf(turnArrow('lane:road:way/-3246:forward:1', 'through;left', 1).properties),
);
});
it('rejects anything else the user may click', () => {
@@ -120,6 +142,49 @@ describe('elementSelectionFor', () => {
// selecting a group with no members on screen.
expect(elementSelectionFor(stripe(-26452, 1).properties, [])).toBeNull();
});
it('selects all parts of one turn arrow without merging another maneuver on the lane', () => {
const laneId = 'lane:road:way/-3246:forward:1';
const arrows = [
...[0, 1, 2].map((part) => turnArrow(laneId, 'through;left', part)),
...[0, 1, 2].map((part) => turnArrow(laneId, 'left', part)),
];
expect(elementSelectionFor(arrows[1].properties, arrows)).toEqual({
kind: 'lane-maneuver',
laneId,
maneuver: 'through;left',
roadId: 'road:way/-3246:forward',
featureIds: [0, 1, 2].map((part) => `turn-arrow:${laneId}:through;left:${part}`),
});
});
it('selects a centerline as a lane with no current turn arrow', () => {
expect(
elementSelectionFor(
{
native_id: 'lane:road:way/-3246:forward:2',
road_id: 'road:way/-3246:forward',
source: LANE_CENTERLINE_SOURCE,
},
[],
),
).toEqual({
kind: 'lane-maneuver',
laneId: 'lane:road:way/-3246:forward:2',
maneuver: '',
roadId: 'road:way/-3246:forward',
featureIds: ['lane:road:way/-3246:forward:2'],
});
});
it('does not offer the repeated direction arrows as lane-maneuver edits', () => {
expect(
elementSelectionFor(
{ provenance: 'native-road-direction-arrow/v1', lane_id: 'lane:road:way/-3246:forward:1' },
[],
),
).toBeNull();
});
});
describe('handleForElement', () => {
+76 -17
View File
@@ -25,8 +25,12 @@ import { azimuthBetween, eastNorthMeters, haversineMeters, offsetCoordinate, typ
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';
/** Turn arrows are grouped by both the lane and the rendered maneuver. */
export const TURN_ARROW_PROVENANCE = 'native-road-turn-arrow/v1';
/** Lane centerlines are the entry point for lanes which do not yet draw an arrow. */
export const LANE_CENTERLINE_SOURCE = 'native-road-lane-centerline/v3-control-clipped';
export type ElementKind = 'crosswalk' | 'stop-line';
export type ElementKind = 'crosswalk' | 'stop-line' | 'turn-arrow';
/**
* What makes one kind of marking an editable element.
@@ -44,12 +48,12 @@ export interface ElementSpec {
/** 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';
layer: 'crosswalks' | 'vehicleStopLines' | 'turnArrows';
/** The constraint that moves this control element. */
constraintKind?: 'junction-crosswalk-inset' | 'junction-stop-line-offset';
label: string;
/** The property whose value groups members together. */
groupProperty: string;
/** Properties whose ordered values group rendered parts together. */
groupProperties: readonly string[];
}
/**
@@ -62,20 +66,28 @@ export const ELEMENT_SPECS: readonly ElementSpec[] = [
{
kind: 'crosswalk',
provenance: CROSSWALK_PROVENANCE,
idPrefix: 'crosswalk',
idPrefix: 'crosswalk:node/',
layer: 'crosswalks',
constraintKind: 'junction-crosswalk-inset',
label: '斑马线',
groupProperty: 'crossing_node_id',
groupProperties: ['crossing_node_id'],
},
{
kind: 'stop-line',
provenance: STOP_LINE_PROVENANCE,
idPrefix: 'stop-line',
idPrefix: 'stop-line:node/',
layer: 'vehicleStopLines',
constraintKind: 'junction-stop-line-offset',
label: '停止线',
groupProperty: 'crossing_node_id',
groupProperties: ['crossing_node_id'],
},
{
kind: 'turn-arrow',
provenance: TURN_ARROW_PROVENANCE,
idPrefix: 'turn-arrow:',
layer: 'turnArrows',
label: '转向箭头',
groupProperties: ['lane_id', 'maneuver'],
},
];
@@ -89,8 +101,8 @@ export function specOfKind(kind: ElementKind): ElementSpec {
return ELEMENT_SPECS.find((spec) => spec.kind === kind)!;
}
export interface ElementSelection {
kind: ElementKind;
export interface ControlElementSelection {
kind: 'crosswalk' | 'stop-line';
/** `<prefix>:node/<crossingId>` — the group, not one of its features. */
elementId: string;
crossingNodeId: string;
@@ -100,6 +112,18 @@ export interface ElementSelection {
featureIds: string[];
}
/** A discrete v1 override target, selected from an arrow or a lane centerline. */
export interface LaneManeuverSelection {
kind: 'lane-maneuver';
laneId: string;
/** The current rendered maneuver; empty when the lane has no turn arrow. */
maneuver: string;
roadId: string;
featureIds: string[];
}
export type ElementSelection = ControlElementSelection | LaneManeuverSelection;
/** The shape this module needs off a GeoJSON feature; anything else is ignored. */
export interface StripeFeature {
properties?: Record<string, unknown> | null;
@@ -121,6 +145,13 @@ function text(value: unknown): string | null {
return null;
}
function groupKeyOf(properties: Record<string, unknown>, spec: ElementSpec): string | null {
const parts = spec.groupProperties.map((property) => text(properties[property]));
// U+001F is an ASCII control separator, so lane ids and maneuvers cannot
// accidentally produce the same key through ordinary punctuation.
return parts.every((part): part is string => part !== null) ? parts.join('\u001f') : null;
}
/**
* 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.
@@ -128,8 +159,8 @@ function text(value: unknown): string | null {
export function elementIdOf(properties: Record<string, unknown> | 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;
const key = groupKeyOf(properties!, spec);
return key ? `${spec.idPrefix}${key}` : null;
}
/** Every feature belonging to one group, in the order the compiler emitted them. */
@@ -149,6 +180,11 @@ export function elementSelectionFor(
features: readonly StripeFeature[],
): ElementSelection | null {
const spec = specFor(properties);
if (!spec && properties?.source === LANE_CENTERLINE_SOURCE) {
const laneId = text(properties.native_id);
const roadId = text(properties.road_id);
return laneId && roadId ? { kind: 'lane-maneuver', laneId, maneuver: '', roadId, featureIds: [laneId] } : null;
}
const elementId = elementIdOf(properties);
if (!spec || !elementId) return null;
const members = groupMembers(features, elementId);
@@ -158,8 +194,24 @@ export function elementSelectionFor(
if (!members.length) return null;
const first = members[0].properties ?? {};
const roadId = text(first.road_id);
if (!roadId) return null;
if (spec.kind === 'turn-arrow') {
const laneId = text(first.lane_id);
const maneuver = text(first.maneuver);
return laneId && maneuver
? {
kind: 'lane-maneuver',
laneId,
maneuver,
roadId,
featureIds: members
.map((member) => text(member.properties?.native_id))
.filter((id): id is string => Boolean(id)),
}
: null;
}
const crossingNodeId = text(first.crossing_node_id);
if (!roadId || !crossingNodeId) return null;
if (!crossingNodeId) return null;
return {
kind: spec.kind,
elementId,
@@ -171,7 +223,14 @@ export function elementSelectionFor(
/** True when the click should replace the current selection rather than keep it. */
export function isSameElement(current: ElementSelection | null, next: ElementSelection | null): boolean {
return current?.elementId === next?.elementId;
if (!current || !next) return current === next;
return current.kind === 'lane-maneuver' && next.kind === 'lane-maneuver'
? current.laneId === next.laneId
: current.kind !== 'lane-maneuver' && next.kind !== 'lane-maneuver' && current.elementId === next.elementId;
}
export function isControlElement(element: ElementSelection): element is ControlElementSelection {
return element.kind !== 'lane-maneuver';
}
/**
@@ -190,7 +249,7 @@ export function isSameElement(current: ElementSelection | null, next: ElementSel
* it computes that handle's ceiling; see `direct-edit-api.md` §11.
*/
export function handleForElement<T extends { kind: string; anchor: Record<string, unknown> }>(
element: ElementSelection,
element: ControlElementSelection,
roads: readonly RoadRef[],
handles: readonly T[],
): T | null {