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.

useHandleEvents.ts 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  2. import { MutableRefObject, useCallback } from 'react'
  3. import state from 'state'
  4. import inputs from 'state/inputs'
  5. export default function useHandleEvents(
  6. id: string,
  7. rGroup: MutableRefObject<SVGElement>
  8. ) {
  9. const handlePointerDown = useCallback(
  10. (e: React.PointerEvent) => {
  11. if (!inputs.canAccept(e.pointerId)) return
  12. e.stopPropagation()
  13. rGroup.current.setPointerCapture(e.pointerId)
  14. const info = inputs.pointerDown(e, id)
  15. state.send('POINTED_HANDLE', info)
  16. },
  17. [id]
  18. )
  19. const handlePointerUp = useCallback(
  20. (e: React.PointerEvent) => {
  21. if (!inputs.canAccept(e.pointerId)) return
  22. e.stopPropagation()
  23. rGroup.current.releasePointerCapture(e.pointerId)
  24. const isDoubleClick = inputs.isDoubleClick()
  25. const info = inputs.pointerUp(e, id)
  26. if (isDoubleClick && !(info.altKey || info.metaKey)) {
  27. state.send('DOUBLE_POINTED_HANDLE', info)
  28. } else {
  29. state.send('STOPPED_POINTING', inputs.pointerUp(e))
  30. }
  31. },
  32. [id]
  33. )
  34. const handlePointerEnter = useCallback(
  35. (e: React.PointerEvent) => {
  36. if (!inputs.canAccept(e.pointerId)) return
  37. state.send('HOVERED_HANDLE', inputs.pointerEnter(e, id))
  38. },
  39. [id]
  40. )
  41. const handlePointerMove = useCallback(
  42. (e: React.PointerEvent) => {
  43. if (!inputs.canAccept(e.pointerId)) return
  44. state.send('MOVED_OVER_HANDLE', inputs.pointerEnter(e, id))
  45. },
  46. [id]
  47. )
  48. const handlePointerLeave = useCallback(
  49. (e: React.PointerEvent) => {
  50. if (!inputs.canAccept(e.pointerId)) return
  51. state.send('UNHOVERED_HANDLE', { target: id })
  52. },
  53. [id]
  54. )
  55. return {
  56. onPointerDown: handlePointerDown,
  57. onPointerUp: handlePointerUp,
  58. onPointerEnter: handlePointerEnter,
  59. onPointerMove: handlePointerMove,
  60. onPointerLeave: handlePointerLeave,
  61. }
  62. }