-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[google-maps-input] Provide diff component
- Loading branch information
Showing
16 changed files
with
334 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"rules": { | ||
"react/prop-types": "off", | ||
"react/require-default-props": "off", | ||
"complexity": ["warn", 15] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/@sanity/google-maps-input/src/diff/GeopointFieldDiff.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.root { | ||
height: 300px; | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/@sanity/google-maps-input/src/diff/GeopointFieldDiff.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as React from 'react' | ||
import {ObjectDiff} from '@sanity/diff' | ||
import {GeopointSchemaType, Geopoint, Annotation} from '../types' | ||
import {GoogleMapsLoadProxy} from '../loader/GoogleMapsLoadProxy' | ||
import {GoogleMap} from '../map/Map' | ||
import {GeopointMove} from './GeopointMove' | ||
import styles from './GeopointFieldDiff.css' | ||
|
||
export interface DiffProps { | ||
diff: ObjectDiff<Annotation, Geopoint> | ||
schemaType: GeopointSchemaType | ||
} | ||
|
||
export const GeopointFieldDiff: React.ComponentType<DiffProps> = ({diff, schemaType}) => { | ||
return ( | ||
<div className={styles.root}> | ||
<GoogleMapsLoadProxy> | ||
{api => <GeopointDiff api={api} diff={diff} schemaType={schemaType} />} | ||
</GoogleMapsLoadProxy> | ||
</div> | ||
) | ||
} | ||
|
||
function GeopointDiff({api, diff}: DiffProps & {api: typeof window.google.maps}) { | ||
const {fromValue, toValue} = diff | ||
const center = getCenter(diff, api) | ||
const bounds = fromValue && toValue ? getBounds(fromValue, toValue, api) : undefined | ||
|
||
return ( | ||
<GoogleMap api={api} location={center} mapTypeControl={false} controlSize={20} bounds={bounds}> | ||
{map => <GeopointMove api={api} map={map} diff={diff} />} | ||
</GoogleMap> | ||
) | ||
} | ||
|
||
function getBounds( | ||
fromValue: google.maps.LatLngLiteral, | ||
toValue: google.maps.LatLngLiteral, | ||
api: typeof window.google.maps | ||
): google.maps.LatLngBounds { | ||
return new api.LatLngBounds().extend(fromValue).extend(toValue) | ||
} | ||
|
||
function getCenter( | ||
diff: DiffProps['diff'], | ||
api: typeof window.google.maps | ||
): google.maps.LatLngLiteral { | ||
const {fromValue, toValue} = diff | ||
if (fromValue && toValue) { | ||
return getBounds(fromValue, toValue, api) | ||
.getCenter() | ||
.toJSON() | ||
} | ||
|
||
if (fromValue) { | ||
return fromValue | ||
} | ||
|
||
if (toValue) { | ||
return toValue | ||
} | ||
|
||
throw new Error('Neither a from or a to value present') | ||
} |
95 changes: 95 additions & 0 deletions
95
packages/@sanity/google-maps-input/src/diff/GeopointMove.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as React from 'react' | ||
import * as ReactDOM from 'react-dom' | ||
import {ObjectDiff} from '@sanity/diff' | ||
import {useUser} from '@sanity/react-hooks' | ||
import {UserAvatar} from '@sanity/components/presence' | ||
import {Marker} from '../map/Marker' | ||
import {Arrow} from '../map/Arrow' | ||
import {Geopoint, Annotation} from '../types' | ||
|
||
interface Props { | ||
api: typeof window.google.maps | ||
map: google.maps.Map | ||
diff: ObjectDiff<Annotation, Geopoint> | ||
label?: string | ||
} | ||
|
||
export function GeopointMove({diff, api, map, label}: Props) { | ||
const {fromValue: from, toValue: to} = diff | ||
const annotation = diff.isChanged ? diff.annotation : undefined | ||
const fromRef = React.useRef<google.maps.Marker>() | ||
const toRef = React.useRef<google.maps.Marker>() | ||
const infoEl = React.useRef(document.createElement('div')) | ||
const infoWindow = React.useRef(new api.InfoWindow({content: infoEl.current})) | ||
const openInfoOn = (ref: React.MutableRefObject<google.maps.Marker | undefined>) => | ||
React.useCallback(() => { | ||
const current = ref.current | ||
if (!current) { | ||
return | ||
} | ||
|
||
infoWindow.current.open(map, current) | ||
|
||
const position = current.getPosition() | ||
if (position) { | ||
map.panTo(position) | ||
} | ||
}, [infoWindow.current, map]) | ||
|
||
const openInfoOnFrom = openInfoOn(fromRef) | ||
const openInfoOnTo = openInfoOn(toRef) | ||
|
||
return ( | ||
<> | ||
{from && ( | ||
<Marker | ||
api={api} | ||
map={map} | ||
position={from} | ||
zIndex={0} | ||
opacity={0.75} | ||
onClick={openInfoOnFrom} | ||
markerRef={fromRef} | ||
label={label} | ||
/> | ||
)} | ||
{from && to && <Arrow api={api} map={map} from={from} to={to} zIndex={1} />} | ||
{to && ( | ||
<Marker | ||
api={api} | ||
map={map} | ||
position={to} | ||
zIndex={2} | ||
onClick={openInfoOnTo} | ||
markerRef={toRef} | ||
label={label} | ||
/> | ||
)} | ||
{annotation && | ||
ReactDOM.createPortal(<AnnotationInfo annotation={annotation} />, infoEl.current)} | ||
</> | ||
) | ||
} | ||
|
||
function UserItem(props: {userId: string}) { | ||
const {isLoading, error, value: user} = useUser(props.userId) | ||
|
||
// @todo handle? | ||
if (error) { | ||
return null | ||
} | ||
|
||
if (isLoading) { | ||
return <em>Loading…</em> | ||
} | ||
|
||
return user ? ( | ||
<> | ||
<UserAvatar user={user} /> {user.displayName} | ||
</> | ||
) : null | ||
} | ||
|
||
function AnnotationInfo({annotation}: {annotation: Annotation}) { | ||
return <UserItem userId={annotation.author} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {GeopointSchemaType} from '../types' | ||
import {GeopointFieldDiff} from './GeopointFieldDiff' | ||
|
||
export default function diffResolver({schemaType}: {schemaType: GeopointSchemaType}) { | ||
if (schemaType.name === 'geopoint') { | ||
return GeopointFieldDiff | ||
} | ||
|
||
return undefined | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
10 changes: 5 additions & 5 deletions
10
.../google-maps-input/src/GeopointSelect.tsx → ...e-maps-input/src/input/GeopointSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as React from 'react' | ||
import {LatLng} from '../types' | ||
|
||
interface Props { | ||
api: typeof window.google.maps | ||
map: google.maps.Map | ||
from: LatLng | ||
to: LatLng | ||
zIndex?: number | ||
arrowRef?: React.MutableRefObject<google.maps.Polyline | undefined> | ||
onClick?: (event: google.maps.MouseEvent) => void | ||
} | ||
|
||
export class Arrow extends React.Component<Props> { | ||
line: google.maps.Polyline | undefined | ||
|
||
eventHandlers: { | ||
click?: google.maps.MapsEventListener | ||
} = {} | ||
|
||
componentDidMount() { | ||
const {from, to, api, map, zIndex, onClick, arrowRef} = this.props | ||
const lineSymbol = { | ||
path: api.SymbolPath.FORWARD_OPEN_ARROW | ||
} | ||
|
||
this.line = new api.Polyline({ | ||
map, | ||
zIndex, | ||
path: [from, to], | ||
icons: [{icon: lineSymbol, offset: '50%'}], | ||
strokeOpacity: 0.8, | ||
strokeColor: 'blue' | ||
}) | ||
|
||
if (onClick) { | ||
this.eventHandlers.click = api.event.addListener(this.line, 'click', onClick) | ||
} | ||
|
||
if (arrowRef) { | ||
arrowRef.current = this.line | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps: Props) { | ||
if (!this.line) { | ||
return | ||
} | ||
|
||
const {from, to, map} = this.props | ||
if (prevProps.from !== from || prevProps.to !== to) { | ||
this.line.setPath([from, to]) | ||
} | ||
|
||
if (prevProps.map !== map) { | ||
this.line.setMap(map) | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.line) { | ||
this.line.setMap(null) | ||
} | ||
|
||
if (this.eventHandlers.click) { | ||
this.eventHandlers.click.remove() | ||
} | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
render() { | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.