@sanity/google-maps-input 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,305 +1,159 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- });
6
- var jsxRuntime = require('react/jsx-runtime');
7
- var React = require('react');
8
- var lodash = require('lodash');
9
- var ui = require('@sanity/ui');
10
- var icons = require('@sanity/icons');
11
- var sanity = require('sanity');
12
- var styledComponents = require('styled-components');
13
- function _interopNamespaceCompat(e) {
14
- if (e && typeof e === 'object' && 'default' in e) return e;
15
- var n = Object.create(null);
16
- if (e) {
17
- Object.keys(e).forEach(function (k) {
18
- if (k !== 'default') {
19
- var d = Object.getOwnPropertyDescriptor(e, k);
20
- Object.defineProperty(n, k, d.get ? d : {
21
- enumerable: true,
22
- get: function () {
23
- return e[k];
24
- }
25
- });
26
- }
27
- });
28
- }
29
- n.default = e;
30
- return Object.freeze(n);
31
- }
32
- var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
33
- const callbackName = "___sanity_googleMapsApiCallback";
34
- const authFailureCallbackName = "gm_authFailure";
35
- let AuthError$1 = class AuthError extends Error {};
36
- function _loadGoogleMapsApi(config) {
1
+ import { jsxs, jsx, Fragment } from "react/jsx-runtime";
2
+ import { useState, useEffect, PureComponent, createRef, useCallback, useId, useRef } from "react";
3
+ import { Card, Box, Text, Code, TextInput, Stack, Grid, Button, Dialog } from "@sanity/ui";
4
+ import { EditIcon, TrashIcon } from "@sanity/icons";
5
+ import { setIfMissing, set, unset, ChangeIndicator, useUserColor, getAnnotationAtPath, DiffTooltip, definePlugin } from "sanity";
6
+ import { styled } from "styled-components";
7
+ const callbackName = "___sanity_googleMapsApiCallback", authFailureCallbackName = "gm_authFailure";
8
+ let AuthError$1 = class extends Error {
9
+ };
10
+ function _loadGoogleMapsApi(config2) {
37
11
  return new Promise((resolve, reject) => {
38
12
  window[authFailureCallbackName] = () => {
39
13
  reject(new AuthError$1("Authentication error when loading Google Maps API."));
40
- };
41
- window[callbackName] = () => {
14
+ }, window[callbackName] = () => {
42
15
  resolve(window.google.maps);
43
16
  };
44
17
  const script = document.createElement("script");
45
- script.onerror = (event, source, lineno, colno, error) => reject(new Error(coeerceError(event, error)));
46
- script.src = "https://maps.googleapis.com/maps/api/js?key=".concat(config.apiKey, "&libraries=places&callback=").concat(callbackName, "&language=").concat(config.locale);
47
- document.getElementsByTagName("head")[0].appendChild(script);
18
+ script.onerror = (event, source, lineno, colno, error) => reject(new Error(coeerceError(event, error))), script.src = `https://maps.googleapis.com/maps/api/js?key=${config2.apiKey}&libraries=places&callback=${callbackName}&language=${config2.locale}`, document.getElementsByTagName("head")[0].appendChild(script);
48
19
  }).finally(() => {
49
- delete window[callbackName];
50
- delete window[authFailureCallbackName];
20
+ delete window[callbackName], delete window[authFailureCallbackName];
51
21
  });
52
22
  }
53
23
  let memo = null;
54
- function loadGoogleMapsApi(config) {
55
- if (memo) {
56
- return memo;
57
- }
58
- memo = _loadGoogleMapsApi(config);
59
- memo.catch(() => {
24
+ function loadGoogleMapsApi(config2) {
25
+ return memo || (memo = _loadGoogleMapsApi(config2), memo.catch(() => {
60
26
  memo = null;
61
- });
62
- return memo;
27
+ }), memo);
63
28
  }
64
29
  function coeerceError(event, error) {
65
- if (error) {
66
- return error.message;
67
- }
68
- if (typeof event === "string") {
69
- return event;
70
- }
71
- return isErrorEvent(event) ? event.message : "Failed to load Google Maps API";
30
+ return error ? error.message : typeof event == "string" ? event : isErrorEvent(event) ? event.message : "Failed to load Google Maps API";
72
31
  }
73
32
  function isErrorEvent(event) {
74
- if (typeof event !== "object" || event === null) {
75
- return false;
76
- }
77
- if (!("message" in event)) {
78
- return false;
79
- }
80
- return typeof event.message === "string";
33
+ return typeof event != "object" || event === null || !("message" in event) ? !1 : typeof event.message == "string";
81
34
  }
82
35
  function LoadError(props) {
83
36
  var _a;
84
- return /* @__PURE__ */jsxRuntime.jsxs(ui.Card, {
85
- tone: "critical",
86
- radius: 1,
87
- children: [/* @__PURE__ */jsxRuntime.jsx(ui.Box, {
88
- as: "header",
89
- paddingX: 4,
90
- paddingTop: 4,
91
- paddingBottom: 1,
92
- children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
93
- as: "h2",
94
- weight: "bold",
95
- children: "Google Maps failed to load"
96
- })
97
- }), /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
98
- paddingX: 4,
99
- paddingTop: 4,
100
- paddingBottom: 1,
101
- children: props.isAuthError ? /* @__PURE__ */jsxRuntime.jsx(AuthError, {}) : /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
102
- children: [/* @__PURE__ */jsxRuntime.jsx(ui.Text, {
103
- as: "h3",
104
- children: "Error details:"
105
- }), /* @__PURE__ */jsxRuntime.jsx("pre", {
106
- children: /* @__PURE__ */jsxRuntime.jsx(ui.Code, {
107
- size: 1,
108
- children: "error" in props && ((_a = props.error) == null ? void 0 : _a.message)
109
- })
110
- })]
111
- })
112
- })]
113
- });
37
+ return /* @__PURE__ */ jsxs(Card, { tone: "critical", radius: 1, children: [
38
+ /* @__PURE__ */ jsx(Box, { as: "header", paddingX: 4, paddingTop: 4, paddingBottom: 1, children: /* @__PURE__ */ jsx(Text, { as: "h2", weight: "bold", children: "Google Maps failed to load" }) }),
39
+ /* @__PURE__ */ jsx(Box, { paddingX: 4, paddingTop: 4, paddingBottom: 1, children: props.isAuthError ? /* @__PURE__ */ jsx(AuthError2, {}) : /* @__PURE__ */ jsxs(Fragment, { children: [
40
+ /* @__PURE__ */ jsx(Text, { as: "h3", children: "Error details:" }),
41
+ /* @__PURE__ */ jsx("pre", { children: /* @__PURE__ */ jsx(Code, { size: 1, children: "error" in props && ((_a = props.error) == null ? void 0 : _a.message) }) })
42
+ ] }) })
43
+ ] });
114
44
  }
