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