@geolonia/maps-react 0.5.0 → 0.7.0
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/README.md +113 -3
- package/dist/index.d.ts +111 -12
- package/dist/index.js +644 -197
- package/package.json +11 -10
package/dist/index.js
CHANGED
|
@@ -64,20 +64,11 @@ function Control({
|
|
|
64
64
|
return container ? createPortal(children, container) : null;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
// src/components/controls.tsx
|
|
67
|
+
// src/components/controls/attribution-control.tsx
|
|
68
68
|
import * as maplibregl from "maplibre-gl";
|
|
69
69
|
import { useEffect as useEffect2 } from "react";
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const position = options?.position;
|
|
73
|
-
useEffect2(() => {
|
|
74
|
-
const ctrl = factory();
|
|
75
|
-
map.addControl(ctrl, position);
|
|
76
|
-
return () => {
|
|
77
|
-
safeRemoveControl(map, ctrl);
|
|
78
|
-
};
|
|
79
|
-
}, [map, position]);
|
|
80
|
-
}
|
|
70
|
+
|
|
71
|
+
// src/components/controls/defined.ts
|
|
81
72
|
function defined(obj) {
|
|
82
73
|
const out = {};
|
|
83
74
|
for (const k of Object.keys(obj)) {
|
|
@@ -85,24 +76,332 @@ function defined(obj) {
|
|
|
85
76
|
}
|
|
86
77
|
return out;
|
|
87
78
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
79
|
+
|
|
80
|
+
// src/components/controls/attribution-control.tsx
|
|
81
|
+
function AttributionControl2({
|
|
82
|
+
position = "bottom-right",
|
|
83
|
+
compact,
|
|
84
|
+
customAttribution
|
|
93
85
|
} = {}) {
|
|
94
86
|
const map = useMap();
|
|
95
87
|
useEffect2(() => {
|
|
96
|
-
const ctrl = new maplibregl.
|
|
97
|
-
defined({
|
|
88
|
+
const ctrl = new maplibregl.AttributionControl(
|
|
89
|
+
defined({ compact, customAttribution })
|
|
98
90
|
);
|
|
99
91
|
map.addControl(ctrl, position);
|
|
100
92
|
return () => {
|
|
101
93
|
safeRemoveControl(map, ctrl);
|
|
102
94
|
};
|
|
103
|
-
}, [map, position,
|
|
95
|
+
}, [map, position, compact, customAttribution]);
|
|
104
96
|
return null;
|
|
105
97
|
}
|
|
98
|
+
|
|
99
|
+
// src/components/controls/fullscreen-control.tsx
|
|
100
|
+
import * as maplibregl2 from "maplibre-gl";
|
|
101
|
+
import { useEffect as useEffect3 } from "react";
|
|
102
|
+
function FullscreenControl2({
|
|
103
|
+
position = "top-right",
|
|
104
|
+
container
|
|
105
|
+
} = {}) {
|
|
106
|
+
const map = useMap();
|
|
107
|
+
useEffect3(() => {
|
|
108
|
+
const ctrl = new maplibregl2.FullscreenControl(defined({ container }));
|
|
109
|
+
map.addControl(ctrl, position);
|
|
110
|
+
return () => {
|
|
111
|
+
safeRemoveControl(map, ctrl);
|
|
112
|
+
};
|
|
113
|
+
}, [map, position, container]);
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/components/controls/geocoder-control.tsx
|
|
118
|
+
import { useEffect as useEffect5, useRef as useRef2, useState as useState2 } from "react";
|
|
119
|
+
|
|
120
|
+
// src/geocode.ts
|
|
121
|
+
var GeocodeError = class extends Error {
|
|
122
|
+
constructor(message, code) {
|
|
123
|
+
super(message);
|
|
124
|
+
this.name = "GeocodeError";
|
|
125
|
+
this.code = code;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
var HALF_SPAN = {
|
|
129
|
+
1: 180 / 2 ** 9,
|
|
130
|
+
2: 180 / 2 ** 12,
|
|
131
|
+
3: 180 / 2 ** 15,
|
|
132
|
+
8: 180 / 2 ** 18
|
|
133
|
+
};
|
|
134
|
+
var TIMEOUT_MS = 1e4;
|
|
135
|
+
function isValidPoint(p) {
|
|
136
|
+
return Number.isFinite(p.lat) && Number.isFinite(p.lng) && Math.abs(p.lat) <= 90 && Math.abs(p.lng) <= 180;
|
|
137
|
+
}
|
|
138
|
+
function cityPoint(r) {
|
|
139
|
+
const lngLat = r.metadata?.city?.point;
|
|
140
|
+
if (!r.city || !lngLat) return void 0;
|
|
141
|
+
return { lat: lngLat[1], lng: lngLat[0], level: 2 };
|
|
142
|
+
}
|
|
143
|
+
function toResult(r, point) {
|
|
144
|
+
const parts = [
|
|
145
|
+
[r.addr ?? "", 8],
|
|
146
|
+
[r.town ?? "", 3],
|
|
147
|
+
[r.city ?? "", 2],
|
|
148
|
+
[r.pref ?? "", 1]
|
|
149
|
+
];
|
|
150
|
+
const parsed = parts.filter(([name]) => name);
|
|
151
|
+
const included = parsed.filter(([, level]) => level <= point.level);
|
|
152
|
+
const half = HALF_SPAN[point.level] ?? HALF_SPAN[1];
|
|
153
|
+
const parsedLevel = parsed[0]?.[1] ?? 0;
|
|
154
|
+
return {
|
|
155
|
+
center: [point.lng, point.lat],
|
|
156
|
+
bounds: [
|
|
157
|
+
[point.lng - half, point.lat - half],
|
|
158
|
+
[point.lng + half, point.lat + half]
|
|
159
|
+
],
|
|
160
|
+
level: point.level,
|
|
161
|
+
address: included.map(([name]) => name).reverse().join(""),
|
|
162
|
+
partial: point.level < parsedLevel || (r.other ?? "").trim() !== ""
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
async function geocode(address) {
|
|
166
|
+
const input = typeof address === "string" ? address.trim() : "";
|
|
167
|
+
if (!input) {
|
|
168
|
+
throw new GeocodeError(
|
|
169
|
+
"geocode: an address is required.",
|
|
170
|
+
"INVALID_REQUEST"
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
let r;
|
|
174
|
+
let timer;
|
|
175
|
+
try {
|
|
176
|
+
const load = async () => {
|
|
177
|
+
const { normalize } = await import("@geolonia/normalize-japanese-addresses");
|
|
178
|
+
return normalize(input);
|
|
179
|
+
};
|
|
180
|
+
const timeout = new Promise((_, reject) => {
|
|
181
|
+
timer = setTimeout(
|
|
182
|
+
() => reject(new Error(`no answer in ${TIMEOUT_MS / 1e3}s`)),
|
|
183
|
+
TIMEOUT_MS
|
|
184
|
+
);
|
|
185
|
+
});
|
|
186
|
+
r = await Promise.race([load(), timeout]);
|
|
187
|
+
} catch (e) {
|
|
188
|
+
throw new GeocodeError(
|
|
189
|
+
`geocode: failed to load address data (${e instanceof Error ? e.message : String(e)}).`,
|
|
190
|
+
"ERROR"
|
|
191
|
+
);
|
|
192
|
+
} finally {
|
|
193
|
+
clearTimeout(timer);
|
|
194
|
+
}
|
|
195
|
+
const point = r.point ?? cityPoint(r);
|
|
196
|
+
if (!point) {
|
|
197
|
+
throw new GeocodeError(
|
|
198
|
+
`geocode: no result for "${input}".`,
|
|
199
|
+
"ZERO_RESULTS"
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
if (!isValidPoint(point)) {
|
|
203
|
+
throw new GeocodeError(
|
|
204
|
+
`geocode: the address data has invalid coordinates for "${input}".`,
|
|
205
|
+
"ERROR"
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return toResult(r, point);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// src/components/marker.tsx
|
|
212
|
+
import { GeoloniaMarker } from "@geolonia/maps-core";
|
|
213
|
+
import * as maplibregl3 from "maplibre-gl";
|
|
214
|
+
import { useEffect as useEffect4, useRef } from "react";
|
|
215
|
+
import { createPortal as createPortal2 } from "react-dom";
|
|
216
|
+
function Marker(props) {
|
|
217
|
+
const {
|
|
218
|
+
lat,
|
|
219
|
+
lng,
|
|
220
|
+
color,
|
|
221
|
+
openPopup,
|
|
222
|
+
draggable,
|
|
223
|
+
rotation,
|
|
224
|
+
offset,
|
|
225
|
+
anchor,
|
|
226
|
+
rotationAlignment,
|
|
227
|
+
pitchAlignment,
|
|
228
|
+
className,
|
|
229
|
+
children
|
|
230
|
+
} = props;
|
|
231
|
+
const map = useMap();
|
|
232
|
+
const markerRef = useRef(null);
|
|
233
|
+
const popupEl = useRef(document.createElement("div"));
|
|
234
|
+
const handlersRef = useRef({});
|
|
235
|
+
handlersRef.current = {
|
|
236
|
+
onClick: props.onClick,
|
|
237
|
+
onDragStart: props.onDragStart,
|
|
238
|
+
onDrag: props.onDrag,
|
|
239
|
+
onDragEnd: props.onDragEnd
|
|
240
|
+
};
|
|
241
|
+
useEffect4(() => {
|
|
242
|
+
const marker = new GeoloniaMarker({
|
|
243
|
+
color,
|
|
244
|
+
draggable,
|
|
245
|
+
rotation,
|
|
246
|
+
offset,
|
|
247
|
+
anchor,
|
|
248
|
+
rotationAlignment,
|
|
249
|
+
pitchAlignment,
|
|
250
|
+
className
|
|
251
|
+
}).setLngLat([lng, lat]).addTo(map);
|
|
252
|
+
if (children) {
|
|
253
|
+
marker.setPopup(
|
|
254
|
+
new maplibregl3.Popup({ offset: [0, -30] }).setDOMContent(
|
|
255
|
+
popupEl.current
|
|
256
|
+
)
|
|
257
|
+
);
|
|
258
|
+
if (openPopup) marker.togglePopup();
|
|
259
|
+
}
|
|
260
|
+
const onClickWrapper = (e) => {
|
|
261
|
+
handlersRef.current.onClick?.(e);
|
|
262
|
+
};
|
|
263
|
+
const onDragStartWrapper = (e) => {
|
|
264
|
+
handlersRef.current.onDragStart?.(e);
|
|
265
|
+
};
|
|
266
|
+
const onDragWrapper = (e) => {
|
|
267
|
+
handlersRef.current.onDrag?.(e);
|
|
268
|
+
};
|
|
269
|
+
const onDragEndWrapper = (e) => {
|
|
270
|
+
handlersRef.current.onDragEnd?.(e);
|
|
271
|
+
};
|
|
272
|
+
const element = marker.getElement();
|
|
273
|
+
element.addEventListener("click", onClickWrapper);
|
|
274
|
+
marker.on("dragstart", onDragStartWrapper);
|
|
275
|
+
marker.on("drag", onDragWrapper);
|
|
276
|
+
marker.on("dragend", onDragEndWrapper);
|
|
277
|
+
markerRef.current = marker;
|
|
278
|
+
return () => {
|
|
279
|
+
element.removeEventListener("click", onClickWrapper);
|
|
280
|
+
marker.off("dragstart", onDragStartWrapper);
|
|
281
|
+
marker.off("drag", onDragWrapper);
|
|
282
|
+
marker.off("dragend", onDragEndWrapper);
|
|
283
|
+
marker.remove();
|
|
284
|
+
};
|
|
285
|
+
}, [map, color, anchor, className]);
|
|
286
|
+
useEffect4(() => {
|
|
287
|
+
markerRef.current?.setLngLat([lng, lat]);
|
|
288
|
+
}, [lat, lng]);
|
|
289
|
+
useEffect4(() => {
|
|
290
|
+
if (draggable === void 0) return;
|
|
291
|
+
markerRef.current?.setDraggable(draggable);
|
|
292
|
+
}, [draggable]);
|
|
293
|
+
useEffect4(() => {
|
|
294
|
+
if (rotation === void 0) return;
|
|
295
|
+
markerRef.current?.setRotation(rotation);
|
|
296
|
+
}, [rotation]);
|
|
297
|
+
useEffect4(() => {
|
|
298
|
+
if (offset === void 0) return;
|
|
299
|
+
markerRef.current?.setOffset(offset);
|
|
300
|
+
}, [offset]);
|
|
301
|
+
useEffect4(() => {
|
|
302
|
+
if (rotationAlignment === void 0) return;
|
|
303
|
+
markerRef.current?.setRotationAlignment(rotationAlignment);
|
|
304
|
+
}, [rotationAlignment]);
|
|
305
|
+
useEffect4(() => {
|
|
306
|
+
if (pitchAlignment === void 0) return;
|
|
307
|
+
markerRef.current?.setPitchAlignment(pitchAlignment);
|
|
308
|
+
}, [pitchAlignment]);
|
|
309
|
+
useEffect4(() => {
|
|
310
|
+
const marker = markerRef.current;
|
|
311
|
+
const popup = marker?.getPopup();
|
|
312
|
+
if (!popup) return;
|
|
313
|
+
const isOpen = popup.isOpen();
|
|
314
|
+
if (!!openPopup !== isOpen) marker?.togglePopup();
|
|
315
|
+
}, [openPopup]);
|
|
316
|
+
return children ? createPortal2(children, popupEl.current) : null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// src/components/controls/geocoder-control.tsx
|
|
320
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
321
|
+
var DEFAULT_LABEL = "\u4F4F\u6240\u3092\u5165\u529B";
|
|
322
|
+
function createContainer() {
|
|
323
|
+
const div = document.createElement("div");
|
|
324
|
+
div.className = "maplibregl-ctrl maplibregl-ctrl-group maps-react-geocoder";
|
|
325
|
+
return div;
|
|
326
|
+
}
|
|
327
|
+
function GeocoderControl({
|
|
328
|
+
position = "top-left",
|
|
329
|
+
placeholder = DEFAULT_LABEL,
|
|
330
|
+
marker = true,
|
|
331
|
+
fitBounds = true,
|
|
332
|
+
onResult,
|
|
333
|
+
onError
|
|
334
|
+
} = {}) {
|
|
335
|
+
const map = useMap();
|
|
336
|
+
const [input, setInput] = useState2("");
|
|
337
|
+
const [found, setFound] = useState2(null);
|
|
338
|
+
const [failed, setFailed] = useState2(false);
|
|
339
|
+
const seqRef = useRef2(0);
|
|
340
|
+
const aliveRef = useRef2(true);
|
|
341
|
+
useEffect5(() => {
|
|
342
|
+
aliveRef.current = true;
|
|
343
|
+
return () => {
|
|
344
|
+
aliveRef.current = false;
|
|
345
|
+
};
|
|
346
|
+
}, []);
|
|
347
|
+
const latest = useRef2({ fitBounds, onResult, onError });
|
|
348
|
+
useEffect5(() => {
|
|
349
|
+
latest.current = { fitBounds, onResult, onError };
|
|
350
|
+
});
|
|
351
|
+
async function search(e) {
|
|
352
|
+
e.preventDefault();
|
|
353
|
+
const address = input;
|
|
354
|
+
const seq = ++seqRef.current;
|
|
355
|
+
let result;
|
|
356
|
+
try {
|
|
357
|
+
result = await geocode(address);
|
|
358
|
+
} catch (err) {
|
|
359
|
+
if (!aliveRef.current || seq !== seqRef.current) return;
|
|
360
|
+
setFailed(true);
|
|
361
|
+
latest.current.onError?.(
|
|
362
|
+
err instanceof GeocodeError ? err : new GeocodeError(String(err), "ERROR")
|
|
363
|
+
);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (!aliveRef.current || seq !== seqRef.current) return;
|
|
367
|
+
setFailed(false);
|
|
368
|
+
setFound(result);
|
|
369
|
+
if (latest.current.fitBounds) map.fitBounds(result.bounds);
|
|
370
|
+
latest.current.onResult?.(result, address);
|
|
371
|
+
}
|
|
372
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
373
|
+
/* @__PURE__ */ jsx(Control, { position, onAdd: createContainer, children: /* @__PURE__ */ jsxs(
|
|
374
|
+
"form",
|
|
375
|
+
{
|
|
376
|
+
onSubmit: search,
|
|
377
|
+
style: { display: "flex", gap: 4, padding: 4, background: "#fff" },
|
|
378
|
+
children: [
|
|
379
|
+
/* @__PURE__ */ jsx(
|
|
380
|
+
"input",
|
|
381
|
+
{
|
|
382
|
+
type: "search",
|
|
383
|
+
value: input,
|
|
384
|
+
placeholder,
|
|
385
|
+
"aria-label": placeholder || DEFAULT_LABEL,
|
|
386
|
+
"aria-invalid": failed,
|
|
387
|
+
onChange: (e) => {
|
|
388
|
+
setInput(e.target.value);
|
|
389
|
+
setFailed(false);
|
|
390
|
+
},
|
|
391
|
+
style: { border: "1px solid #ccc", padding: "4px 8px" }
|
|
392
|
+
}
|
|
393
|
+
),
|
|
394
|
+
/* @__PURE__ */ jsx("button", { type: "submit", children: "\u691C\u7D22" })
|
|
395
|
+
]
|
|
396
|
+
}
|
|
397
|
+
) }),
|
|
398
|
+
marker && found && /* @__PURE__ */ jsx(Marker, { lng: found.center[0], lat: found.center[1] })
|
|
399
|
+
] });
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// src/components/controls/geolocate-control.tsx
|
|
403
|
+
import * as maplibregl4 from "maplibre-gl";
|
|
404
|
+
import { useEffect as useEffect6 } from "react";
|
|
106
405
|
function GeolocateControl2({
|
|
107
406
|
position = "top-right",
|
|
108
407
|
positionOptions,
|
|
@@ -112,8 +411,8 @@ function GeolocateControl2({
|
|
|
112
411
|
showUserLocation
|
|
113
412
|
} = {}) {
|
|
114
413
|
const map = useMap();
|
|
115
|
-
|
|
116
|
-
const ctrl = new
|
|
414
|
+
useEffect6(() => {
|
|
415
|
+
const ctrl = new maplibregl4.GeolocateControl(
|
|
117
416
|
defined({
|
|
118
417
|
positionOptions,
|
|
119
418
|
fitBoundsOptions,
|
|
@@ -137,55 +436,75 @@ function GeolocateControl2({
|
|
|
137
436
|
]);
|
|
138
437
|
return null;
|
|
139
438
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
439
|
+
|
|
440
|
+
// src/components/controls/globe-control.tsx
|
|
441
|
+
import * as maplibregl5 from "maplibre-gl";
|
|
442
|
+
|
|
443
|
+
// src/components/controls/use-control.ts
|
|
444
|
+
import { useEffect as useEffect7 } from "react";
|
|
445
|
+
function useControl(factory, options) {
|
|
144
446
|
const map = useMap();
|
|
145
|
-
|
|
146
|
-
|
|
447
|
+
const position = options?.position;
|
|
448
|
+
useEffect7(() => {
|
|
449
|
+
const ctrl = factory();
|
|
147
450
|
map.addControl(ctrl, position);
|
|
148
451
|
return () => {
|
|
149
452
|
safeRemoveControl(map, ctrl);
|
|
150
453
|
};
|
|
151
|
-
}, [map, position
|
|
454
|
+
}, [map, position]);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// src/components/controls/globe-control.tsx
|
|
458
|
+
function GlobeControl2({
|
|
459
|
+
position = "top-right"
|
|
460
|
+
} = {}) {
|
|
461
|
+
useControl(() => new maplibregl5.GlobeControl(), { position });
|
|
152
462
|
return null;
|
|
153
463
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
464
|
+
|
|
465
|
+
// src/components/controls/navigation-control.tsx
|
|
466
|
+
import * as maplibregl6 from "maplibre-gl";
|
|
467
|
+
import { useEffect as useEffect8 } from "react";
|
|
468
|
+
function NavigationControl2({
|
|
469
|
+
position = "top-right",
|
|
470
|
+
showCompass,
|
|
471
|
+
showZoom,
|
|
472
|
+
visualizePitch
|
|
158
473
|
} = {}) {
|
|
159
474
|
const map = useMap();
|
|
160
|
-
|
|
161
|
-
const ctrl = new
|
|
475
|
+
useEffect8(() => {
|
|
476
|
+
const ctrl = new maplibregl6.NavigationControl(
|
|
477
|
+
defined({ showCompass, showZoom, visualizePitch })
|
|
478
|
+
);
|
|
162
479
|
map.addControl(ctrl, position);
|
|
163
480
|
return () => {
|
|
164
481
|
safeRemoveControl(map, ctrl);
|
|
165
482
|
};
|
|
166
|
-
}, [map, position,
|
|
483
|
+
}, [map, position, showCompass, showZoom, visualizePitch]);
|
|
167
484
|
return null;
|
|
168
485
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
486
|
+
|
|
487
|
+
// src/components/controls/scale-control.tsx
|
|
488
|
+
import * as maplibregl7 from "maplibre-gl";
|
|
489
|
+
import { useEffect as useEffect9 } from "react";
|
|
490
|
+
function ScaleControl2({
|
|
491
|
+
position = "bottom-left",
|
|
492
|
+
maxWidth,
|
|
493
|
+
unit
|
|
173
494
|
} = {}) {
|
|
174
495
|
const map = useMap();
|
|
175
|
-
|
|
176
|
-
const ctrl = new
|
|
177
|
-
defined({ compact, customAttribution })
|
|
178
|
-
);
|
|
496
|
+
useEffect9(() => {
|
|
497
|
+
const ctrl = new maplibregl7.ScaleControl(defined({ maxWidth, unit }));
|
|
179
498
|
map.addControl(ctrl, position);
|
|
180
499
|
return () => {
|
|
181
500
|
safeRemoveControl(map, ctrl);
|
|
182
501
|
};
|
|
183
|
-
}, [map, position,
|
|
502
|
+
}, [map, position, maxWidth, unit]);
|
|
184
503
|
return null;
|
|
185
504
|
}
|
|
186
505
|
|
|
187
506
|
// src/components/layer.tsx
|
|
188
|
-
import { useEffect as
|
|
507
|
+
import { useEffect as useEffect10, useRef as useRef3 } from "react";
|
|
189
508
|
|
|
190
509
|
// src/source-context.ts
|
|
191
510
|
import { createContext as createContext2, useContext as useContext2 } from "react";
|
|
@@ -230,8 +549,19 @@ function Layer({ beforeId, ...layerSpec }) {
|
|
|
230
549
|
const map = useMap();
|
|
231
550
|
const ctxSourceId = useSourceId();
|
|
232
551
|
const spec = ctxSourceId && !("source" in layerSpec && layerSpec.source) ? { ...layerSpec, source: ctxSourceId } : layerSpec;
|
|
233
|
-
|
|
234
|
-
|
|
552
|
+
if (spec.type !== "background" && !("source" in spec && spec.source)) {
|
|
553
|
+
throw new Error(
|
|
554
|
+
`<Layer id="${layerSpec.id}"> needs a source: put it inside <Source>, or pass the source prop.`
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
const specRef = useRef3(spec);
|
|
558
|
+
specRef.current = spec;
|
|
559
|
+
const beforeIdRef = useRef3(beforeId);
|
|
560
|
+
beforeIdRef.current = beforeId;
|
|
561
|
+
const insideSourceRef = useRef3(false);
|
|
562
|
+
insideSourceRef.current = Boolean(ctxSourceId);
|
|
563
|
+
const prevRef = useRef3({});
|
|
564
|
+
useEffect10(() => {
|
|
235
565
|
map.addLayer(spec, beforeId);
|
|
236
566
|
prevRef.current = {
|
|
237
567
|
paint: layerSpec.paint ?? void 0,
|
|
@@ -250,7 +580,37 @@ function Layer({ beforeId, ...layerSpec }) {
|
|
|
250
580
|
}
|
|
251
581
|
};
|
|
252
582
|
}, [map, layerSpec.id]);
|
|
253
|
-
|
|
583
|
+
useEffect10(() => {
|
|
584
|
+
const id = layerSpec.id;
|
|
585
|
+
function restore() {
|
|
586
|
+
if (insideSourceRef.current || !map.isStyleLoaded()) return;
|
|
587
|
+
const current = specRef.current;
|
|
588
|
+
try {
|
|
589
|
+
if (map.getLayer(id)) return;
|
|
590
|
+
if (current.source && !map.getSource(current.source)) return;
|
|
591
|
+
const before = beforeIdRef.current;
|
|
592
|
+
map.addLayer(
|
|
593
|
+
current,
|
|
594
|
+
before && map.getLayer(before) ? before : void 0
|
|
595
|
+
);
|
|
596
|
+
prevRef.current = {
|
|
597
|
+
paint: current.paint ?? void 0,
|
|
598
|
+
layout: current.layout ?? void 0,
|
|
599
|
+
filter: current.filter,
|
|
600
|
+
minzoom: current.minzoom,
|
|
601
|
+
maxzoom: current.maxzoom,
|
|
602
|
+
beforeId: beforeIdRef.current
|
|
603
|
+
};
|
|
604
|
+
} catch {
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
const events = ["styledata", "style.load", "idle"];
|
|
608
|
+
for (const event of events) map.on(event, restore);
|
|
609
|
+
return () => {
|
|
610
|
+
for (const event of events) map.off(event, restore);
|
|
611
|
+
};
|
|
612
|
+
}, [map, layerSpec.id]);
|
|
613
|
+
useEffect10(() => {
|
|
254
614
|
try {
|
|
255
615
|
if (!map.getLayer(layerSpec.id)) return;
|
|
256
616
|
const prev = prevRef.current;
|
|
@@ -316,12 +676,12 @@ import { MapMouseEvent } from "maplibre-gl";
|
|
|
316
676
|
import {
|
|
317
677
|
forwardRef,
|
|
318
678
|
useContext as useContext3,
|
|
319
|
-
useEffect as
|
|
679
|
+
useEffect as useEffect11,
|
|
320
680
|
useImperativeHandle,
|
|
321
|
-
useRef as
|
|
322
|
-
useState as
|
|
681
|
+
useRef as useRef4,
|
|
682
|
+
useState as useState3
|
|
323
683
|
} from "react";
|
|
324
|
-
import { jsx } from "react/jsx-runtime";
|
|
684
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
325
685
|
var DEFAULT_CENTER = [0, 0];
|
|
326
686
|
var DEFAULT_ZOOM = 0;
|
|
327
687
|
var HANDLER_NAMES = {
|
|
@@ -392,23 +752,23 @@ var Map = forwardRef((props, ref) => {
|
|
|
392
752
|
children,
|
|
393
753
|
...rest
|
|
394
754
|
} = props;
|
|
395
|
-
const containerRef =
|
|
396
|
-
const [map, setMap] =
|
|
755
|
+
const containerRef = useRef4(null);
|
|
756
|
+
const [map, setMap] = useState3(null);
|
|
397
757
|
const registry = useContext3(MapProviderContext);
|
|
398
|
-
const registryRef =
|
|
758
|
+
const registryRef = useRef4(registry);
|
|
399
759
|
registryRef.current = registry;
|
|
400
|
-
const handlersRef =
|
|
760
|
+
const handlersRef = useRef4({});
|
|
401
761
|
for (const key of Object.keys(HANDLER_NAMES)) {
|
|
402
762
|
handlersRef.current[key] = props[key];
|
|
403
763
|
}
|
|
404
|
-
const interactiveLayerIdsRef =
|
|
764
|
+
const interactiveLayerIdsRef = useRef4(
|
|
405
765
|
interactiveLayerIds
|
|
406
766
|
);
|
|
407
767
|
interactiveLayerIdsRef.current = interactiveLayerIds;
|
|
408
768
|
const layerIdsKey = interactiveLayerIds?.join("|") ?? "";
|
|
409
|
-
const hoverStateRef =
|
|
769
|
+
const hoverStateRef = useRef4({ over: false });
|
|
410
770
|
useImperativeHandle(ref, () => map, [map]);
|
|
411
|
-
|
|
771
|
+
useEffect11(() => {
|
|
412
772
|
if (!map || !id) return;
|
|
413
773
|
const r = registryRef.current;
|
|
414
774
|
if (!r) return;
|
|
@@ -417,7 +777,7 @@ var Map = forwardRef((props, ref) => {
|
|
|
417
777
|
registryRef.current?.unregister(id);
|
|
418
778
|
};
|
|
419
779
|
}, [map, id]);
|
|
420
|
-
|
|
780
|
+
useEffect11(() => {
|
|
421
781
|
if (!containerRef.current) return;
|
|
422
782
|
const mapOptions = { ...rest };
|
|
423
783
|
for (const key of Object.keys(HANDLER_NAMES)) {
|
|
@@ -426,8 +786,9 @@ var Map = forwardRef((props, ref) => {
|
|
|
426
786
|
if (mapOptions.stage === void 0) {
|
|
427
787
|
delete mapOptions.stage;
|
|
428
788
|
}
|
|
789
|
+
const fitsToGeojson = Boolean(mapOptions.geojson) && !mapOptions.center;
|
|
429
790
|
const instance = new GeoloniaMap({
|
|
430
|
-
center: DEFAULT_CENTER,
|
|
791
|
+
...fitsToGeojson ? {} : { center: DEFAULT_CENTER },
|
|
431
792
|
zoom: DEFAULT_ZOOM,
|
|
432
793
|
stage: DEFAULT_STAGE,
|
|
433
794
|
...mapOptions,
|
|
@@ -505,12 +866,12 @@ var Map = forwardRef((props, ref) => {
|
|
|
505
866
|
setMap(null);
|
|
506
867
|
};
|
|
507
868
|
}, []);
|
|
508
|
-
|
|
869
|
+
useEffect11(() => {
|
|
509
870
|
if (!hoverStateRef.current.over) return;
|
|
510
871
|
hoverStateRef.current.over = false;
|
|
511
872
|
handlersRef.current.onMouseLeave?.({ features: [] });
|
|
512
873
|
}, [layerIdsKey]);
|
|
513
|
-
|
|
874
|
+
useEffect11(() => {
|
|
514
875
|
if (!map || cursor === void 0) return;
|
|
515
876
|
const canvas = map.getCanvas();
|
|
516
877
|
const prev = canvas.style.cursor;
|
|
@@ -530,8 +891,9 @@ var Map = forwardRef((props, ref) => {
|
|
|
530
891
|
const minPitch = rest.minPitch;
|
|
531
892
|
const maxPitch = rest.maxPitch;
|
|
532
893
|
const style = rest.style;
|
|
533
|
-
const
|
|
534
|
-
|
|
894
|
+
const projection = rest.projection;
|
|
895
|
+
const prevStyleRef = useRef4(style);
|
|
896
|
+
useEffect11(() => {
|
|
535
897
|
if (!map) return;
|
|
536
898
|
const m = map;
|
|
537
899
|
const opts = {};
|
|
@@ -548,46 +910,67 @@ var Map = forwardRef((props, ref) => {
|
|
|
548
910
|
if (transitionType === "jump") m.jumpTo(opts);
|
|
549
911
|
else m.flyTo(opts);
|
|
550
912
|
}, [map, lng, lat, zoom, bearing, pitch, transitionType]);
|
|
551
|
-
|
|
913
|
+
useEffect11(() => {
|
|
552
914
|
if (!map) return;
|
|
553
915
|
map.setMinZoom(
|
|
554
916
|
minZoom ?? null
|
|
555
917
|
);
|
|
556
918
|
}, [map, minZoom]);
|
|
557
|
-
|
|
919
|
+
useEffect11(() => {
|
|
558
920
|
if (!map) return;
|
|
559
921
|
map.setMaxZoom(
|
|
560
922
|
maxZoom ?? null
|
|
561
923
|
);
|
|
562
924
|
}, [map, maxZoom]);
|
|
563
|
-
|
|
925
|
+
useEffect11(() => {
|
|
564
926
|
if (!map) return;
|
|
565
927
|
map.setMinPitch(
|
|
566
928
|
minPitch ?? null
|
|
567
929
|
);
|
|
568
930
|
}, [map, minPitch]);
|
|
569
|
-
|
|
931
|
+
useEffect11(() => {
|
|
570
932
|
if (!map) return;
|
|
571
933
|
map.setMaxPitch(
|
|
572
934
|
maxPitch ?? null
|
|
573
935
|
);
|
|
574
936
|
}, [map, maxPitch]);
|
|
575
|
-
|
|
937
|
+
useEffect11(() => {
|
|
938
|
+
if (!map || !projection) return;
|
|
939
|
+
const m = map;
|
|
940
|
+
let pending = true;
|
|
941
|
+
const apply = () => {
|
|
942
|
+
pending = false;
|
|
943
|
+
if (m.getProjection()?.type !== projection) {
|
|
944
|
+
m.setProjection({ type: projection });
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
const onIdle = () => {
|
|
948
|
+
if (pending && m.isStyleLoaded()) apply();
|
|
949
|
+
};
|
|
950
|
+
if (m.isStyleLoaded()) apply();
|
|
951
|
+
m.on("style.load", apply);
|
|
952
|
+
m.on("idle", onIdle);
|
|
953
|
+
return () => {
|
|
954
|
+
m.off("style.load", apply);
|
|
955
|
+
m.off("idle", onIdle);
|
|
956
|
+
};
|
|
957
|
+
}, [map, projection]);
|
|
958
|
+
useEffect11(() => {
|
|
576
959
|
if (!map) return;
|
|
577
960
|
if (!style) return;
|
|
578
961
|
if (prevStyleRef.current === style) return;
|
|
579
962
|
prevStyleRef.current = style;
|
|
580
963
|
map.setStyle(style);
|
|
581
964
|
}, [map, style]);
|
|
582
|
-
return /* @__PURE__ */
|
|
965
|
+
return /* @__PURE__ */ jsx2("div", { ref: containerRef, className, style: containerStyle, children: map && /* @__PURE__ */ jsx2(MapContext.Provider, { value: map, children }) });
|
|
583
966
|
});
|
|
584
967
|
Map.displayName = "Map";
|
|
585
968
|
|
|
586
969
|
// src/components/map-provider.tsx
|
|
587
|
-
import { useCallback, useMemo, useState as
|
|
588
|
-
import { jsx as
|
|
970
|
+
import { useCallback, useMemo, useState as useState4 } from "react";
|
|
971
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
589
972
|
function MapProvider({ children }) {
|
|
590
|
-
const [maps, setMaps] =
|
|
973
|
+
const [maps, setMaps] = useState4({});
|
|
591
974
|
const register = useCallback((id, map) => {
|
|
592
975
|
setMaps((prev) => {
|
|
593
976
|
if (prev[id] === map) return prev;
|
|
@@ -606,120 +989,64 @@ function MapProvider({ children }) {
|
|
|
606
989
|
() => ({ maps, register, unregister }),
|
|
607
990
|
[maps, register, unregister]
|
|
608
991
|
);
|
|
609
|
-
return /* @__PURE__ */
|
|
992
|
+
return /* @__PURE__ */ jsx3(MapProviderContext.Provider, { value, children });
|
|
610
993
|
}
|
|
611
994
|
|
|
612
|
-
// src/components/
|
|
613
|
-
import {
|
|
614
|
-
import
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
function Marker(props) {
|
|
618
|
-
const {
|
|
619
|
-
lat,
|
|
620
|
-
lng,
|
|
621
|
-
color,
|
|
622
|
-
openPopup,
|
|
623
|
-
draggable,
|
|
624
|
-
rotation,
|
|
625
|
-
offset,
|
|
626
|
-
anchor,
|
|
627
|
-
rotationAlignment,
|
|
628
|
-
pitchAlignment,
|
|
629
|
-
className,
|
|
630
|
-
children
|
|
631
|
-
} = props;
|
|
995
|
+
// src/components/mask.tsx
|
|
996
|
+
import { Mask as CoreMask } from "@geolonia/maps-core";
|
|
997
|
+
import { useEffect as useEffect12, useRef as useRef5 } from "react";
|
|
998
|
+
var SOURCE_ID = "geolonia-mask";
|
|
999
|
+
function Mask({ fitBounds, ...options }) {
|
|
632
1000
|
const map = useMap();
|
|
633
|
-
const
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
useEffect5(() => {
|
|
643
|
-
const marker = new GeoloniaMarker({
|
|
644
|
-
color,
|
|
645
|
-
draggable,
|
|
646
|
-
rotation,
|
|
647
|
-
offset,
|
|
648
|
-
anchor,
|
|
649
|
-
rotationAlignment,
|
|
650
|
-
pitchAlignment,
|
|
651
|
-
className
|
|
652
|
-
}).setLngLat([lng, lat]).addTo(map);
|
|
653
|
-
if (children) {
|
|
654
|
-
marker.setPopup(
|
|
655
|
-
new maplibregl2.Popup({ offset: [0, -30] }).setDOMContent(
|
|
656
|
-
popupEl.current
|
|
657
|
-
)
|
|
658
|
-
);
|
|
659
|
-
if (openPopup) marker.togglePopup();
|
|
1001
|
+
const fitBoundsRef = useRef5(fitBounds);
|
|
1002
|
+
useEffect12(() => {
|
|
1003
|
+
fitBoundsRef.current = fitBounds;
|
|
1004
|
+
});
|
|
1005
|
+
const optionsKey = JSON.stringify(options);
|
|
1006
|
+
useEffect12(() => {
|
|
1007
|
+
const given = {};
|
|
1008
|
+
for (const [key, value] of Object.entries(options)) {
|
|
1009
|
+
if (value !== void 0) given[key] = value;
|
|
660
1010
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
1011
|
+
let cancelled = false;
|
|
1012
|
+
let instance = null;
|
|
1013
|
+
let refused = false;
|
|
1014
|
+
function build() {
|
|
1015
|
+
instance = new CoreMask(given);
|
|
1016
|
+
instance.addTo(map);
|
|
1017
|
+
if (!map.getSource(SOURCE_ID)) {
|
|
1018
|
+
refused = true;
|
|
1019
|
+
return;
|
|
1020
|
+
}
|
|
1021
|
+
if (fitBoundsRef.current) instance.fitBounds();
|
|
1022
|
+
}
|
|
1023
|
+
function sync() {
|
|
1024
|
+
if (cancelled || refused || !map.isStyleLoaded()) return;
|
|
1025
|
+
if (!instance) {
|
|
1026
|
+
build();
|
|
1027
|
+
} else if (!map.getSource(SOURCE_ID) || !map.getLayer(SOURCE_ID)) {
|
|
1028
|
+
instance.remove();
|
|
1029
|
+
build();
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
const events = ["styledata", "style.load", "idle"];
|
|
1033
|
+
for (const event of events) map.on(event, sync);
|
|
1034
|
+
sync();
|
|
679
1035
|
return () => {
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
1036
|
+
cancelled = true;
|
|
1037
|
+
for (const event of events) map.off(event, sync);
|
|
1038
|
+
try {
|
|
1039
|
+
instance?.remove();
|
|
1040
|
+
} catch {
|
|
1041
|
+
}
|
|
685
1042
|
};
|
|
686
|
-
}, [map,
|
|
687
|
-
|
|
688
|
-
markerRef.current?.setLngLat([lng, lat]);
|
|
689
|
-
}, [lat, lng]);
|
|
690
|
-
useEffect5(() => {
|
|
691
|
-
if (draggable === void 0) return;
|
|
692
|
-
markerRef.current?.setDraggable(draggable);
|
|
693
|
-
}, [draggable]);
|
|
694
|
-
useEffect5(() => {
|
|
695
|
-
if (rotation === void 0) return;
|
|
696
|
-
markerRef.current?.setRotation(rotation);
|
|
697
|
-
}, [rotation]);
|
|
698
|
-
useEffect5(() => {
|
|
699
|
-
if (offset === void 0) return;
|
|
700
|
-
markerRef.current?.setOffset(offset);
|
|
701
|
-
}, [offset]);
|
|
702
|
-
useEffect5(() => {
|
|
703
|
-
if (rotationAlignment === void 0) return;
|
|
704
|
-
markerRef.current?.setRotationAlignment(rotationAlignment);
|
|
705
|
-
}, [rotationAlignment]);
|
|
706
|
-
useEffect5(() => {
|
|
707
|
-
if (pitchAlignment === void 0) return;
|
|
708
|
-
markerRef.current?.setPitchAlignment(pitchAlignment);
|
|
709
|
-
}, [pitchAlignment]);
|
|
710
|
-
useEffect5(() => {
|
|
711
|
-
const marker = markerRef.current;
|
|
712
|
-
const popup = marker?.getPopup();
|
|
713
|
-
if (!popup) return;
|
|
714
|
-
const isOpen = popup.isOpen();
|
|
715
|
-
if (!!openPopup !== isOpen) marker?.togglePopup();
|
|
716
|
-
}, [openPopup]);
|
|
717
|
-
return children ? createPortal2(children, popupEl.current) : null;
|
|
1043
|
+
}, [map, optionsKey]);
|
|
1044
|
+
return null;
|
|
718
1045
|
}
|
|
719
1046
|
|
|
720
1047
|
// src/components/popup.tsx
|
|
721
|
-
import * as
|
|
722
|
-
import { useEffect as
|
|
1048
|
+
import * as maplibregl8 from "maplibre-gl";
|
|
1049
|
+
import { useEffect as useEffect13, useRef as useRef6 } from "react";
|
|
723
1050
|
import { createPortal as createPortal3 } from "react-dom";
|
|
724
1051
|
function defined2(obj) {
|
|
725
1052
|
const out = {};
|
|
@@ -743,17 +1070,17 @@ function Popup3(props) {
|
|
|
743
1070
|
onClose
|
|
744
1071
|
} = props;
|
|
745
1072
|
const map = useMap();
|
|
746
|
-
const containerEl =
|
|
747
|
-
const popupRef =
|
|
748
|
-
const onCloseRef =
|
|
1073
|
+
const containerEl = useRef6(document.createElement("div"));
|
|
1074
|
+
const popupRef = useRef6(null);
|
|
1075
|
+
const onCloseRef = useRef6(onClose);
|
|
749
1076
|
onCloseRef.current = onClose;
|
|
750
|
-
|
|
1077
|
+
useEffect13(() => {
|
|
751
1078
|
let mapRemoving = false;
|
|
752
1079
|
const handleMapRemove = () => {
|
|
753
1080
|
mapRemoving = true;
|
|
754
1081
|
};
|
|
755
1082
|
map.on("remove", handleMapRemove);
|
|
756
|
-
const popup = new
|
|
1083
|
+
const popup = new maplibregl8.Popup(
|
|
757
1084
|
defined2({
|
|
758
1085
|
anchor,
|
|
759
1086
|
offset,
|
|
@@ -775,17 +1102,17 @@ function Popup3(props) {
|
|
|
775
1102
|
if (popup.isOpen()) popup.remove();
|
|
776
1103
|
};
|
|
777
1104
|
}, [map, anchor, closeButton, closeOnClick, closeOnMove]);
|
|
778
|
-
|
|
1105
|
+
useEffect13(() => {
|
|
779
1106
|
popupRef.current?.setLngLat([lng, lat]);
|
|
780
1107
|
}, [lat, lng]);
|
|
781
|
-
|
|
1108
|
+
useEffect13(() => {
|
|
782
1109
|
popupRef.current?.setOffset(offset);
|
|
783
1110
|
}, [offset]);
|
|
784
|
-
|
|
1111
|
+
useEffect13(() => {
|
|
785
1112
|
popupRef.current?.setMaxWidth(maxWidth ?? "240px");
|
|
786
1113
|
}, [maxWidth]);
|
|
787
|
-
const prevClassNameRef =
|
|
788
|
-
|
|
1114
|
+
const prevClassNameRef = useRef6(className);
|
|
1115
|
+
useEffect13(() => {
|
|
789
1116
|
const popup = popupRef.current;
|
|
790
1117
|
if (!popup) return;
|
|
791
1118
|
const prev = prevClassNameRef.current;
|
|
@@ -797,9 +1124,98 @@ function Popup3(props) {
|
|
|
797
1124
|
return createPortal3(children, containerEl.current);
|
|
798
1125
|
}
|
|
799
1126
|
|
|
1127
|
+
// src/components/simple-style.tsx
|
|
1128
|
+
import {
|
|
1129
|
+
SimpleStyle as CoreSimpleStyle
|
|
1130
|
+
} from "@geolonia/maps-core";
|
|
1131
|
+
import { useEffect as useEffect14, useRef as useRef7 } from "react";
|
|
1132
|
+
var DEFAULT_ID = "geolonia-simple-style";
|
|
1133
|
+
function SimpleStyle({
|
|
1134
|
+
geojson,
|
|
1135
|
+
id,
|
|
1136
|
+
cluster,
|
|
1137
|
+
clusterColor,
|
|
1138
|
+
choropleth,
|
|
1139
|
+
heatmap,
|
|
1140
|
+
circle,
|
|
1141
|
+
extrusion,
|
|
1142
|
+
fitBounds
|
|
1143
|
+
}) {
|
|
1144
|
+
const map = useMap();
|
|
1145
|
+
const geojsonRef = useRef7(geojson);
|
|
1146
|
+
geojsonRef.current = geojson;
|
|
1147
|
+
const fitBoundsRef = useRef7(fitBounds);
|
|
1148
|
+
fitBoundsRef.current = fitBounds;
|
|
1149
|
+
const instanceRef = useRef7(null);
|
|
1150
|
+
const appliedRef = useRef7(geojson);
|
|
1151
|
+
const optionsKey = JSON.stringify([
|
|
1152
|
+
id,
|
|
1153
|
+
cluster,
|
|
1154
|
+
clusterColor,
|
|
1155
|
+
choropleth,
|
|
1156
|
+
heatmap,
|
|
1157
|
+
circle,
|
|
1158
|
+
extrusion
|
|
1159
|
+
]);
|
|
1160
|
+
useEffect14(() => {
|
|
1161
|
+
const options = {};
|
|
1162
|
+
const given = {
|
|
1163
|
+
id,
|
|
1164
|
+
cluster,
|
|
1165
|
+
clusterColor,
|
|
1166
|
+
choropleth,
|
|
1167
|
+
heatmap,
|
|
1168
|
+
circle,
|
|
1169
|
+
extrusion
|
|
1170
|
+
};
|
|
1171
|
+
for (const [key, value] of Object.entries(given)) {
|
|
1172
|
+
if (value !== void 0) options[key] = value;
|
|
1173
|
+
}
|
|
1174
|
+
let cancelled = false;
|
|
1175
|
+
let instance = null;
|
|
1176
|
+
function build() {
|
|
1177
|
+
instance = new CoreSimpleStyle(geojsonRef.current, options);
|
|
1178
|
+
appliedRef.current = geojsonRef.current;
|
|
1179
|
+
instance.addTo(map);
|
|
1180
|
+
instanceRef.current = instance;
|
|
1181
|
+
if (fitBoundsRef.current) instance.fitBounds();
|
|
1182
|
+
}
|
|
1183
|
+
function sync() {
|
|
1184
|
+
if (cancelled || !map.isStyleLoaded()) return;
|
|
1185
|
+
if (!instance) {
|
|
1186
|
+
build();
|
|
1187
|
+
} else if (!map.getSource(options.id ?? DEFAULT_ID)) {
|
|
1188
|
+
instance.remove();
|
|
1189
|
+
build();
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
const events = ["styledata", "style.load", "idle"];
|
|
1193
|
+
for (const event of events) map.on(event, sync);
|
|
1194
|
+
sync();
|
|
1195
|
+
return () => {
|
|
1196
|
+
cancelled = true;
|
|
1197
|
+
for (const event of events) map.off(event, sync);
|
|
1198
|
+
instanceRef.current = null;
|
|
1199
|
+
try {
|
|
1200
|
+
instance?.remove();
|
|
1201
|
+
} catch {
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
}, [map, optionsKey]);
|
|
1205
|
+
useEffect14(() => {
|
|
1206
|
+
const instance = instanceRef.current;
|
|
1207
|
+
if (!instance || appliedRef.current === geojson) return;
|
|
1208
|
+
if (!map.getSource(id ?? DEFAULT_ID)) return;
|
|
1209
|
+
appliedRef.current = geojson;
|
|
1210
|
+
if (typeof geojson === "string") instance.setGeoJSON(geojson);
|
|
1211
|
+
else instance.updateData(geojson);
|
|
1212
|
+
}, [map, id, geojson]);
|
|
1213
|
+
return null;
|
|
1214
|
+
}
|
|
1215
|
+
|
|
800
1216
|
// src/components/source.tsx
|
|
801
|
-
import { useEffect as
|
|
802
|
-
import { jsx as
|
|
1217
|
+
import { Fragment as Fragment2, useEffect as useEffect15, useRef as useRef8, useState as useState5 } from "react";
|
|
1218
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
803
1219
|
var SUPPORTED_UPDATE_PROPS = {
|
|
804
1220
|
geojson: ["data"],
|
|
805
1221
|
image: ["url", "coordinates"],
|
|
@@ -812,16 +1228,40 @@ var SUPPORTED_UPDATE_PROPS = {
|
|
|
812
1228
|
};
|
|
813
1229
|
function Source({ id, children, ...spec }) {
|
|
814
1230
|
const map = useMap();
|
|
815
|
-
const [ready, setReady] =
|
|
816
|
-
const
|
|
817
|
-
|
|
1231
|
+
const [ready, setReady] = useState5(false);
|
|
1232
|
+
const [generation, setGeneration] = useState5(0);
|
|
1233
|
+
const prevSpecRef = useRef8(null);
|
|
1234
|
+
const specRef = useRef8(spec);
|
|
1235
|
+
specRef.current = spec;
|
|
1236
|
+
useEffect15(() => {
|
|
818
1237
|
let cancelled = false;
|
|
1238
|
+
let added = false;
|
|
1239
|
+
let ownSource;
|
|
819
1240
|
function add() {
|
|
820
1241
|
if (cancelled || map.getSource(id)) return;
|
|
821
1242
|
map.addSource(id, spec);
|
|
1243
|
+
added = true;
|
|
1244
|
+
ownSource = map.getSource(id);
|
|
822
1245
|
prevSpecRef.current = { ...spec };
|
|
823
1246
|
if (!cancelled) setReady(true);
|
|
824
1247
|
}
|
|
1248
|
+
function restore() {
|
|
1249
|
+
if (cancelled || !added || !map.isStyleLoaded()) return;
|
|
1250
|
+
const current = map.getSource(id);
|
|
1251
|
+
if (current && current === ownSource) return;
|
|
1252
|
+
if (!current) {
|
|
1253
|
+
try {
|
|
1254
|
+
map.addSource(id, specRef.current);
|
|
1255
|
+
} catch {
|
|
1256
|
+
return;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
ownSource = map.getSource(id);
|
|
1260
|
+
prevSpecRef.current = { ...specRef.current };
|
|
1261
|
+
setGeneration((g) => g + 1);
|
|
1262
|
+
}
|
|
1263
|
+
const restoreEvents = ["styledata", "style.load", "idle"];
|
|
1264
|
+
for (const event of restoreEvents) map.on(event, restore);
|
|
825
1265
|
if (map.isStyleLoaded()) {
|
|
826
1266
|
add();
|
|
827
1267
|
} else {
|
|
@@ -830,6 +1270,7 @@ function Source({ id, children, ...spec }) {
|
|
|
830
1270
|
return () => {
|
|
831
1271
|
cancelled = true;
|
|
832
1272
|
map.off("styledata", add);
|
|
1273
|
+
for (const event of restoreEvents) map.off(event, restore);
|
|
833
1274
|
setReady(false);
|
|
834
1275
|
try {
|
|
835
1276
|
if (map.getSource(id)) {
|
|
@@ -842,7 +1283,7 @@ function Source({ id, children, ...spec }) {
|
|
|
842
1283
|
}
|
|
843
1284
|
};
|
|
844
1285
|
}, [map, id]);
|
|
845
|
-
|
|
1286
|
+
useEffect15(() => {
|
|
846
1287
|
if (!ready) return;
|
|
847
1288
|
const source = map.getSource(id);
|
|
848
1289
|
if (!source) return;
|
|
@@ -915,24 +1356,30 @@ function Source({ id, children, ...spec }) {
|
|
|
915
1356
|
}
|
|
916
1357
|
prevSpecRef.current = { ...current };
|
|
917
1358
|
});
|
|
918
|
-
return /* @__PURE__ */
|
|
1359
|
+
return /* @__PURE__ */ jsx4(SourceContext.Provider, { value: id, children: /* @__PURE__ */ jsx4(Fragment2, { children: ready && children }, generation) });
|
|
919
1360
|
}
|
|
920
1361
|
export {
|
|
921
1362
|
AttributionControl2 as AttributionControl,
|
|
922
1363
|
Control,
|
|
923
1364
|
FullscreenControl2 as FullscreenControl,
|
|
1365
|
+
GeocodeError,
|
|
1366
|
+
GeocoderControl,
|
|
924
1367
|
GeolocateControl2 as GeolocateControl,
|
|
1368
|
+
GlobeControl2 as GlobeControl,
|
|
925
1369
|
Layer,
|
|
926
1370
|
Map,
|
|
927
1371
|
MapContext,
|
|
928
1372
|
MapProvider,
|
|
929
1373
|
MapProviderContext,
|
|
930
1374
|
Marker,
|
|
1375
|
+
Mask,
|
|
931
1376
|
NavigationControl2 as NavigationControl,
|
|
932
1377
|
Popup3 as Popup,
|
|
933
1378
|
ScaleControl2 as ScaleControl,
|
|
1379
|
+
SimpleStyle,
|
|
934
1380
|
Source,
|
|
935
1381
|
SourceContext,
|
|
1382
|
+
geocode,
|
|
936
1383
|
useControl,
|
|
937
1384
|
useMap,
|
|
938
1385
|
useMaps,
|