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

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