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.4KB

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