feat: edit lane turn arrows in workbench
This commit is contained in:
1 parent
8b84166e6f
commit
e48eb15827
9 files changed
+330
-28
No files matched your search
@@ -140,6 +140,19 @@ match `main` exactly. Guard once at construction, not per call site.
|
||||
while `compileGeometry()` ignores `profile.interval`. A control whose drag changes
|
||||
nothing is worse than no control.
|
||||
|
||||
### V1 override selection is not direct edit
|
||||
|
||||
`lane-maneuver` is a saved v1 override for a discrete lane attribute, not a spatial
|
||||
constraint. Its click selection and inspector selector are intentionally available
|
||||
without `directEdit`: they stage `{ kind: 'lane-maneuver', laneId, maneuver }` and
|
||||
wait for the existing save/recompile path. Do not route them through `EditSession`,
|
||||
the preview endpoint, or a draggable element outline.
|
||||
|
||||
The distinction is observable: the ordinary workbench may select a turn-arrow part
|
||||
or lane centerline and show this selector, while the `directEdit` flag still means
|
||||
there are no edit sources, manifest requests, handles, ghosts, previews, or control
|
||||
marking outlines.
|
||||
|
||||
---
|
||||
|
||||
## 10. Saving is not the same as applying
|
||||
@@ -243,4 +256,3 @@ one. `handle.constraintId` — present only when a constraint exists — is also
|
||||
- [ ] Every input channel meets at the shared exits, not a parallel path
|
||||
- [ ] Displayed bounds round inward, so an offered extreme is accepted
|
||||
- [ ] A focused field is not rewritten by an arriving preview
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
{"_example": "Fill with {\"file\": \"<path>\", \"reason\": \"<why>\"}. Put spec/research files only — no code paths. Run `python3 .trellis/scripts/get_context.py --mode packages` to list available specs. Delete this line once real entries are added."}
|
||||
{"file": ".trellis/spec/frontend/direct-edit-client.md", "reason": "Verify that new lane selection does not write baseline layers or join the drag-handle path."}
|
||||
{"file": ".trellis/spec/guides/cross-layer-thinking-guide.md", "reason": "Verify the staged override remains a compiler-valid lane-maneuver payload."}
|
||||
@@ -0,0 +1,48 @@
|
||||
# Lane Maneuver UI Design
|
||||
|
||||
## Boundary
|
||||
|
||||
This increment exposes the existing v1 `lane-maneuver` override in the workbench.
|
||||
It changes neither compiler validation nor the placement algorithms. In particular,
|
||||
`native-road-direction-arrow/v1` remains display-only because it represents repeated
|
||||
straight-ahead markings controlled by a separate interval policy.
|
||||
|
||||
## Selection Model
|
||||
|
||||
`edit/element.ts` remains the sole conversion point from clicked GeoJSON properties
|
||||
to a logical element. Its registry gains a turn-arrow entry whose group key is the
|
||||
ordered tuple `[lane_id, maneuver]`; the key is serialized with a dedicated, stable
|
||||
separator rather than inferred from `native_id`. This groups the 3/5 polygon parts
|
||||
of one visual arrow without merging arrows from different maneuvers on the same lane.
|
||||
|
||||
The current crosswalk/stop-line `ElementSelection` is junction-control-specific.
|
||||
Turn arrows instead produce a lane selection carrying `laneId`, `maneuver`, and the
|
||||
group feature ids. They deliberately do not call `handleForElement` or create an
|
||||
outline/drag target: changing a maneuver is a v1 discrete override rather than a
|
||||
v2 spatial constraint.
|
||||
|
||||
Lane centerlines already publish `native_id` equal to the compiler lane id. The
|
||||
click handler recognizes that id and creates the same lane selection, with the
|
||||
current maneuver resolved from the stored/staged override first, then from the
|
||||
clicked turn arrow if present, otherwise empty. That makes an unmarked lane editable.
|
||||
|
||||
## Inspector And Save Flow
|
||||
|
||||
The inspector owns the discrete selector. It receives the selected lane and the
|
||||
effective override list, offers the compiler's six exact canonical values plus
|
||||
empty-string removal, and stages one deterministic id per lane. The existing
|
||||
`stage` function replaces entries with equal ids, so a lane has exactly one pending
|
||||
override and save/recompile continues through the established API path.
|
||||
|
||||
This is intentionally not a direct-edit (`EditSession`) operation: no preview
|
||||
endpoint consumes lane-maneuver and a client-side visual preview would disagree with
|
||||
the authoritative compiler output. The existing save-then-recompile copy remains
|
||||
the user-visible boundary.
|
||||
|
||||
## Tests
|
||||
|
||||
Pure tests cover tuple grouping, part selection, lane-centerline selection and
|
||||
non-selection of direction arrows. A component-level test is unnecessary for the
|
||||
existing app test style; the inspector's emitted object is kept simple enough to
|
||||
exercise by extracting a pure override builder if needed. Typecheck and build cover
|
||||
the wiring.
|
||||
@@ -1 +1,2 @@
|
||||
{"_example": "Fill with {\"file\": \"<path>\", \"reason\": \"<why>\"}. Put spec/research files only — no code paths. Run `python3 .trellis/scripts/get_context.py --mode packages` to list available specs. Delete this line once real entries are added."}
|
||||
{"file": ".trellis/spec/frontend/direct-edit-client.md", "reason": "Preserve direct-edit layer and handle ownership while adding a separate v1 discrete selection path."}
|
||||
{"file": ".trellis/spec/guides/cross-layer-thinking-guide.md", "reason": "Keep the UI's staged override shape aligned with the compiler and API contract."}
|
||||
@@ -0,0 +1,16 @@
|
||||
# Implementation Plan
|
||||
|
||||
1. Generalize the element registry's group-key reader and add a lane-maneuver
|
||||
selection type for turn arrows and lane centerlines.
|
||||
2. Wire map clicks to preserve the existing control-element path while selecting
|
||||
lane arrows or centerlines through the new path.
|
||||
3. Add an inspector selector that stages a lane-maneuver override with canonical
|
||||
values and an explicit removal value.
|
||||
4. Add focused element tests for multi-property grouping and unmarked-lane entry.
|
||||
5. Run formatting, server tests, client type/unit tests, and production build.
|
||||
|
||||
## Rollback
|
||||
|
||||
The UI is isolated to `workbench/client/src/edit/element.ts`, `App.tsx`, and its
|
||||
unit tests. Reverting those files restores the previous display-only arrow behavior;
|
||||
the compiler's already-shipped v1 override contract remains untouched.
|
||||
@@ -84,3 +84,42 @@ v1 override 里已有覆盖 OSM 派生值的先例:`road` kind 覆盖 `laneCou
|
||||
接 UI 时需要的前置:导向箭头的分组键是 `lane_id + maneuver`(实测 108 个 → 28 组,
|
||||
每组 3 或 5 个 part),与斑马线的 `crossing_node_id` 不同形态,
|
||||
`element.ts` 的注册表需要扩展 `groupProperty` 为可组合的形式。
|
||||
|
||||
## UI 增量(规划中,2026-09-01)
|
||||
|
||||
### 已确认事实
|
||||
|
||||
- 工作台点击事件会传递所有命中的 feature,但只有开启 `?directEdit=1` 时才会请求
|
||||
`element.ts` 判断是否为可编辑元素;该注册表目前仅登记斑马线和停止线,故转向箭头
|
||||
会清空元素选择。
|
||||
- 路口转向箭头的 provenance 是 `native-road-turn-arrow/v1`,一个逻辑箭头由
|
||||
`lane_id + maneuver` 共同标识,并绘制为 3 或 5 个 part。
|
||||
- `lane-maneuver` 是 v1 离散 override,不是 v2 位置约束;它不能接入现有
|
||||
`ElementSelection -> handleForElement -> 拖拽手柄` 路径。
|
||||
- 沿车道重复的道路方向箭头 provenance 是 `native-road-direction-arrow/v1`,固定为
|
||||
`through`,由 32m 间距规则生成,当前没有对应的 override 合同。
|
||||
- 仅允许点击已生成的转向箭头无法满足“给 OSM 未标注车道新增箭头”:该车道没有 feature
|
||||
可点,故 UI 还必须允许从车道中心线或道路检查器选择目标车道。
|
||||
|
||||
### 拟议范围
|
||||
|
||||
1. 点击任一转向箭头 part 时,选中其完整 `lane_id + maneuver` 组,并在检查器展示
|
||||
六种合法转向及“无箭头”。
|
||||
2. 从车道中心线进入同一编辑器,使没有现存转向箭头的车道也能新增箭头。
|
||||
3. 选择值以单一、按 `laneId` upsert 的 `lane-maneuver` override 暂存;保存并重新生成后
|
||||
更新正式几何。空字符串仍表示移除。
|
||||
4. 扩展 element 注册表的分组键表达能力,但不把 v1 override 伪装成 v2 拖拽约束。
|
||||
|
||||
### 验收标准(UI)
|
||||
|
||||
- [x] 点击任一 `native-road-turn-arrow/v1` part 会选中同一
|
||||
`lane_id + maneuver` 的所有 part,且不会并入同车道的其他 maneuver。
|
||||
- [x] 点击车道中心线能选中该车道并展示相同的转向编辑器;即使该车道当前没有转向箭头,
|
||||
用户仍能选择一种转向并暂存。
|
||||
- [x] 转向编辑器只提供编译器接受的六种规范值和“无箭头”;选择会按 laneId upsert
|
||||
一个 `lane-maneuver` override,空字符串表示移除。
|
||||
- [x] “保存修改”后 override 被服务端接受;“保存并重新生成”后,编译结果反映所选
|
||||
maneuver 或移除,并可重新选择新的箭头。
|
||||
- [x] `native-road-direction-arrow/v1` 不显示转向编辑器,32m 间距及其生成数量不变。
|
||||
- [x] 既有斑马线、停止线选择及直接编辑手柄不回归;`npm run format:check`、
|
||||
`npm run test`、`npm run test:client`、`npm run test:client:unit`、`npm run build` 通过。
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
elementSelectionFor,
|
||||
groupMembers,
|
||||
handleForElement,
|
||||
isControlElement,
|
||||
orientedBoundsOf,
|
||||
specFor,
|
||||
specOfKind,
|
||||
@@ -187,7 +188,7 @@ function App() {
|
||||
* dot — no second drag path, no second constraint.
|
||||
*/
|
||||
const elementOutline = useMemo<ElementOutline | null>(() => {
|
||||
if (!directEditEnabled || !element || !state) return null;
|
||||
if (!directEditEnabled || !element || !isControlElement(element) || !state) return null;
|
||||
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*
|
||||
@@ -504,10 +505,15 @@ function App() {
|
||||
// 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 spec = specFor(properties);
|
||||
const nextElement = elementSelectionFor(
|
||||
properties,
|
||||
spec ? (state.layers[spec.layer]?.features ?? []) : (state.layers.laneCenterlines?.features ?? []),
|
||||
);
|
||||
// lane-maneuver is a regular v1 override, not a direct-edit constraint, so
|
||||
// it remains available in the shipped workbench. The flag still contains
|
||||
// control-marking outlines and all v2 handles behind the experimental path.
|
||||
setElement(directEditEnabled || nextElement?.kind === 'lane-maneuver' ? nextElement : null);
|
||||
const uid = properties.signal_uid;
|
||||
if (typeof uid === 'string') setStatus(`已选中原生红绿灯 ${uid}`);
|
||||
else if (properties.effective_style) setStatus('已选中标线,可在右侧暂存样式');
|
||||
@@ -525,7 +531,9 @@ function App() {
|
||||
<span className={editDiagnostics.some((item) => item.severity === 'error') ? 'dirty' : ''}>
|
||||
{editDiagnostics.find((item) => item.severity === 'error')?.message ||
|
||||
(element
|
||||
? `已选中${specOfKind(element.kind).label} ${element.crossingNodeId}(${element.featureIds.length} 条)`
|
||||
? element.kind === 'lane-maneuver'
|
||||
? `已选中第 ${laneIndex(element.laneId)} 车道(${element.featureIds.length} 个箭头 part)`
|
||||
: `已选中${specOfKind(element.kind).label} ${element.crossingNodeId}(${element.featureIds.length} 条)`
|
||||
: editHandles.length
|
||||
? `手柄 ${editHandles.length} 个`
|
||||
: selected
|
||||
@@ -658,6 +666,7 @@ function App() {
|
||||
state={state}
|
||||
staged={staged}
|
||||
marking={marking}
|
||||
element={element}
|
||||
stage={stage}
|
||||
status={setStatus}
|
||||
setState={setState}
|
||||
@@ -706,6 +715,7 @@ function Inspector({
|
||||
state,
|
||||
staged,
|
||||
marking,
|
||||
element,
|
||||
stage,
|
||||
status,
|
||||
setState,
|
||||
@@ -714,6 +724,7 @@ function Inspector({
|
||||
state: WorkbenchState;
|
||||
staged: Override[];
|
||||
marking: Record<string, unknown> | null;
|
||||
element: ElementSelection | null;
|
||||
stage: (item: Override) => void;
|
||||
status: (value: string) => void;
|
||||
setState: (next: WorkbenchState) => void;
|
||||
@@ -892,6 +903,7 @@ function Inspector({
|
||||
) : null;
|
||||
})}
|
||||
<MarkingEditor marking={marking} stage={stage} />
|
||||
<LaneManeuverEditor element={element} overrides={[...state.overrides.overrides, ...staged]} stage={stage} />
|
||||
<SignalEditor state={state} signal={signal} status={status} update={updateSignal} />
|
||||
<Candidates state={state} setState={setState} status={status} />
|
||||
<details>
|
||||
@@ -907,6 +919,55 @@ function Inspector({
|
||||
</>
|
||||
);
|
||||
}
|
||||
const MANEUVER_OPTIONS: Array<[string, string]> = [
|
||||
['', '无箭头'],
|
||||
['through', '直行'],
|
||||
['left', '左转'],
|
||||
['right', '右转'],
|
||||
['through;left', '直行或左转'],
|
||||
['through;right', '直行或右转'],
|
||||
['through;left;right', '直行、左转或右转'],
|
||||
];
|
||||
function LaneManeuverEditor({
|
||||
element,
|
||||
overrides,
|
||||
stage,
|
||||
}: {
|
||||
element: ElementSelection | null;
|
||||
overrides: Override[];
|
||||
stage: (item: Override) => void;
|
||||
}) {
|
||||
if (!element || element.kind !== 'lane-maneuver') return null;
|
||||
// Old override files may use arbitrary ids. Reuse the latest matching entry
|
||||
// so staging replaces it instead of leaving two conflicting maneuvers on one lane.
|
||||
const override = [...overrides]
|
||||
.reverse()
|
||||
.find((item) => item.kind === 'lane-maneuver' && item.laneId === element.laneId);
|
||||
const id = override?.id || `车道转向:${element.laneId}`;
|
||||
const maneuver = typeof override?.maneuver === 'string' ? override.maneuver : element.maneuver;
|
||||
return (
|
||||
<form>
|
||||
<hr />
|
||||
<h2>车道转向箭头</h2>
|
||||
<output>第 {laneIndex(element.laneId)} 车道</output>
|
||||
<label>
|
||||
转向
|
||||
<select
|
||||
value={maneuver}
|
||||
onChange={(event) =>
|
||||
stage({ id, kind: 'lane-maneuver', laneId: element.laneId, maneuver: event.target.value })
|
||||
}
|
||||
>
|
||||
{MANEUVER_OPTIONS.map(([value, label]) => (
|
||||
<option value={value} key={value}>
|
||||
{label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
function MarkingEditor({
|
||||
marking,
|
||||
stage,
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user