2021-06-21 21:35:28 +00:00
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
2021-06-15 11:58:51 +00:00
|
|
|
import { useCallback } from 'react'
|
2021-06-16 12:09:45 +00:00
|
|
|
import { fastTransform } from 'state/hacks'
|
2021-05-30 13:14:35 +00:00
|
|
|
import inputs from 'state/inputs'
|
|
|
|
import { Edge, Corner } from 'types'
|
2021-05-22 15:45:24 +00:00
|
|
|
|
2021-05-30 13:14:35 +00:00
|
|
|
import state from '../state'
|
2021-05-22 15:45:24 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
export default function useBoundsEvents(handle: Edge | Corner | 'rotate') {
|
2021-05-22 15:45:24 +00:00
|
|
|
const onPointerDown = useCallback(
|
|
|
|
(e) => {
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-22 15:45:24 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
e.currentTarget.setPointerCapture(e.pointerId)
|
2021-06-15 11:58:51 +00:00
|
|
|
|
|
|
|
if (e.button === 0) {
|
|
|
|
const info = inputs.pointerDown(e, handle)
|
|
|
|
|
|
|
|
if (inputs.isDoubleClick() && !(info.altKey || info.metaKey)) {
|
|
|
|
state.send('DOUBLE_POINTED_BOUNDS_HANDLE', info)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.send('POINTED_BOUNDS_HANDLE', info)
|
|
|
|
}
|
2021-05-22 15:45:24 +00:00
|
|
|
},
|
|
|
|
[handle]
|
|
|
|
)
|
|
|
|
|
|
|
|
const onPointerMove = useCallback(
|
|
|
|
(e) => {
|
|
|
|
if (e.buttons !== 1) return
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-22 15:45:24 +00:00
|
|
|
e.stopPropagation()
|
2021-06-16 12:09:45 +00:00
|
|
|
|
|
|
|
const info = inputs.pointerMove(e)
|
|
|
|
|
|
|
|
if (state.isIn('transformingSelection')) {
|
|
|
|
fastTransform(info)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
state.send('MOVED_POINTER', info)
|
2021-05-22 15:45:24 +00:00
|
|
|
},
|
|
|
|
[handle]
|
|
|
|
)
|
|
|
|
|
|
|
|
const onPointerUp = useCallback((e) => {
|
|
|
|
if (e.buttons !== 1) return
|
2021-05-30 13:14:35 +00:00
|
|
|
if (!inputs.canAccept(e.pointerId)) return
|
2021-05-22 15:45:24 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
e.currentTarget.releasePointerCapture(e.pointerId)
|
|
|
|
e.currentTarget.replaceWith(e.currentTarget)
|
2021-06-23 14:39:14 +00:00
|
|
|
state.send('STOPPED_POINTING', inputs.pointerUp(e, 'bounds'))
|
2021-05-22 15:45:24 +00:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
return { onPointerDown, onPointerMove, onPointerUp }
|
|
|
|
}
|