115
- function AuthError() {
116
- return /* @__PURE__ */jsxRuntime.jsxs(ui.Text, {
117
- children: [/* @__PURE__ */jsxRuntime.jsx("p", {
118
- children: "The error appears to be related to authentication"
119
- }), /* @__PURE__ */jsxRuntime.jsx("p", {
120
- children: "Common causes include:"
121
- }), /* @__PURE__ */jsxRuntime.jsxs("ul", {
122
- children: [/* @__PURE__ */jsxRuntime.jsx("li", {
123
- children: "Incorrect API key"
124
- }), /* @__PURE__ */jsxRuntime.jsx("li", {
125
- children: "Referer not allowed"
126
- }), /* @__PURE__ */jsxRuntime.jsx("li", {
127
- children: "Missing authentication scope"
128
- })]
129
- }), /* @__PURE__ */jsxRuntime.jsx("p", {
130
- children: "Check the browser developer tools for more information."
131
- })]
132
- });
45
+ function AuthError2() {
46
+ return /* @__PURE__ */ jsxs(Text, { children: [
47
+ /* @__PURE__ */ jsx("p", { children: "The error appears to be related to authentication" }),
48
+ /* @__PURE__ */ jsx("p", { children: "Common causes include:" }),
49
+ /* @__PURE__ */ jsxs("ul", { children: [
50
+ /* @__PURE__ */ jsx("li", { children: "Incorrect API key" }),
51
+ /* @__PURE__ */ jsx("li", { children: "Referer not allowed" }),
52
+ /* @__PURE__ */ jsx("li", { children: "Missing authentication scope" })
53
+ ] }),
54
+ /* @__PURE__ */ jsx("p", { children: "Check the browser developer tools for more information." })
55
+ ] });
133
56
  }
134
- const browserLocale = typeof window !== "undefined" && window.navigator.language || "en";
135
- function useLoadGoogleMapsApi(config) {
136
- const locale = config.defaultLocale || browserLocale || "en-US";
137
- const [state, setState] = React.useState({
138
- type: "loading"
139
- });
140
- React.useEffect(() => {
141
- loadGoogleMapsApi({
142
- locale,
143
- apiKey: config.apiKey
144
- }).then(api => setState({
145
- type: "loaded",
146
- api
147
- }), err => setState({
148
- type: "error",
149
- error: {
150
- type: err instanceof AuthError$1 ? "authError" : "loadError",
151
- message: err.message
152
- }
153
- }));
154
- }, [locale, config.apiKey]);
155
- return state;
57
+ const browserLocale = typeof window < "u" && window.navigator.language || "en";
58
+ function useLoadGoogleMapsApi(config2) {
59
+ const locale = config2.defaultLocale || browserLocale || "en-US", [state, setState] = useState({ type: "loading" });
60
+ return useEffect(() => {
61
+ typeof window > "u" || loadGoogleMapsApi({ locale, apiKey: config2.apiKey }).then(
62
+ (api) => setState({ type: "loaded", api }),
63
+ (err) => setState({
64
+ type: "error",
65
+ error: { type: err instanceof AuthError$1 ? "authError" : "loadError", message: err.message }
66
+ })
67
+ );
68
+ }, [locale, config2.apiKey]), state;
156
69
  }
157
70
  function GoogleMapsLoadProxy(props) {
158
71
  const loadState = useLoadGoogleMapsApi(props.config);
159
72
  switch (loadState.type) {
160
73
  case "error":
161
- return /* @__PURE__ */jsxRuntime.jsx(LoadError, {
162
- error: loadState.error,
163
- isAuthError: loadState.error.type === "authError"
164
- });
74
+ return /* @__PURE__ */ jsx(LoadError, { error: loadState.error, isAuthError: loadState.error.type === "authError" });
165
75
  case "loading":
166
- return /* @__PURE__ */jsxRuntime.jsx("div", {
167
- children: "Loading Google Maps API"
168
- });
76
+ return /* @__PURE__ */ jsx("div", { children: "Loading Google Maps API" });
169
77
  case "loaded":
170
78
  return props.children(loadState.api);
171
79
  default:
172
80
  return null;
173
81
  }
174
82
  }
175
- var __freeze$3 = Object.freeze;
176
- var __defProp$9 = Object.defineProperty;
177
- var __template$3 = (cooked, raw) => __freeze$3(__defProp$9(cooked, "raw", {
178
- value: __freeze$3(raw || cooked.slice())
179
- }));
180
- var _a$3;
181
- const WrapperContainer = styledComponents.styled.div(_a$3 || (_a$3 = __template$3(["\n position: absolute;\n right: 10px;\n top: 10px;\n width: 220px;\n"])));
182
- var __defProp$8 = Object.defineProperty;
183
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$8(obj, key, {
184
- enumerable: true,
185
- configurable: true,
186
- writable: true,
187
- value
188
- }) : obj[key] = value;
189
- var __publicField$5 = (obj, key, value) => {
190
- __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
191
- return value;
192
- };
193
- class SearchInput extends React__namespace.PureComponent {
83
+ let config;
84
+ function getGeoConfig() {
85
+ return config;
86
+ }
87
+ function setGeoConfig(newConfig) {
88
+ config = newConfig;
89
+ }
90
+ const PreviewImage = styled.img`
91
+ width: 100%;
92
+ height: auto;
93
+ vertical-align: top;
94
+ `, DialogInnerContainer = styled.div`
95
+ height: 40rem;
96
+ `, WrapperContainer = styled.div`
97
+ position: absolute;
98
+ right: 10px;
99
+ top: 10px;
100
+ width: 220px;
101
+ `;
102
+ var __defProp$5 = Object.defineProperty, __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __publicField$3 = (obj, key, value) => __defNormalProp$5(obj, typeof key != "symbol" ? key + "" : key, value);
103
+ class SearchInput extends PureComponent {
194
104
  constructor() {
195
- super(...arguments);
196
- __publicField$5(this, "searchInputRef", React__namespace.createRef());
197
- __publicField$5(this, "autoComplete");
198
- __publicField$5(this, "handleChange", () => {
199
- if (!this.autoComplete) {
200
- return;
201
- }
202
- this.props.onChange(this.autoComplete.getPlace());
203
- if (this.searchInputRef.current) {
204
- this.searchInputRef.current.value = "";
205
- }
105
+ super(...arguments), __publicField$3(this, "searchInputRef", createRef()), __publicField$3(this, "autoComplete"), __publicField$3(this, "handleChange", () => {
106
+ this.autoComplete && (this.props.onChange(this.autoComplete.getPlace()), this.searchInputRef.current && (this.searchInputRef.current.value = ""));
206
107
  });
207
108
  }
208
109
  componentDidMount() {
209
110
  const input = this.searchInputRef.current;
210
- if (!input) {
111
+ if (!input)
211
112
  return;
212
- }
213
- const {
214
- api,
215
- map
216
- } = this.props;
217
- const {
218
- Circle,
219
- places,
220
- event
221
- } = api;
222
- const searchBounds = new Circle({
223
- center: map.getCenter(),
224
- radius: 100
225
- }).getBounds();
113
+ const { api, map } = this.props, { Circle, places, event } = api, searchBounds = new Circle({ center: map.getCenter(), radius: 100 }).getBounds();
226
114
  this.autoComplete = new places.Autocomplete(input, {
227
115
  bounds: searchBounds,
228
116
  types: []
229
117
  // return all kinds of places
230
- });
231
- event.addListener(this.autoComplete, "place_changed", this.handleChange);
118
+ }), event.addListener(this.autoComplete, "place_changed", this.handleChange);
232
119
  }
233
120
  render() {
234
- return /* @__PURE__ */jsxRuntime.jsx(WrapperContainer, {
235
- children: /* @__PURE__ */jsxRuntime.jsx(ui.TextInput, {
121
+ return /* @__PURE__ */ jsx(WrapperContainer, { children: /* @__PURE__ */ jsx(
122
+ TextInput,
123
+ {
236
124
  name: "place",
237
125
  ref: this.searchInputRef,
238
126
  placeholder: "Search for place or address",
239
127
  padding: 4
240
- })
241
- });
128
+ }
129
+ ) });
242
130
  }
243
131
  }
244
132
  function latLngAreEqual(latLng1, latLng2) {
245
- const lat1 = typeof latLng1.lat === "function" ? latLng1.lat() : latLng1.lat;
246
- const lng1 = typeof latLng1.lng === "function" ? latLng1.lng() : latLng1.lng;
247
- const lat2 = typeof latLng2.lat === "function" ? latLng2.lat() : latLng2.lat;
248
- const lng2 = typeof latLng2.lng === "function" ? latLng2.lng() : latLng2.lng;
133
+ const lat1 = typeof latLng1.lat == "function" ? latLng1.lat() : latLng1.lat, lng1 = typeof latLng1.lng == "function" ? latLng1.lng() : latLng1.lng, lat2 = typeof latLng2.lat == "function" ? latLng2.lat() : latLng2.lat, lng2 = typeof latLng2.lng == "function" ? latLng2.lng() : latLng2.lng;
249
134
  return lat1 === lat2 && lng1 === lng2;
250
135
  }
251
- var __freeze$2 = Object.freeze;
252
- var __defProp$7 = Object.defineProperty;
253
- var __template$2 = (cooked, raw) => __freeze$2(__defProp$7(cooked, "raw", {
254
- value: __freeze$2(raw || cooked.slice())
255
- }));
256
- var _a$2;
257
- const MapContainer = styledComponents.styled.div(_a$2 || (_a$2 = __template$2(["\n position: absolute;\n top: 0;\n left: 0;\n height: 100%;\n width: 100%;\n box-sizing: border-box;\n"])));
258
- var __defProp$6 = Object.defineProperty;
259
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$6(obj, key, {
260
- enumerable: true,
261
- configurable: true,
262
- writable: true,
263
- value
264
- }) : obj[key] = value;
265
- var __publicField$4 = (obj, key, value) => {
266
- __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
267
- return value;
268
- };
269
- class GoogleMap extends React__namespace.default.PureComponent {
136
+ const MapContainer = styled.div`
137
+ position: absolute;
138
+ top: 0;
139
+ left: 0;
140
+ height: 100%;
141
+ width: 100%;
142
+ box-sizing: border-box;
143
+ `;
144
+ var __defProp$4 = Object.defineProperty, __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __publicField$2 = (obj, key, value) => __defNormalProp$4(obj, typeof key != "symbol" ? key + "" : key, value);
145
+ class GoogleMap extends PureComponent {
270
146
  constructor() {
271
- super(...arguments);
272
- __publicField$4(this, "state", {
273
- map: void 0
274
- });
275
- __publicField$4(this, "clickHandler");
276
- __publicField$4(this, "mapRef", React__namespace.default.createRef());
277
- __publicField$4(this, "mapEl", null);
278
- __publicField$4(this, "attachClickHandler", () => {
147
+ super(...arguments), __publicField$2(this, "state", { map: void 0 }), __publicField$2(this, "clickHandler"), __publicField$2(this, "mapRef", createRef()), __publicField$2(this, "mapEl", null), __publicField$2(this, "attachClickHandler", () => {
279
148
  const map = this.state.map;
280
- if (!map) {
149
+ if (!map)
281
150
  return;
282
- }
283
- const {
284
- api,
285
- onClick
286
- } = this.props;
287
- const {
288
- event
289
- } = api;
290
- if (this.clickHandler) {
291
- this.clickHandler.remove();
292
- }
293
- if (onClick) {
294
- this.clickHandler = event.addListener(map, "click", onClick);
295
- }
296
- });
297
- __publicField$4(this, "setMapElement", element => {
151
+ const { api, onClick } = this.props, { event } = api;
152
+ this.clickHandler && this.clickHandler.remove(), onClick && (this.clickHandler = event.addListener(map, "click", onClick));
153
+ }), __publicField$2(this, "setMapElement", (element) => {
298
154
  if (element && element !== this.mapEl) {
299
155
  const map = this.constructMap(element);
300
- this.setState({
301
- map
302
- }, this.attachClickHandler);
156
+ this.setState({ map }, this.attachClickHandler);
303
157
  }
304
158
  this.mapEl = element;
305
159
  });
@@ -309,761 +163,447 @@ class GoogleMap extends React__namespace.default.PureComponent {
309
163
  }
310
164
  componentDidUpdate(prevProps) {
311
165
  const map = this.state.map;
312
- if (!map) {
166
+ if (!map)
313
167
  return;
314
- }
315
- const {
316
- onClick,
317
- location,
318
- bounds
319
- } = this.props;
320
- if (prevProps.onClick !== onClick) {
321
- this.attachClickHandler();
322
- }
323
- if (!latLngAreEqual(prevProps.location, location)) {
324
- map.panTo(this.getCenter());
325
- }
326
- if (bounds && (!prevProps.bounds || !bounds.equals(prevProps.bounds))) {
327
- map.fitBounds(bounds);
328
- }
168
+ const { onClick, location, bounds } = this.props;
169
+ prevProps.onClick !== onClick && this.attachClickHandler(), latLngAreEqual(prevProps.location, location) || map.panTo(this.getCenter()), bounds && (!prevProps.bounds || !bounds.equals(prevProps.bounds)) && map.fitBounds(bounds);
329
170
  }
330
171
  componentWillUnmount() {
331
- if (this.clickHandler) {
332
- this.clickHandler.remove();
333
- }
172
+ this.clickHandler && this.clickHandler.remove();
334
173
  }
335
174
  getCenter() {
336
- const {
337
- location,
338
- api
339
- } = this.props;
175
+ const { location, api } = this.props;
340
176
  return new api.LatLng(location.lat, location.lng);
341
177
  }
342
178
  constructMap(el) {
343
- const {
344
- defaultZoom,
345
- api,
346
- mapTypeControl,
347
- controlSize,
348
- bounds,
349
- scrollWheel
350
- } = this.props;
351
- const map = new api.Map(el, {
179
+ const { defaultZoom, api, mapTypeControl, controlSize, bounds, scrollWheel } = this.props, map = new api.Map(el, {
352
180
  zoom: defaultZoom,
353
181
  center: this.getCenter(),
354
182
  scrollwheel: scrollWheel,
355
- streetViewControl: false,
183
+ streetViewControl: !1,
356
184
  mapTypeControl,
357
185
  controlSize
358
186
  });
359
- if (bounds) {
360
- map.fitBounds(bounds);
361
- }
362
- return map;
187
+ return bounds && map.fitBounds(bounds), map;
363
188
  }
364
189
  render() {
365
- const {
366
- children
367
- } = this.props;
368
- const {
369
- map
370
- } = this.state;
371
- return /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
372
- children: [/* @__PURE__ */jsxRuntime.jsx(MapContainer, {
373
- ref: this.setMapElement
374
- }), children && map ? children(map) : null]
375
- });
190
+ const { children } = this.props, { map } = this.state;
191
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
192
+ /* @__PURE__ */ jsx(MapContainer, { ref: this.setMapElement }),
193
+ children && map ? children(map) : null
194
+ ] });
376
195
  }
377
196
  }
378
- __publicField$4(GoogleMap, "defaultProps", {
197
+ __publicField$2(GoogleMap, "defaultProps", {
379
198
  defaultZoom: 8,
380
- scrollWheel: true
199
+ scrollWheel: !0
381
200
  });
382
- var __defProp$5 = Object.defineProperty;
383
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$5(obj, key, {
384
- enumerable: true,
385
- configurable: true,
386
- writable: true,
387
- value
388
- }) : obj[key] = value;
389
- var __publicField$3 = (obj, key, value) => {
390
- __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
391
- return value;
392
- };
201
+ var __defProp$3 = Object.defineProperty, __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __publicField$1 = (obj, key, value) => __defNormalProp$3(obj, typeof key != "symbol" ? key + "" : key, value);
393
202
  const markerPath = "M 3.052 3.7 C 1.56 5.293 0.626 7.612 0.663 9.793 C 0.738 14.352 2.793 16.077 6.078 22.351 C 7.263 25.111 8.497 28.032 9.672 32.871 C 9.835 33.584 9.994 34.246 10.069 34.305 C 10.143 34.362 10.301 33.697 10.465 32.983 C 11.639 28.145 12.875 25.226 14.059 22.466 C 17.344 16.192 19.398 14.466 19.474 9.908 C 19.511 7.727 18.574 5.405 17.083 3.814 C 15.379 1.994 12.809 0.649 10.069 0.593 C 7.328 0.536 4.756 1.882 3.052 3.7 Z";
394
- class Marker extends React__namespace.PureComponent {
203
+ class Marker extends PureComponent {
395
204
  constructor() {
396
- super(...arguments);
397
- __publicField$3(this, "marker");
398
- __publicField$3(this, "eventHandlers", {});
205
+ super(...arguments), __publicField$1(this, "marker"), __publicField$1(this, "eventHandlers", {});
399
206
  }
400
207
  componentDidMount() {
401
- const {
402
- position,
403
- api,
404
- map,
405
- onMove,
406
- zIndex,
407
- opacity,
408
- label,
409
- markerRef,
410
- color
411
- } = this.props;
412
- const {
413
- Marker: GMarker
414
- } = api;
208
+ const { position, api, map, onMove, zIndex, opacity, label, markerRef, color } = this.props, { Marker: GMarker } = api;
415
209
  let icon;
416
- if (color) {
417
- icon = {
418
- path: markerPath,
419
- fillOpacity: 1,
420
- fillColor: color.background,
421
- strokeColor: color.border,
422
- strokeWeight: 2,
423
- anchor: new api.Point(10, 35),
424
- labelOrigin: new api.Point(10, 11)
425
- };
426
- }
427
- this.marker = new GMarker({
428
- draggable: Boolean(onMove),
210
+ color && (icon = {
211
+ path: markerPath,
212
+ fillOpacity: 1,
213
+ fillColor: color.background,
214
+ strokeColor: color.border,
215
+ strokeWeight: 2,
216
+ anchor: new api.Point(10, 35),
217
+ labelOrigin: new api.Point(10, 11)
218
+ }), this.marker = new GMarker({
219
+ draggable: !!onMove,
429
220
  position,
430
221
  map,
431
222
  zIndex,
432
223
  opacity,
433
224
  label,
434
225
  icon
435
- });
436
- if (markerRef) {
437
- markerRef.current = this.marker;
438
- }
439
- this.attachMoveHandler();
440
- this.attachClickHandler();
226
+ }), markerRef && (markerRef.current = this.marker), this.attachMoveHandler(), this.attachClickHandler();
441
227
  }
442
228
  componentDidUpdate(prevProps) {
443
- if (!this.marker) {
229
+ if (!this.marker)
444
230
  return;
445
- }
446
- const {
447
- position,
448
- onMove,
449
- label,
450
- zIndex,
451
- opacity,
452
- map
453
- } = this.props;
454
- if (prevProps.onMove !== onMove) {
455
- this.attachMoveHandler();
456
- }
457
- if (!latLngAreEqual(prevProps.position, position)) {
458
- this.marker.setPosition(position);
459
- }
460
- if (prevProps.label !== label) {
461
- this.marker.setLabel(label || null);
462
- }
463
- if (prevProps.zIndex !== zIndex) {
464
- this.marker.setZIndex(zIndex || null);
465
- }
466
- if (prevProps.opacity !== opacity) {
467
- this.marker.setOpacity(opacity || null);
468
- }
469
- if (prevProps.map !== map) {
470
- this.marker.setMap(map);
471
- }
231
+ const { position, onMove, label, zIndex, opacity, map } = this.props;
232
+ prevProps.onMove !== onMove && this.attachMoveHandler(), latLngAreEqual(prevProps.position, position) || this.marker.setPosition(position), prevProps.label !== label && this.marker.setLabel(label || null), prevProps.zIndex !== zIndex && this.marker.setZIndex(zIndex || null), prevProps.opacity !== opacity && this.marker.setOpacity(opacity || null), prevProps.map !== map && this.marker.setMap(map);
472
233
  }
473
234
  componentWillUnmount() {
474
- if (this.eventHandlers.move) {
475
- this.eventHandlers.move.remove();
476
- }
477
- if (this.marker) {
478
- this.marker.setMap(null);
479
- }
235
+ this.eventHandlers.move && this.eventHandlers.move.remove(), this.marker && this.marker.setMap(null);
480
236
  }
481
237
  attachMoveHandler() {
482
- const {
483
- api,
484
- onMove
485
- } = this.props;
486
- if (this.eventHandlers.move) {
487
- this.eventHandlers.move.remove();
488
- }
489
- if (this.marker && onMove) {
490
- this.eventHandlers.move = api.event.addListener(this.marker, "dragend", onMove);
491
- }
238
+ const { api, onMove } = this.props;
239
+ this.eventHandlers.move && this.eventHandlers.move.remove(), this.marker && onMove && (this.eventHandlers.move = api.event.addListener(this.marker, "dragend", onMove));
492
240
  }
493
241
  attachClickHandler() {
494
- const {
495
- api,
496
- onClick
497
- } = this.props;
498
- if (this.eventHandlers.click) {
499
- this.eventHandlers.click.remove();
500
- }
501
- if (this.marker && onClick) {
502
- this.eventHandlers.click = api.event.addListener(this.marker, "click", onClick);
503
- }
242
+ const { api, onClick } = this.props;
243
+ this.eventHandlers.click && this.eventHandlers.click.remove(), this.marker && onClick && (this.eventHandlers.click = api.event.addListener(this.marker, "click", onClick));
504
244
  }
505
245
  // eslint-disable-next-line class-methods-use-this
506
246
  render() {
507
247
  return null;
508
248
  }
509
249
  }
510
- var __defProp$4 = Object.defineProperty;
511
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$4(obj, key, {
512
- enumerable: true,
513
- configurable: true,
514
- writable: true,
515
- value
516
- }) : obj[key] = value;
517
- var __publicField$2 = (obj, key, value) => {
518
- __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
519
- return value;
520
- };
521
- const fallbackLatLng = {
522
- lat: 40.7058254,
523
- lng: -74.1180863
250
+ var __defProp$2 = Object.defineProperty, __getOwnPropSymbols$1 = Object.getOwnPropertySymbols, __hasOwnProp$1 = Object.prototype.hasOwnProperty, __propIsEnum$1 = Object.prototype.propertyIsEnumerable, __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$1 = (a, b) => {
251
+ for (var prop in b || (b = {}))
252
+ __hasOwnProp$1.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
253
+ if (__getOwnPropSymbols$1)
254
+ for (var prop of __getOwnPropSymbols$1(b))
255
+ __propIsEnum$1.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
256
+ return a;
524
257
  };
525
- class GeopointSelect extends React__namespace.default.PureComponent {
526
- constructor() {
527
- super(...arguments);
528
- __publicField$2(this, "mapRef", React__namespace.default.createRef());
529
- __publicField$2(this, "handlePlaceChanged", place => {
258
+ const fallbackLatLng = { lat: 40.7058254, lng: -74.1180863 }, GeopointSelect = ({
259
+ api,
260
+ value,
261
+ onChange,
262
+ defaultLocation = { lng: 10.74609, lat: 59.91273 },
263
+ defaultZoom = 8
264
+ }) => {
265
+ const getCenter2 = useCallback(() => __spreadValues$1(__spreadValues$1(__spreadValues$1({}, fallbackLatLng), defaultLocation), value), [value, defaultLocation]), setValue = useCallback(
266
+ (geoPoint) => {
267
+ onChange && onChange(geoPoint);
268
+ },
269
+ [onChange]
270
+ ), handlePlaceChanged = useCallback(
271
+ (place) => {
530
272
  var _a;
531
- if (!((_a = place.geometry) == null ? void 0 : _a.location)) {
532
- return;
273
+ (_a = place.geometry) != null && _a.location && setValue(place.geometry.location);
274
+ },
275
+ [setValue]
276
+ ), handleMarkerDragEnd = useCallback(
277
+ (event) => {
278
+ event.latLng && setValue(event.latLng);
279
+ },
280
+ [setValue]
281
+ ), handleMapClick = useCallback(
282
+ (event) => {
283
+ event.latLng && setValue(event.latLng);
284
+ },
285
+ [setValue]
286
+ );
287
+ return /* @__PURE__ */ jsx(GoogleMap, { api, location: getCenter2(), onClick: handleMapClick, defaultZoom, children: (map) => /* @__PURE__ */ jsxs(Fragment, { children: [
288
+ /* @__PURE__ */ jsx(SearchInput, { api, map, onChange: handlePlaceChanged }),
289
+ value && /* @__PURE__ */ jsx(
290
+ Marker,
291
+ {
292
+ api,
293
+ map,
294
+ position: value,
295
+ onMove: onChange ? handleMarkerDragEnd : void 0
533
296
  }
534
- this.setValue(place.geometry.location);
535
- });
536
- __publicField$2(this, "handleMarkerDragEnd", event => {
537
- if (event.latLng) this.setValue(event.latLng);
538
- });
539
- __publicField$2(this, "handleMapClick", event => {
540
- if (event.latLng) this.setValue(event.latLng);
541
- });
542
- }
543
- getCenter() {
544
- const {
545
- value = {},
546
- defaultLocation = {}
547
- } = this.props;
548
- const point = {
549
- ...fallbackLatLng,
550
- ...defaultLocation,
551
- ...value
552
- };
553
- return point;
554
- }
555
- setValue(geoPoint) {
556
- if (this.props.onChange) {
557
- this.props.onChange(geoPoint);
558
- }
559
- }
560
- render() {
561
- const {
562
- api,
563
- defaultZoom,
564
- value,
565
- onChange
566
- } = this.props;
567
- return /* @__PURE__ */jsxRuntime.jsx(GoogleMap, {
568
- api,
569
- location: this.getCenter(),
570
- onClick: this.handleMapClick,
571
- defaultZoom,
572
- children: map => /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
573
- children: [/* @__PURE__ */jsxRuntime.jsx(SearchInput, {
574
- api,
575
- map,
576
- onChange: this.handlePlaceChanged
577
- }), value && /* @__PURE__ */jsxRuntime.jsx(Marker, {
578
- api,
579
- map,
580
- position: value,
581
- onMove: onChange ? this.handleMarkerDragEnd : void 0
582
- })]
583
- })
584
- });
585
- }
586
- }
587
- __publicField$2(GeopointSelect, "defaultProps", {
588
- defaultZoom: 8,
589
- defaultLocation: {
590
- lng: 10.74609,
591
- lat: 59.91273
592
- }
593
- });
594
- var __freeze$1 = Object.freeze;
595
- var __defProp$3 = Object.defineProperty;
596
- var __template$1 = (cooked, raw) => __freeze$1(__defProp$3(cooked, "raw", {
597
- value: __freeze$1(raw || cooked.slice())
598
- }));
599
- var _a$1, _b;
600
- const PreviewImage = styledComponents.styled.img(_a$1 || (_a$1 = __template$1(["\n width: 100%;\n height: auto;\n vertical-align: top;\n"])));
601
- const DialogInnerContainer = styledComponents.styled.div(_b || (_b = __template$1(["\n height: 40rem;\n width: 50rem;\n"])));
602
- let config;
603
- function getGeoConfig() {
604
- return config;
605
- }
606
- function setGeoConfig(newConfig) {
607
- config = newConfig;
608
- }
609
- var __defProp$2 = Object.defineProperty;
610
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$2(obj, key, {
611
- enumerable: true,
612
- configurable: true,
613
- writable: true,
614
- value
615
- }) : obj[key] = value;
616
- var __publicField$1 = (obj, key, value) => {
617
- __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
618
- return value;
619
- };
620
- const getStaticImageUrl = (value, apiKey) => {
621
- const loc = "".concat(value.lat, ",").concat(value.lng);
622
- const params = {
297
+ )
298
+ ] }) });
299
+ }, EMPTY_PATH = [], getStaticImageUrl = (value, apiKey) => {
300
+ const loc = `${value.lat},${value.lng}`;
301
+ return `https://maps.googleapis.com/maps/api/staticmap?${new URLSearchParams({
623
302
  key: apiKey,
624
303
  center: loc,
625
304
  markers: loc,
626
- zoom: 13,
627
- scale: 2,
305
+ zoom: "13",
306
+ scale: "2",
628
307
  size: "640x300"
629
- };
630
- const qs = Object.keys(params).reduce((res, param) => {
631
- return res.concat("".concat(param, "=").concat(encodeURIComponent(params[param])));
632
- }, []);
633
- return "https://maps.googleapis.com/maps/api/staticmap?".concat(qs.join("&"));
308
+ }).toString()}`;
634
309
  };
635
- class GeopointInput extends React__namespace.default.PureComponent {
636
- constructor(props) {
637
- super(props);
638
- __publicField$1(this, "_geopointInputId", lodash.uniqueId("GeopointInput"));
639
- __publicField$1(this, "editButton");
640
- __publicField$1(this, "setEditButton", el => {
641
- this.editButton = el;
642
- });
643
- __publicField$1(this, "handleToggleModal", () => {
644
- this.setState(prevState => ({
645
- modalOpen: !prevState.modalOpen
646
- }));
647
- });
648
- __publicField$1(this, "handleCloseModal", () => {
649
- this.setState({
650
- modalOpen: false
651
- });
652
- });
653
- __publicField$1(this, "handleChange", latLng => {
654
- const {
655
- schemaType,
656
- onChange
657
- } = this.props;
658
- onChange([sanity.setIfMissing({
659
- _type: schemaType.name
660
- }), sanity.set(latLng.lat(), ["lat"]), sanity.set(latLng.lng(), ["lng"])]);
661
- });
662
- __publicField$1(this, "handleClear", () => {
663
- const {
664
- onChange
665
- } = this.props;
666
- onChange(sanity.unset());
667
- });
668
- this.state = {
669
- modalOpen: false
670
- };
671
- }
672
- focus() {
673
- if (this.editButton) {
674
- this.editButton.focus();
675
- }
676
- }
677
- render() {
678
- const {
679
- value,
680
- readOnly,
681
- geoConfig: config,
682
- path,
683
- changed,
684
- focused
685
- } = this.props;
686
- const {
687
- modalOpen
688
- } = this.state;
689
- if (!config || !config.apiKey) {
690
- return /* @__PURE__ */jsxRuntime.jsxs("div", {
691
- children: [/* @__PURE__ */jsxRuntime.jsxs("p", {
692
- children: ["The ", /* @__PURE__ */jsxRuntime.jsx("a", {
693
- href: "https://sanity.io/docs/schema-types/geopoint-type",
694
- children: "Geopoint type"
695
- }), " needs a Google Maps API key with access to:"]
696
- }), /* @__PURE__ */jsxRuntime.jsxs("ul", {
697
- children: [/* @__PURE__ */jsxRuntime.jsx("li", {
698
- children: "Google Maps JavaScript API"
699
- }), /* @__PURE__ */jsxRuntime.jsx("li", {
700
- children: "Google Places API Web Service"
701
- }), /* @__PURE__ */jsxRuntime.jsx("li", {
702
- children: "Google Static Maps API"
703
- })]
704
- }), /* @__PURE__ */jsxRuntime.jsx("p", {
705
- children: "Please enter the API key with access to these services in your googleMapsInput plugin config."
706
- })]
707
- });
708
- }
709
- return /* @__PURE__ */jsxRuntime.jsxs(ui.Stack, {
710
- space: 3,
711
- children: [value && /* @__PURE__ */jsxRuntime.jsx(sanity.ChangeIndicator, {
712
- path,
713
- isChanged: changed,
714
- hasFocus: !!focused,
715
- children: /* @__PURE__ */jsxRuntime.jsx(PreviewImage, {
716
- src: getStaticImageUrl(value, config.apiKey),
717
- alt: "Map location"
718
- })
719
- }), /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
720
- children: /* @__PURE__ */jsxRuntime.jsxs(ui.Grid, {
721
- columns: value ? 2 : 1,
722
- gap: 3,
723
- children: [/* @__PURE__ */jsxRuntime.jsx(ui.Button, {
724
- mode: "ghost",
725
- icon: value && icons.EditIcon,
726
- padding: 3,
727
- ref: this.setEditButton,
728
- text: value ? "Edit" : "Set location",
729
- onClick: this.handleToggleModal,
730
- disabled: readOnly
731
- }), value && /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
732
- tone: "critical",
733
- icon: icons.TrashIcon,
734
- padding: 3,
735
- mode: "ghost",
736
- text: "Remove",
737
- onClick: this.handleClear,
738
- disabled: readOnly
739
- })]
740
- })
741
- }), modalOpen && /* @__PURE__ */jsxRuntime.jsx(ui.Dialog, {
742
- id: "".concat(this._geopointInputId, "_dialog"),
743
- onClose: this.handleCloseModal,
310
+ function GeopointInput(props) {
311
+ const {
312
+ changed,
313
+ elementProps,
314
+ focused,
315
+ geoConfig: config2,
316
+ onChange,
317
+ onPathFocus,
318
+ path,
319
+ readOnly,
320
+ schemaType,
321
+ value
322
+ } = props, {
323
+ id,
324
+ ref: inputRef,
325
+ onBlur: handleBlur,
326
+ onFocus: handleFocus,
327
+ "aria-describedby": ariaDescribedBy
328
+ } = elementProps, schemaTypeName = schemaType.name, dialogId = useId(), dialogRef = useRef(null), handleFocusButton = useCallback(() => {
329
+ var _a;
330
+ return (_a = inputRef == null ? void 0 : inputRef.current) == null ? void 0 : _a.focus();
331
+ }, [inputRef]), [modalOpen, setModalOpen] = useState(!1), handleCloseModal = useCallback(() => {
332
+ dialogRef.current && dialogRef.current.blur(), setModalOpen(!1), handleFocusButton();
333
+ }, [setModalOpen, handleFocusButton]), handleToggleModal = useCallback(
334
+ () => setModalOpen((currentState) => !currentState),
335
+ [setModalOpen]
336
+ ), handleChange = useCallback(
337
+ (latLng) => {
338
+ onChange([
339
+ setIfMissing({ _type: schemaTypeName }),
340
+ set(latLng.lat(), ["lat"]),
341
+ set(latLng.lng(), ["lng"])
342
+ ]);
343
+ },
344
+ [schemaTypeName, onChange]
345
+ ), handleClear = useCallback(() => {
346
+ onChange(unset());
347
+ }, [onChange]);
348
+ return useEffect(() => {
349
+ modalOpen && onPathFocus(EMPTY_PATH);
350
+ }, [modalOpen, onPathFocus]), !config2 || !config2.apiKey ? /* @__PURE__ */ jsxs("div", { children: [
351
+ /* @__PURE__ */ jsxs("p", { children: [
352
+ "The ",
353
+ /* @__PURE__ */ jsx("a", { href: "https://sanity.io/docs/schema-types/geopoint-type", children: "Geopoint type" }),
354
+ " needs a Google Maps API key with access to:"
355
+ ] }),
356
+ /* @__PURE__ */ jsxs("ul", { children: [
357
+ /* @__PURE__ */ jsx("li", { children: "Google Maps JavaScript API" }),
358
+ /* @__PURE__ */ jsx("li", { children: "Google Places API Web Service" }),
359
+ /* @__PURE__ */ jsx("li", { children: "Google Static Maps API" })
360
+ ] }),
361
+ /* @__PURE__ */ jsx("p", { children: "Please enter the API key with access to these services in your googleMapsInput plugin config." })
362
+ ] }) : /* @__PURE__ */ jsxs(Stack, { space: 3, children: [
363
+ value && /* @__PURE__ */ jsx(ChangeIndicator, { path, isChanged: changed, hasFocus: !!focused, children: /* @__PURE__ */ jsx(
364
+ PreviewImage,
365
+ {
366
+ src: getStaticImageUrl(value, config2.apiKey),
367
+ alt: "Map location",
368
+ onClick: handleFocusButton,
369
+ onDoubleClick: handleToggleModal
370
+ }
371
+ ) }),
372
+ /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsxs(Grid, { columns: value ? 2 : 1, gap: 3, children: [
373
+ /* @__PURE__ */ jsx(
374
+ Button,
375
+ {
376
+ "aria-describedby": ariaDescribedBy,
377
+ disabled: readOnly,
378
+ icon: value && EditIcon,
379
+ id,
380
+ mode: "ghost",
381
+ onClick: handleToggleModal,
382
+ onFocus: handleFocus,
383
+ padding: 3,
384
+ ref: inputRef,
385
+ text: value ? "Edit" : "Set location"
386
+ }
387
+ ),
388
+ value && /* @__PURE__ */ jsx(
389
+ Button,
390
+ {
391
+ disabled: readOnly,
392
+ icon: TrashIcon,
393
+ mode: "ghost",
394
+ onClick: handleClear,
395
+ padding: 3,
396
+ text: "Remove",
397
+ tone: "critical"
398
+ }
399
+ )
400
+ ] }) }),
401
+ modalOpen && /* @__PURE__ */ jsx(
402
+ Dialog,
403
+ {
744
404
  header: "Place the marker on the map",
405
+ id: `${dialogId}_dialog`,
406
+ onBlur: handleBlur,
407
+ onClose: handleCloseModal,
408
+ ref: dialogRef,
745
409
  width: 1,
746
- children: /* @__PURE__ */jsxRuntime.jsx(DialogInnerContainer, {
747
- children: /* @__PURE__ */jsxRuntime.jsx(GoogleMapsLoadProxy, {
748
- config: getGeoConfig(),
749
- children: api => /* @__PURE__ */jsxRuntime.jsx(GeopointSelect, {
750
- api,
751
- value: value || void 0,
752
- onChange: readOnly ? void 0 : this.handleChange,
753
- defaultLocation: config.defaultLocation,
754
- defaultZoom: config.defaultZoom
755
- })
756
- })
757
- })
758
- })]
759
- });
760
- }
410
+ children: /* @__PURE__ */ jsx(DialogInnerContainer, { children: /* @__PURE__ */ jsx(GoogleMapsLoadProxy, { config: getGeoConfig(), children: (api) => /* @__PURE__ */ jsx(
411
+ GeopointSelect,
412
+ {
413
+ api,
414
+ value: value || void 0,
415
+ onChange: readOnly ? void 0 : handleChange,
416
+ defaultLocation: config2.defaultLocation,
417
+ defaultZoom: config2.defaultZoom
418
+ }
419
+ ) }) })
420
+ }
421
+ )
422
+ ] });
761
423
  }
762
- var __defProp$1 = Object.defineProperty;
763
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp$1(obj, key, {
764
- enumerable: true,
765
- configurable: true,
766
- writable: true,
767
- value
768
- }) : obj[key] = value;
769
- var __publicField = (obj, key, value) => {
770
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
771
- return value;
772
- };
773
- class Arrow extends React__namespace.PureComponent {
424
+ var __defProp$1 = Object.defineProperty, __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __publicField = (obj, key, value) => __defNormalProp$1(obj, typeof key != "symbol" ? key + "" : key, value);
425
+ class Arrow extends PureComponent {
774
426
  constructor() {
775
- super(...arguments);
776
- __publicField(this, "line");
777
- __publicField(this, "eventHandlers", {});
427
+ super(...arguments), __publicField(this, "line"), __publicField(this, "eventHandlers", {});
778
428
  }
779
429
  componentDidMount() {
780
- const {
781
- from,
782
- to,
783
- api,
784
- map,
785
- zIndex,
786
- onClick,
787
- color,
788
- arrowRef
789
- } = this.props;
790
- const lineSymbol = {
430
+ const { from, to, api, map, zIndex, onClick, color, arrowRef } = this.props, lineSymbol = {
791
431
  path: api.SymbolPath.FORWARD_OPEN_ARROW
792
432
  };
793
433
  this.line = new api.Polyline({
794
434
  map,
795
435
  zIndex,
796
436
  path: [from, to],
797
- icons: [{
798
- icon: lineSymbol,
799
- offset: "50%"
800
- }],
437
+ icons: [{ icon: lineSymbol, offset: "50%" }],
801
438
  strokeOpacity: 0.55,
802
439
  strokeColor: color ? color.text : "black"
803
- });
804
- if (onClick) {
805
- this.eventHandlers.click = api.event.addListener(this.line, "click", onClick);
806
- }
807
- if (arrowRef) {
808
- arrowRef.current = this.line;
809
- }
440
+ }), onClick && (this.eventHandlers.click = api.event.addListener(this.line, "click", onClick)), arrowRef && (arrowRef.current = this.line);
810
441
  }
811
442
  componentDidUpdate(prevProps) {
812
- if (!this.line) {
443
+ if (!this.line)
813
444
  return;
814
- }
815
- const {
816
- from,
817
- to,
818
- map
819
- } = this.props;
820
- if (!latLngAreEqual(prevProps.from, from) || !latLngAreEqual(prevProps.to, to)) {
821
- this.line.setPath([from, to]);
822
- }
823
- if (prevProps.map !== map) {
824
- this.line.setMap(map);
825
- }
445
+ const { from, to, map } = this.props;
446
+ (!latLngAreEqual(prevProps.from, from) || !latLngAreEqual(prevProps.to, to)) && this.line.setPath([from, to]), prevProps.map !== map && this.line.setMap(map);
826
447
  }
827
448
  componentWillUnmount() {
828
- if (this.line) {
829
- this.line.setMap(null);
830
- }
831
- if (this.eventHandlers.click) {
832
- this.eventHandlers.click.remove();
833
- }
449
+ this.line && this.line.setMap(null), this.eventHandlers.click && this.eventHandlers.click.remove();
834
450
  }
835
451
  // eslint-disable-next-line class-methods-use-this
836
452
  render() {
837
453
  return null;
838
454
  }
839
455
  }
840
- function GeopointMove(_ref) {
841
- let {
842
- diff,
843
- api,
844
- map,
845
- label
846
- } = _ref;
847
- const {
848
- fromValue: from,
849
- toValue: to
850
- } = diff;
851
- const annotation = diff.isChanged ? diff.annotation : void 0;
852
- const userColor = sanity.useUserColor(annotation ? annotation.author : null) || void 0;
853
- const fromRef = React__namespace.useRef();
854
- const toRef = React__namespace.useRef();
855
- return /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
856
- children: [from && /* @__PURE__ */jsxRuntime.jsx(Marker, {
857
- api,
858
- map,
859
- position: from,
860
- zIndex: 0,
861
- opacity: 0.55,
862
- markerRef: fromRef,
863
- color: userColor
864
- }), from && to && /* @__PURE__ */jsxRuntime.jsx(Arrow, {
865
- api,
866
- map,
867
- from,
868
- to,
869
- zIndex: 1,
870
- color: userColor
871
- }), to && /* @__PURE__ */jsxRuntime.jsx(Marker, {
872
- api,
873
- map,
874
- position: to,
875
- zIndex: 2,
876
- markerRef: toRef,
877
- label,
878
- color: userColor
879
- })]
880
- });
881
- }
882
- var __freeze = Object.freeze;
883
- var __defProp = Object.defineProperty;
884
- var __template = (cooked, raw) => __freeze(__defProp(cooked, "raw", {
885
- value: __freeze(raw || cooked.slice())
886
- }));
887
- var _a;
888
- const RootContainer = styledComponents.styled.div(_a || (_a = __template(["\n position: relative;\n min-height: 200px;\n\n &:empty {\n background-color: var(--card-skeleton-color-from);\n display: table;\n width: 100%;\n }\n\n &:empty:after {\n content: 'Missing/invalid data';\n display: table-cell;\n vertical-align: middle;\n text-align: center;\n position: relative;\n }\n"])));
889
- const GeopointArrayDiff = _ref2 => {
890
- let {
891
- diff,
892
- schemaType
893
- } = _ref2;
894
- return /* @__PURE__ */jsxRuntime.jsx(RootContainer, {
895
- children: /* @__PURE__ */jsxRuntime.jsx(GoogleMapsLoadProxy, {
896
- config: getGeoConfig(),
897
- children: api => /* @__PURE__ */jsxRuntime.jsx(GeopointDiff$1, {
456
+ function GeopointMove({ diff, api, map, label }) {
457
+ const { fromValue: from, toValue: to } = diff, annotation = diff.isChanged ? diff.annotation : void 0, userColor = useUserColor(annotation ? annotation.author : null) || void 0, fromRef = useRef(), toRef = useRef();
458
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
459
+ from && /* @__PURE__ */ jsx(
460
+ Marker,
461
+ {
898
462
  api,
899
- diff,
900
- schemaType
901
- })
902
- })
903
- });
904
- };
905
- function GeopointDiff$1(_ref3) {
906
- let {
907
- api,
908
- diff
909
- } = _ref3;
910
- const fromValue = (diff.fromValue || []).filter(hasCoordinates);
911
- const toValue = (diff.toValue || []).filter(hasCoordinates);
912
- if (fromValue.length === 0 && toValue.length === 0) {
913
- return null;
463
+ map,
464
+ position: from,
465
+ zIndex: 0,
466
+ opacity: 0.55,
467
+ markerRef: fromRef,
468
+ color: userColor
469
+ }
470
+ ),
471
+ from && to && /* @__PURE__ */ jsx(Arrow, { api, map, from, to, zIndex: 1, color: userColor }),
472
+ to && /* @__PURE__ */ jsx(
473
+ Marker,
474
+ {
475
+ api,
476
+ map,
477
+ position: to,
478
+ zIndex: 2,
479
+ markerRef: toRef,
480
+ label,
481
+ color: userColor
482
+ }
483
+ )
484
+ ] });
485
+ }
486
+ const RootContainer = styled.div`
487
+ position: relative;
488
+ min-height: 200px;
489
+
490
+ &:empty {
491
+ background-color: var(--card-skeleton-color-from);
492
+ display: table;
493
+ width: 100%;
914
494
  }
495
+
496
+ &:empty:after {
497
+ content: 'Missing/invalid data';
498
+ display: table-cell;
499
+ vertical-align: middle;
500
+ text-align: center;
501
+ position: relative;
502
+ }
503
+ `, GeopointArrayDiff = ({
504
+ diff,
505
+ schemaType
506
+ }) => /* @__PURE__ */ jsx(RootContainer, { children: /* @__PURE__ */ jsx(GoogleMapsLoadProxy, { config: getGeoConfig(), children: (api) => /* @__PURE__ */ jsx(GeopointDiff$1, { api, diff, schemaType }) }) });
507
+ function GeopointDiff$1({ api, diff }) {
508
+ const fromValue = (diff.fromValue || []).filter(hasCoordinates), toValue = (diff.toValue || []).filter(hasCoordinates);
509
+ if (fromValue.length === 0 && toValue.length === 0)
510
+ return null;
915
511
  const bounds = getBounds$1(fromValue, toValue, api);
916
- return /* @__PURE__ */jsxRuntime.jsx(GoogleMap, {
917
- api,
918
- location: bounds.getCenter().toJSON(),
919
- mapTypeControl: false,
920
- controlSize: 20,
921
- bounds,
922
- children: map => /* @__PURE__ */jsxRuntime.jsx(jsxRuntime.Fragment, {
923
- children: diff.items.map(_ref4 => {
924
- let {
925
- toIndex,
926
- diff: pointDiff
927
- } = _ref4;
928
- if (!isChangeDiff(pointDiff)) {
929
- return null;
930
- }
931
- return /* @__PURE__ */jsxRuntime.jsx(GeopointMove, {
512
+ return /* @__PURE__ */ jsx(
513
+ GoogleMap,
514
+ {
515
+ api,
516
+ location: bounds.getCenter().toJSON(),
517
+ mapTypeControl: !1,
518
+ controlSize: 20,
519
+ bounds,
520
+ children: (map) => /* @__PURE__ */ jsx(Fragment, { children: diff.items.map(({ toIndex, diff: pointDiff }) => isChangeDiff(pointDiff) ? /* @__PURE__ */ jsx(
521
+ GeopointMove,
522
+ {
932
523
  api,
933
524
  map,
934
525
  diff: pointDiff,
935
- label: "".concat(toIndex)
936
- }, toIndex);
937
- })
938
- })
939
- });
526
+ label: `${toIndex}`
527
+ },
528
+ toIndex
529
+ ) : null) })
530
+ }
531
+ );
940
532
  }
941
533
  function isChangeDiff(diff) {
942
534
  return diff.action !== "unchanged" && diff.type === "object";
943
535
  }
944
536
  function hasCoordinates(point) {
945
- return typeof point.lat === "number" && typeof point.lng === "number";
537
+ return typeof point.lat == "number" && typeof point.lng == "number";
946
538
  }
947
539
  function getBounds$1(fromValue, toValue, api) {
948
540
  const bounds = new api.LatLngBounds();
949
- const points = [...(fromValue || []), ...(toValue || [])];
950
- points.forEach(point => bounds.extend(point));
951
- return bounds;
541
+ return [...fromValue || [], ...toValue || []].forEach((point) => bounds.extend(point)), bounds;
952
542
  }
953
- const GeopointFieldDiff = _ref5 => {
954
- let {
955
- diff,
956
- schemaType
957
- } = _ref5;
958
- return /* @__PURE__ */jsxRuntime.jsx(RootContainer, {
959
- children: /* @__PURE__ */jsxRuntime.jsx(GoogleMapsLoadProxy, {
960
- config: getGeoConfig(),
961
- children: api => /* @__PURE__ */jsxRuntime.jsx(GeopointDiff, {
962
- api,
963
- diff,
964
- schemaType
965
- })
966
- })
967
- });
968
- };
969
- function GeopointDiff(_ref6) {
970
- let {
971
- api,
972
- diff
973
- } = _ref6;
974
- const {
975
- fromValue,
976
- toValue
977
- } = diff;
978
- const annotation = sanity.getAnnotationAtPath(diff, ["lat"]) || sanity.getAnnotationAtPath(diff, ["lng"]) || sanity.getAnnotationAtPath(diff, []);
979
- const center = getCenter(diff, api);
980
- const bounds = fromValue && toValue ? getBounds(fromValue, toValue, api) : void 0;
981
- return /* @__PURE__ */jsxRuntime.jsx(sanity.DiffTooltip, {
982
- annotations: annotation ? [annotation] : [],
983
- description: getAction(diff),
984
- children: /* @__PURE__ */jsxRuntime.jsx("div", {
985
- children: /* @__PURE__ */jsxRuntime.jsx(GoogleMap, {
986
- api,
987
- location: center,
988
- mapTypeControl: false,
989
- controlSize: 20,
990
- bounds,
991
- scrollWheel: false,
992
- children: map => /* @__PURE__ */jsxRuntime.jsx(GeopointMove, {
993
- api,
994
- map,
995
- diff
996
- })
997
- })
998
- })
999
- });
543
+ const GeopointFieldDiff = ({
544
+ diff,
545
+ schemaType
546
+ }) => /* @__PURE__ */ jsx(RootContainer, { children: /* @__PURE__ */ jsx(GoogleMapsLoadProxy, { config: getGeoConfig(), children: (api) => /* @__PURE__ */ jsx(GeopointDiff, { api, diff, schemaType }) }) });
547
+ function GeopointDiff({ api, diff }) {
548
+ const { fromValue, toValue } = diff, annotation = getAnnotationAtPath(diff, ["lat"]) || getAnnotationAtPath(diff, ["lng"]) || getAnnotationAtPath(diff, []), center = getCenter(diff, api), bounds = fromValue && toValue ? getBounds(fromValue, toValue, api) : void 0;
549
+ return /* @__PURE__ */ jsx(DiffTooltip, { annotations: annotation ? [annotation] : [], description: getAction(diff), children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(
550
+ GoogleMap,
551
+ {
552
+ api,
553
+ location: center,
554
+ mapTypeControl: !1,
555
+ controlSize: 20,
556
+ bounds,
557
+ scrollWheel: !1,
558
+ children: (map) => /* @__PURE__ */ jsx(GeopointMove, { api, map, diff })
559
+ }
560
+ ) }) });
1000
561
  }
