@sanity/google-maps-input 0.147.10-ts-client.1 → 1.0.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 (95) hide show
  1. package/LICENSE +1 -1
  2. package/config.dist.json +1 -0
  3. package/dts/diff/GeopointArrayDiff.d.ts +4 -0
  4. package/dts/diff/GeopointFieldDiff.d.ts +4 -0
  5. package/dts/diff/GeopointFieldDiff.styles.d.ts +1 -0
  6. package/dts/diff/GeopointMove.d.ts +12 -0
  7. package/dts/diff/resolver.d.ts +3 -0
  8. package/dts/input/GeopointInput.d.ts +64 -0
  9. package/dts/input/GeopointInput.styles.d.ts +2 -0
  10. package/dts/input/GeopointSelect.d.ts +52 -0
  11. package/dts/loader/GoogleMapsLoadProxy.d.ts +13 -0
  12. package/dts/loader/LoadError.d.ts +9 -0
  13. package/dts/loader/loadGoogleMapsApi.d.ts +17 -0
  14. package/dts/map/Arrow.d.ts +30 -0
  15. package/dts/map/Map.d.ts +38 -0
  16. package/dts/map/Map.styles.d.ts +1 -0
  17. package/dts/map/Marker.d.ts +38 -0
  18. package/dts/map/SearchInput.d.ts +17 -0
  19. package/dts/map/SearchInput.styles.d.ts +1 -0
  20. package/dts/map/util.d.ts +3 -0
  21. package/dts/types.d.ts +13 -0
  22. package/lib/@types/css.d.js +2 -0
  23. package/lib/@types/css.d.js.map +1 -0
  24. package/lib/diff/GeopointArrayDiff.js +66 -0
  25. package/lib/diff/GeopointArrayDiff.js.map +1 -0
  26. package/lib/diff/GeopointFieldDiff.js +78 -0
  27. package/lib/diff/GeopointFieldDiff.js.map +1 -0
  28. package/lib/diff/GeopointFieldDiff.styles.js +13 -0
  29. package/lib/diff/GeopointFieldDiff.styles.js.map +1 -0
  30. package/lib/diff/GeopointMove.js +49 -0
  31. package/lib/diff/GeopointMove.js.map +1 -0
  32. package/lib/diff/resolver.js +21 -0
  33. package/lib/diff/resolver.js.map +1 -0
  34. package/lib/input/GeopointInput.js +163 -0
  35. package/lib/input/GeopointInput.js.map +1 -0
  36. package/lib/input/GeopointInput.styles.js +15 -0
  37. package/lib/input/GeopointInput.styles.js.map +1 -0
  38. package/lib/input/GeopointSelect.js +81 -0
  39. package/lib/input/GeopointSelect.js.map +1 -0
  40. package/lib/loader/GoogleMapsLoadProxy.js +55 -0
  41. package/lib/loader/GoogleMapsLoadProxy.js.map +1 -0
  42. package/lib/loader/LoadError.js +37 -0
  43. package/lib/loader/LoadError.js.map +1 -0
  44. package/lib/loader/loadGoogleMapsApi.js +65 -0
  45. package/lib/loader/loadGoogleMapsApi.js.map +1 -0
  46. package/lib/map/Arrow.js +79 -0
  47. package/lib/map/Arrow.js.map +1 -0
  48. package/lib/map/Map.js +114 -0
  49. package/lib/map/Map.js.map +1 -0
  50. package/lib/map/Map.styles.js +13 -0
  51. package/lib/map/Map.styles.js.map +1 -0
  52. package/lib/map/Marker.js +125 -0
  53. package/lib/map/Marker.js.map +1 -0
  54. package/lib/map/SearchInput.js +60 -0
  55. package/lib/map/SearchInput.js.map +1 -0
  56. package/lib/map/SearchInput.styles.js +13 -0
  57. package/lib/map/SearchInput.styles.js.map +1 -0
  58. package/lib/map/util.js +14 -0
  59. package/lib/map/util.js.map +1 -0
  60. package/lib/types.js +2 -0
  61. package/lib/types.js.map +1 -0
  62. package/package.json +63 -27
  63. package/sanity.json +5 -1
  64. package/src/@types/css.d.ts +4 -0
  65. package/src/diff/GeopointArrayDiff.tsx +83 -0
  66. package/src/diff/GeopointFieldDiff.styles.tsx +20 -0
  67. package/src/diff/GeopointFieldDiff.tsx +94 -0
  68. package/src/diff/GeopointMove.tsx +49 -0
  69. package/src/diff/resolver.ts +21 -0
  70. package/src/input/GeopointInput.styles.tsx +12 -0
  71. package/src/input/GeopointInput.tsx +231 -0
  72. package/src/input/GeopointSelect.tsx +78 -0
  73. package/src/loader/GoogleMapsLoadProxy.tsx +49 -0
  74. package/src/loader/LoadError.tsx +44 -0
  75. package/src/loader/loadGoogleMapsApi.ts +93 -0
  76. package/src/map/Arrow.tsx +76 -0
  77. package/src/map/Map.styles.tsx +10 -0
  78. package/src/map/Map.tsx +125 -0
  79. package/src/map/Marker.tsx +132 -0
  80. package/src/map/SearchInput.styles.tsx +8 -0
  81. package/src/map/SearchInput.tsx +55 -0
  82. package/src/map/util.ts +14 -0
  83. package/src/types.ts +16 -0
  84. package/example/Main.js +0 -28
  85. package/example/config/.checksums +0 -4
  86. package/example/config/@sanity/google-maps-input.json +0 -8
  87. package/example/sanity.json +0 -33
  88. package/example/schema.js +0 -22
  89. package/lib/GeopointInput.js +0 -170
  90. package/lib/GeopointSelect.js +0 -174
  91. package/lib/GoogleMapsLoadProxy.js +0 -65
  92. package/lib/loadGoogleMapsApi.js +0 -36
  93. package/locale/strings.json +0 -11
  94. package/styles/GeopointInput.css +0 -28
  95. package/styles/GeopointSelect.css +0 -27
