Skip to content

Commit 249a791

Browse files
committed
Fix scope resolution for the nested imports (internal-2460)
1 parent 2906b47 commit 249a791

File tree

4 files changed

+102
-9
lines changed

4 files changed

+102
-9
lines changed

src/style/style.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
import PauseablePlacement from './pauseable_placement';
5656
import CrossTileSymbolIndex from '../symbol/cross_tile_symbol_index';
5757
import {validateCustomStyleLayer} from './style_layer/custom_style_layer';
58-
import {isFQID, makeFQID, getNameFromFQID, getScopeFromFQID} from '../util/fqid';
58+
import {isFQID, makeFQID, getNameFromFQID, getInnerScopeFromFQID, getOuterScopeFromFQID} from '../util/fqid';
5959
import {shadowDirectionFromProperties} from '../../3d-style/render/shadow_renderer';
6060
import ModelManager from '../../3d-style/render/model_manager';
6161
import {DEFAULT_MAX_ZOOM, DEFAULT_MIN_ZOOM} from '../geo/transform';
@@ -2236,7 +2236,7 @@ class Style extends Evented<MapEvents> {
22362236
if (fragmentId == null || (fragmentId === '' && this.isRootStyle())) return this;
22372237

22382238
if (isFQID(fragmentId)) {
2239-
const scope = getScopeFromFQID(fragmentId);
2239+
const scope = getInnerScopeFromFQID(fragmentId);
22402240
const fragment = this.fragments.find(({id}) => id === scope);
22412241
assert(fragment, `Fragment with id ${scope} not found in the style.`);
22422242
if (!fragment) return undefined;
@@ -3211,13 +3211,12 @@ class Style extends Evented<MapEvents> {
32113211
const renderedFeatures = this._queryRenderedFeatures(queryGeometry, queries, transform);
32123212
const sortedFeatures = this._flattenAndSortRenderedFeatures(renderedFeatures);
32133213

3214-
const features = [];
3214+
const features: GeoJSONFeature[] = [];
32153215
for (const feature of sortedFeatures) {
3216-
const scope = getScopeFromFQID(feature.layer.id);
3216+
const scope = getOuterScopeFromFQID(feature.layer.id);
32173217
if (scope === this.scope) features.push(feature);
32183218
}
32193219

3220-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
32213220
return features;
32223221
}
32233222

src/util/fqid.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export function isFQID(id: string): boolean {
1111
return id.indexOf(FQIDSeparator) >= 0;
1212
}
1313

14-
export function makeFQID<T = string>(id: T, scope?: string | null): FQID<T> {
15-
if (!scope) return id as FQID<T>;
16-
return `${id}${FQIDSeparator}${scope}` as FQID<T>;
14+
export function makeFQID<T = string>(name: T, scope?: string | null): FQID<T> {
15+
if (!scope) return name as FQID<T>;
16+
return `${name}${FQIDSeparator}${scope}` as FQID<T>;
1717
}
1818

1919
export function getNameFromFQID<T>(fqid: FQID<T>): T;
@@ -23,7 +23,30 @@ export function getNameFromFQID<T>(fqid: FQID<T> | string): T | string {
2323
return (sep >= 0 ? fqid.slice(0, sep) : fqid);
2424
}
2525

26-
export function getScopeFromFQID(fqid: string): string {
26+
/**
27+
* Extracts the scope portion from a Fully Qualified ID.
28+
* When dealing with nested scopes, returns the rightmost scope segment.
29+
* @param fqid - The Fully Qualified ID to extract from
30+
* @returns The scope part of the FQID, or an empty string if no separator is found
31+
* @example
32+
* getInnerScopeFromFQID('name scope1') // returns 'scope1'
33+
* getInnerScopeFromFQID('name scope2 scope1') // returns 'scope1'
34+
*/
35+
export function getInnerScopeFromFQID(fqid: string): string {
36+
const sep = fqid.lastIndexOf(FQIDSeparator);
37+
return sep >= 0 ? fqid.slice(sep + 1) : '';
38+
}
39+
40+
/**
41+
* Extracts the full scope portion from a Fully Qualified ID.
42+
* When dealing with nested scopes, returns everything after the first separator, i.e. all nested scopes.
43+
* @param fqid - The Fully Qualified ID to extract from
44+
* @returns The full scope string, or an empty string if no separator is found
45+
* @example
46+
* getOuterScopeFromFQID('name scope1') // returns 'scope1'
47+
* getOuterScopeFromFQID('name scope2 scope1') // returns 'scope2 scope1'
48+
*/
49+
export function getOuterScopeFromFQID(fqid: string): string {
2750
const sep = fqid.indexOf(FQIDSeparator);
2851
return sep >= 0 ? fqid.slice(sep + 1) : '';
2952
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"version": 8,
3+
"metadata": {
4+
"test": {
5+
"width": 64,
6+
"height": 32
7+
}
8+
},
9+
"fragment": true,
10+
"schema": {
11+
"roadColor": {
12+
"default": "green",
13+
"type": "color"
14+
}
15+
},
16+
"sources": {},
17+
"layers": [],
18+
"imports": [{
19+
"id": "fragment",
20+
"url": "",
21+
"config": {
22+
"roadColor": ["config", "roadColor"]
23+
},
24+
"data": {
25+
"version": 8,
26+
"schema": {
27+
"roadColor": {
28+
"default": "blue",
29+
"type": "color"
30+
}
31+
},
32+
"imports": [{
33+
"id": "fragment",
34+
"url": "",
35+
"config": {
36+
"roadColor": ["config", "roadColor"]
37+
},
38+
"data": {
39+
"version": 8,
40+
"schema": {
41+
"roadColor": {
42+
"default": "red",
43+
"type": "color"
44+
}
45+
},
46+
"sources": {
47+
"geojson": {
48+
"type": "geojson",
49+
"data": {
50+
"type": "LineString",
51+
"coordinates": [[-15, 0], [15, 0]]
52+
}
53+
}
54+
},
55+
"layers": [
56+
{
57+
"id": "roads",
58+
"type": "line",
59+
"source": "geojson",
60+
"paint": {
61+
"line-color": ["config", "roadColor"]
62+
}
63+
}
64+
]
65+
}
66+
}],
67+
"sources": {},
68+
"layers": []
69+
}
70+
}]
71+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy