2021-05-28 14:37:23 +00:00
|
|
|
import { MutableRefObject, useCallback } from 'react'
|
|
|
|
import state from 'state'
|
|
|
|
import inputs from 'state/inputs'
|
|
|
|
|
|
|
|
export default function useShapeEvents(
|
|
|
|
id: string,
|
|
|
|
rGroup: MutableRefObject<SVGElement>
|
|
|
|
) {
|
|
|
|
const handlePointerDown = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
// e.stopPropagation()
|
2021-05-28 14:37:23 +00:00
|
|
|
rGroup.current.setPointerCapture(e.pointerId)
|
|
|
|
state.send('POINTED_SHAPE', inputs.pointerDown(e, id))
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerUp = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
// e.stopPropagation()
|
2021-05-28 14:37:23 +00:00
|
|
|
rGroup.current.releasePointerCapture(e.pointerId)
|
|
|
|
state.send('STOPPED_POINTING', inputs.pointerUp(e))
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerEnter = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-28 14:37:23 +00:00
|
|
|
state.send('HOVERED_SHAPE', inputs.pointerEnter(e, id))
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerMove = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-28 14:37:23 +00:00
|
|
|
state.send('MOVED_OVER_SHAPE', inputs.pointerEnter(e, id))
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerLeave = useCallback(
|
2021-05-30 13:14:35 +00:00
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
|
|
|
state.send('UNHOVERED_SHAPE', { target: id })
|
|
|
|
},
|
2021-05-28 14:37:23 +00:00
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
onPointerDown: handlePointerDown,
|
|
|
|
onPointerUp: handlePointerUp,
|
|
|
|
onPointerEnter: handlePointerEnter,
|
|
|
|
onPointerMove: handlePointerMove,
|
|
|
|
onPointerLeave: handlePointerLeave,
|
|
|
|
}
|
|
|
|
}
|