Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajoute un bouton boussole pour réorienter la carte #807

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions components/map/controls/address-editor-control.js

This file was deleted.

49 changes: 0 additions & 49 deletions components/map/controls/control.js

This file was deleted.

31 changes: 31 additions & 0 deletions components/map/controls/map-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {useState} from 'react'
import PropTypes from 'prop-types'
import {Tooltip, Position, IconButton, Pane} from 'evergreen-ui'

function MapButton({tooltipContent, marginBottom, ...buttonProps}) {
const [hovered, setHovered] = useState(false)
return (
<Pane onMouseEnter={() => setHovered(true)} onMouseLeave={() => setHovered(false)} marginBottom={marginBottom}>
<Tooltip isShown={!buttonProps.disabled && hovered} position={Position.LEFT} content={tooltipContent}>
<IconButton
width={29}
height={29}
{...buttonProps}
/>
</Tooltip>
</Pane>
)
}

MapButton.propTypes = {
tooltipContent: PropTypes.string.isRequired,
onClick: PropTypes.func.isRequired,
marginBottom: PropTypes.number,
// eslint-disable-next-line react/boolean-prop-naming
disabled: PropTypes.bool,
icon: PropTypes.object.isRequired,
intent: PropTypes.string,
appearance: PropTypes.string
}

export default MapButton
142 changes: 142 additions & 0 deletions components/map/controls/map-controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import {useContext, useState, useEffect} from 'react'
import PropTypes from 'prop-types'
import {Pane, Alert, EyeOffIcon, EyeOpenIcon, AddIcon, CrossIcon, CompassIcon} from 'evergreen-ui'
import StyleControl from '@/components/map/controls/style-control'
import MapContext from '@/contexts/map'
import DrawContext from '@/contexts/draw'
import BalDataContext from '@/contexts/bal-data'
import TokenContext from '@/contexts/token'
import MapButton from './map-button'

// It looks better if the black arrow is pointing to the north
const DEFAULT_COMPASS_BEARING = 180
const DEFAULT_COMPASS_PITCH = 0

function MapControls({commune, isLabelsDisplayed, setIsLabelsDisplayed, isAddressFormOpen, handleAddressForm}) {
const [compassTransform, setCompassTransform] = useState({
bearing: DEFAULT_COMPASS_BEARING,
pitch: DEFAULT_COMPASS_PITCH
})
const {style, setStyle, isCadastreDisplayed, setIsCadastreDisplayed, map, setViewport} = useContext(MapContext)
const {hint} = useContext(DrawContext)
const {
voie,
numeros,
toponymes,
isEditing
} = useContext(BalDataContext)
const {token} = useContext(TokenContext)

useEffect(() => {
if (!map) {
return
}

const pitch = map.getPitch()
const bearing = map.getBearing()

setCompassTransform({
bearing: DEFAULT_COMPASS_BEARING - bearing,
pitch: DEFAULT_COMPASS_PITCH - pitch
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [map?.transform.pitch, map?.transform.bearing])

const getShowHintsContent = () => {
if (numeros) {
return isLabelsDisplayed ? 'Masquer les détails' : 'Afficher les détails'
}

return isLabelsDisplayed ? 'Masquer les toponymes' : 'Afficher les toponymes'
}

const onCenterMap = () => {
setViewport(currentViewport => ({
...currentViewport,
pitch: 0,
bearing: 0
}))
setCompassTransform({
bearing: DEFAULT_COMPASS_BEARING,
pitch: DEFAULT_COMPASS_PITCH
})
}

const isRecenterButtonDisabled =
Math.abs(Math.round(compassTransform.bearing)) === DEFAULT_COMPASS_BEARING &&
Math.abs(Math.round(compassTransform.pitch)) === DEFAULT_COMPASS_PITCH

return (
<>
<StyleControl
style={style}
handleStyle={setStyle}
commune={commune}
isCadastreDisplayed={isCadastreDisplayed}
handleCadastre={setIsCadastreDisplayed}
/>

<Pane
position='absolute'
top={90}
right={16}
zIndex={1}
className='map-controls-buttons'
display='flex'
flexDirection='column'
>
<MapButton
tooltipContent='Réorienter la carte'
icon={() => <CompassIcon style={{transform: `rotate(${compassTransform.bearing}deg)`}} />}
onClick={onCenterMap}
disabled={isRecenterButtonDisabled}
marginBottom={15}
/>

{(voie || (toponymes && toponymes.length > 0)) && (
<MapButton
icon={isLabelsDisplayed ? EyeOffIcon : EyeOpenIcon}
tooltipContent={getShowHintsContent()}
onClick={() => setIsLabelsDisplayed(!isLabelsDisplayed)}
marginBottom={5}
/>
)}

{token && (
<MapButton
tooltipContent={isAddressFormOpen ? 'Annuler' : 'Créer une adresse'}
icon={isAddressFormOpen ? CrossIcon : AddIcon}
disabled={isEditing && !isAddressFormOpen}
onClick={() => handleAddressForm(!isAddressFormOpen)}
intent={isAddressFormOpen ? '' : 'success'}
appearance={isAddressFormOpen ? '' : 'primary'}
/>
)}
</Pane>

{hint && (
<Pane
zIndex={1}
position='fixed'
alignSelf='center'
top={130}
>
<Alert title={hint} />
</Pane>
Comment on lines +117 to +125
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Le hint sont des information a afficher sur la map (pour le tracer d'une voie par exemple), je ne mettrais pas ca dans les controls

)}
</>
)
}

MapControls.propTypes = {
commune: PropTypes.shape({
hasOrtho: PropTypes.bool.isRequired
}).isRequired,
isLabelsDisplayed: PropTypes.bool.isRequired,
setIsLabelsDisplayed: PropTypes.func.isRequired,
isAddressFormOpen: PropTypes.bool.isRequired,
handleAddressForm: PropTypes.func.isRequired
}

export default MapControls

68 changes: 9 additions & 59 deletions components/map/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import {useRouter} from 'next/router'
import MapGl from 'react-map-gl'
import maplibregl from 'maplibre-gl'
import {fromJS} from 'immutable'
import {Pane, Alert, EyeOffIcon, EyeOpenIcon} from 'evergreen-ui'
import {Pane} from 'evergreen-ui'

import MapContext from '@/contexts/map'
import BalDataContext from '@/contexts/bal-data'
import TokenContext from '@/contexts/token'
import DrawContext from '@/contexts/draw'
import ParcellesContext from '@/contexts/parcelles'

Expand All @@ -21,13 +20,11 @@ import EditableMarker from '@/components/map/editable-marker'
import NumerosMarkers from '@/components/map/numeros-markers'
import ToponymeMarker from '@/components/map/toponyme-marker'
import Draw from '@/components/map/draw'
import Control from '@/components/map/controls/control'
import NavControl from '@/components/map/controls/nav-control'
import StyleControl from '@/components/map/controls/style-control'
import AddressEditorControl from '@/components/map/controls/address-editor-control'
import useBounds from '@/components/map/hooks/bounds'
import useSources from '@/components/map/hooks/sources'
import useHovered from '@/components/map/hooks/hovered'
import MapControls from './controls/map-controls'

const LAYERS = [
...cadastreLayers,
Expand Down Expand Up @@ -86,7 +83,7 @@ function generateNewStyle(style) {

function Map({commune, isAddressFormOpen, handleAddressForm}) {
const router = useRouter()
const {map, handleMapRef, style, setStyle, defaultStyle, isStyleLoaded, viewport, setViewport, isCadastreDisplayed, setIsCadastreDisplayed} = useContext(MapContext)
const {map, handleMapRef, style, setStyle, defaultStyle, isStyleLoaded, viewport, setViewport, isCadastreDisplayed} = useContext(MapContext)
const {isParcelleSelectionEnabled, handleParcelle} = useContext(ParcellesContext)

const [isLabelsDisplayed, setIsLabelsDisplayed] = useState(true)
Expand All @@ -104,8 +101,7 @@ function Map({commune, isAddressFormOpen, handleAddressForm}) {
setEditingId,
isEditing
} = useContext(BalDataContext)
const {modeId, hint} = useContext(DrawContext)
const {token} = useContext(TokenContext)
const {modeId} = useContext(DrawContext)

const communeHasOrtho = useMemo(() => commune.hasOrtho, [commune])

Expand Down Expand Up @@ -245,59 +241,13 @@ function Map({commune, isAddressFormOpen, handleAddressForm}) {

return (
<Pane display='flex' flexDirection='column' flex={1}>
<StyleControl
style={style}
handleStyle={setStyle}
<MapControls
commune={commune}
isCadastreDisplayed={isCadastreDisplayed}
handleCadastre={setIsCadastreDisplayed}
isLabelsDisplayed={isLabelsDisplayed}
setIsLabelsDisplayed={setIsLabelsDisplayed}
isAddressFormOpen={isAddressFormOpen}
handleAddressForm={handleAddressForm}
/>

{(voie || (toponymes && toponymes.length > 0)) && (
<Pane
position='absolute'
className='maplibregl-ctrl-group maplibregl-ctrl'
top={100}
right={16}
zIndex={2}
>

<Control
icon={isLabelsDisplayed ? EyeOffIcon : EyeOpenIcon}
isEnabled={isLabelsDisplayed}
enabledHint={numeros ? 'Masquer les détails' : 'Masquer les toponymes'}
disabledHint={numeros ? 'Afficher les détails' : 'Afficher les toponymes'}
onChange={setIsLabelsDisplayed}
/>
</Pane>
)}

{token && (
<Pane
position='absolute'
zIndex={1}
top={voie || (toponymes && toponymes.length > 0) ? 136 : 100}
right={15}
>
<AddressEditorControl
isAddressFormOpen={isAddressFormOpen}
handleAddressForm={handleAddressForm}
isDisabled={isEditing && !isAddressFormOpen}
/>
</Pane>
)}

{hint && (
<Pane
zIndex={1}
position='fixed'
alignSelf='center'
top={130}
>
<Alert title={hint} />
</Pane>
)}

<Pane display='flex' flex={1}>
<MapGl
ref={handleMapRef}
Expand Down
9 changes: 6 additions & 3 deletions contexts/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const defaultViewport = {
latitude: 46.5693,
longitude: 1.1771,
zoom: 6,
transitionDuration: 0
transitionDuration: 0,
pitch: 0,
bearing: 0
}

const defaultStyle = 'vector'
Expand Down Expand Up @@ -34,13 +36,14 @@ export function MapContextProvider(props) {
isStyleLoaded,
viewport, setViewport,
isCadastreDisplayed, setIsCadastreDisplayed,
}), [
}
), [
map,
handleMapRef,
isStyleLoaded,
style,
viewport,
isCadastreDisplayed
isCadastreDisplayed,
])

return (
Expand Down