1001
562
  function getBounds(fromValue, toValue, api) {
1002
563
  return new api.LatLngBounds().extend(fromValue).extend(toValue);
1003
564
  }
1004
565
  function getCenter(diff, api) {
1005
- const {
1006
- fromValue,
1007
- toValue
1008
- } = diff;
1009
- if (fromValue && toValue) {
566
+ const { fromValue, toValue } = diff;
567
+ if (fromValue && toValue)
1010
568
  return getBounds(fromValue, toValue, api).getCenter().toJSON();
1011
- }
1012
- if (fromValue) {
569
+ if (fromValue)
1013
570
  return fromValue;
1014
- }
1015
- if (toValue) {
571
+ if (toValue)
1016
572
  return toValue;
1017
- }
1018
573
  throw new Error("Neither a from or a to value present");
1019
574
  }
1020
575
  function getAction(diff) {
1021
- const {
1022
- fromValue,
1023
- toValue
1024
- } = diff;
1025
- if (fromValue && toValue) {
1026
- return "Moved";
1027
- } else if (fromValue) {
1028
- return "Removed";
1029
- } else if (toValue) {
1030
- return "Added";
1031
- }
1032
- return "Unchanged";
576
+ const { fromValue, toValue } = diff;
577
+ return fromValue && toValue ? "Moved" : fromValue ? "Removed" : toValue ? "Added" : "Unchanged";
1033
578
  }
1034
- const googleMapsInput = sanity.definePlugin(config => {
1035
- setGeoConfig(config);
1036
- return {
1037
- name: "google-maps-input",
1038
- form: {
1039
- components: {
1040
- input(props) {
1041
- if (isGeopoint(props.schemaType)) {
1042
- const castedProps = props;
1043
- return /* @__PURE__ */jsxRuntime.jsx(GeopointInput, {
1044
- ...castedProps,
1045
- geoConfig: config
1046
- });
1047
- }
1048
- return props.renderDefault(props);
1049
- }
579
+ var __defProp = Object.defineProperty, __defProps = Object.defineProperties, __getOwnPropDescs = Object.getOwnPropertyDescriptors, __getOwnPropSymbols = Object.getOwnPropertySymbols, __hasOwnProp = Object.prototype.hasOwnProperty, __propIsEnum = Object.prototype.propertyIsEnumerable, __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues = (a, b) => {
580
+ for (var prop in b || (b = {}))
581
+ __hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
582
+ if (__getOwnPropSymbols)
583
+ for (var prop of __getOwnPropSymbols(b))
584
+ __propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
585
+ return a;
586
+ }, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
587
+ const googleMapsInput = definePlugin((config2) => (setGeoConfig(config2), {
588
+ name: "google-maps-input",
589
+ form: {
590
+ components: {
591
+ input(props) {
592
+ return isGeopoint(props.schemaType) ? /* @__PURE__ */ jsx(GeopointInput, __spreadProps(__spreadValues({}, props), { geoConfig: config2 })) : props.renderDefault(props);
1050
593
  }
1051
594
  }
1052
- };
1053
- });
595
+ }
596
+ }));
1054
597
  function isGeopoint(schemaType) {
1055
598
  return isType("geopoint", schemaType);
1056
599
  }
1057
600
  function isType(name, schema) {
1058
- if ((schema == null ? void 0 : schema.name) === name) {
1059
- return true;
1060
- } else if (!(schema == null ? void 0 : schema.name)) {
1061
- return false;
1062
- }
1063
- return isType(name, schema == null ? void 0 : schema.type);
601
+ return (schema == null ? void 0 : schema.name) === name ? !0 : schema != null && schema.name ? isType(name, schema == null ? void 0 : schema.type) : !1;
1064
602
  }
1065
- exports.GeopointArrayDiff = GeopointArrayDiff;
1066
- exports.GeopointFieldDiff = GeopointFieldDiff;
1067
- exports.GeopointInput = GeopointInput;
1068
- exports.googleMapsInput = googleMapsInput;
603
+ export {
604
+ GeopointArrayDiff,
605
+ GeopointFieldDiff,
606
+ GeopointInput,
607
+ googleMapsInput
608
+ };
1069
609
  //# sourceMappingURL=index.js.map