diff --git a/services/rapid3-levels.ts b/services/rapid3-levels.ts index c6e6455..cc34dba 100644 --- a/services/rapid3-levels.ts +++ b/services/rapid3-levels.ts @@ -195,7 +195,10 @@ export function installFarEndFading() { /** Every entity in a Rapid graph: loaded data plus local edits (minus local deletions). */ export function graphEntities(graph: any): any[] { - const ids = new Set([...graph.base.entities.keys(), ...graph.local.entities.keys()]); + // Rapid 3.0.0-pre.4 replaced the graph's `local` cache with `localEntities()`; + // earlier builds have only `local`. + const local: Map = graph.localEntities?.() ?? graph.local?.entities ?? new Map(); + const ids = new Set([...graph.base.entities.keys(), ...local.keys()]); const entities: any[] = []; for (const id of ids) { const entity = graph.hasEntity(id); // undefined if deleted locally @@ -1084,7 +1087,7 @@ export class Rapid3LevelTools { }); this.refresh(); if (wayIds.length === 1) { - this.#context.enter('select-osm', { selection: { osm: wayIds } }); + this.#selectEntities(wayIds); } return wayIds; } @@ -1280,7 +1283,7 @@ export class Rapid3LevelTools { if (levelId) { const level = this.levels.value.find(l => l.levelId === levelId); if (level?.areaIds.length) { - this.#context.enter('select-osm', { selection: { osm: level.areaIds.map(String) } }); + this.#selectEntities(level.areaIds.map(String)); } return; } @@ -1307,7 +1310,21 @@ export class Rapid3LevelTools { if (!entity) return; this.#context.systems.map.fitEntitiesEase([entity]); - this.#context.enter('select-osm', { selection: { osm: [entityId] } }); + this.#selectEntities([entityId]); + } + + /** + * Selects OSM features in the editor. Rapid 3.0.0-pre.4 selects any data with + * one `select` mode, taking data IDs (a feature's is its entity ID); earlier + * builds select OSM features with `select-osm`. + */ + #selectEntities(entityIds: string[]) { + if (this.#context.modes?.['select-osm']) { + this.#context.enter('select-osm', { selection: { osm: entityIds } }); + } + else { + this.#context.enter('select', { selection: entityIds }); + } } /** Sets level_id on the given stops, as one undoable edit. */ diff --git a/test/unit/services/rapid3-levels.test.ts b/test/unit/services/rapid3-levels.test.ts index d2e9ec4..5b36c5a 100644 --- a/test/unit/services/rapid3-levels.test.ts +++ b/test/unit/services/rapid3-levels.test.ts @@ -14,7 +14,8 @@ import { // Rapid3LevelTools against a minimal fake of the Rapid 3 context: a two-layer // graph (base + local edits), an editor that applies the actions it's given, -// and the FilterSystem methods the level filter wraps. +// and the FilterSystem methods the level filter wraps. It's shaped like Rapid +// 3.0.0-pre.3; `fakePre4Context` reshapes it like pre.4. type Entity = { id: string; type: string; tags?: Record; nodes?: string[]; loc?: [number, number] }; @@ -57,10 +58,22 @@ function fakeContext(entities: Entity[]) { map: { fitEntitiesEase: vi.fn() }, gfx: { immediateRedraw: vi.fn() } }, + modes: { 'browse': {}, 'select-osm': {}, 'draw-area': {} } as Record, enter: vi.fn() }; } +// Rapid 3.0.0-pre.4 keeps a graph's own edits behind `localEntities()` (it has +// no `local`), and selects any data with one `select` mode, taking data IDs. +function fakePre4Context(entities: Entity[]) { + const context = fakeContext(entities); + const graph: Record = context.graph; + delete graph.local; + graph.localEntities = () => new Map(context.local); + context.modes = { 'browse': {}, 'select': {}, 'draw-area': {} }; + return context; +} + const node = (id: string, tags?: Record): Entity => ({ id, type: 'node', ...(tags ? { tags } : {}) }); const way = (id: string, nodes: string[], tags?: Record): Entity => ({ id, type: 'way', nodes, ...(tags ? { tags } : {}) }); @@ -1497,3 +1510,35 @@ describe('reference drawings', () => { expect(tools.referenceDrawing.value).toBeUndefined(); }); }); + +describe('with Rapid 3.0.0-pre.4', () => { + it('starts and lists the levels', () => { + const tools = new Rapid3LevelTools(fakePre4Context(station())); + + expect(tools.levels.value.map(level => level.levelId)).toEqual(['L1', 'L0']); + }); + + it('lists loaded and locally added features, leaving out local deletions', () => { + const context = fakePre4Context(station()); + context.local.set('n4', node('n4', { location_type: '3', level_id: 'L1' })); + context.local.set('n3', undefined); + + const ids = graphEntities(context.graph).map(entity => entity.id); + + expect(ids).toContain('a0'); + expect(ids).toContain('n4'); + expect(ids).not.toContain('n3'); + }); + + it('selects features with the select mode, by entity ID', () => { + const context = fakePre4Context(station()); + const tools = new Rapid3LevelTools(context); + + tools.select('n1'); + tools.selectLevelOutline('L0'); + + expect(context.enter).toHaveBeenCalledWith('select', { selection: ['n1'] }); + expect(context.enter).toHaveBeenCalledWith('select', { selection: ['a0'] }); + expect(context.enter).not.toHaveBeenCalledWith('select-osm', expect.anything()); + }); +});