-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(visual): add, change and remove waypoints
- Loading branch information
Showing
11 changed files
with
349 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,27 @@ | ||
import { LatLngTuple } from 'leaflet' | ||
|
||
type PoiType = | ||
| 'GENERIC' | ||
| 'SUMMIT' | ||
| 'VALLEY' | ||
| 'WATER' | ||
| 'FOOD' | ||
| 'DANGER' | ||
| 'LEFT' | ||
| 'RIGHT' | ||
| 'STRAIGHT' | ||
| 'FIRST_AID' | ||
| 'FOURTH_CATEGORY' | ||
| 'THIRD_CATEGORY' | ||
| 'SECOND_CATEGORY' | ||
| 'FIRST_CATEGORY' | ||
| 'HORS_CATEGORY' | ||
| 'RESIDENCE' | ||
| 'SPRINT' | ||
|
||
export type WayPoint = { | ||
id: string | ||
position: LatLngTuple | ||
name: string | ||
type: PoiType | ||
} |
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
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,109 @@ | ||
import React, { FocusEvent, useRef, useState } from 'react' | ||
import L, { LatLngTuple } from 'leaflet' | ||
import { Marker, Popup } from 'react-leaflet' | ||
import { FaTrashCan } from 'react-icons/fa6' | ||
import { WayPoint } from '../../@types/gps.ts' | ||
|
||
type DraggableMarkerProps = { | ||
position: LatLngTuple | ||
waypoint: WayPoint | ||
markerPositions: WayPoint[] | ||
setMarkerPositions: (waypoints: WayPoint[]) => void | ||
} | ||
|
||
const DraggableMarker: React.FC<DraggableMarkerProps> = ({ | ||
position, | ||
waypoint, | ||
markerPositions, | ||
setMarkerPositions, | ||
}) => { | ||
const [isEditing, setIsEditing] = useState(false) | ||
const myInputRef = useRef<HTMLDivElement>(null) | ||
|
||
const changePosition = (event: L.DragEndEvent) => { | ||
const newPosition = event.target.getLatLng() | ||
setMarkerPositions( | ||
markerPositions.map((wp) => | ||
wp.id === waypoint.id | ||
? { | ||
...wp, | ||
position: [newPosition.lat, newPosition.lng], | ||
} | ||
: wp | ||
) | ||
) | ||
} | ||
|
||
const changeName = (event: FocusEvent) => { | ||
const newContent = event.target.textContent | ||
setIsEditing(false) | ||
setMarkerPositions( | ||
markerPositions.map((wp) => | ||
wp.id === waypoint.id | ||
? { | ||
...wp, | ||
name: newContent ?? '', | ||
} | ||
: wp | ||
) | ||
) | ||
} | ||
|
||
const removeWaypoint = (id: string) => () => { | ||
setMarkerPositions(markerPositions.filter((wp) => wp.id !== id)) | ||
} | ||
|
||
const setFocus = () => { | ||
myInputRef.current?.focus() | ||
} | ||
|
||
const removeFocus = () => { | ||
myInputRef.current?.blur() | ||
setIsEditing(false) | ||
} | ||
|
||
return ( | ||
<Marker | ||
position={position} | ||
draggable | ||
eventHandlers={{ dragend: changePosition }} | ||
key={waypoint.id} | ||
> | ||
<Popup> | ||
<div className='flex flex-col'> | ||
<div | ||
ref={myInputRef} | ||
className='text-lg' | ||
contentEditable | ||
onBlur={changeName} | ||
onFocus={() => setIsEditing(true)} | ||
onChange={() => setIsEditing(false)} | ||
dangerouslySetInnerHTML={{ __html: waypoint.name }} | ||
/> | ||
|
||
<div className='flex justify-between'> | ||
<div> | ||
{isEditing ? ( | ||
<div className='underline' onClick={removeFocus}> | ||
Save | ||
</div> | ||
) : ( | ||
<div className='underline' onClick={setFocus}> | ||
Edit | ||
</div> | ||
)} | ||
</div> | ||
<div> | ||
<FaTrashCan | ||
className='ml-1 relative self-end' | ||
onClick={removeWaypoint(waypoint.id)} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</Popup> | ||
</Marker> | ||
) | ||
} | ||
|
||
export default DraggableMarker |
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,24 @@ | ||
import React, { RefObject } from 'react' | ||
import { Polyline as LeafletPolyline } from 'leaflet' | ||
import { useMap } from 'react-leaflet' | ||
import { MdCenterFocusStrong } from 'react-icons/md' | ||
|
||
type FitBoundsButtonProps = { | ||
polylineRef: RefObject<LeafletPolyline> | ||
} | ||
|
||
const FitBoundsButton: React.FC<FitBoundsButtonProps> = ({ polylineRef }) => { | ||
const map = useMap() | ||
|
||
const handleFitBounds = () => { | ||
map.fitBounds(polylineRef.current!.getBounds()) | ||
} | ||
|
||
return ( | ||
<div id='controlButton' onClick={handleFitBounds}> | ||
<MdCenterFocusStrong className='text-5xl text-black' /> | ||
</div> | ||
) | ||
} | ||
|
||
export default FitBoundsButton |
Oops, something went wrong.