@contentful/field-editor-location 2.2.1 → 2.2.2-canary.30
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 +73 -80
- package/dist/cjs/LocationEditor.js +60 -85
- package/dist/esm/GoogleMapView.js +73 -80
- package/dist/esm/LocationEditor.js +60 -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,84 @@ 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 [googleMapsState, setGoogleMapsState] = _react.useState({
|
|
72
|
+
marker: undefined,
|
|
73
|
+
maps: undefined
|
|
74
|
+
});
|
|
75
|
+
const propsRef = _react.useRef(props);
|
|
76
|
+
propsRef.current = props;
|
|
77
|
+
const googleMapsStateRef = _react.useRef(googleMapsState);
|
|
78
|
+
googleMapsStateRef.current = googleMapsState;
|
|
79
|
+
const pendingLoadedMapsRef = _react.useRef(null);
|
|
80
|
+
_react.useLayoutEffect(()=>{
|
|
81
|
+
const { marker, maps } = googleMapsState;
|
|
82
|
+
if (marker && maps) {
|
|
83
|
+
if (props.location) {
|
|
84
|
+
const latLng = new maps.LatLng(props.location.lat, props.location.lng);
|
|
85
|
+
marker.setPosition(latLng);
|
|
86
|
+
marker.setVisible(true);
|
|
90
87
|
} else {
|
|
91
|
-
|
|
88
|
+
marker.setVisible(false);
|
|
92
89
|
}
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
marker.setDraggable(!props.disabled);
|
|
91
|
+
marker.setCursor(props.disabled ? 'not-allowed' : 'auto');
|
|
95
92
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}, /*#__PURE__*/ _react.createElement(_googlemapreact.default, {
|
|
101
|
-
draggable: !this.props.disabled,
|
|
102
|
-
bootstrapURLKeys: this.props.googleMapsKey ? {
|
|
103
|
-
key: this.props.googleMapsKey
|
|
104
|
-
} : undefined,
|
|
105
|
-
defaultCenter: BerlinLocation,
|
|
106
|
-
center: this.props.location,
|
|
107
|
-
options: {
|
|
108
|
-
scrollwheel: false,
|
|
109
|
-
mapTypeId: 'roadmap'
|
|
110
|
-
},
|
|
111
|
-
defaultZoom: 6,
|
|
112
|
-
yesIWantToUseGoogleMapApiInternals: true,
|
|
113
|
-
onGoogleApiLoaded: this.onGoogleApiLoaded
|
|
114
|
-
}));
|
|
115
|
-
}
|
|
116
|
-
constructor(props){
|
|
117
|
-
super(props), _define_property(this, "onGoogleApiLoaded", (event)=>{
|
|
118
|
-
const { maps, map } = event;
|
|
119
|
-
const marker = new maps.Marker({
|
|
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
|
-
});
|
|
93
|
+
if (pendingLoadedMapsRef.current !== null && pendingLoadedMapsRef.current === maps) {
|
|
94
|
+
pendingLoadedMapsRef.current = null;
|
|
95
|
+
propsRef.current.onGoogleApiLoaded({
|
|
96
|
+
maps
|
|
136
97
|
});
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
const handleGoogleApiLoaded = _react.useCallback((event)=>{
|
|
101
|
+
const { maps: loadedMaps, map } = event;
|
|
102
|
+
const loadedMarker = new loadedMaps.Marker({
|
|
103
|
+
map,
|
|
104
|
+
position: map.getCenter(),
|
|
105
|
+
cursor: propsRef.current.disabled ? 'not-allowed' : 'auto',
|
|
106
|
+
draggable: !propsRef.current.disabled,
|
|
107
|
+
visible: Boolean(propsRef.current.location)
|
|
108
|
+
});
|
|
109
|
+
loadedMaps.event.addListener(map, 'click', (event)=>{
|
|
110
|
+
const { marker, maps } = googleMapsStateRef.current;
|
|
111
|
+
if (propsRef.current.disabled || !marker || !maps) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
marker.setPosition(event.latLng);
|
|
115
|
+
marker.setVisible(true);
|
|
116
|
+
propsRef.current.onChangeLocation({
|
|
117
|
+
lat: event.latLng.lat(),
|
|
118
|
+
lng: event.latLng.lng()
|
|
142
119
|
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
maps
|
|
149
|
-
});
|
|
120
|
+
});
|
|
121
|
+
loadedMaps.event.addListener(loadedMarker, 'dragend', (event)=>{
|
|
122
|
+
propsRef.current.onChangeLocation({
|
|
123
|
+
lat: event.latLng.lat(),
|
|
124
|
+
lng: event.latLng.lng()
|
|
150
125
|
});
|
|
151
126
|
});
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
127
|
+
pendingLoadedMapsRef.current = loadedMaps;
|
|
128
|
+
setGoogleMapsState({
|
|
129
|
+
marker: loadedMarker,
|
|
130
|
+
maps: loadedMaps
|
|
131
|
+
});
|
|
132
|
+
}, []);
|
|
133
|
+
return /*#__PURE__*/ _react.createElement("div", {
|
|
134
|
+
className: styles.root
|
|
135
|
+
}, /*#__PURE__*/ _react.createElement(_googlemapreact.default, {
|
|
136
|
+
draggable: !props.disabled,
|
|
137
|
+
bootstrapURLKeys: props.googleMapsKey ? {
|
|
138
|
+
key: props.googleMapsKey
|
|
139
|
+
} : undefined,
|
|
140
|
+
defaultCenter: BerlinLocation,
|
|
141
|
+
center: props.location,
|
|
142
|
+
options: {
|
|
143
|
+
scrollwheel: false,
|
|
144
|
+
mapTypeId: 'roadmap'
|
|
145
|
+
},
|
|
146
|
+
defaultZoom: 6,
|
|
147
|
+
yesIWantToUseGoogleMapApiInternals: true,
|
|
148
|
+
onGoogleApiLoaded: handleGoogleApiLoaded
|
|
149
|
+
}));
|
|
157
150
|
}
|
|
@@ -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,77 @@ 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
|
-
return
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}, ()=>{
|
|
103
|
+
}, 300), []);
|
|
104
|
+
const onSearchAddress = throttledSearchAddress;
|
|
105
|
+
const onGetAddressFromLocation = _react.useCallback((location, address)=>{
|
|
106
|
+
const currentMapsObject = mapsObjectRef.current;
|
|
107
|
+
if (!currentMapsObject || !location) {
|
|
108
|
+
return Promise.resolve('');
|
|
109
|
+
}
|
|
110
|
+
return new Promise((resolve)=>{
|
|
111
|
+
const geocoder = new currentMapsObject.Geocoder();
|
|
112
|
+
geocoder.geocode({
|
|
113
|
+
location
|
|
114
|
+
}, (result)=>{
|
|
115
|
+
if (result && result.length > 0) {
|
|
116
|
+
const addresses = result.map((item)=>item.formatted_address);
|
|
117
|
+
resolve(addresses.find((item)=>item === address) || addresses[0]);
|
|
118
|
+
} else {
|
|
166
119
|
resolve('');
|
|
167
|
-
}
|
|
120
|
+
}
|
|
121
|
+
}, ()=>{
|
|
122
|
+
resolve('');
|
|
168
123
|
});
|
|
169
124
|
});
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
125
|
+
}, []);
|
|
126
|
+
return /*#__PURE__*/ _react.createElement("div", {
|
|
127
|
+
"data-test-id": "location-editor"
|
|
128
|
+
}, /*#__PURE__*/ _react.createElement(_GoogleMapView.GoogleMapView, {
|
|
129
|
+
disabled: disabled || mapsObject === null,
|
|
130
|
+
googleMapsKey: googleMapsKey,
|
|
131
|
+
location: localValue,
|
|
132
|
+
onGoogleApiLoaded: ({ maps })=>{
|
|
133
|
+
setMapsObject(maps);
|
|
134
|
+
},
|
|
135
|
+
onChangeLocation: (coords)=>{
|
|
136
|
+
setLocalValue(coords);
|
|
137
|
+
setValue(toLocationValue(coords));
|
|
138
|
+
}
|
|
139
|
+
}), /*#__PURE__*/ _react.createElement(_LocationSelector.LocationSelector, {
|
|
140
|
+
disabled: disabled || mapsObject === null,
|
|
141
|
+
value: localValue,
|
|
142
|
+
view: selectedView,
|
|
143
|
+
onChangeView: (view)=>{
|
|
144
|
+
setSelectedView(view);
|
|
145
|
+
},
|
|
146
|
+
onChangeLocation: (coords)=>{
|
|
147
|
+
setLocalValue(coords);
|
|
148
|
+
setValue(toLocationValue(coords));
|
|
149
|
+
},
|
|
150
|
+
onSearchAddress: onSearchAddress,
|
|
151
|
+
onGetAddressFromLocation: onGetAddressFromLocation
|
|
152
|
+
}));
|
|
178
153
|
}
|
|
179
154
|
function LocationEditorConnected(props) {
|
|
180
155
|
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,84 @@ 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 [googleMapsState, setGoogleMapsState] = React.useState({
|
|
16
|
+
marker: undefined,
|
|
17
|
+
maps: undefined
|
|
18
|
+
});
|
|
19
|
+
const propsRef = React.useRef(props);
|
|
20
|
+
propsRef.current = props;
|
|
21
|
+
const googleMapsStateRef = React.useRef(googleMapsState);
|
|
22
|
+
googleMapsStateRef.current = googleMapsState;
|
|
23
|
+
const pendingLoadedMapsRef = React.useRef(null);
|
|
24
|
+
React.useLayoutEffect(()=>{
|
|
25
|
+
const { marker, maps } = googleMapsState;
|
|
26
|
+
if (marker && maps) {
|
|
27
|
+
if (props.location) {
|
|
28
|
+
const latLng = new maps.LatLng(props.location.lat, props.location.lng);
|
|
29
|
+
marker.setPosition(latLng);
|
|
30
|
+
marker.setVisible(true);
|
|
34
31
|
} else {
|
|
35
|
-
|
|
32
|
+
marker.setVisible(false);
|
|
36
33
|
}
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
marker.setDraggable(!props.disabled);
|
|
35
|
+
marker.setCursor(props.disabled ? 'not-allowed' : 'auto');
|
|
39
36
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}, /*#__PURE__*/ React.createElement(GoogleMapReact, {
|
|
45
|
-
draggable: !this.props.disabled,
|
|
46
|
-
bootstrapURLKeys: this.props.googleMapsKey ? {
|
|
47
|
-
key: this.props.googleMapsKey
|
|
48
|
-
} : undefined,
|
|
49
|
-
defaultCenter: BerlinLocation,
|
|
50
|
-
center: this.props.location,
|
|
51
|
-
options: {
|
|
52
|
-
scrollwheel: false,
|
|
53
|
-
mapTypeId: 'roadmap'
|
|
54
|
-
},
|
|
55
|
-
defaultZoom: 6,
|
|
56
|
-
yesIWantToUseGoogleMapApiInternals: true,
|
|
57
|
-
onGoogleApiLoaded: this.onGoogleApiLoaded
|
|
58
|
-
}));
|
|
59
|
-
}
|
|
60
|
-
constructor(props){
|
|
61
|
-
super(props), _define_property(this, "onGoogleApiLoaded", (event)=>{
|
|
62
|
-
const { maps, map } = event;
|
|
63
|
-
const marker = new maps.Marker({
|
|
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
|
-
});
|
|
37
|
+
if (pendingLoadedMapsRef.current !== null && pendingLoadedMapsRef.current === maps) {
|
|
38
|
+
pendingLoadedMapsRef.current = null;
|
|
39
|
+
propsRef.current.onGoogleApiLoaded({
|
|
40
|
+
maps
|
|
80
41
|
});
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const handleGoogleApiLoaded = React.useCallback((event)=>{
|
|
45
|
+
const { maps: loadedMaps, map } = event;
|
|
46
|
+
const loadedMarker = new loadedMaps.Marker({
|
|
47
|
+
map,
|
|
48
|
+
position: map.getCenter(),
|
|
49
|
+
cursor: propsRef.current.disabled ? 'not-allowed' : 'auto',
|
|
50
|
+
draggable: !propsRef.current.disabled,
|
|
51
|
+
visible: Boolean(propsRef.current.location)
|
|
52
|
+
});
|
|
53
|
+
loadedMaps.event.addListener(map, 'click', (event)=>{
|
|
54
|
+
const { marker, maps } = googleMapsStateRef.current;
|
|
55
|
+
if (propsRef.current.disabled || !marker || !maps) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
marker.setPosition(event.latLng);
|
|
59
|
+
marker.setVisible(true);
|
|
60
|
+
propsRef.current.onChangeLocation({
|
|
61
|
+
lat: event.latLng.lat(),
|
|
62
|
+
lng: event.latLng.lng()
|
|
86
63
|
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
maps
|
|
93
|
-
});
|
|
64
|
+
});
|
|
65
|
+
loadedMaps.event.addListener(loadedMarker, 'dragend', (event)=>{
|
|
66
|
+
propsRef.current.onChangeLocation({
|
|
67
|
+
lat: event.latLng.lat(),
|
|
68
|
+
lng: event.latLng.lng()
|
|
94
69
|
});
|
|
95
70
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
71
|
+
pendingLoadedMapsRef.current = loadedMaps;
|
|
72
|
+
setGoogleMapsState({
|
|
73
|
+
marker: loadedMarker,
|
|
74
|
+
maps: loadedMaps
|
|
75
|
+
});
|
|
76
|
+
}, []);
|
|
77
|
+
return /*#__PURE__*/ React.createElement("div", {
|
|
78
|
+
className: styles.root
|
|
79
|
+
}, /*#__PURE__*/ React.createElement(GoogleMapReact, {
|
|
80
|
+
draggable: !props.disabled,
|
|
81
|
+
bootstrapURLKeys: props.googleMapsKey ? {
|
|
82
|
+
key: props.googleMapsKey
|
|
83
|
+
} : undefined,
|
|
84
|
+
defaultCenter: BerlinLocation,
|
|
85
|
+
center: props.location,
|
|
86
|
+
options: {
|
|
87
|
+
scrollwheel: false,
|
|
88
|
+
mapTypeId: 'roadmap'
|
|
89
|
+
},
|
|
90
|
+
defaultZoom: 6,
|
|
91
|
+
yesIWantToUseGoogleMapApiInternals: true,
|
|
92
|
+
onGoogleApiLoaded: handleGoogleApiLoaded
|
|
93
|
+
}));
|
|
101
94
|
}
|
|
@@ -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,77 @@ 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
|
-
return
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}, ()=>{
|
|
39
|
+
}, 300), []);
|
|
40
|
+
const onSearchAddress = throttledSearchAddress;
|
|
41
|
+
const onGetAddressFromLocation = React.useCallback((location, address)=>{
|
|
42
|
+
const currentMapsObject = mapsObjectRef.current;
|
|
43
|
+
if (!currentMapsObject || !location) {
|
|
44
|
+
return Promise.resolve('');
|
|
45
|
+
}
|
|
46
|
+
return new Promise((resolve)=>{
|
|
47
|
+
const geocoder = new currentMapsObject.Geocoder();
|
|
48
|
+
geocoder.geocode({
|
|
49
|
+
location
|
|
50
|
+
}, (result)=>{
|
|
51
|
+
if (result && result.length > 0) {
|
|
52
|
+
const addresses = result.map((item)=>item.formatted_address);
|
|
53
|
+
resolve(addresses.find((item)=>item === address) || addresses[0]);
|
|
54
|
+
} else {
|
|
102
55
|
resolve('');
|
|
103
|
-
}
|
|
56
|
+
}
|
|
57
|
+
}, ()=>{
|
|
58
|
+
resolve('');
|
|
104
59
|
});
|
|
105
60
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
61
|
+
}, []);
|
|
62
|
+
return /*#__PURE__*/ React.createElement("div", {
|
|
63
|
+
"data-test-id": "location-editor"
|
|
64
|
+
}, /*#__PURE__*/ React.createElement(GoogleMapView, {
|
|
65
|
+
disabled: disabled || mapsObject === null,
|
|
66
|
+
googleMapsKey: googleMapsKey,
|
|
67
|
+
location: localValue,
|
|
68
|
+
onGoogleApiLoaded: ({ maps })=>{
|
|
69
|
+
setMapsObject(maps);
|
|
70
|
+
},
|
|
71
|
+
onChangeLocation: (coords)=>{
|
|
72
|
+
setLocalValue(coords);
|
|
73
|
+
setValue(toLocationValue(coords));
|
|
74
|
+
}
|
|
75
|
+
}), /*#__PURE__*/ React.createElement(LocationSelector, {
|
|
76
|
+
disabled: disabled || mapsObject === null,
|
|
77
|
+
value: localValue,
|
|
78
|
+
view: selectedView,
|
|
79
|
+
onChangeView: (view)=>{
|
|
80
|
+
setSelectedView(view);
|
|
81
|
+
},
|
|
82
|
+
onChangeLocation: (coords)=>{
|
|
83
|
+
setLocalValue(coords);
|
|
84
|
+
setValue(toLocationValue(coords));
|
|
85
|
+
},
|
|
86
|
+
onSearchAddress: onSearchAddress,
|
|
87
|
+
onGetAddressFromLocation: onGetAddressFromLocation
|
|
88
|
+
}));
|
|
114
89
|
}
|
|
115
90
|
export function LocationEditorConnected(props) {
|
|
116
91
|
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.30+69033935",
|
|
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.30+69033935",
|
|
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": "69033935571ab64c941e30158340242c5a449c64"
|
|
58
58
|
}
|