@contentful/field-editor-location 2.2.1 → 2.2.2-canary.29
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/dist/cjs/GoogleMapView.js +66 -81
- package/dist/cjs/LocationEditor.js +58 -85
- package/dist/esm/GoogleMapView.js +66 -81
- package/dist/esm/LocationEditor.js +58 -85
- package/dist/types/GoogleMapView.d.ts +1 -13
- package/dist/types/LocationEditor.d.ts +2 -10
- package/package.json +3 -3
|
@@ -11,19 +11,6 @@ Object.defineProperty(exports, "GoogleMapView", {
|
|
|
11
11
|
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
|
|
12
12
|
const _css = require("@emotion/css");
|
|
13
13
|
const _googlemapreact = /*#__PURE__*/ _interop_require_default(require("google-map-react"));
|
|
14
|
-
function _define_property(obj, key, value) {
|
|
15
|
-
if (key in obj) {
|
|
16
|
-
Object.defineProperty(obj, key, {
|
|
17
|
-
value: value,
|
|
18
|
-
enumerable: true,
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true
|
|
21
|
-
});
|
|
22
|
-
} else {
|
|
23
|
-
obj[key] = value;
|
|
24
|
-
}
|
|
25
|
-
return obj;
|
|
26
|
-
}
|
|
27
14
|
function _interop_require_default(obj) {
|
|
28
15
|
return obj && obj.__esModule ? obj : {
|
|
29
16
|
default: obj
|
|
@@ -80,78 +67,76 @@ const BerlinLocation = {
|
|
|
80
67
|
lat: 52.5018,
|
|
81
68
|
lng: 13.41115439
|
|
82
69
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
70
|
+
function GoogleMapView(props) {
|
|
71
|
+
const [marker, setMarker] = _react.useState();
|
|
72
|
+
const [maps, setMaps] = _react.useState();
|
|
73
|
+
const propsRef = _react.useRef(props);
|
|
74
|
+
propsRef.current = props;
|
|
75
|
+
_react.useEffect(()=>{
|
|
76
|
+
if (marker && maps) {
|
|
77
|
+
if (props.location) {
|
|
78
|
+
const latLng = new maps.LatLng(props.location.lat, props.location.lng);
|
|
79
|
+
marker.setPosition(latLng);
|
|
80
|
+
marker.setVisible(true);
|
|
90
81
|
} else {
|
|
91
|
-
|
|
82
|
+
marker.setVisible(false);
|
|
92
83
|
}
|
|
93
|
-
|
|
94
|
-
|
|
84
|
+
marker.setDraggable(!props.disabled);
|
|
85
|
+
marker.setCursor(props.disabled ? 'not-allowed' : 'auto');
|
|
95
86
|
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
map,
|
|
121
|
-
position: map.getCenter(),
|
|
122
|
-
cursor: this.props.disabled ? 'not-allowed' : 'auto',
|
|
123
|
-
draggable: !this.props.disabled,
|
|
124
|
-
visible: Boolean(this.props.location)
|
|
125
|
-
});
|
|
126
|
-
maps.event.addListener(map, 'click', (event)=>{
|
|
127
|
-
if (this.props.disabled || !this.state.marker || !this.state.maps) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
this.state.marker.setPosition(event.latLng);
|
|
131
|
-
this.state.marker.setVisible(true);
|
|
132
|
-
this.props.onChangeLocation({
|
|
133
|
-
lat: event.latLng.lat(),
|
|
134
|
-
lng: event.latLng.lng()
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
maps.event.addListener(marker, 'dragend', (event)=>{
|
|
138
|
-
this.props.onChangeLocation({
|
|
139
|
-
lat: event.latLng.lat(),
|
|
140
|
-
lng: event.latLng.lng()
|
|
141
|
-
});
|
|
87
|
+
}, [
|
|
88
|
+
marker,
|
|
89
|
+
maps,
|
|
90
|
+
props.disabled,
|
|
91
|
+
props.location
|
|
92
|
+
]);
|
|
93
|
+
const handleGoogleApiLoaded = (event)=>{
|
|
94
|
+
const { maps: loadedMaps, map } = event;
|
|
95
|
+
const loadedMarker = new loadedMaps.Marker({
|
|
96
|
+
map,
|
|
97
|
+
position: map.getCenter(),
|
|
98
|
+
cursor: propsRef.current.disabled ? 'not-allowed' : 'auto',
|
|
99
|
+
draggable: !propsRef.current.disabled,
|
|
100
|
+
visible: Boolean(propsRef.current.location)
|
|
101
|
+
});
|
|
102
|
+
loadedMaps.event.addListener(map, 'click', (event)=>{
|
|
103
|
+
if (propsRef.current.disabled) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
loadedMarker.setPosition(event.latLng);
|
|
107
|
+
loadedMarker.setVisible(true);
|
|
108
|
+
propsRef.current.onChangeLocation({
|
|
109
|
+
lat: event.latLng.lat(),
|
|
110
|
+
lng: event.latLng.lng()
|
|
142
111
|
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
maps
|
|
149
|
-
});
|
|
112
|
+
});
|
|
113
|
+
loadedMaps.event.addListener(loadedMarker, 'dragend', (event)=>{
|
|
114
|
+
propsRef.current.onChangeLocation({
|
|
115
|
+
lat: event.latLng.lat(),
|
|
116
|
+
lng: event.latLng.lng()
|
|
150
117
|
});
|
|
151
118
|
});
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
119
|
+
setMarker(loadedMarker);
|
|
120
|
+
setMaps(loadedMaps);
|
|
121
|
+
propsRef.current.onGoogleApiLoaded({
|
|
122
|
+
maps: loadedMaps
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
return /*#__PURE__*/ _react.createElement("div", {
|
|
126
|
+
className: styles.root
|
|
127
|
+
}, /*#__PURE__*/ _react.createElement(_googlemapreact.default, {
|
|
128
|
+
draggable: !props.disabled,
|
|
129
|
+
bootstrapURLKeys: props.googleMapsKey ? {
|
|
130
|
+
key: props.googleMapsKey
|
|
131
|
+
} : undefined,
|
|
132
|
+
defaultCenter: BerlinLocation,
|
|
133
|
+
center: props.location,
|
|
134
|
+
options: {
|
|
135
|
+
scrollwheel: false,
|
|
136
|
+
mapTypeId: 'roadmap'
|
|
137
|
+
},
|
|
138
|
+
defaultZoom: 6,
|
|
139
|
+
yesIWantToUseGoogleMapApiInternals: true,
|
|
140
|
+
onGoogleApiLoaded: handleGoogleApiLoaded
|
|
141
|
+
}));
|
|
157
142
|
}
|
|
@@ -23,19 +23,6 @@ const _throttle = /*#__PURE__*/ _interop_require_default(require("lodash/throttl
|
|
|
23
23
|
const _GoogleMapView = require("./GoogleMapView");
|
|
24
24
|
const _LocationSelector = require("./LocationSelector");
|
|
25
25
|
const _types = require("./types");
|
|
26
|
-
function _define_property(obj, key, value) {
|
|
27
|
-
if (key in obj) {
|
|
28
|
-
Object.defineProperty(obj, key, {
|
|
29
|
-
value: value,
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true,
|
|
32
|
-
writable: true
|
|
33
|
-
});
|
|
34
|
-
} else {
|
|
35
|
-
obj[key] = value;
|
|
36
|
-
}
|
|
37
|
-
return obj;
|
|
38
|
-
}
|
|
39
26
|
function _interop_require_default(obj) {
|
|
40
27
|
return obj && obj.__esModule ? obj : {
|
|
41
28
|
default: obj
|
|
@@ -92,89 +79,75 @@ function toLocationValue(coords) {
|
|
|
92
79
|
return null;
|
|
93
80
|
}
|
|
94
81
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
mapsObject: maps
|
|
107
|
-
});
|
|
108
|
-
},
|
|
109
|
-
onChangeLocation: (coords)=>{
|
|
110
|
-
this.setState({
|
|
111
|
-
localValue: coords
|
|
112
|
-
});
|
|
113
|
-
this.props.setValue(toLocationValue(coords));
|
|
114
|
-
}
|
|
115
|
-
}), /*#__PURE__*/ _react.createElement(_LocationSelector.LocationSelector, {
|
|
116
|
-
disabled: this.props.disabled || mapsObject === null,
|
|
117
|
-
value: localValue,
|
|
118
|
-
view: this.props.selectedView,
|
|
119
|
-
onChangeView: (view)=>{
|
|
120
|
-
this.props.setSelectedView(view);
|
|
121
|
-
},
|
|
122
|
-
onChangeLocation: (coords)=>{
|
|
123
|
-
this.setState({
|
|
124
|
-
localValue: coords
|
|
125
|
-
});
|
|
126
|
-
this.props.setValue(toLocationValue(coords));
|
|
127
|
-
},
|
|
128
|
-
onSearchAddress: this.onSearchAddress,
|
|
129
|
-
onGetAddressFromLocation: this.onGetAddressFromLocation
|
|
130
|
-
}));
|
|
131
|
-
}
|
|
132
|
-
constructor(props){
|
|
133
|
-
super(props), _define_property(this, "onSearchAddress", (0, _throttle.default)((value)=>{
|
|
134
|
-
if (!this.state.mapsObject) {
|
|
135
|
-
return Promise.resolve(null);
|
|
136
|
-
}
|
|
137
|
-
const { mapsObject } = this.state;
|
|
138
|
-
if (!value) {
|
|
82
|
+
function LocationEditor({ disabled, value, setValue, googleMapsKey, selectedView, setSelectedView }) {
|
|
83
|
+
const [localValue, setLocalValue] = _react.useState(()=>value?.lon || value?.lat ? {
|
|
84
|
+
lng: value.lon ?? 0,
|
|
85
|
+
lat: value.lat ?? 0
|
|
86
|
+
} : undefined);
|
|
87
|
+
const [mapsObject, setMapsObject] = _react.useState(null);
|
|
88
|
+
const mapsObjectRef = _react.useRef(mapsObject);
|
|
89
|
+
mapsObjectRef.current = mapsObject;
|
|
90
|
+
const throttledSearchAddress = _react.useMemo(()=>(0, _throttle.default)((searchValue)=>{
|
|
91
|
+
const currentMapsObject = mapsObjectRef.current;
|
|
92
|
+
if (!currentMapsObject || !searchValue) {
|
|
139
93
|
return Promise.resolve(null);
|
|
140
94
|
}
|
|
141
95
|
return new Promise((resolve)=>{
|
|
142
|
-
const geocoder = new
|
|
96
|
+
const geocoder = new currentMapsObject.Geocoder();
|
|
143
97
|
geocoder.geocode({
|
|
144
|
-
address:
|
|
98
|
+
address: searchValue
|
|
145
99
|
}, resolve, ()=>{
|
|
146
100
|
resolve(null);
|
|
147
101
|
});
|
|
148
102
|
});
|
|
149
|
-
}, 300)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
103
|
+
}, 300), []);
|
|
104
|
+
_react.useEffect(()=>()=>throttledSearchAddress.cancel(), [
|
|
105
|
+
throttledSearchAddress
|
|
106
|
+
]);
|
|
107
|
+
const onSearchAddress = (searchValue)=>throttledSearchAddress(searchValue) ?? Promise.resolve(null);
|
|
108
|
+
const onGetAddressFromLocation = (location, address)=>{
|
|
109
|
+
if (!mapsObject || !location) {
|
|
110
|
+
return Promise.resolve('');
|
|
111
|
+
}
|
|
112
|
+
return new Promise((resolve)=>{
|
|
113
|
+
const geocoder = new mapsObject.Geocoder();
|
|
114
|
+
geocoder.geocode({
|
|
115
|
+
location
|
|
116
|
+
}, (result)=>{
|
|
117
|
+
if (result && result.length > 0) {
|
|
118
|
+
const addresses = result.map((item)=>item.formatted_address);
|
|
119
|
+
resolve(addresses.find((item)=>item === address) || addresses[0]);
|
|
120
|
+
} else {
|
|
166
121
|
resolve('');
|
|
167
|
-
}
|
|
122
|
+
}
|
|
123
|
+
}, ()=>{
|
|
124
|
+
resolve('');
|
|
168
125
|
});
|
|
169
126
|
});
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
127
|
+
};
|
|
128
|
+
const onChangeLocation = (coords)=>{
|
|
129
|
+
setLocalValue(coords);
|
|
130
|
+
setValue(toLocationValue(coords));
|
|
131
|
+
};
|
|
132
|
+
return /*#__PURE__*/ _react.createElement("div", {
|
|
133
|
+
"data-test-id": "location-editor"
|
|
134
|
+
}, /*#__PURE__*/ _react.createElement(_GoogleMapView.GoogleMapView, {
|
|
135
|
+
disabled: disabled || mapsObject === null,
|
|
136
|
+
googleMapsKey: googleMapsKey,
|
|
137
|
+
location: localValue,
|
|
138
|
+
onGoogleApiLoaded: ({ maps })=>{
|
|
139
|
+
setMapsObject(maps);
|
|
140
|
+
},
|
|
141
|
+
onChangeLocation: onChangeLocation
|
|
142
|
+
}), /*#__PURE__*/ _react.createElement(_LocationSelector.LocationSelector, {
|
|
143
|
+
disabled: disabled || mapsObject === null,
|
|
144
|
+
value: localValue,
|
|
145
|
+
view: selectedView,
|
|
146
|
+
onChangeView: setSelectedView,
|
|
147
|
+
onChangeLocation: onChangeLocation,
|
|
148
|
+
onSearchAddress: onSearchAddress,
|
|
149
|
+
onGetAddressFromLocation: onGetAddressFromLocation
|
|
150
|
+
}));
|
|
178
151
|
}
|
|
179
152
|
function LocationEditorConnected(props) {
|
|
180
153
|
const { field } = props;
|
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
} else {
|
|
10
|
-
obj[key] = value;
|
|
11
|
-
}
|
|
12
|
-
return obj;
|
|
13
|
-
}
|
|
14
1
|
import * as React from 'react';
|
|
15
2
|
import { css } from '@emotion/css';
|
|
16
3
|
import GoogleMapReact from 'google-map-react';
|
|
@@ -24,78 +11,76 @@ const BerlinLocation = {
|
|
|
24
11
|
lat: 52.5018,
|
|
25
12
|
lng: 13.41115439
|
|
26
13
|
};
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
14
|
+
export function GoogleMapView(props) {
|
|
15
|
+
const [marker, setMarker] = React.useState();
|
|
16
|
+
const [maps, setMaps] = React.useState();
|
|
17
|
+
const propsRef = React.useRef(props);
|
|
18
|
+
propsRef.current = props;
|
|
19
|
+
React.useEffect(()=>{
|
|
20
|
+
if (marker && maps) {
|
|
21
|
+
if (props.location) {
|
|
22
|
+
const latLng = new maps.LatLng(props.location.lat, props.location.lng);
|
|
23
|
+
marker.setPosition(latLng);
|
|
24
|
+
marker.setVisible(true);
|
|
34
25
|
} else {
|
|
35
|
-
|
|
26
|
+
marker.setVisible(false);
|
|
36
27
|
}
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
marker.setDraggable(!props.disabled);
|
|
29
|
+
marker.setCursor(props.disabled ? 'not-allowed' : 'auto');
|
|
39
30
|
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
map,
|
|
65
|
-
position: map.getCenter(),
|
|
66
|
-
cursor: this.props.disabled ? 'not-allowed' : 'auto',
|
|
67
|
-
draggable: !this.props.disabled,
|
|
68
|
-
visible: Boolean(this.props.location)
|
|
69
|
-
});
|
|
70
|
-
maps.event.addListener(map, 'click', (event)=>{
|
|
71
|
-
if (this.props.disabled || !this.state.marker || !this.state.maps) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
this.state.marker.setPosition(event.latLng);
|
|
75
|
-
this.state.marker.setVisible(true);
|
|
76
|
-
this.props.onChangeLocation({
|
|
77
|
-
lat: event.latLng.lat(),
|
|
78
|
-
lng: event.latLng.lng()
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
maps.event.addListener(marker, 'dragend', (event)=>{
|
|
82
|
-
this.props.onChangeLocation({
|
|
83
|
-
lat: event.latLng.lat(),
|
|
84
|
-
lng: event.latLng.lng()
|
|
85
|
-
});
|
|
31
|
+
}, [
|
|
32
|
+
marker,
|
|
33
|
+
maps,
|
|
34
|
+
props.disabled,
|
|
35
|
+
props.location
|
|
36
|
+
]);
|
|
37
|
+
const handleGoogleApiLoaded = (event)=>{
|
|
38
|
+
const { maps: loadedMaps, map } = event;
|
|
39
|
+
const loadedMarker = new loadedMaps.Marker({
|
|
40
|
+
map,
|
|
41
|
+
position: map.getCenter(),
|
|
42
|
+
cursor: propsRef.current.disabled ? 'not-allowed' : 'auto',
|
|
43
|
+
draggable: !propsRef.current.disabled,
|
|
44
|
+
visible: Boolean(propsRef.current.location)
|
|
45
|
+
});
|
|
46
|
+
loadedMaps.event.addListener(map, 'click', (event)=>{
|
|
47
|
+
if (propsRef.current.disabled) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
loadedMarker.setPosition(event.latLng);
|
|
51
|
+
loadedMarker.setVisible(true);
|
|
52
|
+
propsRef.current.onChangeLocation({
|
|
53
|
+
lat: event.latLng.lat(),
|
|
54
|
+
lng: event.latLng.lng()
|
|
86
55
|
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
maps
|
|
93
|
-
});
|
|
56
|
+
});
|
|
57
|
+
loadedMaps.event.addListener(loadedMarker, 'dragend', (event)=>{
|
|
58
|
+
propsRef.current.onChangeLocation({
|
|
59
|
+
lat: event.latLng.lat(),
|
|
60
|
+
lng: event.latLng.lng()
|
|
94
61
|
});
|
|
95
62
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
63
|
+
setMarker(loadedMarker);
|
|
64
|
+
setMaps(loadedMaps);
|
|
65
|
+
propsRef.current.onGoogleApiLoaded({
|
|
66
|
+
maps: loadedMaps
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
return /*#__PURE__*/ React.createElement("div", {
|
|
70
|
+
className: styles.root
|
|
71
|
+
}, /*#__PURE__*/ React.createElement(GoogleMapReact, {
|
|
72
|
+
draggable: !props.disabled,
|
|
73
|
+
bootstrapURLKeys: props.googleMapsKey ? {
|
|
74
|
+
key: props.googleMapsKey
|
|
75
|
+
} : undefined,
|
|
76
|
+
defaultCenter: BerlinLocation,
|
|
77
|
+
center: props.location,
|
|
78
|
+
options: {
|
|
79
|
+
scrollwheel: false,
|
|
80
|
+
mapTypeId: 'roadmap'
|
|
81
|
+
},
|
|
82
|
+
defaultZoom: 6,
|
|
83
|
+
yesIWantToUseGoogleMapApiInternals: true,
|
|
84
|
+
onGoogleApiLoaded: handleGoogleApiLoaded
|
|
85
|
+
}));
|
|
101
86
|
}
|
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
} else {
|
|
10
|
-
obj[key] = value;
|
|
11
|
-
}
|
|
12
|
-
return obj;
|
|
13
|
-
}
|
|
14
1
|
import * as React from 'react';
|
|
15
2
|
import { FieldConnector } from '@contentful/field-editor-shared';
|
|
16
3
|
import isNumber from 'lodash/isNumber';
|
|
@@ -28,89 +15,75 @@ function toLocationValue(coords) {
|
|
|
28
15
|
return null;
|
|
29
16
|
}
|
|
30
17
|
}
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
mapsObject: maps
|
|
43
|
-
});
|
|
44
|
-
},
|
|
45
|
-
onChangeLocation: (coords)=>{
|
|
46
|
-
this.setState({
|
|
47
|
-
localValue: coords
|
|
48
|
-
});
|
|
49
|
-
this.props.setValue(toLocationValue(coords));
|
|
50
|
-
}
|
|
51
|
-
}), /*#__PURE__*/ React.createElement(LocationSelector, {
|
|
52
|
-
disabled: this.props.disabled || mapsObject === null,
|
|
53
|
-
value: localValue,
|
|
54
|
-
view: this.props.selectedView,
|
|
55
|
-
onChangeView: (view)=>{
|
|
56
|
-
this.props.setSelectedView(view);
|
|
57
|
-
},
|
|
58
|
-
onChangeLocation: (coords)=>{
|
|
59
|
-
this.setState({
|
|
60
|
-
localValue: coords
|
|
61
|
-
});
|
|
62
|
-
this.props.setValue(toLocationValue(coords));
|
|
63
|
-
},
|
|
64
|
-
onSearchAddress: this.onSearchAddress,
|
|
65
|
-
onGetAddressFromLocation: this.onGetAddressFromLocation
|
|
66
|
-
}));
|
|
67
|
-
}
|
|
68
|
-
constructor(props){
|
|
69
|
-
super(props), _define_property(this, "onSearchAddress", throttle((value)=>{
|
|
70
|
-
if (!this.state.mapsObject) {
|
|
71
|
-
return Promise.resolve(null);
|
|
72
|
-
}
|
|
73
|
-
const { mapsObject } = this.state;
|
|
74
|
-
if (!value) {
|
|
18
|
+
export function LocationEditor({ disabled, value, setValue, googleMapsKey, selectedView, setSelectedView }) {
|
|
19
|
+
const [localValue, setLocalValue] = React.useState(()=>value?.lon || value?.lat ? {
|
|
20
|
+
lng: value.lon ?? 0,
|
|
21
|
+
lat: value.lat ?? 0
|
|
22
|
+
} : undefined);
|
|
23
|
+
const [mapsObject, setMapsObject] = React.useState(null);
|
|
24
|
+
const mapsObjectRef = React.useRef(mapsObject);
|
|
25
|
+
mapsObjectRef.current = mapsObject;
|
|
26
|
+
const throttledSearchAddress = React.useMemo(()=>throttle((searchValue)=>{
|
|
27
|
+
const currentMapsObject = mapsObjectRef.current;
|
|
28
|
+
if (!currentMapsObject || !searchValue) {
|
|
75
29
|
return Promise.resolve(null);
|
|
76
30
|
}
|
|
77
31
|
return new Promise((resolve)=>{
|
|
78
|
-
const geocoder = new
|
|
32
|
+
const geocoder = new currentMapsObject.Geocoder();
|
|
79
33
|
geocoder.geocode({
|
|
80
|
-
address:
|
|
34
|
+
address: searchValue
|
|
81
35
|
}, resolve, ()=>{
|
|
82
36
|
resolve(null);
|
|
83
37
|
});
|
|
84
38
|
});
|
|
85
|
-
}, 300)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
39
|
+
}, 300), []);
|
|
40
|
+
React.useEffect(()=>()=>throttledSearchAddress.cancel(), [
|
|
41
|
+
throttledSearchAddress
|
|
42
|
+
]);
|
|
43
|
+
const onSearchAddress = (searchValue)=>throttledSearchAddress(searchValue) ?? Promise.resolve(null);
|
|
44
|
+
const onGetAddressFromLocation = (location, address)=>{
|
|
45
|
+
if (!mapsObject || !location) {
|
|
46
|
+
return Promise.resolve('');
|
|
47
|
+
}
|
|
48
|
+
return new Promise((resolve)=>{
|
|
49
|
+
const geocoder = new mapsObject.Geocoder();
|
|
50
|
+
geocoder.geocode({
|
|
51
|
+
location
|
|
52
|
+
}, (result)=>{
|
|
53
|
+
if (result && result.length > 0) {
|
|
54
|
+
const addresses = result.map((item)=>item.formatted_address);
|
|
55
|
+
resolve(addresses.find((item)=>item === address) || addresses[0]);
|
|
56
|
+
} else {
|
|
102
57
|
resolve('');
|
|
103
|
-
}
|
|
58
|
+
}
|
|
59
|
+
}, ()=>{
|
|
60
|
+
resolve('');
|
|
104
61
|
});
|
|
105
62
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
63
|
+
};
|
|
64
|
+
const onChangeLocation = (coords)=>{
|
|
65
|
+
setLocalValue(coords);
|
|
66
|
+
setValue(toLocationValue(coords));
|
|
67
|
+
};
|
|
68
|
+
return /*#__PURE__*/ React.createElement("div", {
|
|
69
|
+
"data-test-id": "location-editor"
|
|
70
|
+
}, /*#__PURE__*/ React.createElement(GoogleMapView, {
|
|
71
|
+
disabled: disabled || mapsObject === null,
|
|
72
|
+
googleMapsKey: googleMapsKey,
|
|
73
|
+
location: localValue,
|
|
74
|
+
onGoogleApiLoaded: ({ maps })=>{
|
|
75
|
+
setMapsObject(maps);
|
|
76
|
+
},
|
|
77
|
+
onChangeLocation: onChangeLocation
|
|
78
|
+
}), /*#__PURE__*/ React.createElement(LocationSelector, {
|
|
79
|
+
disabled: disabled || mapsObject === null,
|
|
80
|
+
value: localValue,
|
|
81
|
+
view: selectedView,
|
|
82
|
+
onChangeView: setSelectedView,
|
|
83
|
+
onChangeLocation: onChangeLocation,
|
|
84
|
+
onSearchAddress: onSearchAddress,
|
|
85
|
+
onGetAddressFromLocation: onGetAddressFromLocation
|
|
86
|
+
}));
|
|
114
87
|
}
|
|
115
88
|
export function LocationEditorConnected(props) {
|
|
116
89
|
const { field } = props;
|
|
@@ -9,17 +9,5 @@ type GoogleMapViewProps = {
|
|
|
9
9
|
onChangeLocation: (location: Coords) => void;
|
|
10
10
|
googleMapsKey?: string;
|
|
11
11
|
};
|
|
12
|
-
|
|
13
|
-
marker: any;
|
|
14
|
-
maps: any;
|
|
15
|
-
};
|
|
16
|
-
export declare class GoogleMapView extends React.Component<GoogleMapViewProps, GoogleMapsViewState> {
|
|
17
|
-
constructor(props: GoogleMapViewProps);
|
|
18
|
-
componentDidUpdate(): void;
|
|
19
|
-
onGoogleApiLoaded: (event: {
|
|
20
|
-
maps: any;
|
|
21
|
-
map: any;
|
|
22
|
-
}) => void;
|
|
23
|
-
render(): React.JSX.Element;
|
|
24
|
-
}
|
|
12
|
+
export declare function GoogleMapView(props: GoogleMapViewProps): React.JSX.Element;
|
|
25
13
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { FieldAPI, ParametersAPI } from '@contentful/field-editor-shared';
|
|
3
|
-
import { ViewType, NullableLocationValue
|
|
3
|
+
import { ViewType, NullableLocationValue } from './types';
|
|
4
4
|
export interface LocationEditorConnectedProps {
|
|
5
5
|
/**
|
|
6
6
|
* is the field disabled initially
|
|
@@ -25,15 +25,7 @@ type LocationEditorProps = {
|
|
|
25
25
|
selectedView: ViewType;
|
|
26
26
|
setSelectedView: (view: ViewType) => void;
|
|
27
27
|
};
|
|
28
|
-
export declare
|
|
29
|
-
localValue?: Coords;
|
|
30
|
-
mapsObject: any;
|
|
31
|
-
}> {
|
|
32
|
-
constructor(props: LocationEditorProps);
|
|
33
|
-
onSearchAddress: (value: string) => Promise<GeocodeApiResponse>;
|
|
34
|
-
onGetAddressFromLocation: (location: Coords | undefined, value: string) => Promise<string>;
|
|
35
|
-
render(): React.JSX.Element;
|
|
36
|
-
}
|
|
28
|
+
export declare function LocationEditor({ disabled, value, setValue, googleMapsKey, selectedView, setSelectedView, }: LocationEditorProps): React.JSX.Element;
|
|
37
29
|
export declare function LocationEditorConnected(props: LocationEditorConnectedProps): React.JSX.Element;
|
|
38
30
|
export declare namespace LocationEditorConnected {
|
|
39
31
|
var defaultProps: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-location",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.2-canary.29+3ee64435",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@contentful/f36-components": "^6.7.1",
|
|
38
38
|
"@contentful/f36-tokens": "^6.1.2",
|
|
39
|
-
"@contentful/field-editor-shared": "^4.5.
|
|
39
|
+
"@contentful/field-editor-shared": "^4.5.2-canary.29+3ee64435",
|
|
40
40
|
"@emotion/css": "^11.13.5",
|
|
41
41
|
"@types/google-map-react": "2.1.10",
|
|
42
42
|
"google-map-react": "2.2.1",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"publishConfig": {
|
|
55
55
|
"registry": "https://npm.pkg.github.com/"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "3ee64435a6b1f0cb073174e7de406dc8cb4a74ad"
|
|
58
58
|
}
|