1
1
import { MapClickOverride , useMapStateContext } from '../utils/MapStateContext' ;
2
2
import { useStarsContext } from '../utils/StarsContext' ;
3
- import React , { useEffect , useRef , useState } from 'react' ;
3
+ import React , { useEffect , useMemo , useRef , useState } from 'react' ;
4
4
import { abortFetch } from '../../services/fetch' ;
5
5
import {
6
6
fetchGeocoderOptions ,
@@ -13,14 +13,15 @@ import { Autocomplete, InputAdornment, TextField } from '@mui/material';
13
13
import { useMapCenter } from '../SearchBox/utils' ;
14
14
import { useUserThemeContext } from '../../helpers/theme' ;
15
15
import { renderOptionFactory } from '../SearchBox/renderOptionFactory' ;
16
- import PlaceIcon from '@mui/icons-material/Place' ;
17
16
import { Option } from '../SearchBox/types' ;
18
17
import { getOptionLabel } from '../SearchBox/getOptionLabel' ;
19
18
import { useUserSettingsContext } from '../utils/UserSettingsContext' ;
20
19
import { getCoordsOption } from '../SearchBox/options/coords' ;
21
20
import { LonLat } from '../../services/types' ;
22
21
import { 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' ;
24
25
25
26
const StyledTextField = styled ( TextField ) `
26
27
input::placeholder {
@@ -35,6 +36,7 @@ const DirectionsInput = ({
35
36
label,
36
37
onFocus,
37
38
onBlur,
39
+ pointIndex,
38
40
} ) => {
39
41
const { InputLabelProps, InputProps, ...restParams } = params ;
40
42
@@ -59,8 +61,11 @@ const DirectionsInput = ({
59
61
fullWidth
60
62
InputProps = { {
61
63
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 } />
64
69
</ InputAdornment >
65
70
) ,
66
71
} }
@@ -137,14 +142,15 @@ type Props = {
137
142
label : string ;
138
143
value : Option ;
139
144
setValue : ( value : Option ) => void ;
145
+ pointIndex : number ;
140
146
} ;
141
147
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 ) => {
148
154
const autocompleteRef = useRef ( ) ;
149
155
const { inputValue, setInputValue } = useInputValueState ( ) ;
150
156
const selectedOptionInputValue = useRef < string | null > ( null ) ;
@@ -153,19 +159,45 @@ export const DirectionsAutocomplete = ({ label, value, setValue }: Props) => {
153
159
const { userSettings } = useUserSettingsContext ( ) ;
154
160
const { isImperial } = userSettings ;
155
161
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
+
156
179
const markerRef = useRef < maplibregl . Marker > ( ) ;
180
+
157
181
useEffect ( ( ) => {
158
- console . log ( '___' , value ) ;
159
182
const map = getGlobalMap ( ) ;
160
183
if ( value ?. type === 'coords' ) {
161
- markerRef . current = new maplibregl . Marker ( PREVIEW_MARKER )
184
+ markerRef . current = new maplibregl . Marker ( ALPHABETICAL_MARKER )
162
185
. setLngLat ( value . coords . center as LngLatLike )
163
186
. addTo ( map ) ;
164
187
}
165
188
return ( ) => {
166
189
markerRef . current ?. remove ( ) ;
167
190
} ;
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 ) ;
169
201
170
202
const options = useOptions ( inputValue ) ;
171
203
@@ -224,6 +256,7 @@ export const DirectionsAutocomplete = ({ label, value, setValue }: Props) => {
224
256
label = { label }
225
257
onFocus = { onInputFocus }
226
258
onBlur = { handleBlur }
259
+ pointIndex = { pointIndex }
227
260
/>
228
261
) }
229
262
renderOption = { renderOptionFactory (
0 commit comments