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.

useShapeEvents.ts 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { MutableRefObject, useCallback, useRef } from 'react'
  2. import state from 'state'
  3. import inputs from 'state/inputs'
  4. export default function useShapeEvents(
  5. id: string,
  6. isGroup: boolean,
  7. rGroup: MutableRefObject<SVGElement>
  8. ) {
  9. const handlePointerDown = useCallback(
  10. (e: React.PointerEvent) => {
  11. if (isGroup) return
  12. if (!inputs.canAccept(e.pointerId)) return
  13. e.stopPropagation()
  14. rGroup.current.setPointerCapture(e.pointerId)
  15. if (e.button === 0) {
  16. if (inputs.isDoubleClick()) {
  17. state.send('DOUBLE_POINTED_SHAPE', inputs.pointerDown(e, id))
  18. }
  19. state.send('POINTED_SHAPE', inputs.pointerDown(e, id))
  20. } else {
  21. state.send('RIGHT_POINTED', inputs.pointerDown(e, id))
  22. }
  23. },
  24. [id]
  25. )
  26. const handlePointerUp = useCallback(
  27. (e: React.PointerEvent) => {
  28. if (!inputs.canAccept(e.pointerId)) return
  29. e.stopPropagation()
  30. rGroup.current.releasePointerCapture(e.pointerId)
  31. state.send('STOPPED_POINTING', inputs.pointerUp(e))
  32. },
  33. [id]
  34. )
  35. const handlePointerEnter = useCallback(
  36. (e: React.PointerEvent) => {
  37. if (!inputs.canAccept(e.pointerId)) return
  38. if (isGroup) {
  39. state.send('HOVERED_GROUP', inputs.pointerEnter(e, id))
  40. } else {
  41. state.send('HOVERED_SHAPE', inputs.pointerEnter(e, id))
  42. }
  43. },
  44. [id]
  45. )
  46. const handlePointerMove = useCallback(
  47. (e: React.PointerEvent) => {
  48. if (!inputs.canAccept(e.pointerId)) return
  49. if (isGroup) {
  50. state.send('MOVED_OVER_GROUP', inputs.pointerEnter(e, id))
  51. } else {
  52. state.send('MOVED_OVER_SHAPE', inputs.pointerEnter(e, id))
  53. }
  54. },
  55. [id]
  56. )
  57. const handlePointerLeave = useCallback(
  58. (e: React.PointerEvent) => {
  59. if (!inputs.canAccept(e.pointerId)) return
  60. if (isGroup) {
  61. state.send('UNHOVERED_GROUP', { target: id })
  62. } else {
  63. state.send('UNHOVERED_SHAPE', { target: id })
  64. }
  65. },
  66. [id]
  67. )
  68. return {
  69. onPointerDown: handlePointerDown,
  70. onPointerUp: handlePointerUp,
  71. onPointerEnter: handlePointerEnter,
  72. onPointerMove: handlePointerMove,
  73. onPointerLeave: handlePointerLeave,
  74. }
  75. }