You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useBoundsEvents.ts 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { useCallback } from 'react'
  2. import inputs from 'state/inputs'
  3. import { Edge, Corner } from 'types'
  4. import state from '../state'
  5. export default function useBoundsEvents(handle: Edge | Corner | 'rotate') {
  6. const onPointerDown = useCallback(
  7. (e) => {
  8. if (!inputs.canAccept(e.pointerId)) return
  9. e.stopPropagation()
  10. e.currentTarget.setPointerCapture(e.pointerId)
  11. if (e.button === 0) {
  12. const info = inputs.pointerDown(e, handle)
  13. if (inputs.isDoubleClick() && !(info.altKey || info.metaKey)) {
  14. state.send('DOUBLE_POINTED_BOUNDS_HANDLE', info)
  15. }
  16. state.send('POINTED_BOUNDS_HANDLE', info)
  17. }
  18. },
  19. [handle]
  20. )
  21. const onPointerMove = useCallback(
  22. (e) => {
  23. if (e.buttons !== 1) return
  24. if (!inputs.canAccept(e.pointerId)) return
  25. e.stopPropagation()
  26. state.send('MOVED_POINTER', inputs.pointerMove(e))
  27. },
  28. [handle]
  29. )
  30. const onPointerUp = useCallback((e) => {
  31. if (e.buttons !== 1) return
  32. if (!inputs.canAccept(e.pointerId)) return
  33. e.stopPropagation()
  34. e.currentTarget.releasePointerCapture(e.pointerId)
  35. e.currentTarget.replaceWith(e.currentTarget)
  36. state.send('STOPPED_POINTING', inputs.pointerUp(e))
  37. }, [])
  38. return { onPointerDown, onPointerMove, onPointerUp }
  39. }