mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-15 01:41:20 +08:00
35 lines
822 B
TypeScript
35 lines
822 B
TypeScript
import type { DirectiveBinding, ObjectDirective } from 'vue'
|
|
|
|
const RepeatClick: ObjectDirective = {
|
|
beforeMount(el: HTMLElement, binding: DirectiveBinding) {
|
|
let interval: ReturnType<typeof setInterval> | null = null
|
|
let isHandlerCalled = false
|
|
|
|
const handler = () => binding.value && binding.value()
|
|
|
|
const clear = () => {
|
|
clearInterval(interval!)
|
|
interval = null
|
|
|
|
if (!isHandlerCalled) {
|
|
handler()
|
|
}
|
|
isHandlerCalled = false
|
|
}
|
|
|
|
el.addEventListener('mousedown', (e: MouseEvent) => {
|
|
if (e.button !== 0) return
|
|
|
|
document.addEventListener('mouseup', clear, { once: true })
|
|
|
|
clearInterval(interval!)
|
|
interval = setInterval(() => {
|
|
isHandlerCalled = true
|
|
handler()
|
|
}, 100)
|
|
})
|
|
},
|
|
}
|
|
|
|
export default RepeatClick
|