@sanity/google-maps-input 2.35.1 → 2.36.0-v2-studio.1

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.
Files changed (108) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +11 -1
  3. package/{dist/dts → dts}/diff/GeopointArrayDiff.d.ts +1 -2
  4. package/{dist/dts → dts}/diff/GeopointFieldDiff.d.ts +1 -2
  5. package/dts/diff/GeopointFieldDiff.styles.d.ts +1 -0
  6. package/{dist/dts → dts}/diff/GeopointMove.d.ts +0 -1
  7. package/{dist/dts → dts}/diff/resolver.d.ts +0 -1
  8. package/{dist/dts → dts}/input/GeopointInput.d.ts +26 -2
  9. package/dts/input/GeopointInput.styles.d.ts +2 -0
  10. package/dts/input/GeopointSelect.d.ts +52 -0
  11. package/{dist/dts → dts}/loader/GoogleMapsLoadProxy.d.ts +0 -1
  12. package/{dist/dts → dts}/loader/LoadError.d.ts +1 -2
  13. package/{dist/dts → dts}/loader/loadGoogleMapsApi.d.ts +1 -2
  14. package/{dist/dts → dts}/map/Arrow.d.ts +2 -1
  15. package/{dist/dts → dts}/map/Map.d.ts +2 -1
  16. package/dts/map/Map.styles.d.ts +1 -0
  17. package/{dist/dts → dts}/map/Marker.d.ts +3 -1
  18. package/{dist/dts → dts}/map/SearchInput.d.ts +2 -1
  19. package/dts/map/SearchInput.styles.d.ts +1 -0
  20. package/{dist/dts → dts}/map/util.d.ts +0 -1
  21. package/{dist/dts → dts}/types.d.ts +1 -2
  22. package/lib/@types/css.d.js +2 -1
  23. package/lib/@types/css.d.js.map +1 -0
  24. package/lib/diff/GeopointArrayDiff.js +2 -1
  25. package/lib/diff/GeopointArrayDiff.js.map +1 -0
  26. package/lib/diff/GeopointFieldDiff.js +2 -1
  27. package/lib/diff/GeopointFieldDiff.js.map +1 -0
  28. package/lib/diff/GeopointFieldDiff.styles.js +2 -1
  29. package/lib/diff/GeopointFieldDiff.styles.js.map +1 -0
  30. package/lib/diff/GeopointMove.js +2 -1
  31. package/lib/diff/GeopointMove.js.map +1 -0
  32. package/lib/diff/resolver.js +2 -1
  33. package/lib/diff/resolver.js.map +1 -0
  34. package/lib/input/GeopointInput.js +4 -3
  35. package/lib/input/GeopointInput.js.map +1 -0
  36. package/lib/input/GeopointInput.styles.js +2 -1
  37. package/lib/input/GeopointInput.styles.js.map +1 -0
  38. package/lib/input/GeopointSelect.js +2 -1
  39. package/lib/input/GeopointSelect.js.map +1 -0
  40. package/lib/loader/GoogleMapsLoadProxy.js +2 -1
  41. package/lib/loader/GoogleMapsLoadProxy.js.map +1 -0
  42. package/lib/loader/LoadError.js +2 -1
  43. package/lib/loader/LoadError.js.map +1 -0
  44. package/lib/loader/loadGoogleMapsApi.js +2 -1
  45. package/lib/loader/loadGoogleMapsApi.js.map +1 -0
  46. package/lib/map/Arrow.js +2 -1
  47. package/lib/map/Arrow.js.map +1 -0
  48. package/lib/map/Map.js +2 -1
  49. package/lib/map/Map.js.map +1 -0
  50. package/lib/map/Map.styles.js +2 -1
  51. package/lib/map/Map.styles.js.map +1 -0
  52. package/lib/map/Marker.js +2 -1
  53. package/lib/map/Marker.js.map +1 -0
  54. package/lib/map/SearchInput.js +2 -1
  55. package/lib/map/SearchInput.js.map +1 -0
  56. package/lib/map/SearchInput.styles.js +2 -1
  57. package/lib/map/SearchInput.styles.js.map +1 -0
  58. package/lib/map/util.js +2 -1
  59. package/lib/map/util.js.map +1 -0
  60. package/lib/types.js +1 -4
  61. package/lib/types.js.map +1 -0
  62. package/package.json +53 -22
  63. package/src/@types/css.d.ts +4 -0
  64. package/src/diff/GeopointArrayDiff.tsx +83 -0
  65. package/src/diff/GeopointFieldDiff.styles.tsx +20 -0
  66. package/src/diff/GeopointFieldDiff.tsx +94 -0
  67. package/src/diff/GeopointMove.tsx +49 -0
  68. package/src/diff/resolver.ts +21 -0
  69. package/src/input/GeopointInput.styles.tsx +12 -0
  70. package/src/input/GeopointInput.tsx +231 -0
  71. package/src/input/GeopointSelect.tsx +78 -0
  72. package/src/loader/GoogleMapsLoadProxy.tsx +49 -0
  73. package/src/loader/LoadError.tsx +44 -0
  74. package/src/loader/loadGoogleMapsApi.ts +93 -0
  75. package/src/map/Arrow.tsx +76 -0
  76. package/src/map/Map.styles.tsx +10 -0
  77. package/src/map/Map.tsx +125 -0
  78. package/src/map/Marker.tsx +132 -0
  79. package/src/map/SearchInput.styles.tsx +8 -0
  80. package/src/map/SearchInput.tsx +55 -0
  81. package/src/map/util.ts +14 -0
  82. package/src/types.ts +16 -0
  83. package/.depcheckignore.json +0 -3
  84. package/dist/dts/diff/GeopointArrayDiff.d.ts.map +0 -1
  85. package/dist/dts/diff/GeopointFieldDiff.d.ts.map +0 -1
  86. package/dist/dts/diff/GeopointFieldDiff.styles.d.ts +0 -2
  87. package/dist/dts/diff/GeopointFieldDiff.styles.d.ts.map +0 -1
  88. package/dist/dts/diff/GeopointMove.d.ts.map +0 -1
  89. package/dist/dts/diff/resolver.d.ts.map +0 -1
  90. package/dist/dts/input/GeopointInput.d.ts.map +0 -1
  91. package/dist/dts/input/GeopointInput.styles.d.ts +0 -3
  92. package/dist/dts/input/GeopointInput.styles.d.ts.map +0 -1
  93. package/dist/dts/input/GeopointSelect.d.ts +0 -28
  94. package/dist/dts/input/GeopointSelect.d.ts.map +0 -1
  95. package/dist/dts/loader/GoogleMapsLoadProxy.d.ts.map +0 -1
  96. package/dist/dts/loader/LoadError.d.ts.map +0 -1
  97. package/dist/dts/loader/loadGoogleMapsApi.d.ts.map +0 -1
  98. package/dist/dts/map/Arrow.d.ts.map +0 -1
  99. package/dist/dts/map/Map.d.ts.map +0 -1
  100. package/dist/dts/map/Map.styles.d.ts +0 -2
  101. package/dist/dts/map/Map.styles.d.ts.map +0 -1
  102. package/dist/dts/map/Marker.d.ts.map +0 -1
  103. package/dist/dts/map/SearchInput.d.ts.map +0 -1
  104. package/dist/dts/map/SearchInput.styles.d.ts +0 -2
  105. package/dist/dts/map/SearchInput.styles.d.ts.map +0 -1
  106. package/dist/dts/map/util.d.ts.map +0 -1
  107. package/dist/dts/types.d.ts.map +0 -1
  108. package/tsconfig.json +0 -20
