Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

useShapeEvents.ts 2.4KB

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