• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / react-map-gl / 14139551773

28 Mar 2025 11:20PM UTC coverage: 85.063% (-0.3%) from 85.368%
14139551773

push

github

web-flow
feat(mapbox): Replace shadow transform with proxied approach (#2514)

945 of 1169 branches covered (80.84%)

Branch coverage included in aggregate %.

231 of 278 new or added lines in 3 files covered. (83.09%)

5564 of 6483 relevant lines covered (85.82%)

70.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.11
/modules/react-mapbox/src/utils/transform.ts
1
import type {ViewState} from '../types/common';
1✔
2
import type {Transform} from '../types/internal';
1✔
3

1✔
4
/**
1✔
5
 * Capture a transform's current state
1✔
6
 * @param transform
1✔
7
 * @returns descriptor of the view state
1✔
8
 */
1✔
9
export function transformToViewState(tr: Transform): ViewState {
1✔
10
  return {
23✔
11
    longitude: tr.center.lng,
23✔
12
    latitude: tr.center.lat,
23✔
13
    zoom: tr._seaLevelZoom ?? tr.zoom,
23✔
14
    pitch: tr.pitch,
23✔
15
    bearing: tr.bearing,
23✔
16
    padding: tr.padding,
23✔
17
    elevation: tr._centerAltitude
23✔
18
  };
23✔
19
}
23✔
20

1✔
21
/** Returns `true` if the given props can potentially override view state updates */
1✔
22
export function isViewStateControlled(v: Partial<ViewState>): boolean {
1✔
NEW
23
  return (
×
NEW
24
    Number.isFinite(v.longitude) ||
×
NEW
25
    Number.isFinite(v.latitude) ||
×
NEW
26
    Number.isFinite(v.zoom) ||
×
NEW
27
    Number.isFinite(v.pitch) ||
×
NEW
28
    Number.isFinite(v.bearing)
×
NEW
29
  );
×
NEW
30
}
×
31

1✔
32
/**
1✔
33
 * Returns `true` if transform needs to be updated to match view state
1✔
34
 */
1✔
35
export function compareViewStateWithTransform(tr: Transform, v: Partial<ViewState>): boolean {
1✔
36
  if (Number.isFinite(v.longitude) && tr.center.lng !== v.longitude) {
52✔
37
    return true;
15✔
38
  }
15✔
39
  if (Number.isFinite(v.latitude) && tr.center.lat !== v.latitude) {
52!
NEW
40
    return true;
×
NEW
41
  }
✔
42
  if (Number.isFinite(v.bearing) && tr.bearing !== v.bearing) {
52✔
43
    return true;
1✔
44
  }
1✔
45
  if (Number.isFinite(v.pitch) && tr.pitch !== v.pitch) {
52✔
46
    return true;
1✔
47
  }
1✔
48
  if (Number.isFinite(v.zoom) && (tr._seaLevelZoom ?? tr.zoom) !== v.zoom) {
52✔
49
    return true;
1✔
50
  }
1✔
51
  if (v.padding && !tr.isPaddingEqual(v.padding)) {
52✔
52
    return true;
1✔
53
  }
1✔
54
  return false;
33✔
55
}
33✔
56

1✔
57
function noOp() {}
26✔
58

1✔
59
/* eslint-disable complexity */
1✔
60
/**
1✔
61
 * Mutate a transform to match the given view state. Should reverse `transformToViewState`
1✔
62
 * @param transform
1✔
63
 * @param viewState
1✔
64
 */
1✔
65
export function applyViewStateToTransform(tr: Transform, v: Partial<ViewState>) {
1✔
66
  // prevent constrain from running until all properties are set
53✔
67
  // eslint-disable-next-line @typescript-eslint/unbound-method
53✔
68
  const constrain = tr._constrain;
53✔
69
  // eslint-disable-next-line @typescript-eslint/unbound-method
53✔
70
  const calcMatrices = tr._calcMatrices;
53✔
71
  tr._constrain = noOp;
53✔
72
  tr._calcMatrices = noOp;
53✔
73

53✔
74
  if (Number.isFinite(v.bearing)) {
53✔
75
    tr.bearing = v.bearing;
13✔
76
  }
13✔
77
  if (Number.isFinite(v.pitch)) {
53✔
78
    tr.pitch = v.pitch;
14✔
79
  }
14✔
80
  if (v.padding && !tr.isPaddingEqual(v.padding)) {
53✔
81
    tr.padding = v.padding;
1✔
82
  }
1✔
83
  if (Number.isFinite(v.longitude) || Number.isFinite(v.latitude)) {
53✔
84
    const center = tr.center;
19✔
85
    // @ts-expect-error LngLat constructor is not typed
19✔
86
    tr._center = new center.constructor(v.longitude ?? center.lng, v.latitude ?? center.lat);
19!
87
  }
19✔
88
  if (Number.isFinite(v.zoom)) {
53✔
89
    tr._centerAltitude = v.elevation ?? 0;
19✔
90
    if (tr.elevation) {
19!
NEW
91
      tr._seaLevelZoom = v.zoom;
×
NEW
92
      const mercatorElevation = (tr.pixelsPerMeter / tr.worldSize) * tr._centerAltitude;
×
NEW
93
      const altitude = tr._mercatorZfromZoom(v.zoom);
×
NEW
94
      const minHeight = tr._mercatorZfromZoom(tr._maxZoom);
×
NEW
95
      const height = Math.max(altitude - mercatorElevation, minHeight);
×
NEW
96
      tr._setZoom(tr._zoomFromMercatorZ(height));
×
97
    } else {
19✔
98
      tr._seaLevelZoom = null;
19✔
99
      tr.zoom = v.zoom;
19✔
100
    }
19✔
101
  }
19✔
102

53✔
103
  // restore methods
53✔
104
  tr._constrain = constrain;
53✔
105
  tr._calcMatrices = calcMatrices;
53✔
106
  if (!tr._unmodified) {
53✔
107
    tr._constrain();
53✔
108
    tr._calcMatrices();
53✔
109
  }
53✔
110
}
53✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc