11import { MapClickOverride , useMapStateContext } from '../utils/MapStateContext' ;
22import { useStarsContext } from '../utils/StarsContext' ;
3- import React , { useEffect , useRef , useState } from 'react' ;
3+ import React , { useEffect , useMemo , useRef , useState } from 'react' ;
44import { abortFetch } from '../../services/fetch' ;
55import {
66 fetchGeocoderOptions ,
@@ -13,14 +13,15 @@ import { Autocomplete, InputAdornment, TextField } from '@mui/material';
1313import { useMapCenter } from '../SearchBox/utils' ;
1414import { useUserThemeContext } from '../../helpers/theme' ;
1515import { renderOptionFactory } from '../SearchBox/renderOptionFactory' ;
16- import PlaceIcon from '@mui/icons-material/Place' ;
1716import { Option } from '../SearchBox/types' ;
1817import { getOptionLabel } from '../SearchBox/getOptionLabel' ;
1918import { useUserSettingsContext } from '../utils/UserSettingsContext' ;
2019import { getCoordsOption } from '../SearchBox/options/coords' ;
2120import { LonLat } from '../../services/types' ;
2221import { getGlobalMap } from '../../services/mapStorage' ;
23- import maplibregl , { LngLatLike } from 'maplibre-gl' ;
22+ import maplibregl , { LngLatLike , PointLike } from 'maplibre-gl' ;
23+ import ReactDOMServer from 'react-dom/server' ;
24+ import { AlphabeticalMarker } from './TextMarker' ;
2425
2526const StyledTextField = styled ( TextField ) `
2627 input::placeholder {
@@ -35,6 +36,7 @@ const DirectionsInput = ({
3536 label,
3637 onFocus,
3738 onBlur,
39+ pointIndex,
3840} ) => {
3941 const { InputLabelProps, InputProps, ...restParams } = params ;
4042
@@ -59,8 +61,11 @@ const DirectionsInput = ({
5961 fullWidth
6062 InputProps = { {
6163 startAdornment : (
62- < InputAdornment position = "start" >
63- < PlaceIcon fontSize = "small" />
64+ < InputAdornment
65+ position = "start"
66+ sx = { { position : 'relative' , top : 5 , left : 5 } }
67+ >
68+ < AlphabeticalMarker hasPin = { false } index = { pointIndex } height = { 32 } />
6469 </ InputAdornment >
6570 ) ,
6671 } }
@@ -137,14 +142,15 @@ type Props = {
137142 label : string ;
138143 value : Option ;
139144 setValue : ( value : Option ) => void ;
145+ pointIndex : number ;
140146} ;
141147
142- const PREVIEW_MARKER = {
143- color : 'salmon' ,
144- draggable : false ,
145- } ;
146-
147- export const DirectionsAutocomplete = ( { label , value , setValue } : Props ) => {
148+ export const DirectionsAutocomplete = ( {
149+ label ,
150+ value ,
151+ setValue ,
152+ pointIndex ,
153+ } : Props ) => {
148154 const autocompleteRef = useRef ( ) ;
149155 const { inputValue, setInputValue } = useInputValueState ( ) ;
150156 const selectedOptionInputValue = useRef < string | null > ( null ) ;
@@ -153,19 +159,45 @@ export const DirectionsAutocomplete = ({ label, value, setValue }: Props) => {
153159 const { userSettings } = useUserSettingsContext ( ) ;
154160 const { isImperial } = userSettings ;
155161
162+ const ALPHABETICAL_MARKER = useMemo ( ( ) => {
163+ let svgElement ;
164+ if ( typeof window !== 'undefined' && typeof document !== 'undefined' ) {
165+ svgElement = document . createElement ( 'div' ) ;
166+ svgElement . innerHTML = ReactDOMServer . renderToStaticMarkup (
167+ < AlphabeticalMarker index = { pointIndex } hasShadow width = { 27 } /> ,
168+ ) ;
169+ } else svgElement = undefined ;
170+
171+ return {
172+ color : 'salmon' ,
173+ draggable : true ,
174+ element : svgElement ,
175+ offset : [ 0 , - 10 ] as PointLike ,
176+ } ;
177+ } , [ pointIndex ] ) ;
178+
156179 const markerRef = useRef < maplibregl . Marker > ( ) ;
180+
157181 useEffect ( ( ) => {
158- console . log ( '___' , value ) ;
159182 const map = getGlobalMap ( ) ;
160183 if ( value ?. type === 'coords' ) {
161- markerRef . current = new maplibregl . Marker ( PREVIEW_MARKER )
184+ markerRef . current = new maplibregl . Marker ( ALPHABETICAL_MARKER )
162185 . setLngLat ( value . coords . center as LngLatLike )
163186 . addTo ( map ) ;
164187 }
165188 return ( ) => {
166189 markerRef . current ?. remove ( ) ;
167190 } ;
168- } , [ value ] ) ;
191+ } , [ ALPHABETICAL_MARKER , value ] ) ;
192+
193+ const onDragEnd = ( ) => {
194+ const lngLat = markerRef . current ?. getLngLat ( ) ;
195+ if ( lngLat ) {
196+ setValue ( getCoordsOption ( [ lngLat . lng , lngLat . lat ] ) ) ;
197+ }
198+ } ;
199+
200+ markerRef . current ?. on ( 'dragend' , onDragEnd ) ;
169201
170202 const options = useOptions ( inputValue ) ;
171203
@@ -224,6 +256,7 @@ export const DirectionsAutocomplete = ({ label, value, setValue }: Props) => {
224256 label = { label }
225257 onFocus = { onInputFocus }
226258 onBlur = { handleBlur }
259+ pointIndex = { pointIndex }
227260 />
228261 ) }
229262 renderOption = { renderOptionFactory (
0 commit comments