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

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