index.mjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { isFunction } from "../../utils/types.mjs";
  2. //#region ../../packages/directives/repeat-click/index.ts
  3. const REPEAT_INTERVAL = 100;
  4. const REPEAT_DELAY = 600;
  5. const SCOPE = "_RepeatClick";
  6. const vRepeatClick = {
  7. beforeMount(el, binding) {
  8. const value = binding.value;
  9. const { interval = REPEAT_INTERVAL, delay = REPEAT_DELAY } = isFunction(value) ? {} : value;
  10. let intervalId;
  11. let delayId;
  12. const handler = () => isFunction(value) ? value() : value.handler();
  13. const clear = () => {
  14. if (delayId) {
  15. clearTimeout(delayId);
  16. delayId = void 0;
  17. }
  18. if (intervalId) {
  19. clearInterval(intervalId);
  20. intervalId = void 0;
  21. }
  22. };
  23. const start = (evt) => {
  24. if (evt.button !== 0) return;
  25. clear();
  26. handler();
  27. document.addEventListener("mouseup", clear, { once: true });
  28. delayId = setTimeout(() => {
  29. intervalId = setInterval(() => {
  30. handler();
  31. }, interval);
  32. }, delay);
  33. };
  34. el[SCOPE] = {
  35. start,
  36. clear
  37. };
  38. el.addEventListener("mousedown", start);
  39. },
  40. unmounted(el) {
  41. if (!el[SCOPE]) return;
  42. const { start, clear } = el[SCOPE];
  43. if (start) el.removeEventListener("mousedown", start);
  44. if (clear) {
  45. clear();
  46. document.removeEventListener("mouseup", clear);
  47. }
  48. el[SCOPE] = null;
  49. }
  50. };
  51. //#endregion
  52. export { REPEAT_DELAY, REPEAT_INTERVAL, vRepeatClick };
  53. //# sourceMappingURL=index.mjs.map