react-bkoi-gl 1.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/README.md +96 -0
- package/bkoi-gl/package.json +6 -0
- package/dist/components/attribution-control.d.ts +11 -0
- package/dist/components/attribution-control.js +19 -0
- package/dist/components/attribution-control.js.map +1 -0
- package/dist/components/fullscreen-control.d.ts +14 -0
- package/dist/components/fullscreen-control.js +21 -0
- package/dist/components/fullscreen-control.js.map +1 -0
- package/dist/components/geolocate-control.d.ts +37 -0
- package/dist/components/geolocate-control.js +51 -0
- package/dist/components/geolocate-control.js.map +1 -0
- package/dist/components/layer.d.ts +17 -0
- package/dist/components/layer.js +99 -0
- package/dist/components/layer.js.map +1 -0
- package/dist/components/map.d.ts +23 -0
- package/dist/components/map.js +102 -0
- package/dist/components/map.js.map +1 -0
- package/dist/components/marker.d.ts +41 -0
- package/dist/components/marker.js +102 -0
- package/dist/components/marker.js.map +1 -0
- package/dist/components/navigation-control.d.ts +11 -0
- package/dist/components/navigation-control.js +19 -0
- package/dist/components/navigation-control.js.map +1 -0
- package/dist/components/popup.d.ts +33 -0
- package/dist/components/popup.js +81 -0
- package/dist/components/popup.js.map +1 -0
- package/dist/components/scale-control.d.ts +13 -0
- package/dist/components/scale-control.js +31 -0
- package/dist/components/scale-control.js.map +1 -0
- package/dist/components/source.d.ts +7 -0
- package/dist/components/source.js +98 -0
- package/dist/components/source.js.map +1 -0
- package/dist/components/use-control.d.ts +9 -0
- package/dist/components/use-control.js +31 -0
- package/dist/components/use-control.js.map +1 -0
- package/dist/components/use-map.d.ts +20 -0
- package/dist/components/use-map.js +55 -0
- package/dist/components/use-map.js.map +1 -0
- package/dist/exports-bkoi-gl.cjs +1512 -0
- package/dist/exports-bkoi-gl.d.ts +89 -0
- package/dist/exports-bkoi-gl.js +36 -0
- package/dist/exports-bkoi-gl.js.map +1 -0
- package/dist/exports-mapbox.d.ts +139 -0
- package/dist/exports-mapbox.js +36 -0
- package/dist/exports-mapbox.js.map +1 -0
- package/dist/index.cjs +1514 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/mapbox/create-ref.d.ts +9 -0
- package/dist/mapbox/create-ref.js +64 -0
- package/dist/mapbox/create-ref.js.map +1 -0
- package/dist/mapbox/mapbox.d.ts +81 -0
- package/dist/mapbox/mapbox.js +477 -0
- package/dist/mapbox/mapbox.js.map +1 -0
- package/dist/styles/react-bkoi-gl.css +1 -0
- package/dist/types/common.d.ts +78 -0
- package/dist/types/common.js +2 -0
- package/dist/types/common.js.map +1 -0
- package/dist/types/events-bkoi-gl.d.ts +49 -0
- package/dist/types/events-bkoi-gl.js +2 -0
- package/dist/types/events-bkoi-gl.js.map +1 -0
- package/dist/types/events-mapbox.d.ts +49 -0
- package/dist/types/events-mapbox.js +2 -0
- package/dist/types/events-mapbox.js.map +1 -0
- package/dist/types/events.d.ts +39 -0
- package/dist/types/events.js +2 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +70 -0
- package/dist/types/index.js +5 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/lib.d.ts +224 -0
- package/dist/types/lib.js +2 -0
- package/dist/types/lib.js.map +1 -0
- package/dist/types/public.d.ts +2 -0
- package/dist/types/public.js +2 -0
- package/dist/types/public.js.map +1 -0
- package/dist/types/style-spec-bkoi-gl.d.ts +9 -0
- package/dist/types/style-spec-bkoi-gl.js +2 -0
- package/dist/types/style-spec-bkoi-gl.js.map +1 -0
- package/dist/types/style-spec-mapbox.d.ts +7 -0
- package/dist/types/style-spec-mapbox.js +2 -0
- package/dist/types/style-spec-mapbox.js.map +1 -0
- package/dist/utils/apply-react-style.d.ts +2 -0
- package/dist/utils/apply-react-style.js +16 -0
- package/dist/utils/apply-react-style.js.map +1 -0
- package/dist/utils/assert.d.ts +1 -0
- package/dist/utils/assert.js +6 -0
- package/dist/utils/assert.js.map +1 -0
- package/dist/utils/deep-equal.d.ts +15 -0
- package/dist/utils/deep-equal.js +46 -0
- package/dist/utils/deep-equal.js.map +1 -0
- package/dist/utils/set-globals.d.ts +22 -0
- package/dist/utils/set-globals.js +19 -0
- package/dist/utils/set-globals.js.map +1 -0
- package/dist/utils/style-utils.d.ts +2 -0
- package/dist/utils/style-utils.js +42 -0
- package/dist/utils/style-utils.js.map +1 -0
- package/dist/utils/transform.d.ts +26 -0
- package/dist/utils/transform.js +56 -0
- package/dist/utils/transform.js.map +1 -0
- package/dist/utils/use-isomorphic-layout-effect.d.ts +3 -0
- package/dist/utils/use-isomorphic-layout-effect.js +4 -0
- package/dist/utils/use-isomorphic-layout-effect.js.map +1 -0
- package/package.json +78 -0
- package/src/components/attribution-control.ts +32 -0
- package/src/components/fullscreen-control.tsx +37 -0
- package/src/components/geolocate-control.ts +90 -0
- package/src/components/layer.ts +133 -0
- package/src/components/map.tsx +155 -0
- package/src/components/marker.ts +137 -0
- package/src/components/navigation-control.ts +29 -0
- package/src/components/popup.ts +116 -0
- package/src/components/scale-control.ts +46 -0
- package/src/components/source.ts +158 -0
- package/src/components/use-control.ts +64 -0
- package/src/components/use-map.tsx +69 -0
- package/src/exports-bkoi-gl.ts +156 -0
- package/src/exports-mapbox.ts +159 -0
- package/src/index.ts +2 -0
- package/src/mapbox/create-ref.ts +118 -0
- package/src/mapbox/mapbox.ts +742 -0
- package/src/types/common.ts +94 -0
- package/src/types/events-bkoi-gl.ts +78 -0
- package/src/types/events-mapbox.ts +76 -0
- package/src/types/events.ts +73 -0
- package/src/types/index.ts +93 -0
- package/src/types/lib.ts +279 -0
- package/src/types/public.ts +9 -0
- package/src/types/style-spec-bkoi-gl.ts +81 -0
- package/src/types/style-spec-mapbox.ts +80 -0
- package/src/utils/apply-react-style.ts +20 -0
- package/src/utils/assert.ts +5 -0
- package/src/utils/deep-equal.ts +61 -0
- package/src/utils/set-globals.ts +57 -0
- package/src/utils/style-utils.ts +59 -0
- package/src/utils/transform.ts +86 -0
- package/src/utils/use-isomorphic-layout-effect.ts +7 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/* global document */
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import {createPortal} from 'react-dom';
|
|
4
|
+
import {useImperativeHandle, useEffect, useMemo, useRef, useContext, forwardRef, memo} from 'react';
|
|
5
|
+
import {applyReactStyle} from '../utils/apply-react-style';
|
|
6
|
+
|
|
7
|
+
import type {PopupEvent, PopupInstance} from '../types';
|
|
8
|
+
|
|
9
|
+
import {MapContext} from './map';
|
|
10
|
+
import {deepEqual} from '../utils/deep-equal';
|
|
11
|
+
|
|
12
|
+
export type PopupProps<OptionsT, PopupT extends PopupInstance> = OptionsT & {
|
|
13
|
+
/** Longitude of the anchor location */
|
|
14
|
+
longitude: number;
|
|
15
|
+
/** Latitude of the anchor location */
|
|
16
|
+
latitude: number;
|
|
17
|
+
|
|
18
|
+
// These types will be further constraint by OptionsT
|
|
19
|
+
anchor?: string;
|
|
20
|
+
offset?: any;
|
|
21
|
+
className?: string;
|
|
22
|
+
maxWidth?: string;
|
|
23
|
+
|
|
24
|
+
/** CSS style override, applied to the control's container */
|
|
25
|
+
style?: React.CSSProperties;
|
|
26
|
+
|
|
27
|
+
onOpen?: (e: PopupEvent<PopupT>) => void;
|
|
28
|
+
onClose?: (e: PopupEvent<PopupT>) => void;
|
|
29
|
+
children?: React.ReactNode;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Adapted from https://github.com/mapbox/mapbox-gl-js/blob/v1.13.0/src/ui/popup.js
|
|
33
|
+
function getClassList(className: string) {
|
|
34
|
+
return new Set(className ? className.trim().split(/\s+/) : []);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* eslint-disable complexity,max-statements */
|
|
38
|
+
function Popup<PopupOptions, PopupT extends PopupInstance>(
|
|
39
|
+
props: PopupProps<PopupOptions, PopupT>,
|
|
40
|
+
ref: React.Ref<PopupT>
|
|
41
|
+
) {
|
|
42
|
+
const {map, mapLib} = useContext(MapContext);
|
|
43
|
+
const container = useMemo(() => {
|
|
44
|
+
return document.createElement('div');
|
|
45
|
+
}, []);
|
|
46
|
+
const thisRef = useRef({props});
|
|
47
|
+
thisRef.current.props = props;
|
|
48
|
+
|
|
49
|
+
const popup: PopupT = useMemo(() => {
|
|
50
|
+
const options = {...props};
|
|
51
|
+
const pp = new mapLib.Popup(options) as PopupT;
|
|
52
|
+
pp.setLngLat([props.longitude, props.latitude]);
|
|
53
|
+
pp.once('open', e => {
|
|
54
|
+
thisRef.current.props.onOpen?.(e as PopupEvent<PopupT>);
|
|
55
|
+
});
|
|
56
|
+
return pp;
|
|
57
|
+
}, []);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const onClose = e => {
|
|
61
|
+
thisRef.current.props.onClose?.(e as PopupEvent<PopupT>);
|
|
62
|
+
};
|
|
63
|
+
popup.on('close', onClose);
|
|
64
|
+
popup.setDOMContent(container).addTo(map.getMap());
|
|
65
|
+
|
|
66
|
+
return () => {
|
|
67
|
+
// https://github.com/visgl/react-map-gl/issues/1825
|
|
68
|
+
// onClose should not be fired if the popup is removed by unmounting
|
|
69
|
+
// When using React strict mode, the component is mounted twice.
|
|
70
|
+
// Firing the onClose callback here would be a false signal to remove the component.
|
|
71
|
+
popup.off('close', onClose);
|
|
72
|
+
if (popup.isOpen()) {
|
|
73
|
+
popup.remove();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}, []);
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
applyReactStyle(popup.getElement(), props.style);
|
|
80
|
+
}, [props.style]);
|
|
81
|
+
|
|
82
|
+
useImperativeHandle(ref, () => popup, []);
|
|
83
|
+
|
|
84
|
+
if (popup.isOpen()) {
|
|
85
|
+
if (popup.getLngLat().lng !== props.longitude || popup.getLngLat().lat !== props.latitude) {
|
|
86
|
+
popup.setLngLat([props.longitude, props.latitude]);
|
|
87
|
+
}
|
|
88
|
+
if (props.offset && !deepEqual(popup.options.offset, props.offset)) {
|
|
89
|
+
popup.setOffset(props.offset);
|
|
90
|
+
}
|
|
91
|
+
if (popup.options.anchor !== props.anchor || popup.options.maxWidth !== props.maxWidth) {
|
|
92
|
+
popup.options.anchor = props.anchor;
|
|
93
|
+
popup.setMaxWidth(props.maxWidth);
|
|
94
|
+
}
|
|
95
|
+
if (popup.options.className !== props.className) {
|
|
96
|
+
const prevClassList = getClassList(popup.options.className);
|
|
97
|
+
const nextClassList = getClassList(props.className);
|
|
98
|
+
|
|
99
|
+
for (const c of prevClassList) {
|
|
100
|
+
if (!nextClassList.has(c)) {
|
|
101
|
+
popup.removeClassName(c);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const c of nextClassList) {
|
|
105
|
+
if (!prevClassList.has(c)) {
|
|
106
|
+
popup.addClassName(c);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
popup.options.className = props.className;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return createPortal(props.children, container);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export default memo(forwardRef(Popup));
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {useEffect, useRef, memo} from 'react';
|
|
3
|
+
import {applyReactStyle} from '../utils/apply-react-style';
|
|
4
|
+
import useControl from './use-control';
|
|
5
|
+
|
|
6
|
+
import type {ControlPosition, ScaleControlInstance} from '../types';
|
|
7
|
+
|
|
8
|
+
export type ScaleControlProps<OptionsT> = OptionsT & {
|
|
9
|
+
// These props will be further constraint by OptionsT
|
|
10
|
+
unit?: string;
|
|
11
|
+
maxWidth?: number;
|
|
12
|
+
|
|
13
|
+
/** Placement of the control relative to the map. */
|
|
14
|
+
position?: ControlPosition;
|
|
15
|
+
/** CSS style override, applied to the control's container */
|
|
16
|
+
style?: React.CSSProperties;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function ScaleControl<ScaleControlOptions, ControlT extends ScaleControlInstance>(
|
|
20
|
+
props: ScaleControlProps<ScaleControlOptions>
|
|
21
|
+
): null {
|
|
22
|
+
const ctrl = useControl<ControlT>(({mapLib}) => new mapLib.ScaleControl(props) as ControlT, {
|
|
23
|
+
position: props.position
|
|
24
|
+
});
|
|
25
|
+
const propsRef = useRef<ScaleControlProps<ScaleControlOptions>>(props);
|
|
26
|
+
|
|
27
|
+
const prevProps = propsRef.current;
|
|
28
|
+
propsRef.current = props;
|
|
29
|
+
|
|
30
|
+
const {style} = props;
|
|
31
|
+
|
|
32
|
+
if (props.maxWidth !== undefined && props.maxWidth !== prevProps.maxWidth) {
|
|
33
|
+
ctrl.options.maxWidth = props.maxWidth;
|
|
34
|
+
}
|
|
35
|
+
if (props.unit !== undefined && props.unit !== prevProps.unit) {
|
|
36
|
+
ctrl.setUnit(props.unit);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
applyReactStyle(ctrl._container, style);
|
|
41
|
+
}, [style]);
|
|
42
|
+
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default memo(ScaleControl);
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {useContext, useEffect, useMemo, useState, useRef} from 'react';
|
|
3
|
+
import {cloneElement} from 'react';
|
|
4
|
+
import {MapContext} from './map';
|
|
5
|
+
import assert from '../utils/assert';
|
|
6
|
+
import {deepEqual} from '../utils/deep-equal';
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
MapInstance,
|
|
10
|
+
ISource,
|
|
11
|
+
CustomSource,
|
|
12
|
+
GeoJSONSourceImplementation,
|
|
13
|
+
ImageSourceImplemtation,
|
|
14
|
+
AnySourceImplementation
|
|
15
|
+
} from '../types';
|
|
16
|
+
import type {GeoJSONSourceRaw, ImageSourceRaw, VectorSourceRaw} from '../types/style-spec-bkoi-gl';
|
|
17
|
+
|
|
18
|
+
export type SourceProps<SourceT> = (SourceT | CustomSource) & {
|
|
19
|
+
id?: string;
|
|
20
|
+
children?: any;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
let sourceCounter = 0;
|
|
24
|
+
|
|
25
|
+
function createSource<SourceT extends ISource>(
|
|
26
|
+
map: MapInstance,
|
|
27
|
+
id: string,
|
|
28
|
+
props: SourceProps<SourceT>
|
|
29
|
+
) {
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
if (map.style && map.style._loaded) {
|
|
32
|
+
const options = {...props};
|
|
33
|
+
delete options.id;
|
|
34
|
+
delete options.children;
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
map.addSource(id, options);
|
|
37
|
+
return map.getSource(id);
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/* eslint-disable complexity */
|
|
43
|
+
function updateSource<SourceT extends ISource>(
|
|
44
|
+
source: AnySourceImplementation,
|
|
45
|
+
props: SourceProps<SourceT>,
|
|
46
|
+
prevProps: SourceProps<SourceT>
|
|
47
|
+
) {
|
|
48
|
+
assert(props.id === prevProps.id, 'source id changed');
|
|
49
|
+
assert(props.type === prevProps.type, 'source type changed');
|
|
50
|
+
|
|
51
|
+
let changedKey = '';
|
|
52
|
+
let changedKeyCount = 0;
|
|
53
|
+
|
|
54
|
+
for (const key in props) {
|
|
55
|
+
if (key !== 'children' && key !== 'id' && !deepEqual(prevProps[key], props[key])) {
|
|
56
|
+
changedKey = key;
|
|
57
|
+
changedKeyCount++;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!changedKeyCount) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const type = props.type;
|
|
66
|
+
|
|
67
|
+
if (type === 'geojson') {
|
|
68
|
+
(source as GeoJSONSourceImplementation).setData(
|
|
69
|
+
(props as unknown as GeoJSONSourceRaw).data as any
|
|
70
|
+
);
|
|
71
|
+
} else if (type === 'image') {
|
|
72
|
+
(source as ImageSourceImplemtation).updateImage({
|
|
73
|
+
url: (props as unknown as ImageSourceRaw).url,
|
|
74
|
+
coordinates: (props as unknown as ImageSourceRaw).coordinates
|
|
75
|
+
});
|
|
76
|
+
} else if ('setCoordinates' in source && changedKeyCount === 1 && changedKey === 'coordinates') {
|
|
77
|
+
source.setCoordinates((props as ImageSourceRaw).coordinates);
|
|
78
|
+
} else if ('setUrl' in source) {
|
|
79
|
+
// Added in 1.12.0:
|
|
80
|
+
// vectorTileSource.setTiles
|
|
81
|
+
// vectorTileSource.setUrl
|
|
82
|
+
switch (changedKey) {
|
|
83
|
+
case 'url':
|
|
84
|
+
source.setUrl((props as VectorSourceRaw).url);
|
|
85
|
+
break;
|
|
86
|
+
case 'tiles':
|
|
87
|
+
source.setTiles((props as VectorSourceRaw).tiles);
|
|
88
|
+
break;
|
|
89
|
+
default:
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
// eslint-disable-next-line
|
|
93
|
+
console.warn(`Unable to update <Source> prop: ${changedKey}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/* eslint-enable complexity */
|
|
97
|
+
|
|
98
|
+
function Source<SourceT extends ISource>(props: SourceProps<SourceT>) {
|
|
99
|
+
const map = useContext(MapContext).map.getMap();
|
|
100
|
+
const propsRef = useRef(props);
|
|
101
|
+
const [, setStyleLoaded] = useState(0);
|
|
102
|
+
|
|
103
|
+
const id = useMemo(() => props.id || `jsx-source-${sourceCounter++}`, []);
|
|
104
|
+
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (map) {
|
|
107
|
+
/* global setTimeout */
|
|
108
|
+
const forceUpdate = () => setTimeout(() => setStyleLoaded(version => version + 1), 0);
|
|
109
|
+
map.on('styledata', forceUpdate);
|
|
110
|
+
forceUpdate();
|
|
111
|
+
|
|
112
|
+
return () => {
|
|
113
|
+
map.off('styledata', forceUpdate);
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
if (map.style && map.style._loaded && map.getSource(id)) {
|
|
116
|
+
// Parent effects are destroyed before child ones, see
|
|
117
|
+
// https://github.com/facebook/react/issues/16728
|
|
118
|
+
// Source can only be removed after all child layers are removed
|
|
119
|
+
const allLayers = map.getStyle()?.layers;
|
|
120
|
+
if (allLayers) {
|
|
121
|
+
for (const layer of allLayers) {
|
|
122
|
+
// @ts-ignore (2339) source does not exist on all layer types
|
|
123
|
+
if (layer.source === id) {
|
|
124
|
+
map.removeLayer(layer.id);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
map.removeSource(id);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}, [map]);
|
|
134
|
+
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
let source = map && map.style && map.getSource(id);
|
|
137
|
+
if (source) {
|
|
138
|
+
updateSource(source, props, propsRef.current);
|
|
139
|
+
} else {
|
|
140
|
+
source = createSource(map, id, props);
|
|
141
|
+
}
|
|
142
|
+
propsRef.current = props;
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
(source &&
|
|
146
|
+
React.Children.map(
|
|
147
|
+
props.children,
|
|
148
|
+
child =>
|
|
149
|
+
child &&
|
|
150
|
+
cloneElement(child, {
|
|
151
|
+
source: id
|
|
152
|
+
})
|
|
153
|
+
)) ||
|
|
154
|
+
null
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export default Source;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {useContext, useMemo, useEffect} from 'react';
|
|
2
|
+
import type {IControl, ControlPosition} from '../types';
|
|
3
|
+
import {MapContext} from './map';
|
|
4
|
+
import type {MapContextValue} from './map';
|
|
5
|
+
|
|
6
|
+
type ControlOptions = {
|
|
7
|
+
position?: ControlPosition;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
function useControl<T extends IControl>(
|
|
11
|
+
onCreate: (context: MapContextValue) => T,
|
|
12
|
+
opts?: ControlOptions
|
|
13
|
+
): T;
|
|
14
|
+
|
|
15
|
+
function useControl<T extends IControl>(
|
|
16
|
+
onCreate: (context: MapContextValue) => T,
|
|
17
|
+
onRemove: (context: MapContextValue) => void,
|
|
18
|
+
opts?: ControlOptions
|
|
19
|
+
): T;
|
|
20
|
+
|
|
21
|
+
function useControl<T extends IControl>(
|
|
22
|
+
onCreate: (context: MapContextValue) => T,
|
|
23
|
+
onAdd: (context: MapContextValue) => void,
|
|
24
|
+
onRemove: (context: MapContextValue) => void,
|
|
25
|
+
opts?: ControlOptions
|
|
26
|
+
): T;
|
|
27
|
+
|
|
28
|
+
function useControl<T extends IControl>(
|
|
29
|
+
onCreate: (context: MapContextValue) => T,
|
|
30
|
+
arg1?: ((context: MapContextValue) => void) | ControlOptions,
|
|
31
|
+
arg2?: ((context: MapContextValue) => void) | ControlOptions,
|
|
32
|
+
arg3?: ControlOptions
|
|
33
|
+
): T {
|
|
34
|
+
const context = useContext(MapContext);
|
|
35
|
+
const ctrl = useMemo(() => onCreate(context), []);
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const opts = (arg3 || arg2 || arg1) as ControlOptions;
|
|
39
|
+
const onAdd = typeof arg1 === 'function' && typeof arg2 === 'function' ? arg1 : null;
|
|
40
|
+
const onRemove = typeof arg2 === 'function' ? arg2 : typeof arg1 === 'function' ? arg1 : null;
|
|
41
|
+
|
|
42
|
+
const {map} = context;
|
|
43
|
+
if (!map.hasControl(ctrl)) {
|
|
44
|
+
map.addControl(ctrl, opts?.position);
|
|
45
|
+
if (onAdd) {
|
|
46
|
+
onAdd(context);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return () => {
|
|
51
|
+
if (onRemove) {
|
|
52
|
+
onRemove(context);
|
|
53
|
+
}
|
|
54
|
+
// Map might have been removed (parent effects are destroyed before child ones)
|
|
55
|
+
if (map.hasControl(ctrl)) {
|
|
56
|
+
map.removeControl(ctrl);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}, []);
|
|
60
|
+
|
|
61
|
+
return ctrl;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export default useControl;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {useState, useCallback, useMemo, useContext} from 'react';
|
|
3
|
+
|
|
4
|
+
import {MapRef} from '../mapbox/create-ref';
|
|
5
|
+
import {MapContext} from './map';
|
|
6
|
+
import {MapInstance} from '../types';
|
|
7
|
+
|
|
8
|
+
type MountedMapsContextValue = {
|
|
9
|
+
maps: {[id: string]: MapRef<MapInstance>};
|
|
10
|
+
onMapMount: (map: MapRef<MapInstance>, id: string) => void;
|
|
11
|
+
onMapUnmount: (id: string) => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const MountedMapsContext = React.createContext<MountedMapsContextValue>(null);
|
|
15
|
+
|
|
16
|
+
export const MapProvider: React.FC<{children?: React.ReactNode}> = props => {
|
|
17
|
+
const [maps, setMaps] = useState<{[id: string]: MapRef<MapInstance>}>({});
|
|
18
|
+
|
|
19
|
+
const onMapMount = useCallback((map: MapRef<MapInstance>, id: string = 'default') => {
|
|
20
|
+
setMaps(currMaps => {
|
|
21
|
+
if (id === 'current') {
|
|
22
|
+
throw new Error("'current' cannot be used as map id");
|
|
23
|
+
}
|
|
24
|
+
if (currMaps[id]) {
|
|
25
|
+
throw new Error(`Multiple maps with the same id: ${id}`);
|
|
26
|
+
}
|
|
27
|
+
return {...currMaps, [id]: map};
|
|
28
|
+
});
|
|
29
|
+
}, []);
|
|
30
|
+
|
|
31
|
+
const onMapUnmount = useCallback((id: string = 'default') => {
|
|
32
|
+
setMaps(currMaps => {
|
|
33
|
+
if (currMaps[id]) {
|
|
34
|
+
const nextMaps = {...currMaps};
|
|
35
|
+
delete nextMaps[id];
|
|
36
|
+
return nextMaps;
|
|
37
|
+
}
|
|
38
|
+
return currMaps;
|
|
39
|
+
});
|
|
40
|
+
}, []);
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<MountedMapsContext.Provider
|
|
44
|
+
value={{
|
|
45
|
+
maps,
|
|
46
|
+
onMapMount,
|
|
47
|
+
onMapUnmount
|
|
48
|
+
}}
|
|
49
|
+
>
|
|
50
|
+
{props.children}
|
|
51
|
+
</MountedMapsContext.Provider>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type MapCollection<MapT extends MapInstance> = {
|
|
56
|
+
[id: string]: MapRef<MapT> | undefined;
|
|
57
|
+
current?: MapRef<MapT>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export function useMap<MapT extends MapInstance>(): MapCollection<MapT> {
|
|
61
|
+
const maps = useContext(MountedMapsContext)?.maps;
|
|
62
|
+
const currentMap = useContext(MapContext);
|
|
63
|
+
|
|
64
|
+
const mapsWithCurrent = useMemo(() => {
|
|
65
|
+
return {...maps, current: currentMap?.map};
|
|
66
|
+
}, [maps, currentMap]);
|
|
67
|
+
|
|
68
|
+
return mapsWithCurrent as MapCollection<MapT>;
|
|
69
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Map as BkoiMap,
|
|
4
|
+
MapOptions,
|
|
5
|
+
Marker as BkoiMarker,
|
|
6
|
+
MarkerOptions,
|
|
7
|
+
Popup as BkoiPopup,
|
|
8
|
+
PopupOptions,
|
|
9
|
+
AttributionControl as BkoiAttributionControl,
|
|
10
|
+
FullscreenControl as BkoiFullscreenControl,
|
|
11
|
+
GeolocateControl as BkoiGeolocateControl,
|
|
12
|
+
NavigationControl as BkoiNavigationControl,
|
|
13
|
+
ScaleControl as BkoiScaleControl
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
// eslint-disable-next-line import/no-unresolved
|
|
16
|
+
} from 'bkoi-gl';
|
|
17
|
+
import { MapStyle, AnyLayer, AnySource } from './types/style-spec-bkoi-gl';
|
|
18
|
+
|
|
19
|
+
import { default as _Map, MapProps as _MapProps } from './components/map';
|
|
20
|
+
import { default as _Marker, MarkerProps as _MarkerProps } from './components/marker';
|
|
21
|
+
import { default as _Popup, PopupProps as _PopupProps } from './components/popup';
|
|
22
|
+
import {
|
|
23
|
+
default as _AttributionControl,
|
|
24
|
+
AttributionControlProps as _AttributionControlProps
|
|
25
|
+
} from './components/attribution-control';
|
|
26
|
+
import {
|
|
27
|
+
default as _FullscreenControl,
|
|
28
|
+
FullscreenControlProps as _FullscreenControlProps
|
|
29
|
+
} from './components/fullscreen-control';
|
|
30
|
+
import {
|
|
31
|
+
default as _GeolocateControl,
|
|
32
|
+
GeolocateControlProps as _GeolocateControlProps
|
|
33
|
+
} from './components/geolocate-control';
|
|
34
|
+
import {
|
|
35
|
+
default as _NavigationControl,
|
|
36
|
+
NavigationControlProps as _NavigationControlProps
|
|
37
|
+
} from './components/navigation-control';
|
|
38
|
+
import {
|
|
39
|
+
default as _ScaleControl,
|
|
40
|
+
ScaleControlProps as _ScaleControlProps
|
|
41
|
+
} from './components/scale-control';
|
|
42
|
+
import { default as _Layer, LayerProps as _LayerProps } from './components/layer';
|
|
43
|
+
import { default as _Source, SourceProps as _SourceProps } from './components/source';
|
|
44
|
+
import { useMap as _useMap } from './components/use-map';
|
|
45
|
+
import type { MapRef as _MapRef } from './mapbox/create-ref';
|
|
46
|
+
import type * as events from './types/events';
|
|
47
|
+
import type { MapCallbacks } from './types/events-bkoi-gl';
|
|
48
|
+
|
|
49
|
+
export function useMap() {
|
|
50
|
+
return _useMap<BkoiMap>();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type MapProps = _MapProps<MapOptions, MapStyle, MapCallbacks, BkoiMap>;
|
|
54
|
+
export type MapRef = _MapRef<BkoiMap>;
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
// eslint-disable-next-line import/no-unresolved
|
|
57
|
+
const mapLib = import('bkoi-gl');
|
|
58
|
+
export const Map = (() => {
|
|
59
|
+
return React.forwardRef(function Map(props: MapProps, ref: React.Ref<MapRef>) {
|
|
60
|
+
return _Map<MapOptions, MapStyle, MapCallbacks, BkoiMap>(props, ref, mapLib);
|
|
61
|
+
});
|
|
62
|
+
})();
|
|
63
|
+
|
|
64
|
+
export type MarkerProps = _MarkerProps<MarkerOptions, BkoiMarker>;
|
|
65
|
+
export const Marker = _Marker as (
|
|
66
|
+
props: MarkerProps & React.RefAttributes<BkoiMarker>
|
|
67
|
+
) => React.ReactElement | null;
|
|
68
|
+
|
|
69
|
+
export type PopupProps = _PopupProps<PopupOptions, BkoiPopup>;
|
|
70
|
+
export const Popup = _Popup as (
|
|
71
|
+
props: PopupProps & React.RefAttributes<BkoiPopup>
|
|
72
|
+
) => React.ReactElement | null;
|
|
73
|
+
|
|
74
|
+
type AttributionControlOptions = ConstructorParameters<typeof BkoiAttributionControl>[0];
|
|
75
|
+
export type AttributionControlProps = _AttributionControlProps<AttributionControlOptions>;
|
|
76
|
+
export const AttributionControl = _AttributionControl as (
|
|
77
|
+
props: AttributionControlProps
|
|
78
|
+
) => React.ReactElement | null;
|
|
79
|
+
|
|
80
|
+
type FullscreenControlOptions = ConstructorParameters<typeof BkoiFullscreenControl>[0];
|
|
81
|
+
export type FullscreenControlProps = _FullscreenControlProps<FullscreenControlOptions>;
|
|
82
|
+
export const FullscreenControl = _FullscreenControl as (
|
|
83
|
+
props: FullscreenControlProps
|
|
84
|
+
) => React.ReactElement | null;
|
|
85
|
+
|
|
86
|
+
type NavigationControlOptions = ConstructorParameters<typeof BkoiNavigationControl>[0];
|
|
87
|
+
export type NavigationControlProps = _NavigationControlProps<NavigationControlOptions>;
|
|
88
|
+
export const NavigationControl = _NavigationControl as (
|
|
89
|
+
props: NavigationControlProps
|
|
90
|
+
) => React.ReactElement | null;
|
|
91
|
+
|
|
92
|
+
type GeolocateControlOptions = ConstructorParameters<typeof BkoiGeolocateControl>[0];
|
|
93
|
+
export type GeolocateControlProps = _GeolocateControlProps<
|
|
94
|
+
GeolocateControlOptions,
|
|
95
|
+
BkoiGeolocateControl
|
|
96
|
+
>;
|
|
97
|
+
export const GeolocateControl = _GeolocateControl as (
|
|
98
|
+
props: GeolocateControlProps & React.RefAttributes<BkoiGeolocateControl>
|
|
99
|
+
) => React.ReactElement | null;
|
|
100
|
+
|
|
101
|
+
type ScaleControlOptions = ConstructorParameters<typeof BkoiScaleControl>[0];
|
|
102
|
+
export type ScaleControlProps = _ScaleControlProps<ScaleControlOptions>;
|
|
103
|
+
export const ScaleControl = _ScaleControl as (
|
|
104
|
+
props: ScaleControlProps
|
|
105
|
+
) => React.ReactElement | null;
|
|
106
|
+
|
|
107
|
+
export type LayerProps = _LayerProps<AnyLayer>;
|
|
108
|
+
export const Layer = _Layer as (props: LayerProps) => React.ReactElement | null;
|
|
109
|
+
|
|
110
|
+
export type SourceProps = _SourceProps<AnySource>;
|
|
111
|
+
export const Source = _Source as (props: SourceProps) => React.ReactElement | null;
|
|
112
|
+
|
|
113
|
+
export { default as useControl } from './components/use-control';
|
|
114
|
+
export { MapProvider } from './components/use-map';
|
|
115
|
+
|
|
116
|
+
export default Map;
|
|
117
|
+
|
|
118
|
+
// Types
|
|
119
|
+
export * from './types/public';
|
|
120
|
+
export type { default as Point } from '@mapbox/point-geometry';
|
|
121
|
+
// export type {
|
|
122
|
+
// PointLike,
|
|
123
|
+
// LngLat,
|
|
124
|
+
// LngLatLike,
|
|
125
|
+
// LngLatBounds,
|
|
126
|
+
// LngLatBoundsLike,
|
|
127
|
+
// PaddingOptions,
|
|
128
|
+
// MapGeoJSONFeature,
|
|
129
|
+
// GeoJSONSource,
|
|
130
|
+
// VideoSource,
|
|
131
|
+
// ImageSource,
|
|
132
|
+
// CanvasSource,
|
|
133
|
+
// VectorTileSource
|
|
134
|
+
// } from 'maplibre-gl';
|
|
135
|
+
export * from './types/style-spec-bkoi-gl';
|
|
136
|
+
|
|
137
|
+
// Events
|
|
138
|
+
export type {
|
|
139
|
+
MapEvent,
|
|
140
|
+
MapMouseEvent,
|
|
141
|
+
MapLayerMouseEvent,
|
|
142
|
+
MapTouchEvent,
|
|
143
|
+
MapLayerTouchEvent,
|
|
144
|
+
MapStyleDataEvent,
|
|
145
|
+
MapSourceDataEvent,
|
|
146
|
+
MapWheelEvent,
|
|
147
|
+
MapBoxZoomEvent,
|
|
148
|
+
ErrorEvent,
|
|
149
|
+
ViewStateChangeEvent
|
|
150
|
+
} from './types/events-bkoi-gl';
|
|
151
|
+
export type PopupEvent = events.PopupEvent<BkoiPopup>;
|
|
152
|
+
export type MarkerEvent = events.MarkerEvent<BkoiMarker>;
|
|
153
|
+
export type MarkerDragEvent = events.MarkerDragEvent<BkoiMarker>;
|
|
154
|
+
export type GeolocateEvent = events.GeolocateEvent<BkoiGeolocateControl>;
|
|
155
|
+
export type GeolocateResultEvent = events.GeolocateResultEvent<BkoiGeolocateControl>;
|
|
156
|
+
export type GeolocateErrorEvent = events.GeolocateErrorEvent<BkoiGeolocateControl>;
|