logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

gesture_service.js (5909B)


  1. const DIRECTION_LEFT = [-1, 0]
  2. const DIRECTION_RIGHT = [1, 0]
  3. const DIRECTION_UP = [0, -1]
  4. const DIRECTION_DOWN = [0, 1]
  5. const BUTTON_LEFT = 0
  6. const deltaCoord = (oldCoord, newCoord) => [newCoord[0] - oldCoord[0], newCoord[1] - oldCoord[1]]
  7. const touchCoord = touch => [touch.screenX, touch.screenY]
  8. const touchEventCoord = e => touchCoord(e.touches[0])
  9. const pointerEventCoord = e => [e.clientX, e.clientY]
  10. const vectorLength = v => Math.sqrt(v[0] * v[0] + v[1] * v[1])
  11. const perpendicular = v => [v[1], -v[0]]
  12. const dotProduct = (v1, v2) => v1[0] * v2[0] + v1[1] * v2[1]
  13. const project = (v1, v2) => {
  14. const scalar = (dotProduct(v1, v2) / dotProduct(v2, v2))
  15. return [scalar * v2[0], scalar * v2[1]]
  16. }
  17. // direction: either use the constants above or an arbitrary 2d vector.
  18. // threshold: how many Px to move from touch origin before checking if the
  19. // callback should be called.
  20. // divergentTolerance: a scalar for much of divergent direction we tolerate when
  21. // above threshold. for example, with 1.0 we only call the callback if
  22. // divergent component of delta is < 1.0 * direction component of delta.
  23. const swipeGesture = (direction, onSwipe, threshold = 30, perpendicularTolerance = 1.0) => {
  24. return {
  25. direction,
  26. onSwipe,
  27. threshold,
  28. perpendicularTolerance,
  29. _startPos: [0, 0],
  30. _swiping: false
  31. }
  32. }
  33. const beginSwipe = (event, gesture) => {
  34. gesture._startPos = touchEventCoord(event)
  35. gesture._swiping = true
  36. }
  37. const updateSwipe = (event, gesture) => {
  38. if (!gesture._swiping) return
  39. // movement too small
  40. const delta = deltaCoord(gesture._startPos, touchEventCoord(event))
  41. if (vectorLength(delta) < gesture.threshold) return
  42. // movement is opposite from direction
  43. if (dotProduct(delta, gesture.direction) < 0) return
  44. // movement perpendicular to direction is too much
  45. const towardsDir = project(delta, gesture.direction)
  46. const perpendicularDir = perpendicular(gesture.direction)
  47. const towardsPerpendicular = project(delta, perpendicularDir)
  48. if (
  49. vectorLength(towardsDir) * gesture.perpendicularTolerance <
  50. vectorLength(towardsPerpendicular)
  51. ) return
  52. gesture.onSwipe()
  53. gesture._swiping = false
  54. }
  55. class SwipeAndClickGesture {
  56. // swipePreviewCallback(offsets: Array[Number])
  57. // offsets: the offset vector which the underlying component should move, from the starting position
  58. // swipeEndCallback(sign: 0|-1|1)
  59. // sign: if the swipe does not meet the threshold, 0
  60. // if the swipe meets the threshold in the positive direction, 1
  61. // if the swipe meets the threshold in the negative direction, -1
  62. constructor ({
  63. direction,
  64. // swipeStartCallback
  65. swipePreviewCallback,
  66. swipeEndCallback,
  67. swipeCancelCallback,
  68. swipelessClickCallback,
  69. threshold = 30,
  70. perpendicularTolerance = 1.0,
  71. disableClickThreshold = 1
  72. }) {
  73. const nop = () => {}
  74. this.direction = direction
  75. this.swipePreviewCallback = swipePreviewCallback || nop
  76. this.swipeEndCallback = swipeEndCallback || nop
  77. this.swipeCancelCallback = swipeCancelCallback || nop
  78. this.swipelessClickCallback = swipelessClickCallback || nop
  79. this.threshold = typeof threshold === 'function' ? threshold : () => threshold
  80. this.disableClickThreshold = typeof disableClickThreshold === 'function' ? disableClickThreshold : () => disableClickThreshold
  81. this.perpendicularTolerance = perpendicularTolerance
  82. this._reset()
  83. }
  84. _reset () {
  85. this._startPos = [0, 0]
  86. this._pointerId = -1
  87. this._swiping = false
  88. this._swiped = false
  89. this._preventNextClick = false
  90. }
  91. start (event) {
  92. // Only handle left click
  93. if (event.button !== BUTTON_LEFT) {
  94. return
  95. }
  96. this._startPos = pointerEventCoord(event)
  97. this._pointerId = event.pointerId
  98. this._swiping = true
  99. this._swiped = false
  100. }
  101. move (event) {
  102. if (this._swiping && this._pointerId === event.pointerId) {
  103. this._swiped = true
  104. const coord = pointerEventCoord(event)
  105. const delta = deltaCoord(this._startPos, coord)
  106. this.swipePreviewCallback(delta)
  107. }
  108. }
  109. cancel (event) {
  110. if (!this._swiping || this._pointerId !== event.pointerId) {
  111. return
  112. }
  113. this.swipeCancelCallback()
  114. }
  115. end (event) {
  116. if (!this._swiping) {
  117. return
  118. }
  119. if (this._pointerId !== event.pointerId) {
  120. return
  121. }
  122. this._swiping = false
  123. // movement too small
  124. const coord = pointerEventCoord(event)
  125. const delta = deltaCoord(this._startPos, coord)
  126. const sign = (() => {
  127. if (vectorLength(delta) < this.threshold()) {
  128. return 0
  129. }
  130. // movement is opposite from direction
  131. const isPositive = dotProduct(delta, this.direction) > 0
  132. // movement perpendicular to direction is too much
  133. const towardsDir = project(delta, this.direction)
  134. const perpendicularDir = perpendicular(this.direction)
  135. const towardsPerpendicular = project(delta, perpendicularDir)
  136. if (
  137. vectorLength(towardsDir) * this.perpendicularTolerance <
  138. vectorLength(towardsPerpendicular)
  139. ) {
  140. return 0
  141. }
  142. return isPositive ? 1 : -1
  143. })()
  144. if (this._swiped) {
  145. this.swipeEndCallback(sign)
  146. }
  147. this._reset()
  148. // Only a mouse will fire click event when
  149. // the end point is far from the starting point
  150. // so for other kinds of pointers do not check
  151. // whether we have swiped
  152. if (vectorLength(delta) >= this.disableClickThreshold() && event.pointerType === 'mouse') {
  153. this._preventNextClick = true
  154. }
  155. }
  156. click (event) {
  157. if (!this._preventNextClick) {
  158. this.swipelessClickCallback()
  159. }
  160. this._reset()
  161. }
  162. }
  163. const GestureService = {
  164. DIRECTION_LEFT,
  165. DIRECTION_RIGHT,
  166. DIRECTION_UP,
  167. DIRECTION_DOWN,
  168. swipeGesture,
  169. beginSwipe,
  170. updateSwipe,
  171. SwipeAndClickGesture
  172. }
  173. export default GestureService