Skip to content

Commit

Permalink
Merge branch 'master' into fix-web-api
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki authored Dec 13, 2023
2 parents ddbf8ef + 15cb3a2 commit 0c52176
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
40 changes: 25 additions & 15 deletions packages/api-proxy/src/common/js/web.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isBrowser } from './utils'

function webHandleSuccess (result, success, complete) {
typeof success === 'function' && success(result)
typeof complete === 'function' && complete(result)
Expand Down Expand Up @@ -33,22 +35,30 @@ function createDom (tag, attrs = {}, children = []) {
// 在H5中,直接绑定 click 可能出现延时问题,很多点击可以关闭的组件被唤出之后,有概率立马触发点击事件,导致组件被关闭。
// 使用该方法通过 touchstart 和 touchend 模拟 click 事件,解决延时问题。
function bindTap (dom, handler) {
let startTime = 0; let x = 0; let y = 0
const touchStart = (e) => {
startTime = Date.now()
x = e.touches[0].pageX
y = e.touches[0].pageY
}
const touchEnd = (e) => {
if (Date.now() - startTime < 300 && Math.abs(e.changedTouches[0].pageX - x) < 10 && Math.abs(e.changedTouches[0].pageY - y) < 10) {
handler(e)
const isTouchDevice = isBrowser && document && ('ontouchstart' in document.documentElement)
if (isTouchDevice) {
let startTime = 0; let x = 0; let y = 0
const touchStart = (e) => {
startTime = Date.now()
x = e.touches[0].pageX
y = e.touches[0].pageY
}
const touchEnd = (e) => {
if (Date.now() - startTime < 300 && Math.abs(e.changedTouches[0].pageX - x) < 10 && Math.abs(e.changedTouches[0].pageY - y) < 10) {
handler(e)
}
}
dom.addEventListener('touchstart', touchStart)
dom.addEventListener('touchend', touchEnd)
return () => {
dom.removeEventListener('touchstart', touchStart)
dom.removeEventListener('touchend', touchEnd)
}
} else {
dom.addEventListener('click', handler)
return () => {
dom.removeEventListener('click', handler)
}
}
dom.addEventListener('touchstart', touchStart)
dom.addEventListener('touchend', touchEnd)
return () => {
dom.removeEventListener('touchstart', touchStart)
dom.removeEventListener('touchend', touchEnd)
}
}

Expand Down
9 changes: 7 additions & 2 deletions test/utils/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export const createTouchEvent = (type, target, params = {}) => {
}

export const dispatchTap = (el) => {
el.dispatchEvent(createTouchEvent('touchstart', el))
el.dispatchEvent(createTouchEvent('touchend', el))
const isTouchDevice = typeof window !== 'undefined' && document && ('ontouchstart' in document.documentElement)
if (isTouchDevice) {
el.dispatchEvent(createTouchEvent('touchstart', el))
el.dispatchEvent(createTouchEvent('touchend', el))
} else {
el.dispatchEvent(new MouseEvent('click', { bubbles: true }))
}
}

0 comments on commit 0c52176

Please sign in to comment.