@mapcomponents/react-maplibre 0.1.91 → 0.1.92
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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## [v0.1.92] - 2024-01-16
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
|
|
7
|
+
- fe33641: fix useSource hook to dynamically adjust to props.sourceId changes
|
|
8
|
+
- fe33641: fix useMap hook to use less setState calls
|
|
9
|
+
- fe33641: fix useLayer hook options.source prop to wait for the source to be added
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- fe33641: add MlGeoJsonLayer labelProp defaults to enable simply passing the property name to props.labelProp to get labels.
|
|
14
|
+
- fe33641: make label layer use the same source as the main layer.
|
|
15
|
+
- fe33641: adjust MlGeoJsonLayer labelProp storybook demo to show the actual use of the property
|
|
16
|
+
|
|
17
|
+
|
|
3
18
|
## [v0.1.91] - 2024-01-16
|
|
4
19
|
|
|
5
20
|
### Fixed
|
|
@@ -21,7 +21,7 @@ export type MlGeoJsonLayerProps = {
|
|
|
21
21
|
/**
|
|
22
22
|
* GeoJSON data that is supposed to be rendered by this component.
|
|
23
23
|
*/
|
|
24
|
-
geojson
|
|
24
|
+
geojson?: Feature | FeatureCollection | undefined;
|
|
25
25
|
/**
|
|
26
26
|
* Type of the layer that will be added to the MapLibre instance.
|
|
27
27
|
* All types from LayerSpecification union type are supported except the type from
|
|
@@ -29,6 +29,7 @@ export type MlGeoJsonLayerProps = {
|
|
|
29
29
|
*/
|
|
30
30
|
type?: Exclude<LayerSpecification['type'], RasterLayerSpecification['type']>;
|
|
31
31
|
/**
|
|
32
|
+
* @deprecated The property should not be used. Please use the options.paint property instead. This will be removed in the next major release.
|
|
32
33
|
* Paint property object, that is passed to the addLayer call.
|
|
33
34
|
* Possible props depend on the layer type.
|
|
34
35
|
* See https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/
|
|
@@ -41,6 +42,7 @@ export type MlGeoJsonLayerProps = {
|
|
|
41
42
|
*/
|
|
42
43
|
paint?: Exclude<LayerSpecification['paint'], RasterLayerSpecification['paint']>;
|
|
43
44
|
/**
|
|
45
|
+
* @deprecated The property should not be used. Please use the options.layout property instead. This will be removed in the next major release.
|
|
44
46
|
* Layout property object, that is passed to the addLayer call.
|
|
45
47
|
* Possible props depend on the layer type.
|
|
46
48
|
* See https://maplibre.org/maplibre-gl-js-docs/style-spec/layers/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useMapType } from
|
|
2
|
-
import MapLibreGlWrapper from
|
|
3
|
-
import { Source, SourceSpecification } from
|
|
1
|
+
import { useMapType } from './useMap';
|
|
2
|
+
import MapLibreGlWrapper from '../components/MapLibreMap/lib/MapLibreGlWrapper';
|
|
3
|
+
import { Source, SourceSpecification } from 'maplibre-gl';
|
|
4
4
|
type useSourceType = {
|
|
5
5
|
map: MapLibreGlWrapper | undefined;
|
|
6
6
|
source: Source | undefined;
|
package/dist/index.esm.js
CHANGED
|
@@ -1320,7 +1320,7 @@ useMapState.propTypes = {
|
|
|
1320
1320
|
|
|
1321
1321
|
function useMap(props) {
|
|
1322
1322
|
var mapContext = React.useContext(MapContext);
|
|
1323
|
-
var _a = React.useState(),
|
|
1323
|
+
var _a = React.useState({ map: undefined, ready: false }), state = _a[0], setState = _a[1];
|
|
1324
1324
|
var mapState = useMapState({
|
|
1325
1325
|
mapId: props === null || props === void 0 ? void 0 : props.mapId,
|
|
1326
1326
|
watch: {
|
|
@@ -1334,7 +1334,6 @@ function useMap(props) {
|
|
|
1334
1334
|
});
|
|
1335
1335
|
var mapRef = React.useRef();
|
|
1336
1336
|
var componentId = React.useRef(uuid.v4());
|
|
1337
|
-
var _b = React.useState(false), mapIsReady = _b[0], setMapIsReady = _b[1];
|
|
1338
1337
|
var cleanup = function () {
|
|
1339
1338
|
if (mapRef.current) {
|
|
1340
1339
|
mapRef.current.cleanup(componentId.current);
|
|
@@ -1343,7 +1342,6 @@ function useMap(props) {
|
|
|
1343
1342
|
React.useEffect(function () {
|
|
1344
1343
|
return function () {
|
|
1345
1344
|
cleanup();
|
|
1346
|
-
setMapIsReady(false);
|
|
1347
1345
|
mapRef.current = undefined;
|
|
1348
1346
|
};
|
|
1349
1347
|
}, []);
|
|
@@ -1351,8 +1349,7 @@ function useMap(props) {
|
|
|
1351
1349
|
var _a;
|
|
1352
1350
|
if (mapRef.current && mapRef.current.cancelled === true) {
|
|
1353
1351
|
mapRef.current = undefined;
|
|
1354
|
-
|
|
1355
|
-
setMapIsReady(false);
|
|
1352
|
+
setState({ map: undefined, ready: false });
|
|
1356
1353
|
}
|
|
1357
1354
|
if (mapRef.current || !mapContext.mapExists(props === null || props === void 0 ? void 0 : props.mapId))
|
|
1358
1355
|
return;
|
|
@@ -1370,12 +1367,11 @@ function useMap(props) {
|
|
|
1370
1367
|
}
|
|
1371
1368
|
}
|
|
1372
1369
|
mapRef.current = mapContext.getMap(props === null || props === void 0 ? void 0 : props.mapId);
|
|
1373
|
-
|
|
1374
|
-
setMapIsReady(true);
|
|
1370
|
+
setState({ map: mapRef.current, ready: true });
|
|
1375
1371
|
}, [mapContext.mapIds, mapState.layers, mapContext, props]);
|
|
1376
1372
|
return {
|
|
1377
|
-
map: map,
|
|
1378
|
-
mapIsReady:
|
|
1373
|
+
map: state.map,
|
|
1374
|
+
mapIsReady: state.ready,
|
|
1379
1375
|
componentId: componentId.current,
|
|
1380
1376
|
layers: mapState.layers,
|
|
1381
1377
|
cleanup: cleanup,
|
|
@@ -2429,6 +2425,7 @@ var legalLayerTypes = [
|
|
|
2429
2425
|
'background',
|
|
2430
2426
|
];
|
|
2431
2427
|
function useLayer(props) {
|
|
2428
|
+
var _a;
|
|
2432
2429
|
var mapHook = useMap({
|
|
2433
2430
|
mapId: props.mapId,
|
|
2434
2431
|
waitForLayer: props.insertBeforeLayer,
|
|
@@ -2436,17 +2433,17 @@ function useLayer(props) {
|
|
|
2436
2433
|
var layerTypeRef = React.useRef('');
|
|
2437
2434
|
var layerPaintConfRef = React.useRef('');
|
|
2438
2435
|
var layerLayoutConfRef = React.useRef('');
|
|
2439
|
-
var
|
|
2436
|
+
var _b = React.useState(), layer = _b[0], setLayer = _b[1];
|
|
2440
2437
|
var initializedRef = React.useRef(false);
|
|
2441
2438
|
var layerId = React.useRef(props.layerId || (props.idPrefix ? props.idPrefix : 'Layer-') + mapHook.componentId);
|
|
2442
2439
|
var createLayer = React.useCallback(function () {
|
|
2443
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
2440
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
2444
2441
|
if (!mapHook.map || (mapHook === null || mapHook === void 0 ? void 0 : mapHook.map.cancelled))
|
|
2445
2442
|
return;
|
|
2446
2443
|
if (mapHook.map.map.getLayer(layerId.current)) {
|
|
2447
2444
|
mapHook.cleanup();
|
|
2448
2445
|
}
|
|
2449
|
-
if (mapHook.map.map.getSource(layerId.current)) {
|
|
2446
|
+
if (typeof ((_a = props === null || props === void 0 ? void 0 : props.options) === null || _a === void 0 ? void 0 : _a.source) !== 'string' && mapHook.map.map.getSource(layerId.current)) {
|
|
2450
2447
|
mapHook.map.map.removeSource(layerId.current);
|
|
2451
2448
|
}
|
|
2452
2449
|
if (typeof props.options.source === 'string') {
|
|
@@ -2460,18 +2457,18 @@ function useLayer(props) {
|
|
|
2460
2457
|
initializedRef.current = true;
|
|
2461
2458
|
try {
|
|
2462
2459
|
mapHook.map.addLayer(__assign(__assign(__assign(__assign({}, props.options), (props.geojson &&
|
|
2463
|
-
(!((
|
|
2464
|
-
(((
|
|
2460
|
+
(!((_b = props.options) === null || _b === void 0 ? void 0 : _b.source) ||
|
|
2461
|
+
(((_d = (_c = props.options) === null || _c === void 0 ? void 0 : _c.source) === null || _d === void 0 ? void 0 : _d.attribution) && !((_f = (_e = props.options) === null || _e === void 0 ? void 0 : _e.source) === null || _f === void 0 ? void 0 : _f.type))) // if either options.source isn't defined or only options.source.attribution is defined
|
|
2465
2462
|
? {
|
|
2466
2463
|
source: {
|
|
2467
2464
|
type: 'geojson',
|
|
2468
2465
|
data: props.geojson,
|
|
2469
|
-
attribution: ((
|
|
2470
|
-
? (
|
|
2466
|
+
attribution: ((_g = props.options.source) === null || _g === void 0 ? void 0 : _g.attribution)
|
|
2467
|
+
? (_h = props.options.source) === null || _h === void 0 ? void 0 : _h.attribution
|
|
2471
2468
|
: '',
|
|
2472
2469
|
},
|
|
2473
2470
|
}
|
|
2474
|
-
: {})), (typeof ((
|
|
2471
|
+
: {})), (typeof ((_j = props.options) === null || _j === void 0 ? void 0 : _j.source) === 'string'
|
|
2475
2472
|
? {
|
|
2476
2473
|
source: props.options.source,
|
|
2477
2474
|
}
|
|
@@ -2502,8 +2499,8 @@ function useLayer(props) {
|
|
|
2502
2499
|
createLayer();
|
|
2503
2500
|
}
|
|
2504
2501
|
}, mapHook.componentId);
|
|
2505
|
-
layerPaintConfRef.current = JSON.stringify((
|
|
2506
|
-
layerLayoutConfRef.current = JSON.stringify((
|
|
2502
|
+
layerPaintConfRef.current = JSON.stringify((_k = props.options) === null || _k === void 0 ? void 0 : _k.paint);
|
|
2503
|
+
layerLayoutConfRef.current = JSON.stringify((_l = props.options) === null || _l === void 0 ? void 0 : _l.layout);
|
|
2507
2504
|
layerTypeRef.current = props.options.type;
|
|
2508
2505
|
}, [props, mapHook.map]);
|
|
2509
2506
|
React.useEffect(function () {
|
|
@@ -2519,7 +2516,7 @@ function useLayer(props) {
|
|
|
2519
2516
|
return;
|
|
2520
2517
|
}
|
|
2521
2518
|
createLayer();
|
|
2522
|
-
}, [mapHook.map, props.options, createLayer]);
|
|
2519
|
+
}, [mapHook.map, mapHook.mapIsReady, props.options, createLayer]);
|
|
2523
2520
|
React.useEffect(function () {
|
|
2524
2521
|
var _a, _b, _c, _d, _e, _f;
|
|
2525
2522
|
if (((_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.cancelled) === true ||
|
|
@@ -2574,6 +2571,29 @@ function useLayer(props) {
|
|
|
2574
2571
|
mapHook.cleanup();
|
|
2575
2572
|
};
|
|
2576
2573
|
}, []);
|
|
2574
|
+
React.useEffect(function () {
|
|
2575
|
+
var _a, _b, _c, _d, _e, _f;
|
|
2576
|
+
if (typeof ((_a = props === null || props === void 0 ? void 0 : props.options) === null || _a === void 0 ? void 0 : _a.source) !== 'string' ||
|
|
2577
|
+
!mapHook.map ||
|
|
2578
|
+
(typeof ((_b = props === null || props === void 0 ? void 0 : props.options) === null || _b === void 0 ? void 0 : _b.source) === 'string' &&
|
|
2579
|
+
((_d = (_c = mapHook === null || mapHook === void 0 ? void 0 : mapHook.map) === null || _c === void 0 ? void 0 : _c.getLayer) === null || _d === void 0 ? void 0 : _d.call(_c, layerId.current)) &&
|
|
2580
|
+
((_f = (_e = mapHook === null || mapHook === void 0 ? void 0 : mapHook.map) === null || _e === void 0 ? void 0 : _e.getSource) === null || _f === void 0 ? void 0 : _f.call(_e, props.options.source)))) {
|
|
2581
|
+
return;
|
|
2582
|
+
}
|
|
2583
|
+
var findSourceHandler = function () {
|
|
2584
|
+
var _a, _b, _c;
|
|
2585
|
+
if (typeof ((_a = props === null || props === void 0 ? void 0 : props.options) === null || _a === void 0 ? void 0 : _a.source) === 'string' &&
|
|
2586
|
+
((_c = (_b = mapHook === null || mapHook === void 0 ? void 0 : mapHook.map) === null || _b === void 0 ? void 0 : _b.getSource) === null || _c === void 0 ? void 0 : _c.call(_b, props.options.source))) {
|
|
2587
|
+
createLayer();
|
|
2588
|
+
}
|
|
2589
|
+
};
|
|
2590
|
+
mapHook.map.on('sourcedata', findSourceHandler);
|
|
2591
|
+
return function () {
|
|
2592
|
+
if (mapHook === null || mapHook === void 0 ? void 0 : mapHook.map) {
|
|
2593
|
+
mapHook.map.off('sourcedata', findSourceHandler);
|
|
2594
|
+
}
|
|
2595
|
+
};
|
|
2596
|
+
}, [mapHook.map, (_a = props.options) === null || _a === void 0 ? void 0 : _a.source]);
|
|
2577
2597
|
return {
|
|
2578
2598
|
map: mapHook.map,
|
|
2579
2599
|
layer: layer,
|
|
@@ -2680,6 +2700,66 @@ var getDefaulLayerTypeByGeometry = function (geojson) {
|
|
|
2680
2700
|
return "fill";
|
|
2681
2701
|
};
|
|
2682
2702
|
|
|
2703
|
+
function useSource(props) {
|
|
2704
|
+
var _a;
|
|
2705
|
+
var mapHook = useMap({
|
|
2706
|
+
mapId: props.mapId,
|
|
2707
|
+
});
|
|
2708
|
+
var initializedRef = React.useRef(false);
|
|
2709
|
+
var _b = React.useState(), source = _b[0], setSource = _b[1];
|
|
2710
|
+
var sourceId = React.useRef(props.sourceId || (props.idPrefix ? props.idPrefix : 'Source-') + mapHook.componentId);
|
|
2711
|
+
var createSource = React.useCallback(function () {
|
|
2712
|
+
var _a;
|
|
2713
|
+
if (!mapHook.map)
|
|
2714
|
+
return;
|
|
2715
|
+
initializedRef.current = true;
|
|
2716
|
+
if (mapHook.map.map.getSource(sourceId.current)) {
|
|
2717
|
+
mapHook.cleanup();
|
|
2718
|
+
}
|
|
2719
|
+
(_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.addSource(sourceId.current, __assign({}, props.source), mapHook.componentId);
|
|
2720
|
+
setSource(mapHook.map.map.getSource(sourceId.current));
|
|
2721
|
+
}, [props, mapHook.map]);
|
|
2722
|
+
React.useEffect(function () {
|
|
2723
|
+
var _a, _b, _c, _d;
|
|
2724
|
+
if (!initializedRef.current || !((_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.getSource(props.sourceId)))
|
|
2725
|
+
return;
|
|
2726
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2727
|
+
//@ts-ignore setData only exists on GeoJsonSource
|
|
2728
|
+
(_d = (_c = mapHook.map.map.getSource(props.sourceId)) === null || _c === void 0 ? void 0 : _c.setData) === null || _d === void 0 ? void 0 : _d.call(_c, props.source.data);
|
|
2729
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2730
|
+
//@ts-ignore data only exists on GeoJsonSource
|
|
2731
|
+
}, [(_a = props.source) === null || _a === void 0 ? void 0 : _a.data]);
|
|
2732
|
+
React.useEffect(function () {
|
|
2733
|
+
var _a, _b, _c;
|
|
2734
|
+
if (((_c = (_b = (_a = mapHook === null || mapHook === void 0 ? void 0 : mapHook.map) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.getSource) === null || _c === void 0 ? void 0 : _c.call(_b, props.sourceId)) && props.sourceId === sourceId.current)
|
|
2735
|
+
return;
|
|
2736
|
+
sourceId.current = props.sourceId;
|
|
2737
|
+
createSource();
|
|
2738
|
+
}, [mapHook.map, props, createSource]);
|
|
2739
|
+
//cleanup
|
|
2740
|
+
React.useEffect(function () {
|
|
2741
|
+
return function () {
|
|
2742
|
+
var _a, _b, _c;
|
|
2743
|
+
initializedRef.current = false;
|
|
2744
|
+
if (mapHook.map && ((_c = (_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.style) === null || _c === void 0 ? void 0 : _c._layers)) {
|
|
2745
|
+
for (var _i = 0, _d = Object.entries(mapHook.map.map.style._layers); _i < _d.length; _i++) {
|
|
2746
|
+
var _e = _d[_i], layerId = _e[0], layer = _e[1];
|
|
2747
|
+
if (layer.source === sourceId.current) {
|
|
2748
|
+
mapHook.map.map.removeLayer(layerId);
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
mapHook.map.map.removeSource(sourceId.current);
|
|
2752
|
+
}
|
|
2753
|
+
};
|
|
2754
|
+
}, [mapHook.map]);
|
|
2755
|
+
return {
|
|
2756
|
+
map: mapHook.map,
|
|
2757
|
+
source: source,
|
|
2758
|
+
componentId: mapHook.componentId,
|
|
2759
|
+
mapHook: mapHook,
|
|
2760
|
+
};
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2683
2763
|
/**
|
|
2684
2764
|
* Adds source and layer to display GeoJSON data on the map.
|
|
2685
2765
|
*
|
|
@@ -2688,14 +2768,28 @@ var getDefaulLayerTypeByGeometry = function (geojson) {
|
|
|
2688
2768
|
var MlGeoJsonLayer = function (props) {
|
|
2689
2769
|
var _a, _b, _c;
|
|
2690
2770
|
var layerType = props.type || getDefaulLayerTypeByGeometry(props.geojson);
|
|
2691
|
-
var layerId = props.layerId || 'MlGeoJsonLayer-' + uuid.v4();
|
|
2692
|
-
var labelLayerId = "label-".concat(layerId);
|
|
2693
|
-
|
|
2771
|
+
var layerId = React.useRef(props.layerId || 'MlGeoJsonLayer-' + uuid.v4());
|
|
2772
|
+
var labelLayerId = "label-".concat(layerId.current);
|
|
2773
|
+
React.useEffect(function () {
|
|
2774
|
+
if (!props.layerId) {
|
|
2775
|
+
layerId.current = 'MlGeoJsonLayer-' + uuid.v4();
|
|
2776
|
+
}
|
|
2777
|
+
else {
|
|
2778
|
+
layerId.current = props.layerId;
|
|
2779
|
+
}
|
|
2780
|
+
}, [props.layerId]);
|
|
2781
|
+
useSource({
|
|
2782
|
+
mapId: props.mapId,
|
|
2783
|
+
sourceId: layerId.current,
|
|
2784
|
+
source: {
|
|
2785
|
+
type: 'geojson',
|
|
2786
|
+
data: props.geojson,
|
|
2787
|
+
},
|
|
2788
|
+
});
|
|
2694
2789
|
useLayer({
|
|
2695
2790
|
mapId: props.mapId,
|
|
2696
|
-
layerId:
|
|
2697
|
-
|
|
2698
|
-
options: __assign(__assign({}, props.options), { paint: __assign(__assign({}, (props.paint || getDefaultPaintPropsByType(layerType, props.defaultPaintOverrides))), (_a = props === null || props === void 0 ? void 0 : props.options) === null || _a === void 0 ? void 0 : _a.paint), layout: __assign(__assign({}, ((props === null || props === void 0 ? void 0 : props.layout) || {})), (_b = props === null || props === void 0 ? void 0 : props.options) === null || _b === void 0 ? void 0 : _b.layout), type: layerType }),
|
|
2791
|
+
layerId: layerId.current,
|
|
2792
|
+
options: __assign(__assign({ source: layerId.current }, props.options), { paint: __assign(__assign({}, (props.paint || getDefaultPaintPropsByType(layerType, props.defaultPaintOverrides))), (_a = props === null || props === void 0 ? void 0 : props.options) === null || _a === void 0 ? void 0 : _a.paint), layout: __assign(__assign({}, ((props === null || props === void 0 ? void 0 : props.layout) || {})), (_b = props === null || props === void 0 ? void 0 : props.options) === null || _b === void 0 ? void 0 : _b.layout), type: layerType }),
|
|
2699
2793
|
insertBeforeLayer: props.insertBeforeLayer,
|
|
2700
2794
|
onHover: props.onHover,
|
|
2701
2795
|
onClick: props.onClick,
|
|
@@ -2704,9 +2798,11 @@ var MlGeoJsonLayer = function (props) {
|
|
|
2704
2798
|
if (props.labelProp) {
|
|
2705
2799
|
useLayer({
|
|
2706
2800
|
mapId: props.mapId,
|
|
2707
|
-
layerId: labelLayerId,
|
|
2708
|
-
|
|
2709
|
-
|
|
2801
|
+
options: __assign(__assign({ source: props.labelProp ? layerId.current : undefined, id: labelLayerId, type: 'symbol', maxzoom: 24, minzoom: 1 }, ((props === null || props === void 0 ? void 0 : props.labelOptions) ? props.labelOptions : {})), { layout: __assign({ 'text-font': ['Open Sans Regular'], 'text-field': "{".concat(props.labelProp, "}") }, (((_c = props === null || props === void 0 ? void 0 : props.labelOptions) === null || _c === void 0 ? void 0 : _c.layout) ? props.labelOptions.layout : {})), paint: {
|
|
2802
|
+
'text-halo-width': 1,
|
|
2803
|
+
'text-halo-color': '#121212',
|
|
2804
|
+
'text-color': '#fefefe',
|
|
2805
|
+
} }),
|
|
2710
2806
|
});
|
|
2711
2807
|
}
|
|
2712
2808
|
return React__default["default"].createElement(React__default["default"].Fragment, null);
|
|
@@ -4579,64 +4675,6 @@ useLayerHoverPopup.defaultProps = {
|
|
|
4579
4675
|
mapId: undefined,
|
|
4580
4676
|
};
|
|
4581
4677
|
|
|
4582
|
-
function useSource(props) {
|
|
4583
|
-
var _a;
|
|
4584
|
-
var mapHook = useMap({
|
|
4585
|
-
mapId: props.mapId,
|
|
4586
|
-
});
|
|
4587
|
-
var initializedRef = React.useRef(false);
|
|
4588
|
-
var _b = React.useState(), source = _b[0], setSource = _b[1];
|
|
4589
|
-
var sourceId = React.useRef(props.sourceId || (props.idPrefix ? props.idPrefix : "Source-") + mapHook.componentId);
|
|
4590
|
-
var createSource = React.useCallback(function () {
|
|
4591
|
-
var _a;
|
|
4592
|
-
if (!mapHook.map)
|
|
4593
|
-
return;
|
|
4594
|
-
initializedRef.current = true;
|
|
4595
|
-
if (mapHook.map.map.getSource(sourceId.current)) {
|
|
4596
|
-
mapHook.cleanup();
|
|
4597
|
-
}
|
|
4598
|
-
(_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.addSource(sourceId.current, __assign({}, props.source), mapHook.componentId);
|
|
4599
|
-
setSource(mapHook.map.map.getSource(sourceId.current));
|
|
4600
|
-
}, [props, mapHook.map]);
|
|
4601
|
-
React.useEffect(function () {
|
|
4602
|
-
if (!mapHook.map || initializedRef.current)
|
|
4603
|
-
return;
|
|
4604
|
-
createSource();
|
|
4605
|
-
}, [mapHook.map, props, createSource]);
|
|
4606
|
-
React.useEffect(function () {
|
|
4607
|
-
var _a, _b, _c, _d;
|
|
4608
|
-
if (!initializedRef.current || !((_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.getSource(props.sourceId)))
|
|
4609
|
-
return;
|
|
4610
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
4611
|
-
//@ts-ignore setData only exists on GeoJsonSource
|
|
4612
|
-
(_d = (_c = mapHook.map.map.getSource(props.sourceId)) === null || _c === void 0 ? void 0 : _c.setData) === null || _d === void 0 ? void 0 : _d.call(_c, props.source.data);
|
|
4613
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
4614
|
-
//@ts-ignore data only exists on GeoJsonSource
|
|
4615
|
-
}, [(_a = props.source) === null || _a === void 0 ? void 0 : _a.data]);
|
|
4616
|
-
//cleanup
|
|
4617
|
-
React.useEffect(function () {
|
|
4618
|
-
return function () {
|
|
4619
|
-
var _a, _b, _c;
|
|
4620
|
-
initializedRef.current = false;
|
|
4621
|
-
if (mapHook.map && ((_c = (_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.style) === null || _c === void 0 ? void 0 : _c._layers)) {
|
|
4622
|
-
for (var _i = 0, _d = Object.entries(mapHook.map.map.style._layers); _i < _d.length; _i++) {
|
|
4623
|
-
var _e = _d[_i], layerId = _e[0], layer = _e[1];
|
|
4624
|
-
if (layer.source === sourceId.current) {
|
|
4625
|
-
mapHook.map.map.removeLayer(layerId);
|
|
4626
|
-
}
|
|
4627
|
-
}
|
|
4628
|
-
mapHook.map.map.removeSource(sourceId.current);
|
|
4629
|
-
}
|
|
4630
|
-
};
|
|
4631
|
-
}, [mapHook.map]);
|
|
4632
|
-
return {
|
|
4633
|
-
map: mapHook.map,
|
|
4634
|
-
source: source,
|
|
4635
|
-
componentId: mapHook.componentId,
|
|
4636
|
-
mapHook: mapHook,
|
|
4637
|
-
};
|
|
4638
|
-
}
|
|
4639
|
-
|
|
4640
4678
|
/**
|
|
4641
4679
|
* MlGpxViewer visualizes a given GPX Track on the map
|
|
4642
4680
|
*/
|