import React, { memo, useCallback, useEffect, useState, useRef, ReactElement } from 'react'; import styled, { css } from 'styled-components'; import copy from 'copy-to-clipboard'; import projections from '@sigeo/epsg-projections'; import proj4 from 'proj4'; import { register } from 'ol/proj/proj4'; import { toStringHDMS } from 'ol/coordinate'; import { ScaleLine } from 'ol/control'; import { toLonLat } from 'ol/proj'; import Text from '@material-ui/core/Typography'; import Menu from '@material-ui/core/Menu'; import MenuItem from '@material-ui/core/MenuItem'; import Grid from '@material-ui/core/Grid'; import IconButton from '@material-ui/core/IconButton'; import ListItemIcon from '@material-ui/core/ListItemIcon'; import AppBar, { AppBarProps } from '@material-ui/core/AppBar'; import Toolbar from '@material-ui/core/Toolbar'; import FileCopyOutlinedIcon from '@material-ui/icons/FileCopyOutlined'; import GpsFixedIcon from '@material-ui/icons/GpsFixed'; import LinearScaleIcon from '@material-ui/icons/LinearScale'; import './BottomBar.css'; const commonStyle = css` span { color: white; } svg { color: var(--color-grey-100); } `; const Coordinates = styled(Grid)` && { ${commonStyle} } `; const CustomScale = styled(Grid)` && { ${commonStyle} } `; const CustomAppBar = styled(AppBar)` && { background-color: rgba(0, 0, 0, 0.3); top: auto; bottom: 0; left: 0; } `; interface IProps extends AppBarProps { map: any; onCoordinatesCopied?: () => void; children?: any; // TODO: improve it coordinatesMenuItems?: ReactElement[]; } export function BottomBar({ map, children, onCoordinatesCopied, coordinatesMenuItems, ...rest }: IProps) { const [anchorMenuEl, changeAnchorMenuEl] = useState(null); const coordinateXRef: any = useRef(null); const coordinateYRef: any = useRef(null); const getProjectionCode = useCallback( () => map ?.getMap?.() ?.getView?.() ?.getProjection?.() ?.getCode?.(), [map] ); const handlePointermove = useCallback( (evt: any) => { if (evt.dragging) { return; } const projection = getProjectionCode(); if (!projection) { return; } const hdms = toStringHDMS(toLonLat(evt.coordinate, projection)); if (!hdms) { return; } const firstIndex = hdms.indexOf('N') + 1; coordinateXRef.current.textContent = hdms.substring(0, firstIndex); coordinateYRef.current.textContent = hdms.substring( firstIndex, hdms.length ); }, [getProjectionCode] ); useEffect(() => { if (!map) { return; } const projectionCode = getProjectionCode(); proj4.defs(projectionCode, projections[projectionCode]); register(proj4); map.getMap?.()?.addControl?.(new ScaleLine({ target: 'mapScale' })); }, [getProjectionCode, map]); useEffect(() => { map?.getMap?.()?.on?.('pointermove', handlePointermove); return () => { map?.getMap?.()?.un?.('pointermove', handlePointermove); }; }, [handlePointermove, map]); const handleCopyToClipboard = useCallback( (value: string) => { copy(value); onCoordinatesCopied?.(); }, [onCoordinatesCopied] ); return ( <> changeAnchorMenuEl(e.currentTarget)} > 0° N 0° E {children} changeAnchorMenuEl(null)} > { const x = coordinateXRef?.current?.textContent; const y = coordinateYRef?.current?.textContent; handleCopyToClipboard(`${x}, ${y}`); changeAnchorMenuEl(null); }} > Copia coordinate {coordinatesMenuItems?.map((el: ReactElement) => React.cloneElement(el, { onClick: () => { changeAnchorMenuEl(null); el?.props?.onClick?.(); } }) )} ); } export default memo(BottomBar);