Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

inputs.tsx 3.7KB

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