mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-15 18:01:24 +08:00
e25df5408f
* fix(directives): [repeat-click] time is too short for single clicks * fix(test-utils): [repeat-click] increase sleep time Updated times according to PR #9466 * fix(test-utils): [repeat-click] increase test time Updated times according to PR #9466 * chore: update * chore: improve test case Co-authored-by: holazz <2418184580@qq.com>
40 lines
980 B
TypeScript
40 lines
980 B
TypeScript
import type { DirectiveBinding, ObjectDirective } from 'vue'
|
|
|
|
export const REPEAT_INTERVAL = 100
|
|
export const REPEAT_DELAY = 600
|
|
|
|
const RepeatClick: ObjectDirective = {
|
|
beforeMount(el: HTMLElement, binding: DirectiveBinding) {
|
|
let interval: ReturnType<typeof setInterval> | null = null
|
|
let delay: ReturnType<typeof setTimeout> | null = null
|
|
|
|
const handler = () => binding.value && binding.value()
|
|
|
|
const clear = () => {
|
|
if (delay) {
|
|
clearTimeout(delay)
|
|
delay = null
|
|
}
|
|
if (interval) {
|
|
clearInterval(interval)
|
|
interval = null
|
|
}
|
|
}
|
|
|
|
el.addEventListener('mousedown', (e: MouseEvent) => {
|
|
if (e.button !== 0) return
|
|
handler()
|
|
|
|
document.addEventListener('mouseup', clear, { once: true })
|
|
clear()
|
|
delay = setTimeout(() => {
|
|
interval = setInterval(() => {
|
|
handler()
|
|
}, REPEAT_INTERVAL)
|
|
}, REPEAT_DELAY)
|
|
})
|
|
},
|
|
}
|
|
|
|
export default RepeatClick
|