directix 1.0.0-beta.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js DELETED
@@ -1,1033 +0,0 @@
1
- let _vueVersion = null;
2
- function getVueVersion() {
3
- var _a, _b;
4
- if (_vueVersion !== null) return _vueVersion;
5
- try {
6
- const vue = require("vue");
7
- if ((_a = vue == null ? void 0 : vue.version) == null ? void 0 : _a.startsWith("2")) {
8
- _vueVersion = 2;
9
- } else if ((_b = vue == null ? void 0 : vue.version) == null ? void 0 : _b.startsWith("3")) {
10
- _vueVersion = 3;
11
- }
12
- } catch {
13
- }
14
- if (_vueVersion === null) {
15
- if (typeof window !== "undefined") {
16
- console.warn(
17
- "[Directix] Unable to detect Vue version, defaulting to Vue 3. Please ensure Vue is installed correctly."
18
- );
19
- }
20
- _vueVersion = 3;
21
- }
22
- return _vueVersion;
23
- }
24
- const isVue2 = () => getVueVersion() === 2;
25
- const isVue3 = () => getVueVersion() === 3;
26
- const isBrowser = () => {
27
- return typeof window !== "undefined" && typeof document !== "undefined";
28
- };
29
- const isSSR = () => !isBrowser();
30
- const supportsPassive = () => {
31
- if (!isBrowser()) return false;
32
- let supports = false;
33
- try {
34
- const options = {
35
- get passive() {
36
- supports = true;
37
- return false;
38
- }
39
- };
40
- window.addEventListener("test", null, options);
41
- window.removeEventListener("test", null, options);
42
- } catch {
43
- supports = false;
44
- }
45
- return supports;
46
- };
47
- const supportsIntersectionObserver = () => {
48
- return isBrowser() && "IntersectionObserver" in window;
49
- };
50
- const supportsResizeObserver = () => {
51
- return isBrowser() && "ResizeObserver" in window;
52
- };
53
- const supportsClipboard = () => {
54
- return isBrowser() && "clipboard" in navigator;
55
- };
56
- const supportsMutationObserver = () => {
57
- return isBrowser() && "MutationObserver" in window;
58
- };
59
- function createVue2Directive(hooks) {
60
- const directive = {
61
- bind(el, binding, vnode) {
62
- const state = {
63
- value: binding.value,
64
- vnode,
65
- cleanup: []
66
- };
67
- el.__directix_state__ = state;
68
- if (hooks.mounted) {
69
- hooks.mounted(el, normalizeBinding(binding), vnode);
70
- }
71
- },
72
- inserted(_el, _binding, _vnode) {
73
- },
74
- update(el, binding, vnode, oldVnode) {
75
- const state = el.__directix_state__;
76
- if (hooks.updated) {
77
- hooks.updated(
78
- el,
79
- normalizeBinding(binding),
80
- vnode,
81
- normalizeBinding({ ...binding, value: binding.oldValue }),
82
- oldVnode
83
- );
84
- }
85
- if (state) {
86
- state.value = binding.value;
87
- state.vnode = vnode;
88
- }
89
- },
90
- componentUpdated(_el, _binding, _vnode, _oldVnode) {
91
- },
92
- unbind(el, binding, vnode) {
93
- if (hooks.unmounted) {
94
- hooks.unmounted(el, normalizeBinding(binding), vnode);
95
- }
96
- const state = el.__directix_state__;
97
- if (state == null ? void 0 : state.cleanup) {
98
- state.cleanup.forEach((fn) => fn());
99
- }
100
- delete el.__directix_state__;
101
- }
102
- };
103
- return directive;
104
- }
105
- function normalizeBinding(binding) {
106
- return {
107
- value: binding.value,
108
- oldValue: binding.oldValue ?? null,
109
- arg: binding.arg,
110
- modifiers: binding.modifiers || {},
111
- instance: binding.instance || null
112
- };
113
- }
114
- function addCleanup$1(el, fn) {
115
- const state = el.__directix_state__;
116
- if (state) {
117
- state.cleanup.push(fn);
118
- }
119
- }
120
- function createVue3Directive(hooks) {
121
- const directive = {
122
- created(el, binding, vnode) {
123
- const state = {
124
- value: binding.value,
125
- vnode,
126
- cleanup: []
127
- };
128
- el.__directix_state__ = state;
129
- },
130
- beforeMount(_el, _binding, _vnode) {
131
- },
132
- mounted(el, binding, vnode) {
133
- if (hooks.mounted) {
134
- hooks.mounted(el, normalizeBindingVue3(binding), vnode);
135
- }
136
- },
137
- beforeUpdate(_el, _binding, _vnode, _prevVnode) {
138
- },
139
- updated(el, binding, vnode, prevVnode) {
140
- const state = el.__directix_state__;
141
- if (hooks.updated) {
142
- hooks.updated(
143
- el,
144
- normalizeBindingVue3(binding),
145
- vnode,
146
- normalizeBindingVue3({ ...binding, value: binding.oldValue }),
147
- prevVnode
148
- );
149
- }
150
- if (state) {
151
- state.value = binding.value;
152
- state.vnode = vnode;
153
- }
154
- },
155
- beforeUnmount(_el, _binding, _vnode) {
156
- },
157
- unmounted(el, binding, vnode) {
158
- if (hooks.unmounted) {
159
- hooks.unmounted(el, normalizeBindingVue3(binding), vnode);
160
- }
161
- const state = el.__directix_state__;
162
- if (state == null ? void 0 : state.cleanup) {
163
- state.cleanup.forEach((fn) => fn());
164
- }
165
- delete el.__directix_state__;
166
- }
167
- };
168
- return directive;
169
- }
170
- function normalizeBindingVue3(binding) {
171
- return {
172
- value: binding.value,
173
- oldValue: binding.oldValue ?? null,
174
- arg: binding.arg,
175
- modifiers: binding.modifiers || {},
176
- instance: binding.instance
177
- };
178
- }
179
- function addCleanup(el, fn) {
180
- const state = el.__directix_state__;
181
- if (state) {
182
- state.cleanup.push(fn);
183
- }
184
- }
185
- function defineDirective(definition) {
186
- var _a;
187
- const { name, version, ssr, defaults, ...hooks } = definition;
188
- if (isSSR() && !ssr) {
189
- if (typeof process !== "undefined" && ((_a = process.env) == null ? void 0 : _a.NODE_ENV) !== "test") {
190
- console.warn(
191
- `[Directix] Directive "${name}" is not compatible with SSR. It will be a no-op on the server side.`
192
- );
193
- }
194
- return createNoOpDirective();
195
- }
196
- const wrappedHooks = {
197
- mounted: hooks.mounted ? (el, binding, vnode) => {
198
- const mergedBinding = applyDefaults(binding, defaults);
199
- hooks.mounted(el, mergedBinding, vnode);
200
- } : void 0,
201
- updated: hooks.updated ? (el, binding, vnode, prevBinding, prevVnode) => {
202
- const mergedBinding = applyDefaults(binding, defaults);
203
- hooks.updated(el, mergedBinding, vnode, prevBinding, prevVnode);
204
- } : void 0,
205
- unmounted: hooks.unmounted
206
- };
207
- if (isVue2()) {
208
- return createVue2Directive(wrappedHooks);
209
- }
210
- return createVue3Directive(wrappedHooks);
211
- }
212
- function applyDefaults(binding, defaults) {
213
- if (!defaults) return binding;
214
- const value = typeof binding.value === "object" && binding.value !== null ? { ...defaults, ...binding.value } : binding.value;
215
- return { ...binding, value };
216
- }
217
- function createNoOpDirective() {
218
- return {
219
- mounted: () => {
220
- },
221
- updated: () => {
222
- },
223
- unmounted: () => {
224
- }
225
- };
226
- }
227
- function defineDirectiveGroup(name, directives) {
228
- return {
229
- name,
230
- directives,
231
- install(app, _options) {
232
- Object.entries(directives).forEach(([directiveName, directive]) => {
233
- const fullName = `${name}-${directiveName}`;
234
- app.directive(fullName, directive);
235
- });
236
- }
237
- };
238
- }
239
- function isElement(value) {
240
- return value instanceof Element;
241
- }
242
- function getElement(target) {
243
- if (!target) return null;
244
- if (typeof target === "string") {
245
- if (!isBrowser()) return null;
246
- return document.querySelector(target);
247
- }
248
- return isElement(target) ? target : null;
249
- }
250
- function on(target, event, handler, options = false) {
251
- if (!isBrowser()) return;
252
- const opts = normalizeOptions$5(options);
253
- target.addEventListener(event, handler, opts);
254
- }
255
- function off(target, event, handler, options = false) {
256
- if (!isBrowser()) return;
257
- const opts = normalizeOptions$5(options);
258
- target.removeEventListener(event, handler, opts);
259
- }
260
- function normalizeOptions$5(options) {
261
- if (typeof options === "boolean") {
262
- return options;
263
- }
264
- const { capture = false, passive = false, once = false } = options;
265
- if (supportsPassive()) {
266
- return { capture, passive, once };
267
- }
268
- return capture;
269
- }
270
- function isString(value) {
271
- return typeof value === "string";
272
- }
273
- function isNumber(value) {
274
- return typeof value === "number" && !Number.isNaN(value);
275
- }
276
- function isBoolean(value) {
277
- return typeof value === "boolean";
278
- }
279
- function isFunction(value) {
280
- return typeof value === "function";
281
- }
282
- function isObject(value) {
283
- return typeof value === "object" && value !== null;
284
- }
285
- function isArray(value) {
286
- return Array.isArray(value);
287
- }
288
- function isEmpty(value) {
289
- if (value === null || value === void 0) return true;
290
- if (isString(value) || isArray(value)) return value.length === 0;
291
- if (isObject(value)) return Object.keys(value).length === 0;
292
- return false;
293
- }
294
- function isPromise(value) {
295
- return isObject(value) && isFunction(value.then);
296
- }
297
- function deepClone(obj) {
298
- if (obj === null || typeof obj !== "object") {
299
- return obj;
300
- }
301
- if (Array.isArray(obj)) {
302
- return obj.map((item) => deepClone(item));
303
- }
304
- const cloned = {};
305
- for (const key in obj) {
306
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
307
- cloned[key] = deepClone(obj[key]);
308
- }
309
- }
310
- return cloned;
311
- }
312
- function deepMerge(target, ...sources) {
313
- if (!sources.length) return target;
314
- const source = sources.shift();
315
- if (isObject(target) && isObject(source)) {
316
- for (const key in source) {
317
- if (isObject(source[key])) {
318
- if (!target[key]) {
319
- Object.assign(target, { [key]: {} });
320
- }
321
- deepMerge(
322
- target[key],
323
- source[key]
324
- );
325
- } else {
326
- Object.assign(target, { [key]: source[key] });
327
- }
328
- }
329
- }
330
- return deepMerge(target, ...sources);
331
- }
332
- function get(obj, path, defaultValue) {
333
- const keys = path.split(".");
334
- let result = obj;
335
- for (const key of keys) {
336
- if (result === null || result === void 0) {
337
- return defaultValue;
338
- }
339
- result = result[key];
340
- }
341
- return result === void 0 ? defaultValue : result;
342
- }
343
- function set(obj, path, value) {
344
- const keys = path.split(".");
345
- const lastKey = keys.pop();
346
- let current = obj;
347
- for (const key of keys) {
348
- if (current[key] === void 0) {
349
- current[key] = {};
350
- }
351
- current = current[key];
352
- }
353
- current[lastKey] = value;
354
- }
355
- function debounce(func, wait = 300, options = {}) {
356
- let timerId = null, lastArgs = null, lastThis = null;
357
- const { leading = false, trailing = true } = options;
358
- const invokeFunc = () => {
359
- if (lastArgs) {
360
- func.apply(lastThis, lastArgs);
361
- lastArgs = null;
362
- lastThis = null;
363
- }
364
- };
365
- const debounced = function(...args) {
366
- lastArgs = args;
367
- lastThis = this;
368
- if (timerId) {
369
- clearTimeout(timerId);
370
- }
371
- if (leading && !timerId) {
372
- invokeFunc();
373
- }
374
- timerId = setTimeout(() => {
375
- if (trailing) {
376
- invokeFunc();
377
- }
378
- timerId = null;
379
- }, wait);
380
- };
381
- debounced.cancel = () => {
382
- if (timerId) {
383
- clearTimeout(timerId);
384
- timerId = null;
385
- }
386
- lastArgs = null;
387
- lastThis = null;
388
- };
389
- debounced.flush = () => {
390
- if (timerId) {
391
- clearTimeout(timerId);
392
- invokeFunc();
393
- timerId = null;
394
- }
395
- };
396
- return debounced;
397
- }
398
- function throttle(func, wait = 300, options = {}) {
399
- let timerId = null, lastArgs = null, lastThis = null, lastCallTime = 0;
400
- const { leading = true, trailing = true } = options;
401
- const invokeFunc = () => {
402
- if (lastArgs) {
403
- func.apply(lastThis, lastArgs);
404
- lastArgs = null;
405
- lastThis = null;
406
- }
407
- };
408
- const throttled = function(...args) {
409
- const now = Date.now();
410
- if (!lastCallTime && !leading) {
411
- lastCallTime = now;
412
- }
413
- const remaining = wait - (now - lastCallTime);
414
- lastArgs = args;
415
- lastThis = this;
416
- if (remaining <= 0 || remaining > wait) {
417
- if (timerId) {
418
- clearTimeout(timerId);
419
- timerId = null;
420
- }
421
- lastCallTime = now;
422
- invokeFunc();
423
- } else if (!timerId && trailing) {
424
- timerId = setTimeout(() => {
425
- lastCallTime = leading ? Date.now() : 0;
426
- timerId = null;
427
- invokeFunc();
428
- }, remaining);
429
- }
430
- };
431
- throttled.cancel = () => {
432
- if (timerId) {
433
- clearTimeout(timerId);
434
- timerId = null;
435
- }
436
- lastCallTime = 0;
437
- lastArgs = null;
438
- lastThis = null;
439
- };
440
- return throttled;
441
- }
442
- function parseTime(arg) {
443
- if (!arg) return null;
444
- if (arg.endsWith("ms")) {
445
- return Number.parseInt(arg, 10);
446
- }
447
- if (arg.endsWith("s")) {
448
- return Number.parseFloat(arg) * 1e3;
449
- }
450
- const num = Number.parseInt(arg, 10);
451
- return Number.isNaN(num) ? null : num;
452
- }
453
- function generateId(prefix = "") {
454
- return `${prefix}${Date.now().toString(36)}${Math.random().toString(36).slice(2, 9)}`;
455
- }
456
- const vClickOutside = defineDirective({
457
- name: "click-outside",
458
- ssr: false,
459
- defaults: {
460
- capture: true,
461
- events: ["click"],
462
- disabled: false,
463
- stop: false,
464
- prevent: false
465
- },
466
- mounted(el, binding) {
467
- const options = normalizeOptions$4(binding.value);
468
- if (options.disabled) return;
469
- const state = {
470
- options,
471
- handlers: /* @__PURE__ */ new Map()
472
- };
473
- el.__clickOutside = state;
474
- const createHandler = (_eventType) => {
475
- return (event) => {
476
- if (!isValidClick(el, event, options)) {
477
- return;
478
- }
479
- if (options.stop) {
480
- event.stopPropagation();
481
- }
482
- if (options.prevent) {
483
- event.preventDefault();
484
- }
485
- options.handler(event);
486
- };
487
- };
488
- options.events.forEach((eventType) => {
489
- const handler = createHandler();
490
- state.handlers.set(eventType, handler);
491
- const listenerOptions = {
492
- capture: options.capture,
493
- passive: !options.prevent
494
- };
495
- on(document, eventType, handler, listenerOptions);
496
- });
497
- },
498
- updated(el, binding) {
499
- const state = el.__clickOutside;
500
- if (!state) return;
501
- const oldOptions = state.options;
502
- const newOptions = normalizeOptions$4(binding.value);
503
- if (oldOptions.disabled !== newOptions.disabled) {
504
- if (newOptions.disabled) {
505
- state.handlers.forEach((handler, eventType) => {
506
- off(document, eventType, handler, { capture: oldOptions.capture });
507
- });
508
- state.handlers.clear();
509
- } else {
510
- const createHandler = (_eventType) => {
511
- return (event) => {
512
- if (!isValidClick(el, event, newOptions)) return;
513
- if (newOptions.stop) event.stopPropagation();
514
- if (newOptions.prevent) event.preventDefault();
515
- newOptions.handler(event);
516
- };
517
- };
518
- newOptions.events.forEach((eventType) => {
519
- const handler = createHandler();
520
- state.handlers.set(eventType, handler);
521
- on(document, eventType, handler, {
522
- capture: newOptions.capture,
523
- passive: !newOptions.prevent
524
- });
525
- });
526
- }
527
- }
528
- state.options = newOptions;
529
- },
530
- unmounted(el) {
531
- const state = el.__clickOutside;
532
- if (!state) return;
533
- state.handlers.forEach((handler, eventType) => {
534
- off(document, eventType, handler, { capture: state.options.capture });
535
- });
536
- delete el.__clickOutside;
537
- }
538
- });
539
- function normalizeOptions$4(binding) {
540
- if (typeof binding === "function") {
541
- return {
542
- handler: binding,
543
- capture: true,
544
- events: ["click"],
545
- disabled: false,
546
- stop: false,
547
- prevent: false
548
- };
549
- }
550
- if (!binding) {
551
- throw new Error("[Directix] v-click-outside: handler is required");
552
- }
553
- return {
554
- capture: binding.capture ?? true,
555
- events: binding.events ?? ["click"],
556
- disabled: binding.disabled ?? false,
557
- stop: binding.stop ?? false,
558
- prevent: binding.prevent ?? false,
559
- ...binding
560
- };
561
- }
562
- function isValidClick(el, event, options) {
563
- var _a;
564
- const target = event.target;
565
- if (el.contains(target)) {
566
- return false;
567
- }
568
- if ((_a = options.exclude) == null ? void 0 : _a.length) {
569
- for (const exclude of options.exclude) {
570
- const excludeEl = typeof exclude === "function" ? exclude() : getElement(exclude);
571
- if (excludeEl && (excludeEl === target || excludeEl.contains(target))) {
572
- return false;
573
- }
574
- }
575
- }
576
- return true;
577
- }
578
- async function copyToClipboard(text) {
579
- if (supportsClipboard()) {
580
- try {
581
- await navigator.clipboard.writeText(text);
582
- return true;
583
- } catch {
584
- console.warn("[Directix] Clipboard API failed, falling back to execCommand");
585
- }
586
- }
587
- return copyWithExecCommand(text);
588
- }
589
- function copyWithExecCommand(text) {
590
- const textarea = document.createElement("textarea");
591
- textarea.value = text;
592
- textarea.style.cssText = `
593
- position: fixed;
594
- top: -9999px;
595
- left: -9999px;
596
- opacity: 0;
597
- pointer-events: none;
598
- `;
599
- document.body.appendChild(textarea);
600
- try {
601
- textarea.select();
602
- textarea.setSelectionRange(0, textarea.value.length);
603
- return document.execCommand("copy");
604
- } catch {
605
- return false;
606
- } finally {
607
- document.body.removeChild(textarea);
608
- }
609
- }
610
- const vCopy = defineDirective({
611
- name: "copy",
612
- ssr: false,
613
- mounted(el, binding) {
614
- const options = normalizeOptions$3(binding.value);
615
- if (options.disabled) return;
616
- if (options.title) {
617
- el.setAttribute("title", options.title);
618
- }
619
- const state = {
620
- handler: null,
621
- options
622
- };
623
- state.handler = async () => {
624
- var _a, _b, _c, _d;
625
- const text = state.options.value;
626
- if (!text) {
627
- console.warn("[Directix] v-copy: No text to copy");
628
- return;
629
- }
630
- try {
631
- const success = await copyToClipboard(text);
632
- if (success) {
633
- (_b = (_a = state.options).onSuccess) == null ? void 0 : _b.call(_a, text);
634
- el.dispatchEvent(new CustomEvent("copy:success", { detail: { text } }));
635
- } else {
636
- throw new Error("Copy failed");
637
- }
638
- } catch (err) {
639
- const error = err;
640
- (_d = (_c = state.options).onError) == null ? void 0 : _d.call(_c, error);
641
- el.dispatchEvent(new CustomEvent("copy:error", { detail: { error } }));
642
- }
643
- };
644
- el.addEventListener("click", state.handler);
645
- el.__copy = state;
646
- },
647
- updated(el, binding) {
648
- const state = el.__copy;
649
- if (!state) return;
650
- state.options = normalizeOptions$3(binding.value);
651
- if (state.options.title) {
652
- el.setAttribute("title", state.options.title);
653
- }
654
- },
655
- unmounted(el) {
656
- const state = el.__copy;
657
- if (!state) return;
658
- el.removeEventListener("click", state.handler);
659
- delete el.__copy;
660
- }
661
- });
662
- function normalizeOptions$3(binding) {
663
- if (typeof binding === "string") {
664
- return { value: binding };
665
- }
666
- return binding;
667
- }
668
- const vDebounce = defineDirective({
669
- name: "debounce",
670
- ssr: false,
671
- defaults: {
672
- wait: 300,
673
- leading: false,
674
- trailing: true
675
- },
676
- mounted(el, binding) {
677
- const options = normalizeOptions$2(binding.value, binding);
678
- const eventType = getEventTypeFromModifiers$1(binding.modifiers) || getEventType$1(el);
679
- const debouncedFn = debounce(options.handler, options.wait, {
680
- leading: options.leading,
681
- trailing: options.trailing
682
- });
683
- el.addEventListener(eventType, debouncedFn);
684
- el.__debounce = {
685
- debouncedFn,
686
- eventType,
687
- options
688
- };
689
- },
690
- updated(el, binding) {
691
- const state = el.__debounce;
692
- if (!state) return;
693
- const newOptions = normalizeOptions$2(binding.value, binding);
694
- if (newOptions.wait !== state.options.wait || newOptions.leading !== state.options.leading || newOptions.trailing !== state.options.trailing) {
695
- state.debouncedFn.cancel();
696
- const debouncedFn = debounce(newOptions.handler, newOptions.wait, {
697
- leading: newOptions.leading,
698
- trailing: newOptions.trailing
699
- });
700
- el.removeEventListener(state.eventType, state.debouncedFn);
701
- el.addEventListener(state.eventType, debouncedFn);
702
- el.__debounce = {
703
- debouncedFn,
704
- eventType: state.eventType,
705
- options: newOptions
706
- };
707
- } else if (newOptions.handler !== state.options.handler) {
708
- state.options.handler = newOptions.handler;
709
- }
710
- },
711
- unmounted(el) {
712
- const state = el.__debounce;
713
- if (!state) return;
714
- state.debouncedFn.cancel();
715
- el.removeEventListener(state.eventType, state.debouncedFn);
716
- delete el.__debounce;
717
- }
718
- });
719
- function normalizeOptions$2(binding, directiveBinding) {
720
- const wait = parseTime(directiveBinding.arg) || 300;
721
- if (typeof binding === "function") {
722
- return { handler: binding, wait };
723
- }
724
- return { ...binding, wait: binding.wait || wait };
725
- }
726
- function getEventType$1(el) {
727
- const tagName = el.tagName.toLowerCase();
728
- if (tagName === "input" || tagName === "textarea") {
729
- return "input";
730
- }
731
- return "click";
732
- }
733
- const EVENT_MODIFIERS$1 = [
734
- "click",
735
- "input",
736
- "change",
737
- "submit",
738
- "scroll",
739
- "resize",
740
- "mouseenter",
741
- "mouseleave",
742
- "mousemove",
743
- "mousedown",
744
- "mouseup",
745
- "keydown",
746
- "keyup",
747
- "focus",
748
- "blur",
749
- "touchstart",
750
- "touchmove",
751
- "touchend"
752
- ];
753
- function getEventTypeFromModifiers$1(modifiers) {
754
- for (const modifier of EVENT_MODIFIERS$1) {
755
- if (modifiers[modifier]) {
756
- return modifier;
757
- }
758
- }
759
- return null;
760
- }
761
- const vThrottle = defineDirective({
762
- name: "throttle",
763
- ssr: false,
764
- defaults: {
765
- wait: 300,
766
- leading: true,
767
- trailing: true
768
- },
769
- mounted(el, binding) {
770
- const options = normalizeOptions$1(binding.value, binding);
771
- const eventType = getEventTypeFromModifiers(binding.modifiers) || getEventType(el);
772
- const throttledFn = throttle(options.handler, options.wait, {
773
- leading: options.leading,
774
- trailing: options.trailing
775
- });
776
- el.addEventListener(eventType, throttledFn);
777
- el.__throttle = {
778
- throttledFn,
779
- eventType,
780
- options
781
- };
782
- },
783
- updated(el, binding) {
784
- const state = el.__throttle;
785
- if (!state) return;
786
- const newOptions = normalizeOptions$1(binding.value, binding);
787
- if (newOptions.wait !== state.options.wait || newOptions.leading !== state.options.leading || newOptions.trailing !== state.options.trailing) {
788
- state.throttledFn.cancel();
789
- const throttledFn = throttle(newOptions.handler, newOptions.wait, {
790
- leading: newOptions.leading,
791
- trailing: newOptions.trailing
792
- });
793
- el.removeEventListener(state.eventType, state.throttledFn);
794
- el.addEventListener(state.eventType, throttledFn);
795
- el.__throttle = {
796
- throttledFn,
797
- eventType: state.eventType,
798
- options: newOptions
799
- };
800
- } else if (newOptions.handler !== state.options.handler) {
801
- state.options.handler = newOptions.handler;
802
- }
803
- },
804
- unmounted(el) {
805
- const state = el.__throttle;
806
- if (!state) return;
807
- state.throttledFn.cancel();
808
- el.removeEventListener(state.eventType, state.throttledFn);
809
- delete el.__throttle;
810
- }
811
- });
812
- function normalizeOptions$1(binding, directiveBinding) {
813
- const wait = parseTime(directiveBinding.arg) || 300;
814
- if (typeof binding === "function") {
815
- return { handler: binding, wait };
816
- }
817
- return { ...binding, wait: binding.wait || wait };
818
- }
819
- function getEventType(el) {
820
- const tagName = el.tagName.toLowerCase();
821
- if (tagName === "input" || tagName === "textarea") {
822
- return "input";
823
- }
824
- return "click";
825
- }
826
- const EVENT_MODIFIERS = [
827
- "click",
828
- "input",
829
- "change",
830
- "submit",
831
- "scroll",
832
- "resize",
833
- "mouseenter",
834
- "mouseleave",
835
- "mousemove",
836
- "mousedown",
837
- "mouseup",
838
- "keydown",
839
- "keyup",
840
- "focus",
841
- "blur",
842
- "touchstart",
843
- "touchmove",
844
- "touchend"
845
- ];
846
- function getEventTypeFromModifiers(modifiers) {
847
- for (const modifier of EVENT_MODIFIERS) {
848
- if (modifiers[modifier]) {
849
- return modifier;
850
- }
851
- }
852
- return null;
853
- }
854
- const FOCUSABLE_TAGS = /* @__PURE__ */ new Set(["input", "textarea", "select", "button"]);
855
- let pendingFocusEl = null;
856
- let pendingFocusTimer = null;
857
- const vFocus = defineDirective({
858
- name: "focus",
859
- ssr: false,
860
- defaults: {
861
- focus: true,
862
- refocus: false
863
- },
864
- mounted(el, binding) {
865
- const options = normalizeOptions(binding.value);
866
- if (!options.focus || !isFocusable(el)) {
867
- if (options.focus) {
868
- console.warn("[Directix] v-focus: Element is not focusable");
869
- }
870
- return;
871
- }
872
- const handleFocus = () => {
873
- var _a;
874
- return (_a = options.onFocus) == null ? void 0 : _a.call(options, el);
875
- };
876
- const handleBlur = () => {
877
- var _a;
878
- return (_a = options.onBlur) == null ? void 0 : _a.call(options, el);
879
- };
880
- el.addEventListener("focus", handleFocus);
881
- el.addEventListener("blur", handleBlur);
882
- el.__focus = {
883
- options,
884
- handleFocus,
885
- handleBlur
886
- };
887
- if (pendingFocusTimer) {
888
- clearTimeout(pendingFocusTimer);
889
- pendingFocusTimer = null;
890
- }
891
- pendingFocusEl = el;
892
- el.focus();
893
- },
894
- updated(el, binding) {
895
- const state = el.__focus;
896
- if (!state) return;
897
- const newOptions = normalizeOptions(binding.value);
898
- if (newOptions.onFocus !== state.options.onFocus) {
899
- el.removeEventListener("focus", state.handleFocus);
900
- state.handleFocus = () => {
901
- var _a;
902
- return (_a = newOptions.onFocus) == null ? void 0 : _a.call(newOptions, el);
903
- };
904
- el.addEventListener("focus", state.handleFocus);
905
- }
906
- if (newOptions.onBlur !== state.options.onBlur) {
907
- el.removeEventListener("blur", state.handleBlur);
908
- state.handleBlur = () => {
909
- var _a;
910
- return (_a = newOptions.onBlur) == null ? void 0 : _a.call(newOptions, el);
911
- };
912
- el.addEventListener("blur", state.handleBlur);
913
- }
914
- state.options = newOptions;
915
- if (newOptions.refocus && newOptions.focus) {
916
- if (pendingFocusEl && pendingFocusEl !== el) {
917
- return;
918
- }
919
- pendingFocusEl = null;
920
- if (pendingFocusTimer) {
921
- clearTimeout(pendingFocusTimer);
922
- }
923
- pendingFocusTimer = setTimeout(() => {
924
- pendingFocusTimer = null;
925
- el.focus();
926
- }, 0);
927
- }
928
- },
929
- unmounted(el) {
930
- const state = el.__focus;
931
- if (!state) return;
932
- el.removeEventListener("focus", state.handleFocus);
933
- el.removeEventListener("blur", state.handleBlur);
934
- delete el.__focus;
935
- }
936
- });
937
- function normalizeOptions(binding) {
938
- if (typeof binding === "boolean") {
939
- return { focus: binding, refocus: false };
940
- }
941
- return {
942
- focus: true,
943
- refocus: false,
944
- ...binding
945
- };
946
- }
947
- function isFocusable(el) {
948
- if (!isBrowser()) return false;
949
- const tagName = el.tagName.toLowerCase();
950
- if (FOCUSABLE_TAGS.has(tagName)) {
951
- return !el.disabled;
952
- }
953
- if (el.isContentEditable) return true;
954
- const tabindex = el.getAttribute("tabindex");
955
- if (tabindex != null) return tabindex !== "-1";
956
- if (tagName === "a" || tagName === "area") {
957
- return el.hasAttribute("href");
958
- }
959
- return false;
960
- }
961
- const allDirectives = {
962
- "click-outside": vClickOutside,
963
- copy: vCopy,
964
- debounce: vDebounce,
965
- throttle: vThrottle,
966
- focus: vFocus
967
- };
968
- const install = (app, options = {}) => {
969
- const { directives, all = false } = options;
970
- if (all || !directives) {
971
- Object.entries(allDirectives).forEach(([name, directive]) => {
972
- app.directive(name, directive);
973
- });
974
- } else {
975
- directives.forEach((name) => {
976
- const directive = allDirectives[name];
977
- if (directive) {
978
- app.directive(name, directive);
979
- } else {
980
- console.warn(`[Directix] Unknown directive: ${name}`);
981
- }
982
- });
983
- }
984
- };
985
- const Directix = {
986
- install
987
- };
988
- export {
989
- Directix,
990
- addCleanup$1 as addCleanupVue2,
991
- addCleanup as addCleanupVue3,
992
- vClickOutside as clickOutside,
993
- vCopy as copy,
994
- createVue2Directive,
995
- createVue3Directive,
996
- vDebounce as debounce,
997
- debounce as debounceFn,
998
- deepClone,
999
- deepMerge,
1000
- defineDirective,
1001
- defineDirectiveGroup,
1002
- vFocus as focus,
1003
- generateId,
1004
- get,
1005
- getVueVersion,
1006
- isArray,
1007
- isBoolean,
1008
- isBrowser,
1009
- isEmpty,
1010
- isFunction,
1011
- isNumber,
1012
- isObject,
1013
- isPromise,
1014
- isSSR,
1015
- isString,
1016
- isVue2,
1017
- isVue3,
1018
- parseTime,
1019
- set,
1020
- supportsClipboard,
1021
- supportsIntersectionObserver,
1022
- supportsMutationObserver,
1023
- supportsPassive,
1024
- supportsResizeObserver,
1025
- vThrottle as throttle,
1026
- throttle as throttleFn,
1027
- vClickOutside,
1028
- vCopy,
1029
- vDebounce,
1030
- vFocus,
1031
- vThrottle
1032
- };
1033
- //# sourceMappingURL=index.esm.js.map