@sanity/google-maps-input 2.29.1 → 2.29.4-purple-unicorn.509
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/LICENSE +1 -1
- package/diff/resolver.js +7 -0
- package/input/GeopointInput.js +7 -0
- package/lib/_Marker-353730d0.js +301 -0
- package/lib/_Marker-353730d0.js.map +1 -0
- package/lib/_Marker-ee551fa7.cjs +330 -0
- package/lib/_Marker-ee551fa7.cjs.map +1 -0
- package/lib/_reExport.js +19 -0
- package/lib/diff/resolver.cjs +203 -0
- package/lib/diff/resolver.cjs.map +1 -0
- package/lib/diff/resolver.js +168 -17
- package/lib/diff/resolver.js.map +1 -0
- package/{dist/dts → lib/dts/src}/diff/GeopointArrayDiff.d.ts +4 -4
- package/lib/dts/src/diff/GeopointArrayDiff.d.ts.map +1 -0
- package/lib/dts/src/diff/GeopointArrayDiff.js +36 -0
- package/lib/dts/src/diff/GeopointArrayDiff.js.map +1 -0
- package/{dist/dts → lib/dts/src}/diff/GeopointFieldDiff.d.ts +4 -4
- package/lib/dts/src/diff/GeopointFieldDiff.d.ts.map +1 -0
- package/lib/dts/src/diff/GeopointFieldDiff.js +51 -0
- package/lib/dts/src/diff/GeopointFieldDiff.js.map +1 -0
- package/{dist/dts → lib/dts/src}/diff/GeopointFieldDiff.styles.d.ts +1 -1
- package/lib/dts/src/diff/GeopointFieldDiff.styles.d.ts.map +1 -0
- package/lib/dts/src/diff/GeopointFieldDiff.styles.js +20 -0
- package/lib/dts/src/diff/GeopointFieldDiff.styles.js.map +1 -0
- package/{dist/dts → lib/dts/src}/diff/GeopointMove.d.ts +12 -12
- package/lib/dts/src/diff/GeopointMove.d.ts.map +1 -0
- package/lib/dts/src/diff/GeopointMove.js +16 -0
- package/lib/dts/src/diff/GeopointMove.js.map +1 -0
- package/lib/dts/src/diff/resolver.d.ts +4 -0
- package/lib/dts/src/diff/resolver.d.ts.map +1 -0
- package/lib/dts/src/diff/resolver.js +15 -0
- package/lib/dts/src/diff/resolver.js.map +1 -0
- package/lib/dts/src/index.d.ts +13 -0
- package/lib/dts/src/index.d.ts.map +1 -0
- package/lib/dts/src/index.js +44 -0
- package/lib/dts/src/index.js.map +1 -0
- package/{dist/dts → lib/dts/src}/input/GeopointInput.d.ts +27 -39
- package/lib/dts/src/input/GeopointInput.d.ts.map +1 -0
- package/lib/dts/src/input/GeopointInput.js +112 -0
- package/lib/dts/src/input/GeopointInput.js.map +1 -0
- package/{dist/dts → lib/dts/src}/input/GeopointInput.styles.d.ts +2 -2
- package/lib/dts/src/input/GeopointInput.styles.d.ts.map +1 -0
- package/lib/dts/src/input/GeopointInput.styles.js +11 -0
- package/lib/dts/src/input/GeopointInput.styles.js.map +1 -0
- package/{dist/dts → lib/dts/src}/input/GeopointSelect.d.ts +27 -27
- package/lib/dts/src/input/GeopointSelect.d.ts.map +1 -0
- package/lib/dts/src/input/GeopointSelect.js +44 -0
- package/lib/dts/src/input/GeopointSelect.js.map +1 -0
- package/{dist/dts → lib/dts/src}/loader/GoogleMapsLoadProxy.d.ts +13 -13
- package/lib/dts/src/loader/GoogleMapsLoadProxy.d.ts.map +1 -0
- package/lib/dts/src/loader/GoogleMapsLoadProxy.js +39 -0
- package/lib/dts/src/loader/GoogleMapsLoadProxy.js.map +1 -0
- package/{dist/dts → lib/dts/src}/loader/LoadError.d.ts +9 -9
- package/lib/dts/src/loader/LoadError.d.ts.map +1 -0
- package/lib/dts/src/loader/LoadError.js +22 -0
- package/lib/dts/src/loader/LoadError.js.map +1 -0
- package/{dist/dts → lib/dts/src}/loader/loadGoogleMapsApi.d.ts +17 -17
- package/lib/dts/src/loader/loadGoogleMapsApi.d.ts.map +1 -0
- package/lib/dts/src/loader/loadGoogleMapsApi.js +49 -0
- package/lib/dts/src/loader/loadGoogleMapsApi.js.map +1 -0
- package/{dist/dts → lib/dts/src}/map/Arrow.d.ts +28 -28
- package/lib/dts/src/map/Arrow.d.ts.map +1 -0
- package/lib/dts/src/map/Arrow.js +53 -0
- package/lib/dts/src/map/Arrow.js.map +1 -0
- package/{dist/dts → lib/dts/src}/map/Map.d.ts +36 -36
- package/lib/dts/src/map/Map.d.ts.map +1 -0
- package/lib/dts/src/map/Map.js +87 -0
- package/lib/dts/src/map/Map.js.map +1 -0
- package/{dist/dts → lib/dts/src}/map/Map.styles.d.ts +1 -1
- package/lib/dts/src/map/Map.styles.d.ts.map +1 -0
- package/lib/dts/src/map/Map.styles.js +10 -0
- package/lib/dts/src/map/Map.styles.js.map +1 -0
- package/{dist/dts → lib/dts/src}/map/Marker.d.ts +33 -35
- package/lib/dts/src/map/Marker.d.ts.map +1 -0
- package/lib/dts/src/map/Marker.js +94 -0
- package/lib/dts/src/map/Marker.js.map +1 -0
- package/{dist/dts → lib/dts/src}/map/SearchInput.d.ts +15 -15
- package/lib/dts/src/map/SearchInput.d.ts.map +1 -0
- package/lib/dts/src/map/SearchInput.js +37 -0
- package/lib/dts/src/map/SearchInput.js.map +1 -0
- package/{dist/dts → lib/dts/src}/map/SearchInput.styles.d.ts +1 -1
- package/lib/dts/src/map/SearchInput.styles.d.ts.map +1 -0
- package/lib/dts/src/map/SearchInput.styles.js +8 -0
- package/lib/dts/src/map/SearchInput.styles.js.map +1 -0
- package/{dist/dts → lib/dts/src}/map/util.d.ts +3 -3
- package/lib/dts/src/map/util.d.ts.map +1 -0
- package/lib/dts/src/map/util.js +8 -0
- package/lib/dts/src/map/util.js.map +1 -0
- package/lib/dts/src/schemaTypes.d.ts +18 -0
- package/lib/dts/src/schemaTypes.d.ts.map +1 -0
- package/lib/dts/src/schemaTypes.js +25 -0
- package/lib/dts/src/schemaTypes.js.map +1 -0
- package/lib/dts/src/types.d.ts +17 -0
- package/lib/dts/src/types.d.ts.map +1 -0
- package/lib/dts/src/types.js +2 -0
- package/lib/dts/src/types.js.map +1 -0
- package/lib/dts/tsconfig.tsbuildinfo +1 -0
- package/lib/input/GeopointInput.cjs +227 -0
- package/lib/input/GeopointInput.cjs.map +1 -0
- package/lib/input/GeopointInput.js +197 -190
- package/lib/input/GeopointInput.js.map +1 -0
- package/package.json +46 -12
- package/src/@types/css.d.ts +4 -0
- package/src/diff/GeopointArrayDiff.tsx +83 -0
- package/src/diff/GeopointFieldDiff.styles.tsx +20 -0
- package/src/diff/GeopointFieldDiff.tsx +94 -0
- package/src/diff/GeopointMove.tsx +48 -0
- package/src/diff/resolver.ts +21 -0
- package/src/input/GeopointInput.styles.tsx +12 -0
- package/src/input/GeopointInput.tsx +219 -0
- package/src/input/GeopointSelect.tsx +78 -0
- package/src/loader/GoogleMapsLoadProxy.tsx +49 -0
- package/src/loader/LoadError.tsx +44 -0
- package/src/loader/loadGoogleMapsApi.ts +91 -0
- package/src/map/Arrow.tsx +76 -0
- package/src/map/Map.styles.tsx +10 -0
- package/src/map/Map.tsx +125 -0
- package/src/map/Marker.tsx +130 -0
- package/src/map/SearchInput.styles.tsx +8 -0
- package/src/map/SearchInput.tsx +56 -0
- package/src/map/util.ts +14 -0
- package/src/types.ts +19 -0
- package/.depcheckignore.json +0 -3
- package/dist/dts/diff/GeopointArrayDiff.d.ts.map +0 -1
- package/dist/dts/diff/GeopointFieldDiff.d.ts.map +0 -1
- package/dist/dts/diff/GeopointFieldDiff.styles.d.ts.map +0 -1
- package/dist/dts/diff/GeopointMove.d.ts.map +0 -1
- package/dist/dts/diff/resolver.d.ts +0 -4
- package/dist/dts/diff/resolver.d.ts.map +0 -1
- package/dist/dts/input/GeopointInput.d.ts.map +0 -1
- package/dist/dts/input/GeopointInput.styles.d.ts.map +0 -1
- package/dist/dts/input/GeopointSelect.d.ts.map +0 -1
- package/dist/dts/loader/GoogleMapsLoadProxy.d.ts.map +0 -1
- package/dist/dts/loader/LoadError.d.ts.map +0 -1
- package/dist/dts/loader/loadGoogleMapsApi.d.ts.map +0 -1
- package/dist/dts/map/Arrow.d.ts.map +0 -1
- package/dist/dts/map/Map.d.ts.map +0 -1
- package/dist/dts/map/Map.styles.d.ts.map +0 -1
- package/dist/dts/map/Marker.d.ts.map +0 -1
- package/dist/dts/map/SearchInput.d.ts.map +0 -1
- package/dist/dts/map/SearchInput.styles.d.ts.map +0 -1
- package/dist/dts/map/util.d.ts.map +0 -1
- package/dist/dts/types.d.ts +0 -14
- package/dist/dts/types.d.ts.map +0 -1
- package/lib/@types/css.d.js +0 -1
- package/lib/diff/GeopointArrayDiff.js +0 -82
- package/lib/diff/GeopointFieldDiff.js +0 -97
- package/lib/diff/GeopointFieldDiff.styles.js +0 -18
- package/lib/diff/GeopointMove.js +0 -55
- package/lib/input/GeopointInput.styles.js +0 -22
- package/lib/input/GeopointSelect.js +0 -103
- package/lib/loader/GoogleMapsLoadProxy.js +0 -70
- package/lib/loader/LoadError.js +0 -43
- package/lib/loader/loadGoogleMapsApi.js +0 -81
- package/lib/map/Arrow.js +0 -97
- package/lib/map/Map.js +0 -147
- package/lib/map/Map.styles.js +0 -18
- package/lib/map/Marker.js +0 -156
- package/lib/map/SearchInput.js +0 -77
- package/lib/map/SearchInput.styles.js +0 -18
- package/lib/map/util.js +0 -14
- package/lib/types.js +0 -5
- package/tsconfig.json +0 -20
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import {SearchInput} from '../map/SearchInput'
|
|
3
|
+
import {GoogleMap} from '../map/Map'
|
|
4
|
+
import {Marker} from '../map/Marker'
|
|
5
|
+
import {LatLng, Geopoint} from '../types'
|
|
6
|
+
|
|
7
|
+
const fallbackLatLng: LatLng = {lat: 40.7058254, lng: -74.1180863}
|
|
8
|
+
|
|
9
|
+
interface SelectProps {
|
|
10
|
+
api: typeof window.google.maps
|
|
11
|
+
value?: Geopoint
|
|
12
|
+
onChange?: (latLng: google.maps.LatLng) => void
|
|
13
|
+
defaultLocation?: LatLng
|
|
14
|
+
defaultZoom?: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class GeopointSelect extends React.PureComponent<SelectProps> {
|
|
18
|
+
static defaultProps = {
|
|
19
|
+
defaultZoom: 8,
|
|
20
|
+
defaultLocation: {lng: 10.74609, lat: 59.91273},
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
mapRef = React.createRef<HTMLDivElement>()
|
|
24
|
+
|
|
25
|
+
getCenter() {
|
|
26
|
+
const {value = {}, defaultLocation = {}} = this.props
|
|
27
|
+
const point: LatLng = {...fallbackLatLng, ...defaultLocation, ...value}
|
|
28
|
+
return point
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
handlePlaceChanged = (place: google.maps.places.PlaceResult) => {
|
|
32
|
+
if (!place.geometry) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.setValue(place.geometry.location)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
handleMarkerDragEnd = (event: google.maps.MapMouseEvent) => {
|
|
40
|
+
this.setValue(event.latLng)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
handleMapClick = (event: google.maps.MapMouseEvent) => {
|
|
44
|
+
this.setValue(event.latLng)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setValue(geoPoint: google.maps.LatLng) {
|
|
48
|
+
if (this.props.onChange) {
|
|
49
|
+
this.props.onChange(geoPoint)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
render() {
|
|
54
|
+
const {api, defaultZoom, value, onChange} = this.props
|
|
55
|
+
return (
|
|
56
|
+
<GoogleMap
|
|
57
|
+
api={api}
|
|
58
|
+
location={this.getCenter()}
|
|
59
|
+
onClick={this.handleMapClick}
|
|
60
|
+
defaultZoom={defaultZoom}
|
|
61
|
+
>
|
|
62
|
+
{(map) => (
|
|
63
|
+
<>
|
|
64
|
+
<SearchInput api={api} map={map} onChange={this.handlePlaceChanged} />
|
|
65
|
+
{value && (
|
|
66
|
+
<Marker
|
|
67
|
+
api={api}
|
|
68
|
+
map={map}
|
|
69
|
+
position={value}
|
|
70
|
+
onMove={onChange ? this.handleMarkerDragEnd : undefined}
|
|
71
|
+
/>
|
|
72
|
+
)}
|
|
73
|
+
</>
|
|
74
|
+
)}
|
|
75
|
+
</GoogleMap>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import {Subscription} from 'rxjs'
|
|
3
|
+
import {loadGoogleMapsApi, GoogleLoadState} from './loadGoogleMapsApi'
|
|
4
|
+
import {LoadError} from './LoadError'
|
|
5
|
+
|
|
6
|
+
interface LoadProps {
|
|
7
|
+
children: (api: typeof window.google.maps) => React.ReactElement
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class GoogleMapsLoadProxy extends React.Component<LoadProps, GoogleLoadState> {
|
|
11
|
+
loadSubscription: Subscription | undefined
|
|
12
|
+
|
|
13
|
+
constructor(props: LoadProps) {
|
|
14
|
+
super(props)
|
|
15
|
+
|
|
16
|
+
this.state = {loadState: 'loading'}
|
|
17
|
+
|
|
18
|
+
let sync = true
|
|
19
|
+
this.loadSubscription = loadGoogleMapsApi().subscribe((loadState) => {
|
|
20
|
+
if (sync) {
|
|
21
|
+
this.state = loadState
|
|
22
|
+
} else {
|
|
23
|
+
this.setState(loadState)
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
sync = false
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
componentWillUnmount() {
|
|
30
|
+
if (this.loadSubscription) {
|
|
31
|
+
this.loadSubscription.unsubscribe()
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
render() {
|
|
36
|
+
switch (this.state.loadState) {
|
|
37
|
+
case 'loadError':
|
|
38
|
+
return <LoadError error={this.state.error} isAuthError={false} />
|
|
39
|
+
case 'authError':
|
|
40
|
+
return <LoadError isAuthError />
|
|
41
|
+
case 'loading':
|
|
42
|
+
return <div>Loading Google Maps API</div>
|
|
43
|
+
case 'loaded':
|
|
44
|
+
return this.props.children(this.state.api) || null
|
|
45
|
+
default:
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {Card, Box, Text, Code} from '@sanity/ui'
|
|
3
|
+
|
|
4
|
+
type Props = {error: Error; isAuthError: false} | {isAuthError: true}
|
|
5
|
+
|
|
6
|
+
export function LoadError(props: Props) {
|
|
7
|
+
return (
|
|
8
|
+
<Card tone="critical" radius={1}>
|
|
9
|
+
<Box as="header" paddingX={4} paddingTop={4} paddingBottom={1}>
|
|
10
|
+
<Text as="h2" weight="bold">
|
|
11
|
+
Google Maps failed to load
|
|
12
|
+
</Text>
|
|
13
|
+
</Box>
|
|
14
|
+
|
|
15
|
+
<Box paddingX={4} paddingTop={4} paddingBottom={1}>
|
|
16
|
+
{props.isAuthError ? (
|
|
17
|
+
<AuthError />
|
|
18
|
+
) : (
|
|
19
|
+
<>
|
|
20
|
+
<Text as="h3">Error details:</Text>
|
|
21
|
+
<pre>
|
|
22
|
+
<Code size={1}>{props.error?.message}</Code>
|
|
23
|
+
</pre>
|
|
24
|
+
</>
|
|
25
|
+
)}
|
|
26
|
+
</Box>
|
|
27
|
+
</Card>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function AuthError() {
|
|
32
|
+
return (
|
|
33
|
+
<Text>
|
|
34
|
+
<p>The error appears to be related to authentication</p>
|
|
35
|
+
<p>Common causes include:</p>
|
|
36
|
+
<ul>
|
|
37
|
+
<li>Incorrect API key</li>
|
|
38
|
+
<li>Referer not allowed</li>
|
|
39
|
+
<li>Missing authentication scope</li>
|
|
40
|
+
</ul>
|
|
41
|
+
<p>Check the browser developer tools for more information.</p>
|
|
42
|
+
</Text>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {Observable, BehaviorSubject} from 'rxjs'
|
|
2
|
+
// @ts-expect-error TODO: update to non-parts
|
|
3
|
+
import config from 'config:@sanity/google-maps-input'
|
|
4
|
+
|
|
5
|
+
const callbackName = '___sanity_googleMapsApiCallback'
|
|
6
|
+
const authFailureCallbackName = 'gm_authFailure'
|
|
7
|
+
const locale = (typeof window !== 'undefined' && window.navigator.language) || 'en'
|
|
8
|
+
|
|
9
|
+
export interface LoadingState {
|
|
10
|
+
loadState: 'loading'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface LoadedState {
|
|
14
|
+
loadState: 'loaded'
|
|
15
|
+
api: typeof window.google.maps
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface LoadErrorState {
|
|
19
|
+
loadState: 'loadError'
|
|
20
|
+
error: Error
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface AuthErrorState {
|
|
24
|
+
loadState: 'authError'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type GoogleLoadState = LoadingState | LoadedState | LoadErrorState | AuthErrorState
|
|
28
|
+
|
|
29
|
+
let subject: BehaviorSubject<GoogleLoadState>
|
|
30
|
+
|
|
31
|
+
export function loadGoogleMapsApi(): Observable<GoogleLoadState> {
|
|
32
|
+
const selectedLocale = config.defaultLocale || locale || 'en-US'
|
|
33
|
+
|
|
34
|
+
if (subject) {
|
|
35
|
+
return subject
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
subject = new BehaviorSubject<GoogleLoadState>({loadState: 'loading'})
|
|
39
|
+
|
|
40
|
+
window[authFailureCallbackName] = () => {
|
|
41
|
+
delete window[authFailureCallbackName]
|
|
42
|
+
subject.next({loadState: 'authError'})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
window[callbackName] = () => {
|
|
46
|
+
delete window[callbackName]
|
|
47
|
+
subject.next({loadState: 'loaded', api: window.google.maps})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const script = document.createElement('script')
|
|
51
|
+
script.onerror = (
|
|
52
|
+
event: Event | string,
|
|
53
|
+
source?: string,
|
|
54
|
+
lineno?: number,
|
|
55
|
+
colno?: number,
|
|
56
|
+
error?: Error
|
|
57
|
+
) =>
|
|
58
|
+
subject.next({
|
|
59
|
+
loadState: 'loadError',
|
|
60
|
+
error: coeerceError(event, error),
|
|
61
|
+
} as LoadErrorState)
|
|
62
|
+
|
|
63
|
+
script.src = `https://maps.googleapis.com/maps/api/js?key=${config.apiKey}&libraries=places&callback=${callbackName}&language=${selectedLocale}`
|
|
64
|
+
document.getElementsByTagName('head')[0].appendChild(script)
|
|
65
|
+
|
|
66
|
+
return subject
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function coeerceError(event: Event | string, error?: Error): Error {
|
|
70
|
+
if (error) {
|
|
71
|
+
return error
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (typeof event === 'string') {
|
|
75
|
+
return new Error(event)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return new Error(isErrorEvent(event) ? event.message : 'Failed to load Google Maps API')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function isErrorEvent(event: unknown): event is ErrorEvent {
|
|
82
|
+
if (typeof event !== 'object' || event === null) {
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!('message' in event)) {
|
|
87
|
+
return false
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return typeof (event as ErrorEvent).message === 'string'
|
|
91
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {LatLng} from '../types'
|
|
3
|
+
import {latLngAreEqual} from './util'
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
api: typeof window.google.maps
|
|
7
|
+
map: google.maps.Map
|
|
8
|
+
from: LatLng
|
|
9
|
+
to: LatLng
|
|
10
|
+
color?: {background: string; border: string; text: string}
|
|
11
|
+
zIndex?: number
|
|
12
|
+
arrowRef?: React.MutableRefObject<google.maps.Polyline | undefined>
|
|
13
|
+
onClick?: (event: google.maps.MapMouseEvent) => void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class Arrow extends React.PureComponent<Props> {
|
|
17
|
+
line: google.maps.Polyline | undefined
|
|
18
|
+
|
|
19
|
+
eventHandlers: {
|
|
20
|
+
click?: google.maps.MapsEventListener
|
|
21
|
+
} = {}
|
|
22
|
+
|
|
23
|
+
componentDidMount() {
|
|
24
|
+
const {from, to, api, map, zIndex, onClick, color, arrowRef} = this.props
|
|
25
|
+
const lineSymbol = {
|
|
26
|
+
path: api.SymbolPath.FORWARD_OPEN_ARROW,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.line = new api.Polyline({
|
|
30
|
+
map,
|
|
31
|
+
zIndex,
|
|
32
|
+
path: [from, to],
|
|
33
|
+
icons: [{icon: lineSymbol, offset: '50%'}],
|
|
34
|
+
strokeOpacity: 0.55,
|
|
35
|
+
strokeColor: color ? color.text : 'black',
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
if (onClick) {
|
|
39
|
+
this.eventHandlers.click = api.event.addListener(this.line, 'click', onClick)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (arrowRef) {
|
|
43
|
+
arrowRef.current = this.line
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
componentDidUpdate(prevProps: Props) {
|
|
48
|
+
if (!this.line) {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const {from, to, map} = this.props
|
|
53
|
+
if (!latLngAreEqual(prevProps.from, from) || !latLngAreEqual(prevProps.to, to)) {
|
|
54
|
+
this.line.setPath([from, to])
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (prevProps.map !== map) {
|
|
58
|
+
this.line.setMap(map)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
componentWillUnmount() {
|
|
63
|
+
if (this.line) {
|
|
64
|
+
this.line.setMap(null)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (this.eventHandlers.click) {
|
|
68
|
+
this.eventHandlers.click.remove()
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// eslint-disable-next-line class-methods-use-this
|
|
73
|
+
render() {
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/map/Map.tsx
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import {LatLng} from '../types'
|
|
3
|
+
import {latLngAreEqual} from './util'
|
|
4
|
+
import {MapContainer} from './Map.styles'
|
|
5
|
+
|
|
6
|
+
interface MapProps {
|
|
7
|
+
api: typeof window.google.maps
|
|
8
|
+
location: LatLng
|
|
9
|
+
bounds?: google.maps.LatLngBounds
|
|
10
|
+
defaultZoom?: number
|
|
11
|
+
mapTypeControl?: boolean
|
|
12
|
+
scrollWheel?: boolean
|
|
13
|
+
controlSize?: number
|
|
14
|
+
onClick?: (event: google.maps.MapMouseEvent) => void
|
|
15
|
+
children?: (map: google.maps.Map) => React.ReactElement
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface MapState {
|
|
19
|
+
map: google.maps.Map | undefined
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class GoogleMap extends React.PureComponent<MapProps, MapState> {
|
|
23
|
+
static defaultProps = {
|
|
24
|
+
defaultZoom: 8,
|
|
25
|
+
scrollWheel: true,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
state: MapState = {map: undefined}
|
|
29
|
+
clickHandler: google.maps.MapsEventListener | undefined
|
|
30
|
+
mapRef = React.createRef<HTMLDivElement>()
|
|
31
|
+
mapEl: HTMLDivElement | null = null
|
|
32
|
+
|
|
33
|
+
componentDidMount() {
|
|
34
|
+
this.attachClickHandler()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
attachClickHandler = () => {
|
|
38
|
+
const map = this.state.map
|
|
39
|
+
if (!map) {
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const {api, onClick} = this.props
|
|
44
|
+
const {event} = api
|
|
45
|
+
|
|
46
|
+
if (this.clickHandler) {
|
|
47
|
+
this.clickHandler.remove()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (onClick) {
|
|
51
|
+
this.clickHandler = event.addListener(map, 'click', onClick)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
componentDidUpdate(prevProps: MapProps) {
|
|
56
|
+
const map = this.state.map
|
|
57
|
+
if (!map) {
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const {onClick, location, bounds} = this.props
|
|
62
|
+
|
|
63
|
+
if (prevProps.onClick !== onClick) {
|
|
64
|
+
this.attachClickHandler()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!latLngAreEqual(prevProps.location, location)) {
|
|
68
|
+
map.panTo(this.getCenter())
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (bounds && (!prevProps.bounds || !bounds.equals(prevProps.bounds))) {
|
|
72
|
+
map.fitBounds(bounds)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
componentWillUnmount() {
|
|
77
|
+
if (this.clickHandler) {
|
|
78
|
+
this.clickHandler.remove()
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getCenter(): google.maps.LatLng {
|
|
83
|
+
const {location, api} = this.props
|
|
84
|
+
return new api.LatLng(location.lat, location.lng)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
constructMap(el: HTMLDivElement) {
|
|
88
|
+
const {defaultZoom, api, mapTypeControl, controlSize, bounds, scrollWheel} = this.props
|
|
89
|
+
|
|
90
|
+
const map = new api.Map(el, {
|
|
91
|
+
zoom: defaultZoom,
|
|
92
|
+
center: this.getCenter(),
|
|
93
|
+
scrollwheel: scrollWheel,
|
|
94
|
+
streetViewControl: false,
|
|
95
|
+
mapTypeControl,
|
|
96
|
+
controlSize,
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
if (bounds) {
|
|
100
|
+
map.fitBounds(bounds)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return map
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
setMapElement = (element: HTMLDivElement | null) => {
|
|
107
|
+
if (element && element !== this.mapEl) {
|
|
108
|
+
const map = this.constructMap(element)
|
|
109
|
+
this.setState({map}, this.attachClickHandler)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
this.mapEl = element
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
render() {
|
|
116
|
+
const {children} = this.props
|
|
117
|
+
const {map} = this.state
|
|
118
|
+
return (
|
|
119
|
+
<>
|
|
120
|
+
<MapContainer ref={this.setMapElement} />
|
|
121
|
+
{children && map ? children(map) : null}
|
|
122
|
+
</>
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {LatLng} from '../types'
|
|
3
|
+
import {latLngAreEqual} from './util'
|
|
4
|
+
|
|
5
|
+
const markerPath =
|
|
6
|
+
'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'
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
api: typeof window.google.maps
|
|
10
|
+
map: google.maps.Map
|
|
11
|
+
onMove?: (event: google.maps.MapMouseEvent) => void
|
|
12
|
+
onClick?: (event: google.maps.MapMouseEvent) => void
|
|
13
|
+
position: LatLng | google.maps.LatLng
|
|
14
|
+
zIndex?: number
|
|
15
|
+
opacity?: number
|
|
16
|
+
label?: string
|
|
17
|
+
markerRef?: React.MutableRefObject<google.maps.Marker | undefined>
|
|
18
|
+
color?: {background: string; border: string; text: string}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Marker extends React.PureComponent<Props> {
|
|
22
|
+
marker: google.maps.Marker | undefined
|
|
23
|
+
|
|
24
|
+
eventHandlers: {
|
|
25
|
+
move?: google.maps.MapsEventListener
|
|
26
|
+
click?: google.maps.MapsEventListener
|
|
27
|
+
} = {}
|
|
28
|
+
|
|
29
|
+
componentDidMount() {
|
|
30
|
+
const {position, api, map, onMove, zIndex, opacity, label, markerRef, color} = this.props
|
|
31
|
+
const {Marker: GMarker} = api
|
|
32
|
+
|
|
33
|
+
let icon: google.maps.ReadonlySymbol | undefined
|
|
34
|
+
if (color) {
|
|
35
|
+
icon = {
|
|
36
|
+
path: markerPath,
|
|
37
|
+
fillOpacity: 1,
|
|
38
|
+
fillColor: color.background,
|
|
39
|
+
strokeColor: color.border,
|
|
40
|
+
strokeWeight: 2,
|
|
41
|
+
anchor: new api.Point(10, 35),
|
|
42
|
+
labelOrigin: new api.Point(10, 11),
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.marker = new GMarker({
|
|
47
|
+
draggable: Boolean(onMove),
|
|
48
|
+
position,
|
|
49
|
+
map,
|
|
50
|
+
zIndex,
|
|
51
|
+
opacity,
|
|
52
|
+
label,
|
|
53
|
+
icon,
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
if (markerRef) {
|
|
57
|
+
markerRef.current = this.marker
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.attachMoveHandler()
|
|
61
|
+
this.attachClickHandler()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
componentDidUpdate(prevProps: Props) {
|
|
65
|
+
if (!this.marker) {
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const {position, onMove, label, zIndex, opacity, map} = this.props
|
|
70
|
+
|
|
71
|
+
if (prevProps.onMove !== onMove) {
|
|
72
|
+
this.attachMoveHandler()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!latLngAreEqual(prevProps.position, position)) {
|
|
76
|
+
this.marker.setPosition(position)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (prevProps.label !== label) {
|
|
80
|
+
this.marker.setLabel(label || null)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (prevProps.zIndex !== zIndex) {
|
|
84
|
+
this.marker.setZIndex(zIndex || null)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (prevProps.opacity !== opacity) {
|
|
88
|
+
this.marker.setOpacity(opacity || null)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (prevProps.map !== map) {
|
|
92
|
+
this.marker.setMap(map)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
componentWillUnmount() {
|
|
97
|
+
if (this.eventHandlers.move) {
|
|
98
|
+
this.eventHandlers.move.remove()
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (this.marker) {
|
|
102
|
+
this.marker.setMap(null)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
attachMoveHandler() {
|
|
107
|
+
const {api, onMove} = this.props
|
|
108
|
+
if (this.eventHandlers.move) {
|
|
109
|
+
this.eventHandlers.move.remove()
|
|
110
|
+
}
|
|
111
|
+
if (this.marker && onMove) {
|
|
112
|
+
this.eventHandlers.move = api.event.addListener(this.marker, 'dragend', onMove)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
attachClickHandler() {
|
|
117
|
+
const {api, onClick} = this.props
|
|
118
|
+
if (this.eventHandlers.click) {
|
|
119
|
+
this.eventHandlers.click.remove()
|
|
120
|
+
}
|
|
121
|
+
if (this.marker && onClick) {
|
|
122
|
+
this.eventHandlers.click = api.event.addListener(this.marker, 'click', onClick)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// eslint-disable-next-line class-methods-use-this
|
|
127
|
+
render() {
|
|
128
|
+
return null
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {TextInput} from '@sanity/ui'
|
|
3
|
+
import {WrapperContainer} from './SearchInput.styles'
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
api: typeof window.google.maps
|
|
7
|
+
map: google.maps.Map
|
|
8
|
+
onChange: (result: google.maps.places.PlaceResult) => void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class SearchInput extends React.PureComponent<Props> {
|
|
12
|
+
searchInputRef = React.createRef<HTMLInputElement>()
|
|
13
|
+
autoComplete: google.maps.places.Autocomplete | undefined
|
|
14
|
+
|
|
15
|
+
handleChange = () => {
|
|
16
|
+
if (!this.autoComplete) {
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this.props.onChange(this.autoComplete.getPlace())
|
|
21
|
+
|
|
22
|
+
if (this.searchInputRef.current) {
|
|
23
|
+
this.searchInputRef.current.value = ''
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
componentDidMount() {
|
|
28
|
+
const input = this.searchInputRef.current
|
|
29
|
+
if (!input) {
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const {api, map} = this.props
|
|
34
|
+
const {Circle, places, event} = api
|
|
35
|
+
const searchBounds = new Circle({center: map.getCenter(), radius: 100}).getBounds()
|
|
36
|
+
this.autoComplete = new places.Autocomplete(input, {
|
|
37
|
+
bounds: searchBounds,
|
|
38
|
+
types: [], // return all kinds of places
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
event.addListener(this.autoComplete, 'place_changed', this.handleChange)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
render() {
|
|
45
|
+
return (
|
|
46
|
+
<WrapperContainer>
|
|
47
|
+
<TextInput
|
|
48
|
+
name="place"
|
|
49
|
+
ref={this.searchInputRef}
|
|
50
|
+
placeholder="Search for place or address"
|
|
51
|
+
padding={4}
|
|
52
|
+
/>
|
|
53
|
+
</WrapperContainer>
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/map/util.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {LatLng} from '../types'
|
|
2
|
+
|
|
3
|
+
export function latLngAreEqual(
|
|
4
|
+
latLng1: LatLng | google.maps.LatLng,
|
|
5
|
+
latLng2: LatLng | google.maps.LatLng
|
|
6
|
+
) {
|
|
7
|
+
const lat1 = typeof latLng1.lat === 'function' ? latLng1.lat() : latLng1.lat
|
|
8
|
+
const lng1 = typeof latLng1.lng === 'function' ? latLng1.lng() : latLng1.lng
|
|
9
|
+
|
|
10
|
+
const lat2 = typeof latLng2.lat === 'function' ? latLng2.lat() : latLng2.lat
|
|
11
|
+
const lng2 = typeof latLng2.lng === 'function' ? latLng2.lng() : latLng2.lng
|
|
12
|
+
|
|
13
|
+
return lat1 === lat2 && lng1 === lng2
|
|
14
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {DiffComponent, DiffComponentOptions, ObjectDiff} from '@sanity/base/_unstable'
|
|
2
|
+
import {ObjectSchemaType} from '@sanity/types'
|
|
3
|
+
|
|
4
|
+
export interface LatLng {
|
|
5
|
+
lat: number
|
|
6
|
+
lng: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Geopoint {
|
|
10
|
+
_type: 'geopoint'
|
|
11
|
+
_key?: string
|
|
12
|
+
lat: number
|
|
13
|
+
lng: number
|
|
14
|
+
alt?: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface GeopointSchemaType extends ObjectSchemaType {
|
|
18
|
+
diffComponent?: DiffComponent<ObjectDiff<Geopoint>> | DiffComponentOptions<ObjectDiff<Geopoint>>
|
|
19
|
+
}
|