@@ -0,0 +1,231 @@
1
+ // @todo: remove the following line when part imports has been removed from this file
2
+ ///<reference types="@sanity/types/parts" />
3
+
4
+ import React from 'react'
5
+ import {uniqueId} from 'lodash'
6
+ import {Box, Grid, Button, Dialog} from '@sanity/ui'
7
+ import {TrashIcon, EditIcon} from '@sanity/icons'
8
+ import {Path, Marker} from '@sanity/types'
9
+ import config from 'config:@sanity/google-maps-input'
10
+ import {FormFieldSet} from '@sanity/base/components'
11
+ import {FormFieldPresence} from '@sanity/base/presence'
12
+ import {PatchEvent, set, setIfMissing, unset} from 'part:@sanity/form-builder/patch-event'
13
+ import {ChangeIndicatorCompareValueProvider, ChangeIndicator} from '@sanity/base/change-indicators'
14
+ import {GoogleMapsLoadProxy} from '../loader/GoogleMapsLoadProxy'
15
+ import {Geopoint, GeopointSchemaType} from '../types'
16
+ import {GeopointSelect} from './GeopointSelect'
17
+ import {PreviewImage, DialogInnerContainer} from './GeopointInput.styles'
18
+
19
+ const getStaticImageUrl = (value) => {
20
+ const loc = `${value.lat},${value.lng}`
21
+ const params = {
22
+ key: config.apiKey,
23
+ center: loc,
24
+ markers: loc,
25
+ zoom: 13,
26
+ scale: 2,
27
+ size: '640x300',
28
+ }
29
+
30
+ const qs = Object.keys(params).reduce((res, param) => {
31
+ return res.concat(`${param}=${encodeURIComponent(params[param])}`)
32
+ }, [] as string[])
33
+
34
+ return `https://maps.googleapis.com/maps/api/staticmap?${qs.join('&')}`
35
+ }
36
+
37
+ interface InputProps {
38
+ markers: Marker[]
39
+ level?: number
40
+ value?: Geopoint
41
+ compareValue?: Geopoint
42
+ type: GeopointSchemaType
43
+ readOnly?: boolean
44
+ onFocus: (path: Path) => void
45
+ onBlur: () => void
46
+ onChange: (patchEvent: unknown) => void
47
+ presence: FormFieldPresence[]
48
+ }
49
+
50
+ // @todo
51
+ // interface Focusable {
52
+ // focus: () => void
53
+ // }
54
+ type Focusable = any
55
+
56
+ interface InputState {
57
+ modalOpen: boolean
58
+ }
59
+
60
+ class GeopointInput extends React.PureComponent<InputProps, InputState> {
61
+ _geopointInputId = uniqueId('GeopointInput')
62
+
63
+ static defaultProps = {
64
+ markers: [],
65
+ }
66
+
67
+ editButton: Focusable | undefined
68
+
69
+ constructor(props) {
70
+ super(props)
71
+
72
+ this.state = {
73
+ modalOpen: false,
74
+ }
75
+ }
76
+
77
+ setEditButton = (el: Focusable) => {
78
+ this.editButton = el
79
+ }
80
+
81
+ focus() {
82
+ if (this.editButton) {
83
+ this.editButton.focus()
84
+ }
85
+ }
86
+
87
+ handleFocus = (event) => {
88
+ this.props.onFocus(event)
89
+ }
90
+
91
+ handleBlur = () => {
92
+ this.props.onBlur()
93
+ }
94
+
95
+ handleToggleModal = () => {
96
+ const {onFocus, onBlur} = this.props
97
+ this.setState(
98
+ (prevState) => ({modalOpen: !prevState.modalOpen}),
99
+ () => {
100
+ if (this.state.modalOpen) {
101
+ onFocus(['$'])
102
+ } else {
103
+ onBlur()
104
+ }
105
+ }
106
+ )
107
+ }
108
+
109
+ handleCloseModal = () => {
110
+ this.setState({modalOpen: false})
111
+ }
112
+
113
+ handleChange = (latLng: google.maps.LatLng) => {
114
+ const {type, onChange} = this.props
115
+ onChange(
116
+ PatchEvent.from([
117
+ setIfMissing({
118
+ _type: type.name,
119
+ }),
120
+ set(latLng.lat(), ['lat']),
121
+ set(latLng.lng(), ['lng']),
122
+ ])
123
+ )
124
+ }
125
+
126
+ handleClear = () => {
127
+ const {onChange} = this.props
128
+ onChange(PatchEvent.from(unset()))
129
+ }
130
+
131
+ render() {
132
+ const {value, compareValue, readOnly, type, markers, level, presence} = this.props
133
+ const {modalOpen} = this.state
134
+
135
+ if (!config || !config.apiKey) {
136
+ return (
137
+ <div>
138
+ <p>
139
+ The <a href="https://sanity.io/docs/schema-types/geopoint-type">Geopoint type</a> needs
140
+ a Google Maps API key with access to:
141
+ </p>
142
+ <ul>
143
+ <li>Google Maps JavaScript API</li>
144
+ <li>Google Places API Web Service</li>
145
+ <li>Google Static Maps API</li>
146
+ </ul>
147
+ <p>
148
+ Please enter the API key with access to these services in
149
+ <code style={{whiteSpace: 'nowrap'}}>
150
+ `&lt;project-root&gt;/config/@sanity/google-maps-input.json`
151
+ </code>
152
+ </p>
153
+ </div>
154
+ )
155
+ }
156
+
157
+ return (
158
+ <FormFieldSet
159
+ level={level}
160
+ title={type.title}
161
+ description={type.description}
162
+ onFocus={this.handleFocus}
163
+ onBlur={this.handleBlur}
164
+ __unstable_presence={presence}
165
+ __unstable_changeIndicator={false}
166
+ __unstable_markers={markers}
167
+ >
168
+ <div>
169
+ {value && (
170
+ <ChangeIndicatorCompareValueProvider value={value} compareValue={compareValue}>
171
+ <ChangeIndicator compareDeep>
172
+ <PreviewImage src={getStaticImageUrl(value)} alt="Map location" />
173
+ </ChangeIndicator>
174
+ </ChangeIndicatorCompareValueProvider>
175
+ )}
176
+
177
+ {!readOnly && (
178
+ <Box marginTop={4}>
179
+ <Grid columns={2} gap={2}>
180
+ <Button
181
+ mode="ghost"
182
+ icon={value && EditIcon}
183
+ padding={3}
184
+ ref={this.setEditButton}
185
+ text={value ? 'Edit' : 'Set location'}
186
+ onClick={this.handleToggleModal}
187
+ />
188
+
189
+ {value && (
190
+ <Button
191
+ tone="critical"
192
+ icon={TrashIcon}
193
+ padding={3}
194
+ mode="ghost"
195
+ text={'Remove'}
196
+ onClick={this.handleClear}
197
+ />
198
+ )}
199
+ </Grid>
200
+ </Box>
201
+ )}
202
+
203
+ {modalOpen && (
204
+ <Dialog
205
+ id={`${this._geopointInputId}_dialog`}
206
+ onClose={this.handleCloseModal}
207
+ header="Place the marker on the map"
208
+ width={1}
209
+ >
210
+ <DialogInnerContainer>
211
+ <GoogleMapsLoadProxy>
212
+ {(api) => (
213
+ <GeopointSelect
214
+ api={api}
215
+ value={value}
216
+ onChange={readOnly ? undefined : this.handleChange}
217
+ defaultLocation={config.defaultLocation}
218
+ defaultZoom={config.defaultZoom}
219
+ />
220
+ )}
221
+ </GoogleMapsLoadProxy>
222
+ </DialogInnerContainer>
223
+ </Dialog>
224
+ )}
225
+ </div>
226
+ </FormFieldSet>
227
+ )
228
+ }
229
+ }
230
+
231
+ export default GeopointInput
@@ -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,93 @@
1
+ // @todo: remove the following line when part imports has been removed from this file
2
+ ///<reference types="@sanity/types/parts" />
3
+
4
+ import {Observable, BehaviorSubject} from 'rxjs'
5
+ import config from 'config:@sanity/google-maps-input'
6
+
7
+ const callbackName = '___sanity_googleMapsApiCallback'
8
+ const authFailureCallbackName = 'gm_authFailure'
9
+ const locale = (typeof window !== 'undefined' && window.navigator.language) || 'en'
10
+
11
+ export interface LoadingState {
12
+ loadState: 'loading'
13
+ }
14
+
15
+ export interface LoadedState {
16
+ loadState: 'loaded'
17
+ api: typeof window.google.maps
18
+ }
19
+
20
+ export interface LoadErrorState {
21
+ loadState: 'loadError'
22
+ error: Error
23
+ }
24
+
25
+ export interface AuthErrorState {
26
+ loadState: 'authError'
27
+ }
28
+
29
+ export type GoogleLoadState = LoadingState | LoadedState | LoadErrorState | AuthErrorState
30
+
31
+ let subject: BehaviorSubject<GoogleLoadState>
32
+
33
+ export function loadGoogleMapsApi(): Observable<GoogleLoadState> {
34
+ const selectedLocale = config.defaultLocale || locale || 'en-US'
35
+
36
+ if (subject) {
37
+ return subject
38
+ }
39
+
40
+ subject = new BehaviorSubject<GoogleLoadState>({loadState: 'loading'})
41
+
42
+ window[authFailureCallbackName] = () => {
43
+ delete window[authFailureCallbackName]
44
+ subject.next({loadState: 'authError'})
45
+ }
46
+
47
+ window[callbackName] = () => {
48
+ delete window[callbackName]
49
+ subject.next({loadState: 'loaded', api: window.google.maps})
50
+ }
51
+
52
+ const script = document.createElement('script')
53
+ script.onerror = (
54
+ event: Event | string,
55
+ source?: string,
56
+ lineno?: number,
57
+ colno?: number,
58
+ error?: Error
59
+ ) =>
60
+ subject.next({
61
+ loadState: 'loadError',
62
+ error: coeerceError(event, error),
63
+ } as LoadErrorState)
64
+
65
+ script.src = `https://maps.googleapis.com/maps/api/js?key=${config.apiKey}&libraries=places&callback=${callbackName}&language=${selectedLocale}`
66
+ document.getElementsByTagName('head')[0].appendChild(script)
67
+
68
+ return subject
69
+ }
70
+
71
+ function coeerceError(event: Event | string, error?: Error): Error {
72
+ if (error) {
73
+ return error
74
+ }
75
+
76
+ if (typeof event === 'string') {
77
+ return new Error(event)
78
+ }
79
+
80
+ return new Error(isErrorEvent(event) ? event.message : 'Failed to load Google Maps API')
81
+ }
82
+
83
+ function isErrorEvent(event: unknown): event is ErrorEvent {
84
+ if (typeof event !== 'object' || event === null) {
85
+ return false
86
+ }
87
+
88
+ if (!('message' in event)) {
89
+ return false
90
+ }
91
+
92
+ return typeof (event as ErrorEvent).message === 'string'
93
+ }
@@ -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
+ }
@@ -0,0 +1,10 @@
1
+ import styled from 'styled-components'
2
+
3
+ export const MapContainer = styled.div`
4
+ position: absolute;
5
+ top: 0;
6
+ left: 0;
7
+ height: 100%;
8
+ width: 100%;
9
+ box-sizing: border-box;
10
+ `
@@ -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
+ }