start.directive.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import type { DirectiveBinding, ObjectDirective } from 'vue';
  2. export const START_HOLD_EVENT = 'start-hold';
  3. export interface StartDirectiveValue {
  4. threshold: number;
  5. fn?: () => void;
  6. /** 默认派发 `start-hold`;`false` 时不派发 */
  7. emit?: boolean;
  8. }
  9. type StartBinding = DirectiveBinding<StartDirectiveValue>;
  10. type Entry = {
  11. binding: StartBinding;
  12. };
  13. const entries = new WeakMap<HTMLElement, Entry>();
  14. const listenOpts: AddEventListenerOptions = { capture: true };
  15. function resolveMode(modifiers: StartBinding['modifiers']): 'duration' | 'distance' {
  16. if (modifiers.distance && modifiers.duration) {
  17. console.warn('[v-start] Both .duration and .distance are set; using .distance.');
  18. return 'distance';
  19. }
  20. if (modifiers.distance) return 'distance';
  21. return 'duration';
  22. }
  23. function readValue(binding: StartBinding): StartDirectiveValue | null {
  24. const v = binding.value;
  25. if (v == null || typeof v !== 'object') return null;
  26. if (Number.isNaN(v.threshold)) return null;
  27. return v;
  28. }
  29. function mount(el: HTMLElement, binding: StartBinding) {
  30. let down: { timeStamp: number; clientX: number; clientY: number; pointerId: number } | null = null;
  31. const onPointerDown = (e: PointerEvent) => {
  32. down = {
  33. timeStamp: e.timeStamp,
  34. clientX: e.clientX,
  35. clientY: e.clientY,
  36. pointerId: e.pointerId,
  37. };
  38. };
  39. const onPointerUp = (e: PointerEvent) => {
  40. if (!down || e.pointerId !== down.pointerId) return;
  41. const entry = entries.get(el);
  42. if (!entry) return;
  43. const value = readValue(entry.binding);
  44. const mode = resolveMode(entry.binding.modifiers);
  45. const start = down;
  46. down = null;
  47. if (!value) return;
  48. const delta =
  49. mode === 'duration'
  50. ? e.timeStamp - start.timeStamp
  51. : Math.hypot(e.clientX - start.clientX, e.clientY - start.clientY);
  52. if (delta <= value.threshold) return;
  53. value.fn?.();
  54. if (value.emit !== false) {
  55. const detail =
  56. mode === 'duration' ? { durationMs: delta } : { distancePx: delta };
  57. el.dispatchEvent(
  58. new CustomEvent(START_HOLD_EVENT, {
  59. detail,
  60. bubbles: true,
  61. }),
  62. );
  63. }
  64. };
  65. el.addEventListener('pointerdown', onPointerDown, listenOpts);
  66. el.addEventListener('pointerup', onPointerUp, listenOpts);
  67. entries.set(el, { binding });
  68. return () => {
  69. el.removeEventListener('pointerdown', onPointerDown, listenOpts);
  70. el.removeEventListener('pointerup', onPointerUp, listenOpts);
  71. entries.delete(el);
  72. };
  73. }
  74. export const startDirective: ObjectDirective<HTMLElement, StartDirectiveValue> = {
  75. mounted(el, binding) {
  76. (el as HTMLElement & { __startCleanup?: () => void }).__startCleanup = mount(el, binding);
  77. },
  78. updated(el, binding) {
  79. const e = entries.get(el);
  80. if (e) e.binding = binding;
  81. },
  82. unmounted(el) {
  83. (el as HTMLElement & { __startCleanup?: () => void }).__startCleanup?.();
  84. (el as HTMLElement & { __startCleanup?: () => void }).__startCleanup = undefined;
  85. },
  86. };