Skip to content

Commit

Permalink
add mouse events for local development
Browse files Browse the repository at this point in the history
  • Loading branch information
brenthagen committed May 16, 2024
1 parent 8880e03 commit ea52345
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions app/src/organisms/WellSelection/SelectionRect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ export function SelectionRect(props: SelectionRectProps): JSX.Element {
}
}

const handleDrag = (e: TouchEvent): void => {
const touch = e.touches[0]
const handleDrag = (e: TouchEvent | MouseEvent): void => {
let xDynamic: number
let yDynamic: number
if (e instanceof TouchEvent) {
const touch = e.touches[0]
xDynamic = touch.clientX
yDynamic = touch.clientY
} else {
xDynamic = e.clientX
yDynamic = e.clientY
}
setPositions(prevPositions => {
if (prevPositions) {
const nextRect = {
...prevPositions,
xDynamic: touch.clientX,
yDynamic: touch.clientY,
xDynamic,
yDynamic,
}
const rect = getRect(nextRect)
onSelectionMove && onSelectionMove(rect)
Expand All @@ -42,8 +51,8 @@ export function SelectionRect(props: SelectionRectProps): JSX.Element {
})
}

const handleTouchEnd = (e: TouchEvent): void => {
if (!(e instanceof TouchEvent)) {
const handleDragEnd = (e: TouchEvent | MouseEvent): void => {
if (!(e instanceof TouchEvent) && !(e instanceof MouseEvent)) {
return
}
const finalRect = positions && getRect(positions)
Expand All @@ -64,18 +73,32 @@ export function SelectionRect(props: SelectionRectProps): JSX.Element {
})
}

const handleMouseDown: React.MouseEventHandler = e => {
setPositions({
xStart: e.clientX,
xDynamic: e.clientX,
yStart: e.clientY,
yDynamic: e.clientY,
})
}

React.useEffect(() => {
document.addEventListener('touchmove', handleDrag)
document.addEventListener('touchend', handleTouchEnd)
document.addEventListener('touchend', handleDragEnd)
document.addEventListener('mousemove', handleDrag)
document.addEventListener('mouseup', handleDragEnd)
return () => {
document.removeEventListener('touchmove', handleDrag)
document.removeEventListener('touchend', handleTouchEnd)
document.removeEventListener('touchend', handleDragEnd)
document.removeEventListener('mousemove', handleDrag)
document.removeEventListener('mouseup', handleDragEnd)
}
}, [handleDrag, handleTouchEnd])
}, [handleDrag, handleDragEnd])

return (
<div
// add mouse event for local development
// mouse events to enable local development
onMouseDown={handleMouseDown}
onTouchStart={handleTouchStart}
ref={ref => {
parentRef.current = ref
Expand Down

0 comments on commit ea52345

Please sign in to comment.