2021-06-21 21:35:28 +00:00
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
2021-05-31 19:13:43 +00:00
|
|
|
import { MutableRefObject, useCallback } from 'react'
|
|
|
|
import state from 'state'
|
|
|
|
import inputs from 'state/inputs'
|
|
|
|
|
|
|
|
export default function useHandleEvents(
|
|
|
|
id: string,
|
|
|
|
rGroup: MutableRefObject<SVGElement>
|
|
|
|
) {
|
|
|
|
const handlePointerDown = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
e.stopPropagation()
|
|
|
|
rGroup.current.setPointerCapture(e.pointerId)
|
2021-06-22 21:06:51 +00:00
|
|
|
const info = inputs.pointerDown(e, id)
|
|
|
|
|
|
|
|
state.send('POINTED_HANDLE', info)
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerUp = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
e.stopPropagation()
|
|
|
|
rGroup.current.releasePointerCapture(e.pointerId)
|
2021-06-22 21:06:51 +00:00
|
|
|
const isDoubleClick = inputs.isDoubleClick()
|
|
|
|
const info = inputs.pointerUp(e, id)
|
|
|
|
|
|
|
|
if (isDoubleClick && !(info.altKey || info.metaKey)) {
|
|
|
|
state.send('DOUBLE_POINTED_HANDLE', info)
|
|
|
|
} else {
|
2021-06-23 14:39:14 +00:00
|
|
|
state.send('STOPPED_POINTING', inputs.pointerUp(e, id))
|
2021-06-22 21:06:51 +00:00
|
|
|
}
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerEnter = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
state.send('HOVERED_HANDLE', inputs.pointerEnter(e, id))
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerMove = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
state.send('MOVED_OVER_HANDLE', inputs.pointerEnter(e, id))
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerLeave = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
state.send('UNHOVERED_HANDLE', { target: id })
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
onPointerDown: handlePointerDown,
|
|
|
|
onPointerUp: handlePointerUp,
|
|
|
|
onPointerEnter: handlePointerEnter,
|
|
|
|
onPointerMove: handlePointerMove,
|
|
|
|
onPointerLeave: handlePointerLeave,
|
|
|
|
}
|
|
|
|
}
|