Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions services/rapid3-levels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = 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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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. */
Expand Down
47 changes: 46 additions & 1 deletion test/unit/services/rapid3-levels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>; nodes?: string[]; loc?: [number, number] };

Expand Down Expand Up @@ -57,10 +58,22 @@ function fakeContext(entities: Entity[]) {
map: { fitEntitiesEase: vi.fn() },
gfx: { immediateRedraw: vi.fn() }
},
modes: { 'browse': {}, 'select-osm': {}, 'draw-area': {} } as Record<string, object>,
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<string, unknown> = 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<string, string>): Entity => ({ id, type: 'node', ...(tags ? { tags } : {}) });
const way = (id: string, nodes: string[], tags?: Record<string, string>): Entity => ({ id, type: 'way', nodes, ...(tags ? { tags } : {}) });

Expand Down Expand Up @@ -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());
});
});