@@ -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
+ }
@@ -0,0 +1,132 @@
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
+ onMouseOver?: (event: google.maps.MapMouseEvent) => void
14
+ onMouseOut?: (event: google.maps.MapMouseEvent) => void
15
+ position: LatLng | google.maps.LatLng
16
+ zIndex?: number
17
+ opacity?: number
18
+ label?: string
19
+ markerRef?: React.MutableRefObject<google.maps.Marker | undefined>
20
+ color?: {background: string; border: string; text: string}
21
+ }
22
+
23
+ export class Marker extends React.PureComponent<Props> {
24
+ marker: google.maps.Marker | undefined
25
+
26
+ eventHandlers: {
27
+ move?: google.maps.MapsEventListener
28
+ click?: google.maps.MapsEventListener
29
+ } = {}
30
+
31
+ componentDidMount() {
32
+ const {position, api, map, onMove, zIndex, opacity, label, markerRef, color} = this.props
33
+ const {Marker: GMarker} = api
34
+
35
+ let icon: google.maps.ReadonlySymbol | undefined
36
+ if (color) {
37
+ icon = {
38
+ path: markerPath,
39
+ fillOpacity: 1,
40
+ fillColor: color.background,
41
+ strokeColor: color.border,
42
+ strokeWeight: 2,
43
+ anchor: new api.Point(10, 35),
44
+ labelOrigin: new api.Point(10, 11),
45
+ }
46
+ }
47
+
48
+ this.marker = new GMarker({
49
+ draggable: Boolean(onMove),
50
+ position,
51
+ map,
52
+ zIndex,
53
+ opacity,
54
+ label,
55
+ icon,
56
+ })
57
+
58
+ if (markerRef) {
59
+ markerRef.current = this.marker
60
+ }
61
+
62
+ this.attachMoveHandler()
63
+ this.attachClickHandler()
64
+ }
65
+
66
+ componentDidUpdate(prevProps: Props) {
67
+ if (!this.marker) {
68
+ return
69
+ }
70
+
71
+ const {position, onMove, label, zIndex, opacity, map} = this.props
72
+
73
+ if (prevProps.onMove !== onMove) {
74
+ this.attachMoveHandler()
75
+ }
76
+
77
+ if (!latLngAreEqual(prevProps.position, position)) {
78
+ this.marker.setPosition(position)
79
+ }
80
+
81
+ if (prevProps.label !== label) {
82
+ this.marker.setLabel(label || null)
83
+ }
84
+
85
+ if (prevProps.zIndex !== zIndex) {
86
+ this.marker.setZIndex(zIndex || null)
87
+ }
88
+
89
+ if (prevProps.opacity !== opacity) {
90
+ this.marker.setOpacity(opacity || null)
91
+ }
92
+
93
+ if (prevProps.map !== map) {
94
+ this.marker.setMap(map)
95
+ }
96
+ }
97
+
98
+ componentWillUnmount() {
99
+ if (this.eventHandlers.move) {
100
+ this.eventHandlers.move.remove()
101
+ }
102
+
103
+ if (this.marker) {
104
+ this.marker.setMap(null)
105
+ }
106
+ }
107
+
108
+ attachMoveHandler() {
109
+ const {api, onMove} = this.props
110
+ if (this.eventHandlers.move) {
111
+ this.eventHandlers.move.remove()
112
+ }
113
+ if (this.marker && onMove) {
114
+ this.eventHandlers.move = api.event.addListener(this.marker, 'dragend', onMove)
115
+ }
116
+ }
117
+
118
+ attachClickHandler() {
119
+ const {api, onClick} = this.props
120
+ if (this.eventHandlers.click) {
121
+ this.eventHandlers.click.remove()
122
+ }
123
+ if (this.marker && onClick) {
124
+ this.eventHandlers.click = api.event.addListener(this.marker, 'click', onClick)
125
+ }
126
+ }
127
+
128
+ // eslint-disable-next-line class-methods-use-this
129
+ render() {
130
+ return null
131
+ }
132
+ }
@@ -0,0 +1,8 @@
1
+ import styled from 'styled-components'
2
+
3
+ export const WrapperContainer = styled.div`
4
+ position: absolute;
5
+ right: 10px;
6
+ top: 10px;
7
+ width: 220px;
8
+ `
@@ -0,0 +1,55 @@
1
+ import * as React from 'react'
2
+ import {TextInput} from '@sanity/ui'
3
+ import {WrapperContainer} from './SearchInput.styles'
4
+ interface Props {
5
+ api: typeof window.google.maps
6
+ map: google.maps.Map
7
+ onChange: (result: google.maps.places.PlaceResult) => void
8
+ }
9
+
10
+ export class SearchInput extends React.PureComponent<Props> {
11
+ searchInputRef = React.createRef<HTMLInputElement>()
12
+ autoComplete: google.maps.places.Autocomplete | undefined
13
+
14
+ handleChange = () => {
15
+ if (!this.autoComplete) {
16
+ return
17
+ }
18
+
19
+ this.props.onChange(this.autoComplete.getPlace())
20
+
21
+ if (this.searchInputRef.current) {
22
+ this.searchInputRef.current.value = ''
23
+ }
24
+ }
25
+
26
+ componentDidMount() {
27
+ const input = this.searchInputRef.current
28
+ if (!input) {
29
+ return
30
+ }
31
+
32
+ const {api, map} = this.props
33
+ const {Circle, places, event} = api
34
+ const searchBounds = new Circle({center: map.getCenter(), radius: 100}).getBounds()
35
+ this.autoComplete = new places.Autocomplete(input, {
36
+ bounds: searchBounds,
37
+ types: [], // return all kinds of places
38
+ })
39
+
40
+ event.addListener(this.autoComplete, 'place_changed', this.handleChange)
41
+ }
42
+
43
+ render() {
44
+ return (
45
+ <WrapperContainer>
46
+ <TextInput
47
+ name="place"
48
+ ref={this.searchInputRef}
49
+ placeholder="Search for place or address"
50
+ padding={4}
51
+ />
52
+ </WrapperContainer>
53
+ )
54
+ }
55
+ }
@@ -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,16 @@
1
+ import {ObjectSchemaType} from '@sanity/field/diff'
2
+
3
+ export interface LatLng {
4
+ lat: number
5
+ lng: number
6
+ }
7
+
8
+ export interface Geopoint {
9
+ _type: 'geopoint'
10
+ _key?: string
11
+ lat: number
12
+ lng: number
13
+ alt?: number
14
+ }
15
+
16
+ export type GeopointSchemaType = ObjectSchemaType<Geopoint>
package/example/Main.js DELETED
@@ -1,28 +0,0 @@
1
- import React from 'react'
2
- import FormBuilder from 'part:@sanity/form-builder'
3
- import SanityIntlProvider from 'part:@sanity/base/sanity-intl-provider'
4
-
5
- export default class QuickstartExample extends React.Component {
6
- state = {
7
- editorValue: FormBuilder.createEmpty('myTestLocation')
8
- }
9
-
10
- handleChange = event => {
11
- this.setState({editorValue: this.state.editorValue.patch(event.patch)})
12
- }
13
-
14
- handleLogClick = event => {
15
- console.log(this.state.editorValue) // eslint-disable-line no-console
16
- }
17
-
18
- render() {
19
- return (
20
- <SanityIntlProvider supportedLanguages={['en-US']}>
21
- <FormBuilder value={this.state.editorValue} onChange={this.handleChange} />
22
- <button type="button" onClick={this.handleLogClick}>
23
- Output current value to console
24
- </button>
25
- </SanityIntlProvider>
26
- )
27
- }
28
- }
@@ -1,4 +0,0 @@
1
- {
2
- "#": "Used by Sanity to keep track of configuration file checksums, do not delete or modify!",
3
- "@sanity/data-aspects": "ba5c2649cc1b1c39ae92b7daf2661f95fa79d7325073ffd410245d2717b240e9"
4
- }
@@ -1,8 +0,0 @@
1
- {
2
- "apiKey": null,
3
- "defaultZoom": 11,
4
- "defaultLocation": {
5
- "lat": 40.7058254,
6
- "lng": -74.1180863
7
- }
8
- }
@@ -1,33 +0,0 @@
1
- {
2
- "server": {
3
- "hostname": "0.0.0.0"
4
- },
5
- "api": {
6
- "dataset": "production",
7
- "projectId": "foobar"
8
- },
9
- "plugins": [
10
- "@sanity/base",
11
- "@sanity/components",
12
- "@sanity/form-builder"
13
- ],
14
- "parts": [
15
- {
16
- "implements": "part:@sanity/base/root",
17
- "path": "./Main"
18
- },
19
- {
20
- "implements": "part:@sanity/base/schema",
21
- "path": "./schema.js"
22
- },
23
- {
24
- "name": "part:@sanity/google-maps-input/input/geopoint",
25
- "implements": "part:@sanity/form-builder/input/geopoint",
26
- "path": "../src/GeopointInput.js"
27
- },
28
- {
29
- "implements": "part:@sanity/base/locale-messages",
30
- "path": "../locale/strings.json"
31
- }
32
- ]
33
- }
package/example/schema.js DELETED
@@ -1,22 +0,0 @@
1
- import bundledTypes from 'part:@sanity/base/bundled-types'
2
- import {keyBy} from 'lodash'
3
-
4
- const keyedTypes = keyBy(bundledTypes.types, 'name')
5
-
6
- export default {
7
- name: 'test',
8
- types: [
9
- keyedTypes.geopoint,
10
- {
11
- name: 'myTestLocation',
12
- type: 'object',
13
- fields: [
14
- {
15
- name: 'location',
16
- type: 'geopoint',
17
- title: 'Location'
18
- }
19
- ]
20
- }
21
- ]
22
- }