Skip to content

Commit 6e979bf

Browse files
committed
feat: make compatible with both v2 and v3
- Makes the library compatible with both `mapbox-gl` v2 and v3. The signature of `CollisionIndex.projectAndGetPerspectiveRatio` was actually changed from v2.11.1 to v2.12.0. So we have to take care of three groups: - v2.11.1 or earlier - v2.12.0 or later but before v3.0.0 - v3.0.0 or later A new script `src/private/mapbox-gl-version.ts` determines the above groups. `src/private/mapbox-types.ts` mixes up type definitions for all the versions. issue #3
1 parent 6a00b9c commit 6e979bf

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
import { Map } from 'mapbox-gl';
1111

1212
import { calculateCollisionBox } from './private/collision-index';
13+
import { getMapboxGlVersion } from './private/mapbox-gl-version';
1314
import { EXTENT, isSymbolBucket } from './private/mapbox-types';
1415
import { waitForPlacement } from './private/placement';
1516
import {
1617
getSymbolPlacementTileProjectionMatrix,
1718
} from './private/projection-util';
1819
import { evaluateSizeForZoom } from './private/symbol-size';
19-
import { Box, FeatureBox } from './types';
20+
import type { Box, FeatureBox } from './types';
2021
export { Box, FeatureBox } from './types';
2122

2223
// placement timeout in milliseconds.
@@ -59,6 +60,7 @@ export async function collectCollisionBoxesAndFeatures(
5960
layerId: string,
6061
): Promise<FeatureBox[]> {
6162
const style = map.style;
63+
const version = getMapboxGlVersion(map);
6264
const placement = style.placement;
6365
const layer = style._layers[layerId];
6466
if (layer == null) {
@@ -68,7 +70,9 @@ export async function collectCollisionBoxesAndFeatures(
6870
throw new RangeError(`layer "${layerId}" is not a symbol layer`);
6971
}
7072
await waitForPlacement(placement, PLACEMENT_TIMEOUT_IN_MS);
71-
const sourceCache = style.getOwnLayerSourceCache(layer);
73+
const sourceCache = version.isV3
74+
? style.getOwnLayerSourceCache(layer)
75+
: style._getLayerSourceCache(layer);
7276
if (sourceCache == null) {
7377
throw new Error(`no SourceCache available`);
7478
}
@@ -117,6 +121,7 @@ export async function collectCollisionBoxesAndFeatures(
117121
posMatrix,
118122
textPixelRatio,
119123
scale,
124+
version,
120125
);
121126
}
122127
}

src/private/collision-index.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { mat4 } from 'gl-matrix';
22

33
import { Box } from '../types';
4+
import type { MapboxGlVersion } from './mapbox-gl-version';
45
import {
56
Placement,
67
SingleCollisionBox,
@@ -17,6 +18,10 @@ import {
1718
* `CollisionIndex#placeCollisionBox`.
1819
* https://github.com/mapbox/mapbox-gl-js/blob/922da6dd02dbcc1ccd852cebba8b830a070d46ca/src/symbol/collision_index.js#L90-L140
1920
*
21+
* @param version
22+
*
23+
* Version information of `mapbox-gl`.
24+
*
2025
* @beta
2126
*/
2227
export function calculateCollisionBox(
@@ -27,6 +32,7 @@ export function calculateCollisionBox(
2732
posMatrix: mat4,
2833
textPixelRatio: number,
2934
scale: number,
35+
version: MapboxGlVersion,
3036
): Box {
3137
// we assumes symbols do not have a text.
3238
// so `shift` is `(0, 0)`.
@@ -55,15 +61,23 @@ export function calculateCollisionBox(
5561
bucket.projection.name === 'globe' ||
5662
!!elevation ||
5763
collisionIndex.transform.pitch > 0;
58-
const projectedPoint = collisionIndex.projectAndGetPerspectiveRatio(
59-
posMatrix,
60-
anchorX,
61-
anchorY,
62-
anchorZ,
63-
box.tileID,
64-
checkOcclusion,
65-
bucket.getProjection(),
66-
);
64+
const projectedPoint = version.isLegacy
65+
? collisionIndex.projectAndGetPerspectiveRatio(
66+
posMatrix,
67+
[anchorX, anchorY, anchorZ],
68+
box.tileID,
69+
checkOcclusion,
70+
bucket.getProjection(),
71+
)
72+
: collisionIndex.projectAndGetPerspectiveRatio(
73+
posMatrix,
74+
anchorX,
75+
anchorY,
76+
anchorZ,
77+
box.tileID,
78+
checkOcclusion,
79+
bucket.getProjection(),
80+
);
6781
const tileToViewport = textPixelRatio * projectedPoint.perspectiveRatio;
6882
const screenX = projectedPoint.point.x;
6983
const screenY = projectedPoint.point.y;

src/private/mapbox-gl-version.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Map } from 'mapbox-gl';
2+
3+
/**
4+
* Version information on `mapbox-gl`.
5+
*
6+
* @beta
7+
*/
8+
export interface MapboxGlVersion {
9+
/** Whether v3 or later. */
10+
readonly isV3: boolean;
11+
12+
/** Whether v2.11.1 or earlier. */
13+
readonly isLegacy: boolean;
14+
}
15+
16+
/**
17+
* Obtains {@link MapboxGlVersion} from a given map instance.
18+
*
19+
* @beta
20+
*/
21+
export function getMapboxGlVersion(map: Map): MapboxGlVersion {
22+
const isV3 = typeof map.style.getOwnLayerSourceCache !== 'undefined';
23+
const isLegacy = typeof map._isDragging === 'undefined';
24+
return { isV3, isLegacy }
25+
}

src/private/mapbox-types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ declare module 'mapbox-gl' {
2222
// but it should not matter because this augmentation is private anyway
2323
style: Style;
2424
painter: Painter;
25+
26+
// v2.12.0 or later
27+
_isDragging(): boolean;
2528
}
2629

2730
interface Style {
@@ -33,6 +36,9 @@ declare module 'mapbox-gl' {
3336
_serializedLayers: { [layerId: string]: StyleLayer };
3437
_availableImages: string[];
3538

39+
// v2
40+
_getLayerSourceCache(layer: StyleLayer): SourceCache | undefined;
41+
// v3
3642
getOwnLayerSourceCache(layer: StyleLayer): SourceCache | undefined;
3743
}
3844
}
@@ -54,6 +60,15 @@ export interface Placement {
5460
export interface CollisionIndex {
5561
transform: Transform;
5662

63+
// v2.11.1 or earlier
64+
projectAndGetPerspectiveRatio(
65+
posMatrix: mat4,
66+
point: vec3,
67+
tileID: OverscaledTileID | undefined | null,
68+
checkOcclusiion: boolean,
69+
bucketProjection: Projection,
70+
): ScreenAnchorPoint;
71+
// v2.12.0 or later
5772
projectAndGetPerspectiveRatio(
5873
posMatrix: mat4,
5974
x: number,

0 commit comments

Comments
 (0)