feat: select a crosswalk group by clicking any of its stripes

A crosswalk renders as six stripe features but is edited as one thing. Clicking
one now resolves the whole group, and clicking anything else drops the selection
so a box cannot outlive what it was drawn around.

The group id needed no compiler change: crosswalk:node/<crossingId> is already
stable, because compileControlMarkings() emits exactly one group per crossing
from a single candidate road. Verified against real output — 48 stripes, 8
groups of 6, nothing ungrouped, and none of the 8 stop lines mistaken for one,
since both share controlFeature() and are told apart by provenance. Grouping
therefore reads a published identifier rather than clustering on proximity,
which prd.md forbids.

Two supporting fixes in MapCanvas: the select callbacks join the existing
callback ref, and an empty click now reports background instead of returning
early. The listener is registered once, so it had been holding the first
render's closures — harmless while they only called stable setters, and not
survivable now that one of them reads current layers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
que01andClaude Opus 5 committed 2026-08-31 13:35:59 +08:00
1 parent 623481fd2c
commit 2694cad619
4 files changed
+267 -9

No files matched your search

+126
View File
@@ -0,0 +1,126 @@
import { describe, expect, it } from 'vitest';
import {
CROSSWALK_PROVENANCE,
STOP_LINE_PROVENANCE,
crosswalkElementIdOf,
elementSelectionFor,
groupMembers,
isSameElement,
type StripeFeature,
} from './element';
/** One stripe, shaped like `controlFeature()` in native-road.js. */
function stripe(crossingNodeId: string | number, part: number, roadId = 'road:way/-3246:forward'): StripeFeature {
return {
properties: {
native_id: `crosswalk:node/${crossingNodeId}:${part}`,
crossing_node_id: crossingNodeId,
road_id: roadId,
lane_id: `lane:${roadId}:1`,
direction: 'forward',
provenance: CROSSWALK_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)),
...[1, 2, 3, 4, 5, 6].map((part) => stripe(-26458, part, 'road:way/-3250:forward')),
];
describe('crosswalkElementIdOf', () => {
it('reads the group off a stripe', () => {
expect(crosswalkElementIdOf(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)));
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),
);
});
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');
});
it('rejects a stop line, which shares the same feature builder', () => {
const stopLine = { properties: { ...stripe(-26452, 1).properties, provenance: STOP_LINE_PROVENANCE } };
expect(crosswalkElementIdOf(stopLine.properties)).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();
});
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();
});
});
describe('groupMembers', () => {
it('collects exactly the stripes of one crossing', () => {
const members = groupMembers(features, 'crosswalk:node/-26452');
expect(members).toHaveLength(6);
expect(new Set(members.map((m) => m.properties?.crossing_node_id))).toEqual(new Set([-26452]));
});
it('returns nothing for a group that is not present', () => {
expect(groupMembers(features, 'crosswalk:node/-99999')).toHaveLength(0);
});
});
describe('elementSelectionFor', () => {
it('turns a click on one stripe into the whole group', () => {
const selection = elementSelectionFor(stripe(-26452, 4).properties, features);
expect(selection).toEqual({
kind: 'crosswalk',
elementId: 'crosswalk:node/-26452',
crossingNodeId: '-26452',
roadId: 'road:way/-3246:forward',
featureIds: [1, 2, 3, 4, 5, 6].map((part) => `crosswalk:node/-26452:${part}`),
});
});
it('selects the same group whichever stripe was clicked', () => {
const fromFirst = elementSelectionFor(stripe(-26452, 1).properties, features);
const fromLast = elementSelectionFor(stripe(-26452, 6).properties, features);
expect(fromFirst).toEqual(fromLast);
});
it('returns null for a click that is not a crosswalk, so the caller clears', () => {
// A road click must not leave a stale box around the previous crosswalk.
expect(elementSelectionFor({ road_id: 'road:way/-3246:forward' }, features)).toBeNull();
});
it('returns null rather than a group whose extent cannot be drawn', () => {
// Properties from one area, features from another: selecting nothing beats
// selecting a group with no members on screen.
expect(elementSelectionFor(stripe(-26452, 1).properties, [])).toBeNull();
});
});
describe('isSameElement', () => {
it('compares by group, not by identity', () => {
const a = elementSelectionFor(stripe(-26452, 1).properties, features);
const b = elementSelectionFor(stripe(-26452, 5).properties, features);
const other = elementSelectionFor(stripe(-26458, 1).properties, features);
expect(a).not.toBe(b);
expect(isSameElement(a, b)).toBe(true);
expect(isSameElement(a, other)).toBe(false);
expect(isSameElement(null, null)).toBe(true);
expect(isSameElement(a, null)).toBe(false);
});
});
+102
View File
@@ -0,0 +1,102 @@
// Clicked feature -> the crosswalk group it belongs to.
//
// A crosswalk is drawn as six separate stripe features, but it is edited as one
// thing. This module is the only place that turns a click on one stripe into the
// group, and it does so by reading identifiers the compiler already publishes —
// never by clustering on proximity, stripe count, or draw order, which is what
// `prd.md` forbids and what would break the moment two crossings sit close
// together.
//
// The group id is `crosswalk:node/<crossingId>`, which is stable because
// `compileControlMarkings()` iterates `model.crossings` and emits exactly one
// group per crossing from a single candidate road. Measured on a live area: 48
// stripes, 8 groups of 6, no crossing spanning two roads.
//
// The crossing node is NOT the junction node. `crosswalk:node/-26452` is the
// pedestrian crossing; `handle:junction-crosswalk-inset:node/1140799725:...` is
// the junction the approach belongs to. Conflating them is what made an earlier
// pass believe no group id existed.
//
// No OpenLayers import: same rule as meters.ts / projection.ts / selection.ts.
/** `provenance` the compiler stamps on every crosswalk stripe. */
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 interface ElementSelection {
kind: ElementKind;
/** `crosswalk:node/<crossingId>` — the group, not a stripe. */
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. */
featureIds: string[];
}
/** The shape this module needs off a GeoJSON feature; anything else is ignored. */
export interface StripeFeature {
properties?: Record<string, unknown> | null;
}
function text(value: unknown): string | null {
if (typeof value === 'string') return value || null;
// OSM ids arrive as numbers for real extracts and negative numbers for
// JOSM-authored data; both are legitimate and neither is a string.
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
return 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.
*/
export function crosswalkElementIdOf(properties: Record<string, unknown> | 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;
}
/** Every stripe 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);
}
/**
* 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.
*/
export function elementSelectionFor(
properties: Record<string, unknown> | null | undefined,
features: readonly StripeFeature[],
): ElementSelection | null {
const elementId = crosswalkElementIdOf(properties);
if (!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.
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',
elementId,
crossingNodeId,
roadId,
featureIds: members.map((member) => text(member.properties?.native_id)).filter((id): id is string => Boolean(id)),
};
}
/** 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;
}