2021-06-21 21:35:28 +00:00
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
|
|
import React, { MutableRefObject, useCallback } from 'react'
|
2021-05-28 14:37:23 +00:00
|
|
|
import state from 'state'
|
|
|
|
import inputs from 'state/inputs'
|
|
|
|
|
|
|
|
export default function useShapeEvents(
|
|
|
|
id: string,
|
2021-06-15 11:58:51 +00:00
|
|
|
isParent: boolean,
|
2021-05-28 14:37:23 +00:00
|
|
|
rGroup: MutableRefObject<SVGElement>
|
|
|
|
) {
|
|
|
|
const handlePointerDown = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
2021-06-15 11:58:51 +00:00
|
|
|
if (isParent) return
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-06-04 16:08:43 +00:00
|
|
|
e.stopPropagation()
|
2021-05-28 14:37:23 +00:00
|
|
|
rGroup.current.setPointerCapture(e.pointerId)
|
2021-06-09 16:10:00 +00:00
|
|
|
|
2021-06-13 14:12:59 +00:00
|
|
|
const info = inputs.pointerDown(e, id)
|
|
|
|
|
2021-06-10 09:49:16 +00:00
|
|
|
if (e.button === 0) {
|
2021-06-13 14:12:59 +00:00
|
|
|
if (inputs.isDoubleClick() && !(info.altKey || info.metaKey)) {
|
|
|
|
state.send('DOUBLE_POINTED_SHAPE', info)
|
2021-06-10 09:49:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 14:12:59 +00:00
|
|
|
state.send('POINTED_SHAPE', info)
|
2021-06-10 09:49:16 +00:00
|
|
|
} else {
|
2021-06-13 14:12:59 +00:00
|
|
|
state.send('RIGHT_POINTED', info)
|
2021-06-04 16:08:43 +00:00
|
|
|
}
|
2021-05-28 14:37:23 +00:00
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerUp = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-06-04 16:08:43 +00:00
|
|
|
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-06-18 15:19:10 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
if (isParent) {
|
2021-06-04 16:08:43 +00:00
|
|
|
state.send('HOVERED_GROUP', inputs.pointerEnter(e, id))
|
|
|
|
} else {
|
|
|
|
state.send('HOVERED_SHAPE', inputs.pointerEnter(e, id))
|
|
|
|
}
|
2021-05-28 14:37:23 +00:00
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerMove = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
if (isParent) {
|
2021-06-04 16:08:43 +00:00
|
|
|
state.send('MOVED_OVER_GROUP', inputs.pointerEnter(e, id))
|
|
|
|
} else {
|
|
|
|
state.send('MOVED_OVER_SHAPE', inputs.pointerEnter(e, id))
|
|
|
|
}
|
2021-05-28 14:37:23 +00:00
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerLeave = useCallback(
|
2021-05-30 13:14:35 +00:00
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-06-18 15:19:10 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
if (isParent) {
|
2021-06-04 16:08:43 +00:00
|
|
|
state.send('UNHOVERED_GROUP', { target: id })
|
|
|
|
} else {
|
|
|
|
state.send('UNHOVERED_SHAPE', { target: id })
|
|
|
|
}
|
2021-05-30 13:14:35 +00:00
|
|
|
},
|
2021-05-28 14:37:23 +00:00
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
2021-06-18 13:55:36 +00:00
|
|
|
const handleTouchStart = useCallback((e: React.TouchEvent) => {
|
|
|
|
e.preventDefault()
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleTouchEnd = useCallback((e: React.TouchEvent) => {
|
|
|
|
e.preventDefault()
|
|
|
|
}, [])
|
|
|
|
|
2021-05-28 14:37:23 +00:00
|
|
|
return {
|
|
|
|
onPointerDown: handlePointerDown,
|
|
|
|
onPointerUp: handlePointerUp,
|
|
|
|
onPointerEnter: handlePointerEnter,
|
|
|
|
onPointerMove: handlePointerMove,
|
|
|
|
onPointerLeave: handlePointerLeave,
|
2021-06-18 13:55:36 +00:00
|
|
|
onTouchStart: handleTouchStart,
|
|
|
|
onTouchEnd: handleTouchEnd,
|
2021-05-28 14:37:23 +00:00
|
|
|
}
|
|
|
|
}
|