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.

inputs.tsx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import React from 'react'
  2. import { PointerInfo } from 'types'
  3. import { isDarwin } from 'utils/utils'
  4. const DOUBLE_CLICK_DURATION = 300
  5. class Inputs {
  6. activePointerId?: number
  7. lastPointerDownTime = 0
  8. points: Record<string, PointerInfo> = {}
  9. touchStart(e: TouchEvent | React.TouchEvent, target: string) {
  10. const { shiftKey, ctrlKey, metaKey, altKey } = e
  11. const touch = e.changedTouches[0]
  12. const info = {
  13. target,
  14. pointerId: touch.identifier,
  15. origin: [touch.clientX, touch.clientY],
  16. point: [touch.clientX, touch.clientY],
  17. shiftKey,
  18. ctrlKey,
  19. metaKey: isDarwin() ? metaKey : ctrlKey,
  20. altKey,
  21. }
  22. this.points[touch.identifier] = info
  23. this.activePointerId = touch.identifier
  24. return info
  25. }
  26. touchMove(e: TouchEvent | React.TouchEvent) {
  27. const { shiftKey, ctrlKey, metaKey, altKey } = e
  28. const touch = e.changedTouches[0]
  29. const prev = this.points[touch.identifier]
  30. const info = {
  31. ...prev,
  32. pointerId: touch.identifier,
  33. point: [touch.clientX, touch.clientY],
  34. shiftKey,
  35. ctrlKey,
  36. metaKey: isDarwin() ? metaKey : ctrlKey,
  37. altKey,
  38. }
  39. if (this.points[touch.identifier]) {
  40. this.points[touch.identifier] = info
  41. }
  42. return info
  43. }
  44. pointerDown(e: PointerEvent | React.PointerEvent, target: string) {
  45. const { shiftKey, ctrlKey, metaKey, altKey } = e
  46. const info = {
  47. target,
  48. pointerId: e.pointerId,
  49. origin: [e.clientX, e.clientY],
  50. point: [e.clientX, e.clientY],
  51. shiftKey,
  52. ctrlKey,
  53. metaKey: isDarwin() ? metaKey : ctrlKey,
  54. altKey,
  55. }
  56. this.points[e.pointerId] = info
  57. this.activePointerId = e.pointerId
  58. return info
  59. }
  60. pointerEnter(e: PointerEvent | React.PointerEvent, target: string) {
  61. const { shiftKey, ctrlKey, metaKey, altKey } = e
  62. const info = {
  63. target,
  64. pointerId: e.pointerId,
  65. origin: [e.clientX, e.clientY],
  66. point: [e.clientX, e.clientY],
  67. shiftKey,
  68. ctrlKey,
  69. metaKey: isDarwin() ? metaKey : ctrlKey,
  70. altKey,
  71. }
  72. return info
  73. }
  74. pointerMove(e: PointerEvent | React.PointerEvent) {
  75. const { shiftKey, ctrlKey, metaKey, altKey } = e
  76. const prev = this.points[e.pointerId]
  77. const info = {
  78. ...prev,
  79. pointerId: e.pointerId,
  80. point: [e.clientX, e.clientY],
  81. shiftKey,
  82. ctrlKey,
  83. metaKey: isDarwin() ? metaKey : ctrlKey,
  84. altKey,
  85. }
  86. if (this.points[e.pointerId]) {
  87. this.points[e.pointerId] = info
  88. }
  89. return info
  90. }
  91. pointerUp(e: PointerEvent | React.PointerEvent) {
  92. const { shiftKey, ctrlKey, metaKey, altKey } = e
  93. const prev = this.points[e.pointerId]
  94. const info = {
  95. ...prev,
  96. origin: prev?.origin || [e.clientX, e.clientY],
  97. point: [e.clientX, e.clientY],
  98. shiftKey,
  99. ctrlKey,
  100. metaKey: isDarwin() ? metaKey : ctrlKey,
  101. altKey,
  102. }
  103. delete this.points[e.pointerId]
  104. delete this.activePointerId
  105. this.lastPointerDownTime = Date.now()
  106. return info
  107. }
  108. wheel(e: WheelEvent) {
  109. const { shiftKey, ctrlKey, metaKey, altKey } = e
  110. return { point: [e.clientX, e.clientY], shiftKey, ctrlKey, metaKey, altKey }
  111. }
  112. canAccept(pointerId: PointerEvent['pointerId']) {
  113. return (
  114. this.activePointerId === undefined || this.activePointerId === pointerId
  115. )
  116. }
  117. isDoubleClick() {
  118. return Date.now() - this.lastPointerDownTime < DOUBLE_CLICK_DURATION
  119. }
  120. get pointer() {
  121. return this.points[Object.keys(this.points)[0]]
  122. }
  123. }
  124. export default new Inputs()