Skip to content
Merged
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
17 changes: 16 additions & 1 deletion gcs/src/components/mapComponents/markerPin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Tooltip } from "@mantine/core"
import { Marker } from "react-map-gl"
import { useDispatch } from "react-redux"
import { coordToInt } from "../../helpers/dataFormatters"
import { getContainerPointFromEvent } from "../../helpers/pointer"
import {
updateContextMenuState,
updateDrawingItem,
Expand All @@ -31,16 +32,30 @@ const MarkerPin = React.memo(

return (
<div
onMouseDown={(e) => {
// Prevent right-click from initiating a drag on the marker
if (e.button === 2) {
e.preventDefault()
e.stopPropagation()
}
}}
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
}}
onContextMenu={(e) => {
e.preventDefault()
e.stopPropagation()
// get map container
const container = e.currentTarget.closest(
".maplibregl-map, .mapboxgl-map",
)
Comment thread
Kwash67 marked this conversation as resolved.
// use helper to get point inside container
const pt = getContainerPointFromEvent(e.nativeEvent, container)
dispatch(
updateContextMenuState({
isOpen: true,
position: { x: e.nativeEvent.layerX, y: e.nativeEvent.layerY },
position: { x: pt.x, y: pt.y },
gpsCoords: { lat: lat, lng: lon },
markerId: id,
}),
Expand Down
7 changes: 6 additions & 1 deletion gcs/src/components/missions/missionsMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
updateContextMenuState,
} from "../../redux/slices/missionSlice"
import ContextMenuSpecificCommandItems from "../mapComponents/contextMenuSpecificCommandItems"
import { getContainerPointFromEvent } from "../../helpers/pointer"

const tailwindColors = resolveConfig(tailwindConfig).theme.colors

Expand Down Expand Up @@ -293,10 +294,14 @@ function MapSectionNonMemo({
onDragStart={onDragstart}
onContextMenu={(e) => {
e.preventDefault()
// get map container
const canvas = e.target.getCanvas()
// use helper to get point inside container
const pt = getContainerPointFromEvent(e.originalEvent ?? e, canvas)
dispatch(
updateContextMenuState({
isOpen: true,
position: e.point,
position: pt,
gpsCoords: e.lngLat,
markerId: null,
}),
Expand Down
22 changes: 22 additions & 0 deletions gcs/src/helpers/pointer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Returns the point (x, y) inside a container for a pointer event.
export function getContainerPointFromEvent(event, container) {
// Validate event and coordinates
const hasClientCoords =
event &&
typeof event.clientX === "number" &&
typeof event.clientY === "number"

if (!hasClientCoords) {
// Fallback safely if event is invalid; caller expects an {x, y} object
return { x: 0, y: 0 }
}

const rect =
container && typeof container.getBoundingClientRect === "function"
? container.getBoundingClientRect()
: { left: 0, top: 0 }

const x = event.clientX - rect.left
const y = event.clientY - rect.top
Comment thread
Kwash67 marked this conversation as resolved.
return { x, y }
}
Comment thread
Kwash67 marked this conversation as resolved.