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.tsx 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import * as React from 'react'
  2. import { useTLContext } from './useTLContext'
  3. export function useHandleEvents(id: string) {
  4. const { inputs, callbacks } = useTLContext()
  5. const onPointerDown = React.useCallback(
  6. (e: React.PointerEvent) => {
  7. if (e.button !== 0) return
  8. if (!inputs.pointerIsValid(e)) return
  9. e.stopPropagation()
  10. e.currentTarget?.setPointerCapture(e.pointerId)
  11. const info = inputs.pointerDown(e, id)
  12. callbacks.onPointHandle?.(info, e)
  13. callbacks.onPointerDown?.(info, e)
  14. },
  15. [inputs, callbacks, id]
  16. )
  17. const onPointerUp = React.useCallback(
  18. (e: React.PointerEvent) => {
  19. if (e.button !== 0) return
  20. if (!inputs.pointerIsValid(e)) return
  21. e.stopPropagation()
  22. const isDoubleClick = inputs.isDoubleClick()
  23. const info = inputs.pointerUp(e, id)
  24. if (e.currentTarget.hasPointerCapture(e.pointerId)) {
  25. e.currentTarget?.releasePointerCapture(e.pointerId)
  26. if (isDoubleClick && !(info.altKey || info.metaKey)) {
  27. callbacks.onDoubleClickHandle?.(info, e)
  28. }
  29. callbacks.onReleaseHandle?.(info, e)
  30. }
  31. callbacks.onPointerUp?.(info, e)
  32. },
  33. [inputs, callbacks]
  34. )
  35. const onPointerMove = React.useCallback(
  36. (e: React.PointerEvent) => {
  37. if (!inputs.pointerIsValid(e)) return
  38. if (e.currentTarget.hasPointerCapture(e.pointerId)) {
  39. const info = inputs.pointerMove(e, id)
  40. callbacks.onDragHandle?.(info, e)
  41. }
  42. const info = inputs.pointerMove(e, id)
  43. callbacks.onPointerMove?.(info, e)
  44. },
  45. [inputs, callbacks, id]
  46. )
  47. const onPointerEnter = React.useCallback(
  48. (e: React.PointerEvent) => {
  49. if (!inputs.pointerIsValid(e)) return
  50. const info = inputs.pointerEnter(e, id)
  51. callbacks.onHoverHandle?.(info, e)
  52. },
  53. [inputs, callbacks, id]
  54. )
  55. const onPointerLeave = React.useCallback(
  56. (e: React.PointerEvent) => {
  57. if (!inputs.pointerIsValid(e)) return
  58. const info = inputs.pointerEnter(e, id)
  59. callbacks.onUnhoverHandle?.(info, e)
  60. },
  61. [inputs, callbacks, id]
  62. )
  63. return {
  64. onPointerDown,
  65. onPointerUp,
  66. onPointerEnter,
  67. onPointerMove,
  68. onPointerLeave,
  69. }
  70. }