All files behavior.ts

11.01% Statements 13/118
100% Branches 0/0
0% Functions 0/6
11.01% Lines 13/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1191x 1x 1x                                                                         1x 1x                                                           1x 1x 1x 1x 1x 1x 1x 1x                                                                                  
import { isMobile } from './is'
 
export function dbClick(element: HTMLElement, fn: (e?: MouseEvent) => void, needPreventDefault = true) {
  let timeout: ReturnType<typeof setTimeout> | null
  const controller = new AbortController()
  const signal = controller.signal
  const removeListener = () => {
    controller.abort()
  }
  if (!isMobile() && 'ondblclick' in window) {
    element.addEventListener(
      'dblclick',
      (e) => {
        needPreventDefault && e.preventDefault()
        fn(e)
      },
      { signal }
    )
  } else {
    element.addEventListener(
      'click',
      (e) => {
        needPreventDefault && e.preventDefault()
        if (timeout) {
          fn(e)
          timeout && clearTimeout(timeout)
          timeout = null
        } else {
          timeout = setTimeout(() => {
            timeout && clearTimeout(timeout)
            timeout = null
          }, 200)
        }
      },
      { signal }
    )
  }
  return { removeListener }
}
 
export function longPress(element: HTMLElement, fn: (e?: MouseEvent) => void, needPreventDefault = true) {
  let timeout: ReturnType<typeof setTimeout> | null
  const controller = new AbortController()
  const signal = controller.signal
  const removeListener = () => {
    controller.abort()
  }
  element.addEventListener(
    'mouseup',
    (e) => {
      needPreventDefault && e.preventDefault()
      timeout && clearTimeout(timeout)
      timeout = null
    },
    { signal }
  )
  element.addEventListener(
    'mousedown',
    (e) => {
      needPreventDefault && e.preventDefault()
      timeout = setTimeout(() => {
        timeout && clearTimeout(timeout)
        timeout = null
        fn(e)
      }, 200)
    },
    { signal }
  )
  return { removeListener }
}
 
export interface MoveDirectionH5Fn {
  toTop?: (e?: TouchEvent) => void
  toRight?: (e?: TouchEvent) => void
  toBottom?: (e?: TouchEvent) => void
  toLeft?: (e?: TouchEvent) => void
}
export function moveDirectionH5(element: HTMLElement, moveDirectionH5Fn: MoveDirectionH5Fn) {
  const { toTop, toRight, toBottom, toLeft } = moveDirectionH5Fn
  const controller = new AbortController()
  const signal = controller.signal
  const removeListener = () => {
    controller.abort()
  }
  let startX: number, startY: number, moveEndX: number, moveEndY: number, X: number, Y: number
  element.addEventListener(
    'touchstart',
    (e) => {
      e.preventDefault()
      startX = e.targetTouches[0].pageX
      startY = e.targetTouches[0].pageY
    },
    { signal }
  )
  element.addEventListener(
    'touchmove',
    (e) => {
      e.preventDefault()
      moveEndX = e.targetTouches[0].pageX
      moveEndY = e.targetTouches[0].pageY
      X = moveEndX - startX
      Y = moveEndY - startY
      if (Math.abs(X) > Math.abs(Y) && X > 0) {
        toRight && toRight(e)
      } else if (Math.abs(X) > Math.abs(Y) && X < 0) {
        toLeft && toLeft(e)
      } else if (Math.abs(Y) > Math.abs(X) && Y > 0) {
        toBottom && toBottom(e)
      } else if (Math.abs(Y) > Math.abs(X) && Y < 0) {
        toTop && toTop(e)
      } else {
        console.log('Failed to detect the sliding behavior in what direction.')
      }
    },
    { signal }
  )
  return { removeListener }
}