2020-11-18 22:41:59 +08:00
|
|
|
/**
|
|
|
|
|
* Prevent repeated clicks
|
|
|
|
|
* @Example v-repeat-click="()=>{}"
|
|
|
|
|
*/
|
2023-11-24 10:32:04 +08:00
|
|
|
import { on, once } from '@/utils/domUtils';
|
2020-11-18 22:41:59 +08:00
|
|
|
import type { Directive, DirectiveBinding } from 'vue';
|
2020-10-30 00:56:11 +08:00
|
|
|
|
2020-11-18 22:41:59 +08:00
|
|
|
const repeatDirective: Directive = {
|
|
|
|
|
beforeMount(el: Element, binding: DirectiveBinding<any>) {
|
|
|
|
|
let interval: Nullable<IntervalHandle> = null;
|
2020-10-30 00:56:11 +08:00
|
|
|
let startTime = 0;
|
2020-11-23 00:35:15 +08:00
|
|
|
const handler = (): void => binding?.value();
|
2020-11-18 22:41:59 +08:00
|
|
|
const clear = (): void => {
|
2020-10-30 00:56:11 +08:00
|
|
|
if (Date.now() - startTime < 100) {
|
|
|
|
|
handler();
|
|
|
|
|
}
|
|
|
|
|
interval && clearInterval(interval);
|
|
|
|
|
interval = null;
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-08 17:30:19 +08:00
|
|
|
on(el, 'mousedown', (e: Event): void => {
|
|
|
|
|
if ((e as MouseEvent).button !== 0) return;
|
2020-10-30 00:56:11 +08:00
|
|
|
startTime = Date.now();
|
|
|
|
|
once(document as any, 'mouseup', clear);
|
|
|
|
|
interval && clearInterval(interval);
|
|
|
|
|
interval = setInterval(handler, 100);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|
2020-11-18 22:41:59 +08:00
|
|
|
|
|
|
|
|
export default repeatDirective;
|