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

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