react-bkoi-gl 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/index.cjs +137 -111
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +370 -0
- package/dist/index.d.ts +370 -2
- package/dist/index.js +1434 -2
- package/dist/index.js.map +1 -1
- package/dist/styles/react-bkoi-gl.css +1 -1
- package/package.json +62 -30
- package/dist/components/attribution-control.d.ts +0 -9
- package/dist/components/attribution-control.js +0 -20
- package/dist/components/attribution-control.js.map +0 -1
- package/dist/components/fullscreen-control.d.ts +0 -12
- package/dist/components/fullscreen-control.js +0 -21
- package/dist/components/fullscreen-control.js.map +0 -1
- package/dist/components/geolocate-control.d.ts +0 -21
- package/dist/components/geolocate-control.js +0 -46
- package/dist/components/geolocate-control.js.map +0 -1
- package/dist/components/layer.d.ts +0 -18
- package/dist/components/layer.js +0 -98
- package/dist/components/layer.js.map +0 -1
- package/dist/components/logo-control.d.ts +0 -9
- package/dist/components/logo-control.js +0 -28
- package/dist/components/logo-control.js.map +0 -1
- package/dist/components/map.d.ts +0 -27
- package/dist/components/map.js +0 -108
- package/dist/components/map.js.map +0 -1
- package/dist/components/marker.d.ts +0 -18
- package/dist/components/marker.js +0 -105
- package/dist/components/marker.js.map +0 -1
- package/dist/components/navigation-control.d.ts +0 -9
- package/dist/components/navigation-control.js +0 -19
- package/dist/components/navigation-control.js.map +0 -1
- package/dist/components/popup.d.ts +0 -15
- package/dist/components/popup.js +0 -68
- package/dist/components/popup.js.map +0 -1
- package/dist/components/scale-control.d.ts +0 -11
- package/dist/components/scale-control.js +0 -31
- package/dist/components/scale-control.js.map +0 -1
- package/dist/components/source.d.ts +0 -6
- package/dist/components/source.js +0 -95
- package/dist/components/source.js.map +0 -1
- package/dist/components/terrain-control.d.ts +0 -10
- package/dist/components/terrain-control.js +0 -19
- package/dist/components/terrain-control.js.map +0 -1
- package/dist/components/use-control.d.ts +0 -9
- package/dist/components/use-control.js +0 -30
- package/dist/components/use-control.js.map +0 -1
- package/dist/components/use-map.d.ts +0 -19
- package/dist/components/use-map.js +0 -54
- package/dist/components/use-map.js.map +0 -1
- package/dist/exports-maplibre-gl.d.ts +0 -33
- package/dist/exports-maplibre-gl.js +0 -21
- package/dist/exports-maplibre-gl.js.map +0 -1
- package/dist/maplibre/create-ref.d.ts +0 -9
- package/dist/maplibre/create-ref.js +0 -30
- package/dist/maplibre/create-ref.js.map +0 -1
- package/dist/maplibre/maplibre.d.ts +0 -81
- package/dist/maplibre/maplibre.js +0 -387
- package/dist/maplibre/maplibre.js.map +0 -1
- package/dist/styles.d.js +0 -2
- package/dist/styles.d.js.map +0 -1
- package/dist/tsconfig.build.tsbuildinfo +0 -1
- package/dist/types/common.d.ts +0 -20
- package/dist/types/common.js +0 -2
- package/dist/types/common.js.map +0 -1
- package/dist/types/events.d.ts +0 -80
- package/dist/types/events.js +0 -2
- package/dist/types/events.js.map +0 -1
- package/dist/types/internal.d.ts +0 -14
- package/dist/types/internal.js +0 -2
- package/dist/types/internal.js.map +0 -1
- package/dist/types/lib.d.ts +0 -40
- package/dist/types/lib.js +0 -2
- package/dist/types/lib.js.map +0 -1
- package/dist/types/style-spec.d.ts +0 -1
- package/dist/types/style-spec.js +0 -2
- package/dist/types/style-spec.js.map +0 -1
- package/dist/utils/apply-react-style.d.ts +0 -2
- package/dist/utils/apply-react-style.js +0 -16
- package/dist/utils/apply-react-style.js.map +0 -1
- package/dist/utils/assert.d.ts +0 -1
- package/dist/utils/assert.js +0 -6
- package/dist/utils/assert.js.map +0 -1
- package/dist/utils/compare-class-names.d.ts +0 -2
- package/dist/utils/compare-class-names.js +0 -23
- package/dist/utils/compare-class-names.js.map +0 -1
- package/dist/utils/deep-equal.d.ts +0 -15
- package/dist/utils/deep-equal.js +0 -46
- package/dist/utils/deep-equal.js.map +0 -1
- package/dist/utils/set-globals.d.ts +0 -20
- package/dist/utils/set-globals.js +0 -31
- package/dist/utils/set-globals.js.map +0 -1
- package/dist/utils/style-utils.d.ts +0 -3
- package/dist/utils/style-utils.js +0 -42
- package/dist/utils/style-utils.js.map +0 -1
- package/dist/utils/transform.d.ts +0 -18
- package/dist/utils/transform.js +0 -33
- package/dist/utils/transform.js.map +0 -1
- package/dist/utils/use-isomorphic-layout-effect.d.ts +0 -3
- package/dist/utils/use-isomorphic-layout-effect.js +0 -4
- package/dist/utils/use-isomorphic-layout-effect.js.map +0 -1
- package/src/components/attribution-control.ts +0 -56
- package/src/components/fullscreen-control.ts +0 -40
- package/src/components/geolocate-control.ts +0 -96
- package/src/components/layer.ts +0 -141
- package/src/components/logo-control.ts +0 -44
- package/src/components/map.tsx +0 -166
- package/src/components/marker.ts +0 -155
- package/src/components/navigation-control.ts +0 -28
- package/src/components/popup.ts +0 -113
- package/src/components/scale-control.ts +0 -44
- package/src/components/source.ts +0 -159
- package/src/components/terrain-control.ts +0 -29
- package/src/components/use-control.ts +0 -68
- package/src/components/use-map.tsx +0 -71
- package/src/exports-maplibre-gl.ts +0 -37
- package/src/index.ts +0 -3
- package/src/maplibre/create-ref.ts +0 -70
- package/src/maplibre/maplibre.ts +0 -624
- package/src/styles.d.ts +0 -4
- package/src/types/common.ts +0 -34
- package/src/types/events.ts +0 -133
- package/src/types/internal.ts +0 -26
- package/src/types/lib.ts +0 -84
- package/src/types/style-spec.ts +0 -34
- package/src/utils/apply-react-style.ts +0 -24
- package/src/utils/assert.ts +0 -5
- package/src/utils/compare-class-names.ts +0 -29
- package/src/utils/deep-equal.ts +0 -61
- package/src/utils/set-globals.ts +0 -51
- package/src/utils/style-utils.ts +0 -68
- package/src/utils/transform.ts +0 -58
- package/src/utils/use-isomorphic-layout-effect.ts +0 -8
package/dist/index.js
CHANGED
|
@@ -1,3 +1,1435 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/components/map.tsx
|
|
2
|
+
import * as React2 from "react";
|
|
3
|
+
import {
|
|
4
|
+
useState as useState2,
|
|
5
|
+
useRef,
|
|
6
|
+
useEffect as useEffect5,
|
|
7
|
+
useContext as useContext3,
|
|
8
|
+
useMemo as useMemo3,
|
|
9
|
+
useImperativeHandle
|
|
10
|
+
} from "react";
|
|
11
|
+
|
|
12
|
+
// src/components/use-map.tsx
|
|
13
|
+
import * as React from "react";
|
|
14
|
+
import { useState, useCallback, useMemo, useContext } from "react";
|
|
15
|
+
var MountedMapsContext = React.createContext(null);
|
|
16
|
+
var MapProvider = (props) => {
|
|
17
|
+
const [maps, setMaps] = useState({});
|
|
18
|
+
const onMapMount = useCallback((map, id = "default") => {
|
|
19
|
+
setMaps((currMaps) => {
|
|
20
|
+
if (id === "current") {
|
|
21
|
+
throw new Error("'current' cannot be used as map id");
|
|
22
|
+
}
|
|
23
|
+
if (currMaps[id]) {
|
|
24
|
+
throw new Error(`Multiple maps with the same id: ${id}`);
|
|
25
|
+
}
|
|
26
|
+
return { ...currMaps, [id]: map };
|
|
27
|
+
});
|
|
28
|
+
}, []);
|
|
29
|
+
const onMapUnmount = useCallback((id = "default") => {
|
|
30
|
+
setMaps((currMaps) => {
|
|
31
|
+
if (currMaps[id]) {
|
|
32
|
+
const nextMaps = { ...currMaps };
|
|
33
|
+
delete nextMaps[id];
|
|
34
|
+
return nextMaps;
|
|
35
|
+
}
|
|
36
|
+
return currMaps;
|
|
37
|
+
});
|
|
38
|
+
}, []);
|
|
39
|
+
return /* @__PURE__ */ React.createElement(
|
|
40
|
+
MountedMapsContext.Provider,
|
|
41
|
+
{
|
|
42
|
+
value: {
|
|
43
|
+
maps,
|
|
44
|
+
onMapMount,
|
|
45
|
+
onMapUnmount
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
props.children
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
function useMap() {
|
|
52
|
+
const maps = useContext(MountedMapsContext)?.maps;
|
|
53
|
+
const currentMap = useContext(MapContext);
|
|
54
|
+
const mapsWithCurrent = useMemo(() => {
|
|
55
|
+
return { ...maps, current: currentMap?.map };
|
|
56
|
+
}, [maps, currentMap]);
|
|
57
|
+
return mapsWithCurrent;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/utils/deep-equal.ts
|
|
61
|
+
function arePointsEqual(a, b) {
|
|
62
|
+
const ax = Array.isArray(a) ? a[0] : a ? a.x : 0;
|
|
63
|
+
const ay = Array.isArray(a) ? a[1] : a ? a.y : 0;
|
|
64
|
+
const bx = Array.isArray(b) ? b[0] : b ? b.x : 0;
|
|
65
|
+
const by = Array.isArray(b) ? b[1] : b ? b.y : 0;
|
|
66
|
+
return ax === bx && ay === by;
|
|
67
|
+
}
|
|
68
|
+
function deepEqual(a, b) {
|
|
69
|
+
if (a === b) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (!a || !b) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(a)) {
|
|
76
|
+
if (!Array.isArray(b) || a.length !== b.length) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
for (let i = 0; i < a.length; i++) {
|
|
80
|
+
if (!deepEqual(a[i], b[i])) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
} else if (Array.isArray(b)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
89
|
+
const aKeys = Object.keys(a);
|
|
90
|
+
const bKeys = Object.keys(b);
|
|
91
|
+
if (aKeys.length !== bKeys.length) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
for (const key of aKeys) {
|
|
95
|
+
if (!Object.prototype.hasOwnProperty.call(b, key)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (!deepEqual(a[key], b[key])) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/utils/transform.ts
|
|
108
|
+
function transformToViewState(tr) {
|
|
109
|
+
return {
|
|
110
|
+
longitude: tr.center.lng,
|
|
111
|
+
latitude: tr.center.lat,
|
|
112
|
+
zoom: tr.zoom,
|
|
113
|
+
pitch: tr.pitch,
|
|
114
|
+
bearing: tr.bearing,
|
|
115
|
+
padding: tr.padding
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function applyViewStateToTransform(tr, props) {
|
|
119
|
+
const v = props.viewState || props;
|
|
120
|
+
const changes = {};
|
|
121
|
+
if ("longitude" in v && "latitude" in v && (v.longitude !== tr.center.lng || v.latitude !== tr.center.lat)) {
|
|
122
|
+
const LngLat = tr.center.constructor;
|
|
123
|
+
changes.center = new LngLat(v.longitude, v.latitude);
|
|
124
|
+
}
|
|
125
|
+
if ("zoom" in v && v.zoom !== tr.zoom) {
|
|
126
|
+
changes.zoom = v.zoom;
|
|
127
|
+
}
|
|
128
|
+
if ("bearing" in v && v.bearing !== tr.bearing) {
|
|
129
|
+
changes.bearing = v.bearing;
|
|
130
|
+
}
|
|
131
|
+
if ("pitch" in v && v.pitch !== tr.pitch) {
|
|
132
|
+
changes.pitch = v.pitch;
|
|
133
|
+
}
|
|
134
|
+
if (v.padding && tr.padding && !deepEqual(v.padding, tr.padding)) {
|
|
135
|
+
changes.padding = v.padding;
|
|
136
|
+
}
|
|
137
|
+
return changes;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/utils/style-utils.ts
|
|
141
|
+
var refProps = [
|
|
142
|
+
"type",
|
|
143
|
+
"source",
|
|
144
|
+
"source-layer",
|
|
145
|
+
"minzoom",
|
|
146
|
+
"maxzoom",
|
|
147
|
+
"filter",
|
|
148
|
+
"layout"
|
|
149
|
+
];
|
|
150
|
+
function normalizeStyle(style) {
|
|
151
|
+
if (!style) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
if (typeof style === "string") {
|
|
155
|
+
return style;
|
|
156
|
+
}
|
|
157
|
+
if ("toJS" in style) {
|
|
158
|
+
style = style.toJS();
|
|
159
|
+
}
|
|
160
|
+
if (!style.layers) {
|
|
161
|
+
return style;
|
|
162
|
+
}
|
|
163
|
+
const layerIndex = {};
|
|
164
|
+
for (const layer of style.layers) {
|
|
165
|
+
layerIndex[layer.id] = layer;
|
|
166
|
+
}
|
|
167
|
+
const layers = style.layers.map((layer) => {
|
|
168
|
+
let normalizedLayer = null;
|
|
169
|
+
if ("interactive" in layer) {
|
|
170
|
+
normalizedLayer = Object.assign({}, layer);
|
|
171
|
+
delete normalizedLayer.interactive;
|
|
172
|
+
}
|
|
173
|
+
const layerRef = layerIndex[layer.ref];
|
|
174
|
+
if (layerRef) {
|
|
175
|
+
normalizedLayer = normalizedLayer || Object.assign({}, layer);
|
|
176
|
+
delete normalizedLayer.ref;
|
|
177
|
+
for (const propName of refProps) {
|
|
178
|
+
if (propName in layerRef) {
|
|
179
|
+
normalizedLayer[propName] = layerRef[propName];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return normalizedLayer || layer;
|
|
184
|
+
});
|
|
185
|
+
return { ...style, layers };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/maplibre/maplibre.ts
|
|
189
|
+
var DEFAULT_STYLE = {
|
|
190
|
+
version: 8,
|
|
191
|
+
sources: {},
|
|
192
|
+
layers: []
|
|
193
|
+
};
|
|
194
|
+
var pointerEvents = {
|
|
195
|
+
mousedown: "onMouseDown",
|
|
196
|
+
mouseup: "onMouseUp",
|
|
197
|
+
mouseover: "onMouseOver",
|
|
198
|
+
mousemove: "onMouseMove",
|
|
199
|
+
click: "onClick",
|
|
200
|
+
dblclick: "onDblClick",
|
|
201
|
+
mouseenter: "onMouseEnter",
|
|
202
|
+
mouseleave: "onMouseLeave",
|
|
203
|
+
mouseout: "onMouseOut",
|
|
204
|
+
contextmenu: "onContextMenu",
|
|
205
|
+
touchstart: "onTouchStart",
|
|
206
|
+
touchend: "onTouchEnd",
|
|
207
|
+
touchmove: "onTouchMove",
|
|
208
|
+
touchcancel: "onTouchCancel"
|
|
209
|
+
};
|
|
210
|
+
var cameraEvents = {
|
|
211
|
+
movestart: "onMoveStart",
|
|
212
|
+
move: "onMove",
|
|
213
|
+
moveend: "onMoveEnd",
|
|
214
|
+
dragstart: "onDragStart",
|
|
215
|
+
drag: "onDrag",
|
|
216
|
+
dragend: "onDragEnd",
|
|
217
|
+
zoomstart: "onZoomStart",
|
|
218
|
+
zoom: "onZoom",
|
|
219
|
+
zoomend: "onZoomEnd",
|
|
220
|
+
rotatestart: "onRotateStart",
|
|
221
|
+
rotate: "onRotate",
|
|
222
|
+
rotateend: "onRotateEnd",
|
|
223
|
+
pitchstart: "onPitchStart",
|
|
224
|
+
pitch: "onPitch",
|
|
225
|
+
pitchend: "onPitchEnd"
|
|
226
|
+
};
|
|
227
|
+
var otherEvents = {
|
|
228
|
+
wheel: "onWheel",
|
|
229
|
+
boxzoomstart: "onBoxZoomStart",
|
|
230
|
+
boxzoomend: "onBoxZoomEnd",
|
|
231
|
+
boxzoomcancel: "onBoxZoomCancel",
|
|
232
|
+
resize: "onResize",
|
|
233
|
+
load: "onLoad",
|
|
234
|
+
render: "onRender",
|
|
235
|
+
idle: "onIdle",
|
|
236
|
+
remove: "onRemove",
|
|
237
|
+
data: "onData",
|
|
238
|
+
styledata: "onStyleData",
|
|
239
|
+
sourcedata: "onSourceData",
|
|
240
|
+
error: "onError"
|
|
241
|
+
};
|
|
242
|
+
var settingNames = [
|
|
243
|
+
"minZoom",
|
|
244
|
+
"maxZoom",
|
|
245
|
+
"minPitch",
|
|
246
|
+
"maxPitch",
|
|
247
|
+
"maxBounds",
|
|
248
|
+
"projection",
|
|
249
|
+
"renderWorldCopies"
|
|
250
|
+
];
|
|
251
|
+
var handlerNames = [
|
|
252
|
+
"scrollZoom",
|
|
253
|
+
"boxZoom",
|
|
254
|
+
"dragRotate",
|
|
255
|
+
"dragPan",
|
|
256
|
+
"keyboard",
|
|
257
|
+
"doubleClickZoom",
|
|
258
|
+
"touchZoomRotate",
|
|
259
|
+
"touchPitch"
|
|
260
|
+
];
|
|
261
|
+
var Maplibre = class _Maplibre {
|
|
262
|
+
constructor(MapClass, props, container) {
|
|
263
|
+
// mapboxgl.Map instance
|
|
264
|
+
this._map = null;
|
|
265
|
+
// Internal states
|
|
266
|
+
this._internalUpdate = false;
|
|
267
|
+
this._hoveredFeatures = null;
|
|
268
|
+
this._propsedCameraUpdate = null;
|
|
269
|
+
this._styleComponents = {};
|
|
270
|
+
this._onEvent = (e) => {
|
|
271
|
+
const cb = this.props[otherEvents[e.type]];
|
|
272
|
+
if (cb) {
|
|
273
|
+
cb(e);
|
|
274
|
+
} else if (e.type === "error") {
|
|
275
|
+
console.error(e.error);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
this._onCameraEvent = (e) => {
|
|
279
|
+
if (this._internalUpdate) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
e.viewState = this._propsedCameraUpdate || transformToViewState(this._map.transform);
|
|
283
|
+
const cb = this.props[cameraEvents[e.type]];
|
|
284
|
+
if (cb) {
|
|
285
|
+
cb(e);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
this._onCameraUpdate = (tr) => {
|
|
289
|
+
if (this._internalUpdate) {
|
|
290
|
+
return tr;
|
|
291
|
+
}
|
|
292
|
+
this._propsedCameraUpdate = transformToViewState(tr);
|
|
293
|
+
return applyViewStateToTransform(tr, this.props);
|
|
294
|
+
};
|
|
295
|
+
this._onPointerEvent = (e) => {
|
|
296
|
+
if (e.type === "mousemove" || e.type === "mouseout") {
|
|
297
|
+
this._updateHover(e);
|
|
298
|
+
}
|
|
299
|
+
const cb = this.props[pointerEvents[e.type]];
|
|
300
|
+
if (cb) {
|
|
301
|
+
if (this.props.interactiveLayerIds && e.type !== "mouseover" && e.type !== "mouseout") {
|
|
302
|
+
e.features = this._hoveredFeatures || this._queryRenderedFeatures(e.point);
|
|
303
|
+
}
|
|
304
|
+
cb(e);
|
|
305
|
+
delete e.features;
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
this._MapClass = MapClass;
|
|
309
|
+
this.props = props;
|
|
310
|
+
this._initialize(container);
|
|
311
|
+
}
|
|
312
|
+
static {
|
|
313
|
+
// eslint-disable-next-line no-use-before-define
|
|
314
|
+
this.savedMaps = [];
|
|
315
|
+
}
|
|
316
|
+
get map() {
|
|
317
|
+
return this._map;
|
|
318
|
+
}
|
|
319
|
+
setProps(props) {
|
|
320
|
+
const oldProps = this.props;
|
|
321
|
+
this.props = props;
|
|
322
|
+
const settingsChanged = this._updateSettings(props, oldProps);
|
|
323
|
+
const sizeChanged = this._updateSize(props);
|
|
324
|
+
const viewStateChanged = this._updateViewState(props);
|
|
325
|
+
this._updateStyle(props, oldProps);
|
|
326
|
+
this._updateStyleComponents(props);
|
|
327
|
+
this._updateHandlers(props, oldProps);
|
|
328
|
+
if (settingsChanged || sizeChanged || viewStateChanged && !this._map.isMoving()) {
|
|
329
|
+
this.redraw();
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
static reuse(props, container) {
|
|
333
|
+
const that = _Maplibre.savedMaps.pop();
|
|
334
|
+
if (!that) {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
const map = that.map;
|
|
338
|
+
const oldContainer = map.getContainer();
|
|
339
|
+
container.className = oldContainer.className;
|
|
340
|
+
while (oldContainer.childNodes.length > 0) {
|
|
341
|
+
container.appendChild(oldContainer.childNodes[0]);
|
|
342
|
+
}
|
|
343
|
+
map._container = container;
|
|
344
|
+
const resizeObserver = map._resizeObserver;
|
|
345
|
+
if (resizeObserver) {
|
|
346
|
+
resizeObserver.disconnect();
|
|
347
|
+
resizeObserver.observe(container);
|
|
348
|
+
}
|
|
349
|
+
that.setProps({ ...props, styleDiffing: false });
|
|
350
|
+
map.resize();
|
|
351
|
+
const { initialViewState } = props;
|
|
352
|
+
if (initialViewState) {
|
|
353
|
+
if (initialViewState.bounds) {
|
|
354
|
+
map.fitBounds(initialViewState.bounds, {
|
|
355
|
+
...initialViewState.fitBoundsOptions,
|
|
356
|
+
duration: 0
|
|
357
|
+
});
|
|
358
|
+
} else {
|
|
359
|
+
that._updateViewState(initialViewState);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (map.isStyleLoaded()) {
|
|
363
|
+
map.fire("load");
|
|
364
|
+
} else {
|
|
365
|
+
map.once("style.load", () => map.fire("load"));
|
|
366
|
+
}
|
|
367
|
+
map._update();
|
|
368
|
+
return that;
|
|
369
|
+
}
|
|
370
|
+
/* eslint-disable complexity,max-statements */
|
|
371
|
+
_initialize(container) {
|
|
372
|
+
const { props } = this;
|
|
373
|
+
const { mapStyle = DEFAULT_STYLE } = props;
|
|
374
|
+
const mapOptions = {
|
|
375
|
+
...props,
|
|
376
|
+
...props.initialViewState,
|
|
377
|
+
container,
|
|
378
|
+
style: normalizeStyle(mapStyle)
|
|
379
|
+
};
|
|
380
|
+
const viewState = mapOptions.initialViewState || mapOptions.viewState || mapOptions;
|
|
381
|
+
Object.assign(mapOptions, {
|
|
382
|
+
center: [viewState.longitude || 0, viewState.latitude || 0],
|
|
383
|
+
zoom: viewState.zoom || 0,
|
|
384
|
+
pitch: viewState.pitch || 0,
|
|
385
|
+
bearing: viewState.bearing || 0
|
|
386
|
+
});
|
|
387
|
+
if (props.gl) {
|
|
388
|
+
const getContext = HTMLCanvasElement.prototype.getContext;
|
|
389
|
+
HTMLCanvasElement.prototype.getContext = () => {
|
|
390
|
+
HTMLCanvasElement.prototype.getContext = getContext;
|
|
391
|
+
return props.gl;
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
const map = new this._MapClass(mapOptions);
|
|
395
|
+
if (viewState.padding) {
|
|
396
|
+
map.setPadding(viewState.padding);
|
|
397
|
+
}
|
|
398
|
+
if (props.cursor) {
|
|
399
|
+
map.getCanvas().style.cursor = props.cursor;
|
|
400
|
+
}
|
|
401
|
+
map.transformCameraUpdate = this._onCameraUpdate;
|
|
402
|
+
map.on("style.load", () => {
|
|
403
|
+
this._styleComponents = {
|
|
404
|
+
light: map.getLight(),
|
|
405
|
+
sky: map.getSky(),
|
|
406
|
+
// @ts-ignore getProjection() does not exist in v4
|
|
407
|
+
projection: map.getProjection?.(),
|
|
408
|
+
terrain: map.getTerrain()
|
|
409
|
+
};
|
|
410
|
+
this._updateStyleComponents(this.props);
|
|
411
|
+
});
|
|
412
|
+
map.on("sourcedata", () => {
|
|
413
|
+
this._updateStyleComponents(this.props);
|
|
414
|
+
});
|
|
415
|
+
for (const eventName in pointerEvents) {
|
|
416
|
+
map.on(eventName, this._onPointerEvent);
|
|
417
|
+
}
|
|
418
|
+
for (const eventName in cameraEvents) {
|
|
419
|
+
map.on(eventName, this._onCameraEvent);
|
|
420
|
+
}
|
|
421
|
+
for (const eventName in otherEvents) {
|
|
422
|
+
map.on(eventName, this._onEvent);
|
|
423
|
+
}
|
|
424
|
+
this._map = map;
|
|
425
|
+
}
|
|
426
|
+
/* eslint-enable complexity,max-statements */
|
|
427
|
+
recycle() {
|
|
428
|
+
const container = this.map.getContainer();
|
|
429
|
+
const children = container.querySelector("[mapboxgl-children]");
|
|
430
|
+
children?.remove();
|
|
431
|
+
_Maplibre.savedMaps.push(this);
|
|
432
|
+
}
|
|
433
|
+
destroy() {
|
|
434
|
+
this._map.remove();
|
|
435
|
+
}
|
|
436
|
+
// Force redraw the map now. Typically resize() and jumpTo() is reflected in the next
|
|
437
|
+
// render cycle, which is managed by Mapbox's animation loop.
|
|
438
|
+
// This removes the synchronization issue caused by requestAnimationFrame.
|
|
439
|
+
redraw() {
|
|
440
|
+
const map = this._map;
|
|
441
|
+
if (map.style) {
|
|
442
|
+
if (map._frame) {
|
|
443
|
+
map._frame.cancel();
|
|
444
|
+
map._frame = null;
|
|
445
|
+
}
|
|
446
|
+
map._render();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
/* Trigger map resize if size is controlled
|
|
450
|
+
@param {object} nextProps
|
|
451
|
+
@returns {bool} true if size has changed
|
|
452
|
+
*/
|
|
453
|
+
_updateSize(nextProps) {
|
|
454
|
+
const { viewState } = nextProps;
|
|
455
|
+
if (viewState) {
|
|
456
|
+
const map = this._map;
|
|
457
|
+
if (viewState.width !== map.transform.width || viewState.height !== map.transform.height) {
|
|
458
|
+
map.resize();
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
// Adapted from map.jumpTo
|
|
465
|
+
/* Update camera to match props
|
|
466
|
+
@param {object} nextProps
|
|
467
|
+
@param {bool} triggerEvents - should fire camera events
|
|
468
|
+
@returns {bool} true if anything is changed
|
|
469
|
+
*/
|
|
470
|
+
_updateViewState(nextProps) {
|
|
471
|
+
const map = this._map;
|
|
472
|
+
const tr = map.transform;
|
|
473
|
+
const isMoving = map.isMoving();
|
|
474
|
+
if (!isMoving) {
|
|
475
|
+
const changes = applyViewStateToTransform(tr, nextProps);
|
|
476
|
+
if (Object.keys(changes).length > 0) {
|
|
477
|
+
this._internalUpdate = true;
|
|
478
|
+
map.jumpTo(changes);
|
|
479
|
+
this._internalUpdate = false;
|
|
480
|
+
return true;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
/* Update camera constraints and projection settings to match props
|
|
486
|
+
@param {object} nextProps
|
|
487
|
+
@param {object} currProps
|
|
488
|
+
@returns {bool} true if anything is changed
|
|
489
|
+
*/
|
|
490
|
+
_updateSettings(nextProps, currProps) {
|
|
491
|
+
const map = this._map;
|
|
492
|
+
let changed = false;
|
|
493
|
+
for (const propName of settingNames) {
|
|
494
|
+
if (propName in nextProps && !deepEqual(nextProps[propName], currProps[propName])) {
|
|
495
|
+
changed = true;
|
|
496
|
+
const setter = map[`set${propName[0].toUpperCase()}${propName.slice(1)}`];
|
|
497
|
+
setter?.call(map, nextProps[propName]);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return changed;
|
|
501
|
+
}
|
|
502
|
+
/* Update map style to match props */
|
|
503
|
+
_updateStyle(nextProps, currProps) {
|
|
504
|
+
if (nextProps.cursor !== currProps.cursor) {
|
|
505
|
+
this._map.getCanvas().style.cursor = nextProps.cursor || "";
|
|
506
|
+
}
|
|
507
|
+
if (nextProps.mapStyle !== currProps.mapStyle) {
|
|
508
|
+
const { mapStyle = DEFAULT_STYLE, styleDiffing = true } = nextProps;
|
|
509
|
+
const options = {
|
|
510
|
+
diff: styleDiffing
|
|
511
|
+
};
|
|
512
|
+
if ("localIdeographFontFamily" in nextProps) {
|
|
513
|
+
options.localIdeographFontFamily = nextProps.localIdeographFontFamily;
|
|
514
|
+
}
|
|
515
|
+
this._map.setStyle(normalizeStyle(mapStyle), options);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
/* Update fog, light, projection and terrain to match props
|
|
519
|
+
* These props are special because
|
|
520
|
+
* 1. They can not be applied right away. Certain conditions (style loaded, source loaded, etc.) must be met
|
|
521
|
+
* 2. They can be overwritten by mapStyle
|
|
522
|
+
*/
|
|
523
|
+
_updateStyleComponents({
|
|
524
|
+
light,
|
|
525
|
+
projection,
|
|
526
|
+
sky,
|
|
527
|
+
terrain
|
|
528
|
+
}) {
|
|
529
|
+
const map = this._map;
|
|
530
|
+
const currProps = this._styleComponents;
|
|
531
|
+
if (map.style._loaded) {
|
|
532
|
+
if (light && !deepEqual(light, currProps.light)) {
|
|
533
|
+
currProps.light = light;
|
|
534
|
+
map.setLight(light);
|
|
535
|
+
}
|
|
536
|
+
if (projection && !deepEqual(projection, currProps.projection) && projection !== currProps.projection?.type) {
|
|
537
|
+
currProps.projection = typeof projection === "string" ? { type: projection } : projection;
|
|
538
|
+
map.setProjection?.(currProps.projection);
|
|
539
|
+
}
|
|
540
|
+
if (sky && !deepEqual(sky, currProps.sky)) {
|
|
541
|
+
currProps.sky = sky;
|
|
542
|
+
map.setSky(sky);
|
|
543
|
+
}
|
|
544
|
+
if (terrain !== void 0 && !deepEqual(terrain, currProps.terrain)) {
|
|
545
|
+
if (!terrain || map.getSource(terrain.source)) {
|
|
546
|
+
currProps.terrain = terrain;
|
|
547
|
+
map.setTerrain(terrain);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
/* Update interaction handlers to match props */
|
|
553
|
+
_updateHandlers(nextProps, currProps) {
|
|
554
|
+
const map = this._map;
|
|
555
|
+
for (const propName of handlerNames) {
|
|
556
|
+
const newValue = nextProps[propName] ?? true;
|
|
557
|
+
const oldValue = currProps[propName] ?? true;
|
|
558
|
+
if (!deepEqual(newValue, oldValue)) {
|
|
559
|
+
if (newValue) {
|
|
560
|
+
map[propName].enable(newValue);
|
|
561
|
+
} else {
|
|
562
|
+
map[propName].disable();
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
_queryRenderedFeatures(point) {
|
|
568
|
+
const map = this._map;
|
|
569
|
+
const { interactiveLayerIds = [] } = this.props;
|
|
570
|
+
try {
|
|
571
|
+
return map.queryRenderedFeatures(point, {
|
|
572
|
+
layers: interactiveLayerIds.filter(map.getLayer.bind(map))
|
|
573
|
+
});
|
|
574
|
+
} catch {
|
|
575
|
+
return [];
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
_updateHover(e) {
|
|
579
|
+
const { props } = this;
|
|
580
|
+
const shouldTrackHoveredFeatures = props.interactiveLayerIds && (props.onMouseMove || props.onMouseEnter || props.onMouseLeave);
|
|
581
|
+
if (shouldTrackHoveredFeatures) {
|
|
582
|
+
const eventType = e.type;
|
|
583
|
+
const wasHovering = this._hoveredFeatures?.length > 0;
|
|
584
|
+
const features = this._queryRenderedFeatures(e.point);
|
|
585
|
+
const isHovering = features.length > 0;
|
|
586
|
+
if (!isHovering && wasHovering) {
|
|
587
|
+
e.type = "mouseleave";
|
|
588
|
+
this._onPointerEvent(e);
|
|
589
|
+
}
|
|
590
|
+
this._hoveredFeatures = features;
|
|
591
|
+
if (isHovering && !wasHovering) {
|
|
592
|
+
e.type = "mouseenter";
|
|
593
|
+
this._onPointerEvent(e);
|
|
594
|
+
}
|
|
595
|
+
e.type = eventType;
|
|
596
|
+
} else {
|
|
597
|
+
this._hoveredFeatures = null;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
// src/maplibre/create-ref.ts
|
|
603
|
+
var skipMethods = [
|
|
604
|
+
"setMaxBounds",
|
|
605
|
+
"setMinZoom",
|
|
606
|
+
"setMaxZoom",
|
|
607
|
+
"setMinPitch",
|
|
608
|
+
"setMaxPitch",
|
|
609
|
+
"setRenderWorldCopies",
|
|
610
|
+
"setProjection",
|
|
611
|
+
"setStyle",
|
|
612
|
+
"addSource",
|
|
613
|
+
"removeSource",
|
|
614
|
+
"addLayer",
|
|
615
|
+
"removeLayer",
|
|
616
|
+
"setLayerZoomRange",
|
|
617
|
+
"setFilter",
|
|
618
|
+
"setPaintProperty",
|
|
619
|
+
"setLayoutProperty",
|
|
620
|
+
"setLight",
|
|
621
|
+
"setTerrain",
|
|
622
|
+
"setFog",
|
|
623
|
+
"remove"
|
|
624
|
+
];
|
|
625
|
+
function createRef(mapInstance) {
|
|
626
|
+
if (!mapInstance) {
|
|
627
|
+
return null;
|
|
628
|
+
}
|
|
629
|
+
const map = mapInstance.map;
|
|
630
|
+
const result = {
|
|
631
|
+
getMap: () => map
|
|
632
|
+
};
|
|
633
|
+
for (const key of getMethodNames(map)) {
|
|
634
|
+
if (!(key in result) && !skipMethods.includes(key)) {
|
|
635
|
+
result[key] = map[key].bind(map);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return result;
|
|
639
|
+
}
|
|
640
|
+
function getMethodNames(obj) {
|
|
641
|
+
const result = /* @__PURE__ */ new Set();
|
|
642
|
+
let proto = obj;
|
|
643
|
+
while (proto) {
|
|
644
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
645
|
+
if (key[0] !== "_" && typeof obj[key] === "function" && key !== "fire" && key !== "setEventedParent") {
|
|
646
|
+
result.add(key);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
proto = Object.getPrototypeOf(proto);
|
|
650
|
+
}
|
|
651
|
+
return Array.from(result);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// src/utils/use-isomorphic-layout-effect.ts
|
|
655
|
+
import { useEffect, useLayoutEffect } from "react";
|
|
656
|
+
var useIsomorphicLayoutEffect = typeof document !== "undefined" ? useLayoutEffect : useEffect;
|
|
657
|
+
var use_isomorphic_layout_effect_default = useIsomorphicLayoutEffect;
|
|
658
|
+
|
|
659
|
+
// src/utils/set-globals.ts
|
|
660
|
+
function setGlobals(mapLib, props) {
|
|
661
|
+
const { RTLTextPlugin, maxParallelImageRequests, workerCount, workerUrl } = props;
|
|
662
|
+
if (RTLTextPlugin && mapLib.getRTLTextPluginStatus && mapLib.getRTLTextPluginStatus() === "unavailable") {
|
|
663
|
+
const { pluginUrl, lazy = true } = typeof RTLTextPlugin === "string" ? { pluginUrl: RTLTextPlugin } : RTLTextPlugin;
|
|
664
|
+
mapLib.setRTLTextPlugin(
|
|
665
|
+
pluginUrl,
|
|
666
|
+
(error) => {
|
|
667
|
+
if (error) {
|
|
668
|
+
console.error(error);
|
|
669
|
+
}
|
|
670
|
+
},
|
|
671
|
+
lazy
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
if (maxParallelImageRequests !== void 0) {
|
|
675
|
+
mapLib.setMaxParallelImageRequests(maxParallelImageRequests);
|
|
676
|
+
}
|
|
677
|
+
if (workerCount !== void 0) {
|
|
678
|
+
mapLib.setWorkerCount(workerCount);
|
|
679
|
+
}
|
|
680
|
+
if (workerUrl !== void 0) {
|
|
681
|
+
mapLib.setWorkerUrl(workerUrl);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// src/components/logo-control.ts
|
|
686
|
+
import { useEffect as useEffect3, memo } from "react";
|
|
687
|
+
|
|
688
|
+
// src/utils/apply-react-style.ts
|
|
689
|
+
var unitlessNumber = /box|flex|grid|column|lineHeight|fontWeight|opacity|order|tabSize|zIndex/;
|
|
690
|
+
function applyReactStyle(element, styles) {
|
|
691
|
+
if (!element || !styles) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const style = element.style;
|
|
695
|
+
for (const key in styles) {
|
|
696
|
+
const value = styles[key];
|
|
697
|
+
if (Number.isFinite(value) && !unitlessNumber.test(key)) {
|
|
698
|
+
style[key] = `${value}px`;
|
|
699
|
+
} else {
|
|
700
|
+
style[key] = value;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// src/components/use-control.ts
|
|
706
|
+
import { useContext as useContext2, useMemo as useMemo2, useEffect as useEffect2 } from "react";
|
|
707
|
+
function useControl(onCreate, arg1, arg2, arg3) {
|
|
708
|
+
const context = useContext2(MapContext);
|
|
709
|
+
const ctrl = useMemo2(() => onCreate(context), []);
|
|
710
|
+
useEffect2(() => {
|
|
711
|
+
const opts = arg3 || arg2 || arg1;
|
|
712
|
+
const onAdd = typeof arg1 === "function" && typeof arg2 === "function" ? arg1 : null;
|
|
713
|
+
const onRemove = typeof arg2 === "function" ? arg2 : typeof arg1 === "function" ? arg1 : null;
|
|
714
|
+
const { map } = context;
|
|
715
|
+
if (!map.hasControl(ctrl)) {
|
|
716
|
+
map.addControl(ctrl, opts?.position);
|
|
717
|
+
if (onAdd) {
|
|
718
|
+
onAdd(context);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return () => {
|
|
722
|
+
if (onRemove) {
|
|
723
|
+
onRemove(context);
|
|
724
|
+
}
|
|
725
|
+
if (map.hasControl(ctrl)) {
|
|
726
|
+
map.removeControl(ctrl);
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
}, []);
|
|
730
|
+
return ctrl;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/components/logo-control.ts
|
|
734
|
+
function _LogoControl(props) {
|
|
735
|
+
const ctrl = useControl(
|
|
736
|
+
() => {
|
|
737
|
+
const control = {
|
|
738
|
+
onAdd: (map) => {
|
|
739
|
+
if (map.getContainer) {
|
|
740
|
+
const existingLogo = map.getContainer().querySelector('a.maplibregl-ctrl-logo[href="https://www.barikoi.com"]');
|
|
741
|
+
if (existingLogo) {
|
|
742
|
+
existingLogo.remove();
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
const container = document.createElement("a");
|
|
746
|
+
container.className = "maplibregl-ctrl-logo";
|
|
747
|
+
container.href = "https://www.barikoi.com";
|
|
748
|
+
container.target = "_blank";
|
|
749
|
+
container.setAttribute("alt", "Barikoi");
|
|
750
|
+
container.setAttribute("aria-label", "Barikoi logo");
|
|
751
|
+
container.setAttribute("rel", "noopener nofollow");
|
|
752
|
+
control._container = container;
|
|
753
|
+
return container;
|
|
754
|
+
},
|
|
755
|
+
onRemove: () => {
|
|
756
|
+
delete control._container;
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
return control;
|
|
760
|
+
},
|
|
761
|
+
{ position: props.position }
|
|
762
|
+
);
|
|
763
|
+
useEffect3(() => {
|
|
764
|
+
applyReactStyle(ctrl._container, props.style);
|
|
765
|
+
}, [props.style, ctrl._container]);
|
|
766
|
+
return null;
|
|
767
|
+
}
|
|
768
|
+
var LogoControl = memo(_LogoControl);
|
|
769
|
+
|
|
770
|
+
// src/components/attribution-control.ts
|
|
771
|
+
import { useEffect as useEffect4, memo as memo2 } from "react";
|
|
772
|
+
function _AttributionControl(props) {
|
|
773
|
+
const { current: map } = useMap();
|
|
774
|
+
const ctrl = useControl(
|
|
775
|
+
({ mapLib }) => new mapLib.AttributionControl({
|
|
776
|
+
compact: true,
|
|
777
|
+
...props
|
|
778
|
+
}),
|
|
779
|
+
{ position: props.position }
|
|
780
|
+
);
|
|
781
|
+
useEffect4(() => {
|
|
782
|
+
applyReactStyle(ctrl._container, props.style);
|
|
783
|
+
if (!ctrl._container || !map) return;
|
|
784
|
+
const onLoad = () => {
|
|
785
|
+
setTimeout(() => {
|
|
786
|
+
const inner = ctrl._container.querySelector(
|
|
787
|
+
".maplibregl-ctrl-attrib-inner"
|
|
788
|
+
);
|
|
789
|
+
if (inner) {
|
|
790
|
+
inner.innerHTML = '\xA9 <a href="https://barikoi.com" target="_blank">Barikoi</a> \xA9 <a href="https://openmaptiles.org" target="_blank">OpenMapTiles</a> \xA9 <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap contributors</a>';
|
|
791
|
+
}
|
|
792
|
+
}, 0);
|
|
793
|
+
};
|
|
794
|
+
if (map.loaded()) {
|
|
795
|
+
onLoad();
|
|
796
|
+
} else {
|
|
797
|
+
map.once("load", onLoad);
|
|
798
|
+
}
|
|
799
|
+
return () => {
|
|
800
|
+
map.off("load", onLoad);
|
|
801
|
+
};
|
|
802
|
+
}, [props.style, ctrl._container, map]);
|
|
803
|
+
return null;
|
|
804
|
+
}
|
|
805
|
+
var AttributionControl = memo2(_AttributionControl);
|
|
806
|
+
|
|
807
|
+
// src/components/map.tsx
|
|
808
|
+
var MapContext = React2.createContext(null);
|
|
809
|
+
function _Map(props, ref) {
|
|
810
|
+
const mountedMapsContext = useContext3(MountedMapsContext);
|
|
811
|
+
const [mapInstance, setMapInstance] = useState2(null);
|
|
812
|
+
const containerRef = useRef();
|
|
813
|
+
const { current: contextValue } = useRef({
|
|
814
|
+
mapLib: null,
|
|
815
|
+
map: null
|
|
816
|
+
});
|
|
817
|
+
useEffect5(() => {
|
|
818
|
+
const mapLib = props.mapLib;
|
|
819
|
+
let isMounted = true;
|
|
820
|
+
let maplibre;
|
|
821
|
+
Promise.resolve(mapLib || import("maplibre-gl")).then((module) => {
|
|
822
|
+
if (!isMounted) {
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
if (!module) {
|
|
826
|
+
throw new Error("Invalid mapLib");
|
|
827
|
+
}
|
|
828
|
+
const mapboxgl = "Map" in module ? module : module.default;
|
|
829
|
+
if (!mapboxgl.Map) {
|
|
830
|
+
throw new Error("Invalid mapLib");
|
|
831
|
+
}
|
|
832
|
+
setGlobals(mapboxgl, props);
|
|
833
|
+
if (props.reuseMaps) {
|
|
834
|
+
maplibre = Maplibre.reuse(props, containerRef.current);
|
|
835
|
+
}
|
|
836
|
+
if (!maplibre) {
|
|
837
|
+
maplibre = new Maplibre(
|
|
838
|
+
mapboxgl.Map,
|
|
839
|
+
{
|
|
840
|
+
...props,
|
|
841
|
+
// @ts-ignore - attributionControl is not in the type definition but is supported by maplibre-gl
|
|
842
|
+
attributionControl: false
|
|
843
|
+
},
|
|
844
|
+
containerRef.current
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
contextValue.map = createRef(maplibre);
|
|
848
|
+
contextValue.mapLib = mapboxgl;
|
|
849
|
+
setMapInstance(maplibre);
|
|
850
|
+
mountedMapsContext?.onMapMount(contextValue.map, props.id);
|
|
851
|
+
}).catch((error) => {
|
|
852
|
+
const { onError } = props;
|
|
853
|
+
if (onError) {
|
|
854
|
+
onError({
|
|
855
|
+
type: "error",
|
|
856
|
+
target: null,
|
|
857
|
+
originalEvent: null,
|
|
858
|
+
error
|
|
859
|
+
});
|
|
860
|
+
} else {
|
|
861
|
+
console.error(error);
|
|
862
|
+
}
|
|
863
|
+
});
|
|
864
|
+
return () => {
|
|
865
|
+
isMounted = false;
|
|
866
|
+
if (maplibre) {
|
|
867
|
+
mountedMapsContext?.onMapUnmount(props.id);
|
|
868
|
+
if (props.reuseMaps) {
|
|
869
|
+
maplibre.recycle();
|
|
870
|
+
} else {
|
|
871
|
+
maplibre.destroy();
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
}, []);
|
|
876
|
+
use_isomorphic_layout_effect_default(() => {
|
|
877
|
+
if (mapInstance) {
|
|
878
|
+
mapInstance.setProps(props);
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
useImperativeHandle(ref, () => contextValue.map, [mapInstance]);
|
|
882
|
+
const style = useMemo3(
|
|
883
|
+
() => ({
|
|
884
|
+
position: "relative",
|
|
885
|
+
width: "100%",
|
|
886
|
+
height: "100%",
|
|
887
|
+
...props.style
|
|
888
|
+
}),
|
|
889
|
+
[props.style]
|
|
890
|
+
);
|
|
891
|
+
const CHILD_CONTAINER_STYLE = {
|
|
892
|
+
height: "100%"
|
|
893
|
+
};
|
|
894
|
+
return /* @__PURE__ */ React2.createElement("div", { id: props.id, ref: containerRef, style }, mapInstance && /* @__PURE__ */ React2.createElement(MapContext.Provider, { value: contextValue }, /* @__PURE__ */ React2.createElement("div", { "mapboxgl-children": "", style: CHILD_CONTAINER_STYLE }, /* @__PURE__ */ React2.createElement(LogoControl, { position: "bottom-left" }), /* @__PURE__ */ React2.createElement(AttributionControl, { position: "bottom-right" }), props.children)));
|
|
895
|
+
}
|
|
896
|
+
var Map = React2.forwardRef(_Map);
|
|
897
|
+
|
|
898
|
+
// src/components/marker.ts
|
|
899
|
+
import * as React3 from "react";
|
|
900
|
+
import { createPortal } from "react-dom";
|
|
901
|
+
import {
|
|
902
|
+
useImperativeHandle as useImperativeHandle2,
|
|
903
|
+
useEffect as useEffect6,
|
|
904
|
+
useMemo as useMemo4,
|
|
905
|
+
useRef as useRef2,
|
|
906
|
+
useContext as useContext4,
|
|
907
|
+
forwardRef as forwardRef2,
|
|
908
|
+
memo as memo3
|
|
909
|
+
} from "react";
|
|
910
|
+
|
|
911
|
+
// src/utils/compare-class-names.ts
|
|
912
|
+
function compareClassNames(prevClassName, nextClassName) {
|
|
913
|
+
if (prevClassName === nextClassName) {
|
|
914
|
+
return null;
|
|
915
|
+
}
|
|
916
|
+
const prevClassList = getClassList(prevClassName);
|
|
917
|
+
const nextClassList = getClassList(nextClassName);
|
|
918
|
+
const diff = [];
|
|
919
|
+
for (const c of nextClassList) {
|
|
920
|
+
if (!prevClassList.has(c)) {
|
|
921
|
+
diff.push(c);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
for (const c of prevClassList) {
|
|
925
|
+
if (!nextClassList.has(c)) {
|
|
926
|
+
diff.push(c);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
return diff.length === 0 ? null : diff;
|
|
930
|
+
}
|
|
931
|
+
function getClassList(className) {
|
|
932
|
+
return new Set(className ? className.trim().split(/\s+/) : []);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// src/components/marker.ts
|
|
936
|
+
var Marker = memo3(
|
|
937
|
+
forwardRef2((props, ref) => {
|
|
938
|
+
const { map, mapLib } = useContext4(MapContext);
|
|
939
|
+
const thisRef = useRef2({ props });
|
|
940
|
+
const marker = useMemo4(() => {
|
|
941
|
+
let hasChildren = false;
|
|
942
|
+
React3.Children.forEach(props.children, (el) => {
|
|
943
|
+
if (el) {
|
|
944
|
+
hasChildren = true;
|
|
945
|
+
}
|
|
946
|
+
});
|
|
947
|
+
const options = {
|
|
948
|
+
...props,
|
|
949
|
+
element: hasChildren ? document.createElement("div") : void 0
|
|
950
|
+
};
|
|
951
|
+
const mk = new mapLib.Marker(options);
|
|
952
|
+
mk.setLngLat([props.longitude, props.latitude]);
|
|
953
|
+
mk.getElement().addEventListener("click", (e) => {
|
|
954
|
+
thisRef.current.props.onClick?.({
|
|
955
|
+
type: "click",
|
|
956
|
+
target: mk,
|
|
957
|
+
originalEvent: e
|
|
958
|
+
});
|
|
959
|
+
});
|
|
960
|
+
mk.on("dragstart", (e) => {
|
|
961
|
+
const evt = e;
|
|
962
|
+
evt.lngLat = marker.getLngLat();
|
|
963
|
+
thisRef.current.props.onDragStart?.(evt);
|
|
964
|
+
});
|
|
965
|
+
mk.on("drag", (e) => {
|
|
966
|
+
const evt = e;
|
|
967
|
+
evt.lngLat = marker.getLngLat();
|
|
968
|
+
thisRef.current.props.onDrag?.(evt);
|
|
969
|
+
});
|
|
970
|
+
mk.on("dragend", (e) => {
|
|
971
|
+
const evt = e;
|
|
972
|
+
evt.lngLat = marker.getLngLat();
|
|
973
|
+
thisRef.current.props.onDragEnd?.(evt);
|
|
974
|
+
});
|
|
975
|
+
return mk;
|
|
976
|
+
}, []);
|
|
977
|
+
useEffect6(() => {
|
|
978
|
+
marker.addTo(map.getMap());
|
|
979
|
+
return () => {
|
|
980
|
+
marker.remove();
|
|
981
|
+
};
|
|
982
|
+
}, []);
|
|
983
|
+
const {
|
|
984
|
+
longitude,
|
|
985
|
+
latitude,
|
|
986
|
+
offset,
|
|
987
|
+
style,
|
|
988
|
+
draggable = false,
|
|
989
|
+
popup = null,
|
|
990
|
+
rotation = 0,
|
|
991
|
+
rotationAlignment = "auto",
|
|
992
|
+
pitchAlignment = "auto"
|
|
993
|
+
} = props;
|
|
994
|
+
useEffect6(() => {
|
|
995
|
+
applyReactStyle(marker.getElement(), style);
|
|
996
|
+
}, [style]);
|
|
997
|
+
useImperativeHandle2(ref, () => marker, []);
|
|
998
|
+
const oldProps = thisRef.current.props;
|
|
999
|
+
if (marker.getLngLat().lng !== longitude || marker.getLngLat().lat !== latitude) {
|
|
1000
|
+
marker.setLngLat([longitude, latitude]);
|
|
1001
|
+
}
|
|
1002
|
+
if (offset && !arePointsEqual(marker.getOffset(), offset)) {
|
|
1003
|
+
marker.setOffset(offset);
|
|
1004
|
+
}
|
|
1005
|
+
if (marker.isDraggable() !== draggable) {
|
|
1006
|
+
marker.setDraggable(draggable);
|
|
1007
|
+
}
|
|
1008
|
+
if (marker.getRotation() !== rotation) {
|
|
1009
|
+
marker.setRotation(rotation);
|
|
1010
|
+
}
|
|
1011
|
+
if (marker.getRotationAlignment() !== rotationAlignment) {
|
|
1012
|
+
marker.setRotationAlignment(rotationAlignment);
|
|
1013
|
+
}
|
|
1014
|
+
if (marker.getPitchAlignment() !== pitchAlignment) {
|
|
1015
|
+
marker.setPitchAlignment(pitchAlignment);
|
|
1016
|
+
}
|
|
1017
|
+
if (marker.getPopup() !== popup) {
|
|
1018
|
+
marker.setPopup(popup);
|
|
1019
|
+
}
|
|
1020
|
+
const classNameDiff = compareClassNames(
|
|
1021
|
+
oldProps.className,
|
|
1022
|
+
props.className
|
|
1023
|
+
);
|
|
1024
|
+
if (classNameDiff) {
|
|
1025
|
+
for (const c of classNameDiff) {
|
|
1026
|
+
marker.toggleClassName(c);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
thisRef.current.props = props;
|
|
1030
|
+
return createPortal(props.children, marker.getElement());
|
|
1031
|
+
})
|
|
1032
|
+
);
|
|
1033
|
+
|
|
1034
|
+
// src/components/popup.ts
|
|
1035
|
+
import { createPortal as createPortal2 } from "react-dom";
|
|
1036
|
+
import {
|
|
1037
|
+
useImperativeHandle as useImperativeHandle3,
|
|
1038
|
+
useEffect as useEffect7,
|
|
1039
|
+
useMemo as useMemo5,
|
|
1040
|
+
useRef as useRef3,
|
|
1041
|
+
useContext as useContext5,
|
|
1042
|
+
forwardRef as forwardRef3,
|
|
1043
|
+
memo as memo4
|
|
1044
|
+
} from "react";
|
|
1045
|
+
var Popup = memo4(
|
|
1046
|
+
forwardRef3((props, ref) => {
|
|
1047
|
+
const { map, mapLib } = useContext5(MapContext);
|
|
1048
|
+
const container = useMemo5(() => {
|
|
1049
|
+
return document.createElement("div");
|
|
1050
|
+
}, []);
|
|
1051
|
+
const thisRef = useRef3({ props });
|
|
1052
|
+
const popup = useMemo5(() => {
|
|
1053
|
+
const options = { ...props };
|
|
1054
|
+
const pp = new mapLib.Popup(options);
|
|
1055
|
+
pp.setLngLat([props.longitude, props.latitude]);
|
|
1056
|
+
pp.once("open", (e) => {
|
|
1057
|
+
thisRef.current.props.onOpen?.(e);
|
|
1058
|
+
});
|
|
1059
|
+
return pp;
|
|
1060
|
+
}, []);
|
|
1061
|
+
useEffect7(() => {
|
|
1062
|
+
const onClose = (e) => {
|
|
1063
|
+
thisRef.current.props.onClose?.(e);
|
|
1064
|
+
};
|
|
1065
|
+
popup.on("close", onClose);
|
|
1066
|
+
popup.setDOMContent(container).addTo(map.getMap());
|
|
1067
|
+
return () => {
|
|
1068
|
+
popup.off("close", onClose);
|
|
1069
|
+
if (popup.isOpen()) {
|
|
1070
|
+
popup.remove();
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
}, []);
|
|
1074
|
+
useEffect7(() => {
|
|
1075
|
+
applyReactStyle(popup.getElement(), props.style);
|
|
1076
|
+
}, [props.style]);
|
|
1077
|
+
useImperativeHandle3(ref, () => popup, []);
|
|
1078
|
+
if (popup.isOpen()) {
|
|
1079
|
+
const oldProps = thisRef.current.props;
|
|
1080
|
+
if (popup.getLngLat().lng !== props.longitude || popup.getLngLat().lat !== props.latitude) {
|
|
1081
|
+
popup.setLngLat([props.longitude, props.latitude]);
|
|
1082
|
+
}
|
|
1083
|
+
if (props.offset && !deepEqual(oldProps.offset, props.offset)) {
|
|
1084
|
+
popup.setOffset(props.offset);
|
|
1085
|
+
}
|
|
1086
|
+
if (oldProps.anchor !== props.anchor || oldProps.maxWidth !== props.maxWidth) {
|
|
1087
|
+
popup.options.anchor = props.anchor;
|
|
1088
|
+
popup.setMaxWidth(props.maxWidth);
|
|
1089
|
+
}
|
|
1090
|
+
const classNameDiff = compareClassNames(
|
|
1091
|
+
oldProps.className,
|
|
1092
|
+
props.className
|
|
1093
|
+
);
|
|
1094
|
+
if (classNameDiff) {
|
|
1095
|
+
for (const c of classNameDiff) {
|
|
1096
|
+
popup.toggleClassName(c);
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
thisRef.current.props = props;
|
|
1100
|
+
}
|
|
1101
|
+
return createPortal2(props.children, container);
|
|
1102
|
+
})
|
|
1103
|
+
);
|
|
1104
|
+
|
|
1105
|
+
// src/components/fullscreen-control.ts
|
|
1106
|
+
import { useEffect as useEffect8, memo as memo5 } from "react";
|
|
1107
|
+
function _FullscreenControl(props) {
|
|
1108
|
+
const ctrl = useControl(
|
|
1109
|
+
({ mapLib }) => new mapLib.FullscreenControl({
|
|
1110
|
+
container: props.containerId && document.getElementById(props.containerId)
|
|
1111
|
+
}),
|
|
1112
|
+
{ position: props.position }
|
|
1113
|
+
);
|
|
1114
|
+
useEffect8(() => {
|
|
1115
|
+
applyReactStyle(ctrl._controlContainer, props.style);
|
|
1116
|
+
}, [props.style]);
|
|
1117
|
+
return null;
|
|
1118
|
+
}
|
|
1119
|
+
var FullscreenControl = memo5(_FullscreenControl);
|
|
1120
|
+
|
|
1121
|
+
// src/components/geolocate-control.ts
|
|
1122
|
+
import {
|
|
1123
|
+
useImperativeHandle as useImperativeHandle4,
|
|
1124
|
+
useRef as useRef4,
|
|
1125
|
+
useEffect as useEffect9,
|
|
1126
|
+
forwardRef as forwardRef4,
|
|
1127
|
+
memo as memo6
|
|
1128
|
+
} from "react";
|
|
1129
|
+
function _GeolocateControl(props, ref) {
|
|
1130
|
+
const thisRef = useRef4({ props });
|
|
1131
|
+
const ctrl = useControl(
|
|
1132
|
+
({ mapLib }) => {
|
|
1133
|
+
const gc = new mapLib.GeolocateControl(props);
|
|
1134
|
+
const setupUI = gc._setupUI;
|
|
1135
|
+
gc._setupUI = () => {
|
|
1136
|
+
if (!gc._container.hasChildNodes()) {
|
|
1137
|
+
setupUI();
|
|
1138
|
+
}
|
|
1139
|
+
};
|
|
1140
|
+
gc.on("geolocate", (e) => {
|
|
1141
|
+
thisRef.current.props.onGeolocate?.(e);
|
|
1142
|
+
});
|
|
1143
|
+
gc.on("error", (e) => {
|
|
1144
|
+
thisRef.current.props.onError?.(e);
|
|
1145
|
+
});
|
|
1146
|
+
gc.on("outofmaxbounds", (e) => {
|
|
1147
|
+
thisRef.current.props.onOutOfMaxBounds?.(e);
|
|
1148
|
+
});
|
|
1149
|
+
gc.on("trackuserlocationstart", (e) => {
|
|
1150
|
+
thisRef.current.props.onTrackUserLocationStart?.(e);
|
|
1151
|
+
});
|
|
1152
|
+
gc.on("trackuserlocationend", (e) => {
|
|
1153
|
+
thisRef.current.props.onTrackUserLocationEnd?.(e);
|
|
1154
|
+
});
|
|
1155
|
+
return gc;
|
|
1156
|
+
},
|
|
1157
|
+
{ position: props.position }
|
|
1158
|
+
);
|
|
1159
|
+
thisRef.current.props = props;
|
|
1160
|
+
useImperativeHandle4(ref, () => ctrl, []);
|
|
1161
|
+
useEffect9(() => {
|
|
1162
|
+
applyReactStyle(ctrl._container, props.style);
|
|
1163
|
+
}, [props.style]);
|
|
1164
|
+
return null;
|
|
1165
|
+
}
|
|
1166
|
+
var GeolocateControl = memo6(
|
|
1167
|
+
forwardRef4(_GeolocateControl)
|
|
1168
|
+
);
|
|
1169
|
+
|
|
1170
|
+
// src/components/navigation-control.ts
|
|
1171
|
+
import { useEffect as useEffect10, memo as memo7 } from "react";
|
|
1172
|
+
function _NavigationControl(props) {
|
|
1173
|
+
const ctrl = useControl(({ mapLib }) => new mapLib.NavigationControl(props), {
|
|
1174
|
+
position: props.position
|
|
1175
|
+
});
|
|
1176
|
+
useEffect10(() => {
|
|
1177
|
+
applyReactStyle(ctrl._container, props.style);
|
|
1178
|
+
}, [props.style]);
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
var NavigationControl = memo7(_NavigationControl);
|
|
1182
|
+
|
|
1183
|
+
// src/components/scale-control.ts
|
|
1184
|
+
import { useEffect as useEffect11, useRef as useRef5, memo as memo8 } from "react";
|
|
1185
|
+
function _ScaleControl(props) {
|
|
1186
|
+
const ctrl = useControl(({ mapLib }) => new mapLib.ScaleControl(props), {
|
|
1187
|
+
position: props.position
|
|
1188
|
+
});
|
|
1189
|
+
const propsRef = useRef5(props);
|
|
1190
|
+
const prevProps = propsRef.current;
|
|
1191
|
+
propsRef.current = props;
|
|
1192
|
+
const { style } = props;
|
|
1193
|
+
if (props.maxWidth !== void 0 && props.maxWidth !== prevProps.maxWidth) {
|
|
1194
|
+
ctrl.options.maxWidth = props.maxWidth;
|
|
1195
|
+
}
|
|
1196
|
+
if (props.unit !== void 0 && props.unit !== prevProps.unit) {
|
|
1197
|
+
ctrl.setUnit(props.unit);
|
|
1198
|
+
}
|
|
1199
|
+
useEffect11(() => {
|
|
1200
|
+
applyReactStyle(ctrl._container, style);
|
|
1201
|
+
}, [style]);
|
|
1202
|
+
return null;
|
|
1203
|
+
}
|
|
1204
|
+
var ScaleControl = memo8(_ScaleControl);
|
|
1205
|
+
|
|
1206
|
+
// src/components/terrain-control.ts
|
|
1207
|
+
import { useEffect as useEffect12, memo as memo9 } from "react";
|
|
1208
|
+
function _TerrainControl(props) {
|
|
1209
|
+
const ctrl = useControl(({ mapLib }) => new mapLib.TerrainControl(props), {
|
|
1210
|
+
position: props.position
|
|
1211
|
+
});
|
|
1212
|
+
useEffect12(() => {
|
|
1213
|
+
applyReactStyle(ctrl._container, props.style);
|
|
1214
|
+
}, [props.style]);
|
|
1215
|
+
return null;
|
|
1216
|
+
}
|
|
1217
|
+
var TerrainControl = memo9(_TerrainControl);
|
|
1218
|
+
|
|
1219
|
+
// src/components/source.ts
|
|
1220
|
+
import * as React4 from "react";
|
|
1221
|
+
import {
|
|
1222
|
+
useContext as useContext6,
|
|
1223
|
+
useEffect as useEffect13,
|
|
1224
|
+
useMemo as useMemo6,
|
|
1225
|
+
useState as useState3,
|
|
1226
|
+
useRef as useRef6,
|
|
1227
|
+
cloneElement
|
|
1228
|
+
} from "react";
|
|
1229
|
+
|
|
1230
|
+
// src/utils/assert.ts
|
|
1231
|
+
function assert(condition, message) {
|
|
1232
|
+
if (!condition) {
|
|
1233
|
+
throw new Error(message);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
// src/components/source.ts
|
|
1238
|
+
var sourceCounter = 0;
|
|
1239
|
+
function createSource(map, id, props) {
|
|
1240
|
+
if (map.style && map.style._loaded) {
|
|
1241
|
+
const options = { ...props };
|
|
1242
|
+
delete options.id;
|
|
1243
|
+
delete options.children;
|
|
1244
|
+
map.addSource(id, options);
|
|
1245
|
+
return map.getSource(id);
|
|
1246
|
+
}
|
|
1247
|
+
return null;
|
|
1248
|
+
}
|
|
1249
|
+
function updateSource(source, props, prevProps) {
|
|
1250
|
+
assert(props.id === prevProps.id, "source id changed");
|
|
1251
|
+
assert(props.type === prevProps.type, "source type changed");
|
|
1252
|
+
let changedKey = "";
|
|
1253
|
+
let changedKeyCount = 0;
|
|
1254
|
+
for (const key in props) {
|
|
1255
|
+
if (key !== "children" && key !== "id" && !deepEqual(prevProps[key], props[key])) {
|
|
1256
|
+
changedKey = key;
|
|
1257
|
+
changedKeyCount++;
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
if (!changedKeyCount) {
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
const type = props.type;
|
|
1264
|
+
if (type === "geojson") {
|
|
1265
|
+
source.setData(props.data);
|
|
1266
|
+
} else if (type === "image") {
|
|
1267
|
+
source.updateImage({
|
|
1268
|
+
url: props.url,
|
|
1269
|
+
coordinates: props.coordinates
|
|
1270
|
+
});
|
|
1271
|
+
} else {
|
|
1272
|
+
switch (changedKey) {
|
|
1273
|
+
case "coordinates":
|
|
1274
|
+
source.setCoordinates?.(props.coordinates);
|
|
1275
|
+
break;
|
|
1276
|
+
case "url":
|
|
1277
|
+
source.setUrl?.(props.url);
|
|
1278
|
+
break;
|
|
1279
|
+
case "tiles":
|
|
1280
|
+
source.setTiles?.(props.tiles);
|
|
1281
|
+
break;
|
|
1282
|
+
default:
|
|
1283
|
+
console.warn(`Unable to update <Source> prop: ${changedKey}`);
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
function Source(props) {
|
|
1288
|
+
const map = useContext6(MapContext).map.getMap();
|
|
1289
|
+
const propsRef = useRef6(props);
|
|
1290
|
+
const [, setStyleLoaded] = useState3(0);
|
|
1291
|
+
const id = useMemo6(() => props.id || `jsx-source-${sourceCounter++}`, []);
|
|
1292
|
+
useEffect13(() => {
|
|
1293
|
+
if (map) {
|
|
1294
|
+
const forceUpdate = () => setTimeout(() => setStyleLoaded((version) => version + 1), 0);
|
|
1295
|
+
map.on("styledata", forceUpdate);
|
|
1296
|
+
forceUpdate();
|
|
1297
|
+
return () => {
|
|
1298
|
+
map.off("styledata", forceUpdate);
|
|
1299
|
+
if (map.style && map.style._loaded && map.getSource(id)) {
|
|
1300
|
+
const allLayers = map.getStyle()?.layers;
|
|
1301
|
+
if (allLayers) {
|
|
1302
|
+
for (const layer of allLayers) {
|
|
1303
|
+
if (layer.source === id) {
|
|
1304
|
+
map.removeLayer(layer.id);
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
map.removeSource(id);
|
|
1309
|
+
}
|
|
1310
|
+
};
|
|
1311
|
+
}
|
|
1312
|
+
return void 0;
|
|
1313
|
+
}, [map]);
|
|
1314
|
+
let source = map && map.style && map.getSource(id);
|
|
1315
|
+
if (source) {
|
|
1316
|
+
updateSource(source, props, propsRef.current);
|
|
1317
|
+
} else {
|
|
1318
|
+
source = createSource(map, id, props);
|
|
1319
|
+
}
|
|
1320
|
+
propsRef.current = props;
|
|
1321
|
+
return source && React4.Children.map(
|
|
1322
|
+
props.children,
|
|
1323
|
+
(child) => child && cloneElement(child, {
|
|
1324
|
+
source: id
|
|
1325
|
+
})
|
|
1326
|
+
) || null;
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
// src/components/layer.ts
|
|
1330
|
+
import { useContext as useContext7, useEffect as useEffect14, useMemo as useMemo7, useState as useState4, useRef as useRef7 } from "react";
|
|
1331
|
+
function updateLayer(map, id, props, prevProps) {
|
|
1332
|
+
assert(props.id === prevProps.id, "layer id changed");
|
|
1333
|
+
assert(props.type === prevProps.type, "layer type changed");
|
|
1334
|
+
if (props.type === "custom" || prevProps.type === "custom") {
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
const { layout = {}, paint = {}, filter, minzoom, maxzoom, beforeId } = props;
|
|
1338
|
+
if (beforeId !== prevProps.beforeId) {
|
|
1339
|
+
map.moveLayer(id, beforeId);
|
|
1340
|
+
}
|
|
1341
|
+
if (layout !== prevProps.layout) {
|
|
1342
|
+
const prevLayout = prevProps.layout || {};
|
|
1343
|
+
for (const key in layout) {
|
|
1344
|
+
if (!deepEqual(layout[key], prevLayout[key])) {
|
|
1345
|
+
map.setLayoutProperty(id, key, layout[key]);
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
for (const key in prevLayout) {
|
|
1349
|
+
if (!Object.prototype.hasOwnProperty.call(layout, key)) {
|
|
1350
|
+
map.setLayoutProperty(id, key, void 0);
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
if (paint !== prevProps.paint) {
|
|
1355
|
+
const prevPaint = prevProps.paint || {};
|
|
1356
|
+
for (const key in paint) {
|
|
1357
|
+
if (!deepEqual(paint[key], prevPaint[key])) {
|
|
1358
|
+
map.setPaintProperty(id, key, paint[key]);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
for (const key in prevPaint) {
|
|
1362
|
+
if (!Object.prototype.hasOwnProperty.call(paint, key)) {
|
|
1363
|
+
map.setPaintProperty(id, key, void 0);
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
if (!deepEqual(filter, prevProps.filter)) {
|
|
1368
|
+
map.setFilter(id, filter);
|
|
1369
|
+
}
|
|
1370
|
+
if (minzoom !== prevProps.minzoom || maxzoom !== prevProps.maxzoom) {
|
|
1371
|
+
map.setLayerZoomRange(id, minzoom, maxzoom);
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
function createLayer(map, id, props) {
|
|
1375
|
+
if (map.style && map.style._loaded && (!("source" in props) || map.getSource(props.source))) {
|
|
1376
|
+
const options = { ...props, id };
|
|
1377
|
+
delete options.beforeId;
|
|
1378
|
+
map.addLayer(options, props.beforeId);
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
var layerCounter = 0;
|
|
1382
|
+
function Layer(props) {
|
|
1383
|
+
const map = useContext7(MapContext).map.getMap();
|
|
1384
|
+
const propsRef = useRef7(props);
|
|
1385
|
+
const [, setStyleLoaded] = useState4(0);
|
|
1386
|
+
const id = useMemo7(() => props.id || `jsx-layer-${layerCounter++}`, []);
|
|
1387
|
+
useEffect14(() => {
|
|
1388
|
+
if (map) {
|
|
1389
|
+
const forceUpdate = () => setStyleLoaded((version) => version + 1);
|
|
1390
|
+
map.on("styledata", forceUpdate);
|
|
1391
|
+
forceUpdate();
|
|
1392
|
+
return () => {
|
|
1393
|
+
map.off("styledata", forceUpdate);
|
|
1394
|
+
if (map.style && map.style._loaded && map.getLayer(id)) {
|
|
1395
|
+
map.removeLayer(id);
|
|
1396
|
+
}
|
|
1397
|
+
};
|
|
1398
|
+
}
|
|
1399
|
+
return void 0;
|
|
1400
|
+
}, [map]);
|
|
1401
|
+
const layer = map && map.style && map.getLayer(id);
|
|
1402
|
+
if (layer) {
|
|
1403
|
+
try {
|
|
1404
|
+
updateLayer(map, id, props, propsRef.current);
|
|
1405
|
+
} catch (error) {
|
|
1406
|
+
console.warn(error);
|
|
1407
|
+
}
|
|
1408
|
+
} else {
|
|
1409
|
+
createLayer(map, id, props);
|
|
1410
|
+
}
|
|
1411
|
+
propsRef.current = props;
|
|
1412
|
+
return null;
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
// src/exports-maplibre-gl.ts
|
|
1416
|
+
var exports_maplibre_gl_default = Map;
|
|
1417
|
+
export {
|
|
1418
|
+
AttributionControl,
|
|
1419
|
+
FullscreenControl,
|
|
1420
|
+
GeolocateControl,
|
|
1421
|
+
Layer,
|
|
1422
|
+
LogoControl,
|
|
1423
|
+
Map,
|
|
1424
|
+
MapProvider,
|
|
1425
|
+
Marker,
|
|
1426
|
+
NavigationControl,
|
|
1427
|
+
Popup,
|
|
1428
|
+
ScaleControl,
|
|
1429
|
+
Source,
|
|
1430
|
+
TerrainControl,
|
|
1431
|
+
exports_maplibre_gl_default as default,
|
|
1432
|
+
useControl,
|
|
1433
|
+
useMap
|
|
1434
|
+
};
|
|
3
1435
|
//# sourceMappingURL=index.js.map
|