tiny-essentials 1.17.0 → 1.18.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/v1/TinyBasicsEs.js +336 -45
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyClipboard.js +459 -0
- package/dist/v1/TinyClipboard.min.js +1 -0
- package/dist/v1/TinyDragger.js +336 -45
- package/dist/v1/TinyDragger.min.js +1 -1
- package/dist/v1/TinyEssentials.js +1218 -45
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyHtml.js +336 -45
- package/dist/v1/TinyHtml.min.js +1 -1
- package/dist/v1/TinySmartScroller.js +336 -45
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyTextRangeEditor.js +497 -0
- package/dist/v1/TinyTextRangeEditor.min.js +1 -0
- package/dist/v1/TinyUploadClicker.js +338 -45
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/build/TinyClipboard.cjs +7 -0
- package/dist/v1/build/TinyClipboard.d.mts +3 -0
- package/dist/v1/build/TinyClipboard.mjs +2 -0
- package/dist/v1/build/TinyTextRangeEditor.cjs +7 -0
- package/dist/v1/build/TinyTextRangeEditor.d.mts +3 -0
- package/dist/v1/build/TinyTextRangeEditor.mjs +2 -0
- package/dist/v1/index.cjs +4 -0
- package/dist/v1/index.d.mts +3 -1
- package/dist/v1/index.mjs +3 -1
- package/dist/v1/libs/TinyClipboard.cjs +420 -0
- package/dist/v1/libs/TinyClipboard.d.mts +155 -0
- package/dist/v1/libs/TinyClipboard.mjs +398 -0
- package/dist/v1/libs/TinyHtml.cjs +336 -45
- package/dist/v1/libs/TinyHtml.d.mts +238 -27
- package/dist/v1/libs/TinyHtml.mjs +320 -47
- package/dist/v1/libs/TinyTextRangeEditor.cjs +458 -0
- package/dist/v1/libs/TinyTextRangeEditor.d.mts +200 -0
- package/dist/v1/libs/TinyTextRangeEditor.mjs +424 -0
- package/docs/v1/README.md +2 -0
- package/docs/v1/libs/TinyClipboard.md +213 -0
- package/docs/v1/libs/TinyHtml.md +211 -15
- package/docs/v1/libs/TinyTextRangeEditor.md +208 -0
- package/package.json +1 -1
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
import * as TinyCollision from '../basics/collision.mjs';
|
|
2
2
|
const { areElsColliding, areElsPerfColliding, areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, } = TinyCollision;
|
|
3
|
+
/**
|
|
4
|
+
* Callback invoked on each animation frame with the current scroll position,
|
|
5
|
+
* normalized animation time (`0` to `1`), and a completion flag.
|
|
6
|
+
*
|
|
7
|
+
* @typedef {(progress: { x: number, y: number, isComplete: boolean, time: number }) => void} OnScrollAnimation
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* A list of supported easing function names for smooth animations.
|
|
11
|
+
*
|
|
12
|
+
* @typedef {'linear' | 'easeInQuad' | 'easeOutQuad' | 'easeInOutQuad' | 'easeInCubic' | 'easeOutCubic' | 'easeInOutCubic'} Easings
|
|
13
|
+
*/
|
|
3
14
|
/**
|
|
4
15
|
* Represents a raw Node element or an instance of TinyHtml.
|
|
5
16
|
* This type is used to abstract interactions with both plain elements
|
|
@@ -61,6 +72,19 @@ const { areElsColliding, areElsPerfColliding, areElsCollTop, areElsCollBottom, a
|
|
|
61
72
|
*
|
|
62
73
|
* @typedef {Element|Window|Document} ElementAndWinAndDoc
|
|
63
74
|
*/
|
|
75
|
+
/**
|
|
76
|
+
* Represents a raw DOM element with document or an instance of TinyHtml.
|
|
77
|
+
* This type is used to abstract interactions with both plain elements
|
|
78
|
+
* and wrapped elements via the TinyHtml class.
|
|
79
|
+
*
|
|
80
|
+
* @typedef {ElementWithDoc|TinyHtml} TinyElementWithDoc
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* Represents a value that can be either a DOM Element, or the document object.
|
|
84
|
+
* Useful for functions that operate generically on measurable targets.
|
|
85
|
+
*
|
|
86
|
+
* @typedef {Element|Document} ElementWithDoc
|
|
87
|
+
*/
|
|
64
88
|
/**
|
|
65
89
|
* A parameter type used for filtering or matching elements.
|
|
66
90
|
* It can be:
|
|
@@ -78,11 +102,6 @@ const { areElsColliding, areElsPerfColliding, areElsCollTop, areElsCollBottom, a
|
|
|
78
102
|
*
|
|
79
103
|
* @typedef {Window|Element|Document|Text} ConstructorElValues
|
|
80
104
|
*/
|
|
81
|
-
/**
|
|
82
|
-
* The handler function used in event listeners.
|
|
83
|
-
*
|
|
84
|
-
* @typedef {(e: Event) => any} EventRegistryHandle
|
|
85
|
-
*/
|
|
86
105
|
/**
|
|
87
106
|
* Options passed to `addEventListener` or `removeEventListener`.
|
|
88
107
|
* Can be a boolean or an object of type `AddEventListenerOptions`.
|
|
@@ -93,7 +112,7 @@ const { areElsColliding, areElsPerfColliding, areElsCollTop, areElsCollBottom, a
|
|
|
93
112
|
* Structure describing a registered event callback and its options.
|
|
94
113
|
*
|
|
95
114
|
* @typedef {Object} EventRegistryItem
|
|
96
|
-
* @property {
|
|
115
|
+
* @property {EventListenerOrEventListenerObject|null} handler - The function to be executed when the event is triggered.
|
|
97
116
|
* @property {EventRegistryOptions} [options] - Optional configuration passed to the listener.
|
|
98
117
|
*/
|
|
99
118
|
/**
|
|
@@ -584,9 +603,9 @@ class TinyHtml {
|
|
|
584
603
|
* Ensures the input is returned as an array.
|
|
585
604
|
* Useful to normalize operations across multiple or single element/window/document elements.
|
|
586
605
|
*
|
|
587
|
-
* @param {TinyElementAndWinAndDoc|TinyElementAndWinAndDoc[]} elems - A single element/window element or array of html elements.
|
|
606
|
+
* @param {TinyElementAndWinAndDoc|TinyElementAndWinAndDoc[]} elems - A single element/document/window element or array of html elements.
|
|
588
607
|
* @param {string} where - The method or context name where validation is being called.
|
|
589
|
-
* @returns {ElementAndWindow[]} - Always returns an array of element/window elements.
|
|
608
|
+
* @returns {ElementAndWindow[]} - Always returns an array of element/document/window elements.
|
|
590
609
|
* @readonly
|
|
591
610
|
*/
|
|
592
611
|
static _preElemsAndWinAndDoc(elems, where) {
|
|
@@ -597,9 +616,9 @@ class TinyHtml {
|
|
|
597
616
|
* Ensures the input is returned as an single element/window/document element.
|
|
598
617
|
* Useful to normalize operations across multiple or single element/window/document elements.
|
|
599
618
|
*
|
|
600
|
-
* @param {TinyElementAndWinAndDoc|TinyElementAndWinAndDoc[]} elems - A single element/window element or array of html elements.
|
|
619
|
+
* @param {TinyElementAndWinAndDoc|TinyElementAndWinAndDoc[]} elems - A single element/document/window element or array of html elements.
|
|
601
620
|
* @param {string} where - The method or context name where validation is being called.
|
|
602
|
-
* @returns {ElementAndWindow} - Always returns an single element/window element.
|
|
621
|
+
* @returns {ElementAndWindow} - Always returns an single element/document/window element.
|
|
603
622
|
* @readonly
|
|
604
623
|
*/
|
|
605
624
|
static _preElemAndWinAndDoc(elems, where) {
|
|
@@ -608,6 +627,30 @@ class TinyHtml {
|
|
|
608
627
|
return result.documentElement;
|
|
609
628
|
return result;
|
|
610
629
|
}
|
|
630
|
+
/**
|
|
631
|
+
* Ensures the input is returned as an array.
|
|
632
|
+
* Useful to normalize operations across multiple or single element with document elements.
|
|
633
|
+
*
|
|
634
|
+
* @param {TinyElementWithDoc|TinyElementWithDoc[]} elems - A single element with document element or array of html elements.
|
|
635
|
+
* @param {string} where - The method or context name where validation is being called.
|
|
636
|
+
* @returns {ElementWithDoc[]} - Always returns an array of element with document elements.
|
|
637
|
+
* @readonly
|
|
638
|
+
*/
|
|
639
|
+
static _preElemsWithDoc(elems, where) {
|
|
640
|
+
return TinyHtml._preElemsTemplate(elems, where, [Element, Document], ['Element', 'Document']);
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Ensures the input is returned as an single element with document element.
|
|
644
|
+
* Useful to normalize operations across multiple or single element with document elements.
|
|
645
|
+
*
|
|
646
|
+
* @param {TinyElementWithDoc|TinyElementWithDoc[]} elems - A single element/window element or array of html elements.
|
|
647
|
+
* @param {string} where - The method or context name where validation is being called.
|
|
648
|
+
* @returns {ElementWithDoc} - Always returns an single element/window element.
|
|
649
|
+
* @readonly
|
|
650
|
+
*/
|
|
651
|
+
static _preElemWithDoc(elems, where) {
|
|
652
|
+
return TinyHtml._preElemTemplate(elems, where, [Element, Document], ['Element', 'Document']);
|
|
653
|
+
}
|
|
611
654
|
/**
|
|
612
655
|
* Normalizes and converts one or more DOM elements (or TinyHtml instances)
|
|
613
656
|
* into an array of `TinyHtml` instances.
|
|
@@ -2179,7 +2222,7 @@ class TinyHtml {
|
|
|
2179
2222
|
static setWinScrollTop(value) {
|
|
2180
2223
|
if (typeof value !== 'number')
|
|
2181
2224
|
throw new TypeError('The value must be a number.');
|
|
2182
|
-
|
|
2225
|
+
TinyHtml.setScrollTop(window, value);
|
|
2183
2226
|
}
|
|
2184
2227
|
/**
|
|
2185
2228
|
* Sets the horizontal scroll position of the window.
|
|
@@ -2188,7 +2231,7 @@ class TinyHtml {
|
|
|
2188
2231
|
static setWinScrollLeft(value) {
|
|
2189
2232
|
if (typeof value !== 'number')
|
|
2190
2233
|
throw new TypeError('The value must be a number.');
|
|
2191
|
-
|
|
2234
|
+
TinyHtml.setScrollLeft(window, value);
|
|
2192
2235
|
}
|
|
2193
2236
|
/**
|
|
2194
2237
|
* Gets the vertical scroll position of the window.
|
|
@@ -2464,6 +2507,28 @@ class TinyHtml {
|
|
|
2464
2507
|
return TinyHtml.outerWidth(this, includeMargin);
|
|
2465
2508
|
}
|
|
2466
2509
|
//////////////////////////////////////////////////
|
|
2510
|
+
/**
|
|
2511
|
+
* Applies an animation to one or multiple TinyElement instances.
|
|
2512
|
+
*
|
|
2513
|
+
* @param {TinyElement|TinyElement[]} el - A single TinyElement or an array of TinyElements to animate.
|
|
2514
|
+
* @param {Keyframe[] | PropertyIndexedKeyframes | null} keyframes - The keyframes used to define the animation.
|
|
2515
|
+
* @param {number | KeyframeAnimationOptions} [ops] - Timing or configuration options for the animation.
|
|
2516
|
+
* @returns {TinyElement|TinyElement[]}
|
|
2517
|
+
*/
|
|
2518
|
+
static animate(el, keyframes, ops) {
|
|
2519
|
+
TinyHtml._preElems(el, 'animate').forEach((elem) => elem.animate(keyframes, ops));
|
|
2520
|
+
return el;
|
|
2521
|
+
}
|
|
2522
|
+
/**
|
|
2523
|
+
* Applies an animation to one or multiple TinyElement instances.
|
|
2524
|
+
*
|
|
2525
|
+
* @param {Keyframe[] | PropertyIndexedKeyframes | null} keyframes - The keyframes used to define the animation.
|
|
2526
|
+
* @param {number | KeyframeAnimationOptions} [ops] - Timing or configuration options for the animation.
|
|
2527
|
+
* @returns {TinyElement|TinyElement[]}
|
|
2528
|
+
*/
|
|
2529
|
+
animate(keyframes, ops) {
|
|
2530
|
+
return TinyHtml.animate(this, keyframes, ops);
|
|
2531
|
+
}
|
|
2467
2532
|
/**
|
|
2468
2533
|
* Gets the offset of the element relative to the document.
|
|
2469
2534
|
* @param {TinyElement} el - Target element.
|
|
@@ -2598,28 +2663,142 @@ class TinyHtml {
|
|
|
2598
2663
|
return TinyHtml.scrollLeft(this);
|
|
2599
2664
|
}
|
|
2600
2665
|
/**
|
|
2601
|
-
*
|
|
2602
|
-
*
|
|
2603
|
-
*
|
|
2604
|
-
* @
|
|
2666
|
+
* Collection of easing functions used for scroll and animation calculations.
|
|
2667
|
+
* Each function receives a normalized time value (`t` from 0 to 1) and returns the eased progress.
|
|
2668
|
+
*
|
|
2669
|
+
* @type {Record<string, (t: number) => number>}
|
|
2605
2670
|
*/
|
|
2606
|
-
static
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2671
|
+
static easings = {
|
|
2672
|
+
linear: (t) => t,
|
|
2673
|
+
easeInQuad: (t) => t * t,
|
|
2674
|
+
easeOutQuad: (t) => t * (2 - t),
|
|
2675
|
+
easeInOutQuad: (t) => (t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t),
|
|
2676
|
+
easeInCubic: (t) => t * t * t,
|
|
2677
|
+
easeOutCubic: (t) => --t * t * t + 1,
|
|
2678
|
+
easeInOutCubic: (t) => (t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1),
|
|
2679
|
+
};
|
|
2680
|
+
/**
|
|
2681
|
+
* Smoothly scrolls one or more elements (or the window) to the specified X and Y coordinates
|
|
2682
|
+
* using a custom duration and easing function.
|
|
2683
|
+
*
|
|
2684
|
+
* If `duration` or a valid `easing` is not provided, the scroll will be performed immediately.
|
|
2685
|
+
*
|
|
2686
|
+
* @param {TinyElementAndWindow | TinyElementAndWindow[]} el - A single element, array of elements, or the window to scroll.
|
|
2687
|
+
* @param {Object} [settings={}] - Configuration object for the scroll animation.
|
|
2688
|
+
* @param {number} [settings.targetX] - The horizontal scroll target in pixels.
|
|
2689
|
+
* @param {number} [settings.targetY] - The vertical scroll target in pixels.
|
|
2690
|
+
* @param {number} [settings.duration] - The duration of the animation in milliseconds.
|
|
2691
|
+
* @param {Easings} [settings.easing] - The easing function name to use for the scroll animation.
|
|
2692
|
+
* @param {OnScrollAnimation} [settings.onAnimation] - Optional callback invoked on each animation
|
|
2693
|
+
* frame with the current scroll position, normalized animation time (`0` to `1`), and a completion flag.
|
|
2694
|
+
* @returns {TinyElementAndWindow|TinyElementAndWindow[]}
|
|
2695
|
+
* @throws {TypeError} If `el` is not a valid element, array, or window.
|
|
2696
|
+
* @throws {TypeError} If `targetX` or `targetY` is defined but not a number.
|
|
2697
|
+
* @throws {TypeError} If `duration` is defined but not a number.
|
|
2698
|
+
* @throws {TypeError} If `easing` is defined but not a valid easing function name.
|
|
2699
|
+
* @throws {TypeError} If `onAnimation` is defined but not a function.
|
|
2700
|
+
*/
|
|
2701
|
+
static scrollToXY(el, { targetX, targetY, duration, easing, onAnimation } = {}) {
|
|
2702
|
+
if (targetX !== undefined && typeof targetX !== 'number')
|
|
2703
|
+
throw new TypeError('`targetX` must be a number if provided.');
|
|
2704
|
+
if (targetY !== undefined && typeof targetY !== 'number')
|
|
2705
|
+
throw new TypeError('`targetY` must be a number if provided.');
|
|
2706
|
+
if (duration !== undefined && typeof duration !== 'number')
|
|
2707
|
+
throw new TypeError('`duration` must be a number if provided.');
|
|
2708
|
+
if (easing !== undefined && typeof easing !== 'string')
|
|
2709
|
+
throw new TypeError('`easing` must be a string if provided.');
|
|
2710
|
+
if (easing !== undefined && typeof TinyHtml.easings[easing] !== 'function')
|
|
2711
|
+
throw new TypeError(`Unknown easing function: "${easing}".`);
|
|
2712
|
+
if (onAnimation !== undefined && typeof onAnimation !== 'function')
|
|
2713
|
+
throw new TypeError('`onAnimation` must be a function if provided.');
|
|
2714
|
+
/**
|
|
2715
|
+
* Performs an instant scroll to the given coordinates.
|
|
2716
|
+
*
|
|
2717
|
+
* @param {ElementAndWindow} elem - The element or window to scroll.
|
|
2718
|
+
* @param {number} newX - The final horizontal scroll position.
|
|
2719
|
+
* @param {number} newY - The final vertical scroll position.
|
|
2720
|
+
* @param {number} time - Normalized progress value.
|
|
2721
|
+
*/
|
|
2722
|
+
const executeScroll = (elem, newX, newY, time) => {
|
|
2723
|
+
if (elem instanceof Window) {
|
|
2724
|
+
window.scrollTo(newX, newY);
|
|
2612
2725
|
}
|
|
2613
2726
|
else if (elem.nodeType === 9) {
|
|
2614
2727
|
// @ts-ignore
|
|
2615
|
-
elem.defaultView.scrollTo(
|
|
2728
|
+
elem.defaultView.scrollTo(newX, newY);
|
|
2616
2729
|
}
|
|
2617
2730
|
else {
|
|
2618
|
-
elem.
|
|
2731
|
+
const startX = elem instanceof Window ? window.scrollX : elem.scrollLeft;
|
|
2732
|
+
const startY = elem instanceof Window ? window.scrollY : elem.scrollTop;
|
|
2733
|
+
if (startX !== newX)
|
|
2734
|
+
elem.scrollLeft = newX;
|
|
2735
|
+
if (startY !== newY)
|
|
2736
|
+
elem.scrollTop = newY;
|
|
2737
|
+
}
|
|
2738
|
+
if (typeof onAnimation === 'function')
|
|
2739
|
+
onAnimation({ x: newX, y: newY, isComplete: time >= 1, time });
|
|
2740
|
+
};
|
|
2741
|
+
TinyHtml._preElemsAndWindow(el, 'scrollToXY').forEach((elem) => {
|
|
2742
|
+
const startX = elem instanceof Window ? window.scrollX : elem.scrollLeft;
|
|
2743
|
+
const startY = elem instanceof Window ? window.scrollY : elem.scrollTop;
|
|
2744
|
+
const targX = targetX ?? startX;
|
|
2745
|
+
const targY = targetY ?? startY;
|
|
2746
|
+
const changeX = targX - startX;
|
|
2747
|
+
const changeY = targY - startY;
|
|
2748
|
+
const ease = (typeof easing === 'string' && TinyHtml.easings[easing]) || null;
|
|
2749
|
+
if (typeof duration !== 'number' || typeof ease !== 'function')
|
|
2750
|
+
return executeScroll(elem, targX, targY, 1);
|
|
2751
|
+
const startTime = performance.now();
|
|
2752
|
+
const dur = duration ?? 0;
|
|
2753
|
+
/**
|
|
2754
|
+
* Animates the scroll position based on easing and time.
|
|
2755
|
+
*
|
|
2756
|
+
* @param {number} currentTime - Timestamp provided by requestAnimationFrame.
|
|
2757
|
+
*/
|
|
2758
|
+
function animateScroll(currentTime) {
|
|
2759
|
+
if (typeof ease !== 'function')
|
|
2760
|
+
return;
|
|
2761
|
+
const time = Math.min(1, (currentTime - startTime) / dur);
|
|
2762
|
+
const easedTime = ease(time);
|
|
2763
|
+
const newX = startX + changeX * easedTime;
|
|
2764
|
+
const newY = startY + changeY * easedTime;
|
|
2765
|
+
executeScroll(elem, newX, newY, time);
|
|
2766
|
+
if (time < 1)
|
|
2767
|
+
requestAnimationFrame(animateScroll);
|
|
2619
2768
|
}
|
|
2769
|
+
requestAnimationFrame(animateScroll);
|
|
2620
2770
|
});
|
|
2621
2771
|
return el;
|
|
2622
2772
|
}
|
|
2773
|
+
/**
|
|
2774
|
+
* Smoothly scrolls one or more elements (or the window) to the specified X and Y coordinates
|
|
2775
|
+
* using a custom duration and easing function.
|
|
2776
|
+
*
|
|
2777
|
+
* If `duration` or a valid `easing` is not provided, the scroll will be performed immediately.
|
|
2778
|
+
*
|
|
2779
|
+
* @param {Object} [settings={}] - Configuration object for the scroll animation.
|
|
2780
|
+
* @param {number} [settings.targetX] - The horizontal scroll target in pixels.
|
|
2781
|
+
* @param {number} [settings.targetY] - The vertical scroll target in pixels.
|
|
2782
|
+
* @param {number} [settings.duration] - The duration of the animation in milliseconds.
|
|
2783
|
+
* @param {Easings} [settings.easing] - The easing function name to use for the scroll animation.
|
|
2784
|
+
* @param {OnScrollAnimation} [settings.onAnimation] - Optional callback invoked on each animation
|
|
2785
|
+
* frame with the current scroll position, normalized animation time (`0` to `1`), and a completion flag.
|
|
2786
|
+
* @returns {TinyElementAndWindow|TinyElementAndWindow[]}
|
|
2787
|
+
*/
|
|
2788
|
+
scrollToXY({ targetX, targetY, duration, easing, onAnimation } = {}) {
|
|
2789
|
+
return TinyHtml.scrollToXY(this, { targetX, targetY, duration, easing, onAnimation });
|
|
2790
|
+
}
|
|
2791
|
+
/**
|
|
2792
|
+
* Sets the vertical scroll position.
|
|
2793
|
+
* @param {TinyElementAndWindow|TinyElementAndWindow[]} el - Element or window.
|
|
2794
|
+
* @param {number} value - Scroll top value.
|
|
2795
|
+
* @returns {TinyElementAndWindow|TinyElementAndWindow[]}
|
|
2796
|
+
*/
|
|
2797
|
+
static setScrollTop(el, value) {
|
|
2798
|
+
if (typeof value !== 'number')
|
|
2799
|
+
throw new TypeError('ScrollTop value must be a number.');
|
|
2800
|
+
return TinyHtml.scrollToXY(el, { targetY: value });
|
|
2801
|
+
}
|
|
2623
2802
|
/**
|
|
2624
2803
|
* Sets the vertical scroll position.
|
|
2625
2804
|
* @param {number} value - Scroll top value.
|
|
@@ -2637,19 +2816,7 @@ class TinyHtml {
|
|
|
2637
2816
|
static setScrollLeft(el, value) {
|
|
2638
2817
|
if (typeof value !== 'number')
|
|
2639
2818
|
throw new TypeError('ScrollLeft value must be a number.');
|
|
2640
|
-
TinyHtml.
|
|
2641
|
-
if (TinyHtml.isWindow(elem)) {
|
|
2642
|
-
elem.scrollTo(value, elem.pageYOffset);
|
|
2643
|
-
}
|
|
2644
|
-
else if (elem.nodeType === 9) {
|
|
2645
|
-
// @ts-ignore
|
|
2646
|
-
elem.defaultView.scrollTo(value, elem.defaultView.pageYOffset);
|
|
2647
|
-
}
|
|
2648
|
-
else {
|
|
2649
|
-
elem.scrollLeft = value;
|
|
2650
|
-
}
|
|
2651
|
-
});
|
|
2652
|
-
return el;
|
|
2819
|
+
return TinyHtml.scrollToXY(el, { targetX: value });
|
|
2653
2820
|
}
|
|
2654
2821
|
/**
|
|
2655
2822
|
* Sets the horizontal scroll position.
|
|
@@ -3446,12 +3613,117 @@ class TinyHtml {
|
|
|
3446
3613
|
return TinyHtml.valBool(this);
|
|
3447
3614
|
}
|
|
3448
3615
|
////////////////////////////////////////////
|
|
3616
|
+
/**
|
|
3617
|
+
* Registers a listener for the "paste" event to extract files and text from the clipboard (e.g., when the user presses Ctrl+V).
|
|
3618
|
+
*
|
|
3619
|
+
* This method allows reacting to pasted content by providing separate callbacks for files and plain text.
|
|
3620
|
+
* Useful for building file upload areas, rich-text editors, or input enhancements.
|
|
3621
|
+
*
|
|
3622
|
+
* @param {TinyElementWithDoc|TinyElementWithDoc[]} el - The target element(s) where the "paste" event will be listened.
|
|
3623
|
+
* @param {Object} [settings={}] - Optional callbacks to handle clipboard content.
|
|
3624
|
+
* @param {(data: DataTransferItem, file: File) => void} [settings.onFilePaste] - Called for each file pasted from the clipboard (e.g., images).
|
|
3625
|
+
* @param {(data: DataTransferItem, text: string) => void} [settings.onTextPaste] - Called when plain text is pasted from the clipboard.
|
|
3626
|
+
* @returns {EventListenerOrEventListenerObject} The internal "paste" event handler used.
|
|
3627
|
+
*/
|
|
3628
|
+
static listenForPaste(el, { onFilePaste, onTextPaste } = {}) {
|
|
3629
|
+
if (typeof onFilePaste !== 'undefined' && typeof onFilePaste !== 'function')
|
|
3630
|
+
throw new TypeError('onFilePaste must be a function.');
|
|
3631
|
+
if (typeof onTextPaste !== 'undefined' && typeof onTextPaste !== 'function')
|
|
3632
|
+
throw new TypeError('onTextPaste must be a function.');
|
|
3633
|
+
/** @type {EventListenerOrEventListenerObject} */
|
|
3634
|
+
const pasteEvent = (event) => {
|
|
3635
|
+
if (!(event instanceof ClipboardEvent))
|
|
3636
|
+
return;
|
|
3637
|
+
const items = event.clipboardData?.items || [];
|
|
3638
|
+
for (const item of items) {
|
|
3639
|
+
if (item.kind === 'file') {
|
|
3640
|
+
if (typeof onFilePaste === 'function') {
|
|
3641
|
+
const file = item.getAsFile();
|
|
3642
|
+
if (file)
|
|
3643
|
+
onFilePaste(item, file);
|
|
3644
|
+
}
|
|
3645
|
+
}
|
|
3646
|
+
else if (item.kind === 'string') {
|
|
3647
|
+
if (typeof onTextPaste === 'function')
|
|
3648
|
+
item.getAsString((text) => onTextPaste(item, text));
|
|
3649
|
+
}
|
|
3650
|
+
}
|
|
3651
|
+
};
|
|
3652
|
+
TinyHtml._preElemsWithDoc(el, 'listenForPaste').forEach((elem) => TinyHtml.on(elem, 'paste', pasteEvent));
|
|
3653
|
+
return pasteEvent;
|
|
3654
|
+
}
|
|
3655
|
+
/**
|
|
3656
|
+
* Registers a listener for the "paste" event to extract files and text from the clipboard (e.g., when the user presses Ctrl+V).
|
|
3657
|
+
*
|
|
3658
|
+
* This method allows reacting to pasted content by providing separate callbacks for files and plain text.
|
|
3659
|
+
* Useful for building file upload areas, rich-text editors, or input enhancements.
|
|
3660
|
+
*
|
|
3661
|
+
* @param {Object} [settings={}] - Optional callbacks to handle clipboard content.
|
|
3662
|
+
* @param {(data: DataTransferItem, file: File) => void} [settings.onFilePaste] - Called for each file pasted from the clipboard (e.g., images).
|
|
3663
|
+
* @param {(data: DataTransferItem, text: string) => void} [settings.onTextPaste] - Called when plain text is pasted from the clipboard.
|
|
3664
|
+
* @returns {EventListenerOrEventListenerObject} The internal "paste" event handler used.
|
|
3665
|
+
*/
|
|
3666
|
+
listenForPaste({ onFilePaste, onTextPaste } = {}) {
|
|
3667
|
+
return TinyHtml.listenForPaste(this, { onFilePaste, onTextPaste });
|
|
3668
|
+
}
|
|
3669
|
+
/**
|
|
3670
|
+
* Checks if the element has a listener for a specific event.
|
|
3671
|
+
*
|
|
3672
|
+
* @param {TinyEventTarget} el - The element to check.
|
|
3673
|
+
* @param {string} event - The event name to check.
|
|
3674
|
+
* @returns {boolean}
|
|
3675
|
+
*/
|
|
3676
|
+
static hasEventListener(el, event) {
|
|
3677
|
+
const elem = TinyHtml._preEventTargetElem(el, 'hasEventListener');
|
|
3678
|
+
if (!__eventRegistry.has(elem))
|
|
3679
|
+
return false;
|
|
3680
|
+
const events = __eventRegistry.get(elem);
|
|
3681
|
+
return !!(events && Array.isArray(events[event]) && events[event].length > 0);
|
|
3682
|
+
}
|
|
3683
|
+
/**
|
|
3684
|
+
* Checks if the element has a listener for a specific event.
|
|
3685
|
+
*
|
|
3686
|
+
* @param {string} event - The event name to check.
|
|
3687
|
+
* @returns {boolean}
|
|
3688
|
+
*/
|
|
3689
|
+
hasEventListener(event) {
|
|
3690
|
+
return TinyHtml.hasEventListener(this, event);
|
|
3691
|
+
}
|
|
3692
|
+
/**
|
|
3693
|
+
* Checks if the element has the exact handler registered for a specific event.
|
|
3694
|
+
*
|
|
3695
|
+
* @param {TinyEventTarget} el - The element to check.
|
|
3696
|
+
* @param {string} event - The event name to check.
|
|
3697
|
+
* @param {EventListenerOrEventListenerObject} handler - The handler function to check.
|
|
3698
|
+
* @returns {boolean}
|
|
3699
|
+
*/
|
|
3700
|
+
static hasExactEventListener(el, event, handler) {
|
|
3701
|
+
const elem = TinyHtml._preEventTargetElem(el, 'hasExactEventListener');
|
|
3702
|
+
if (typeof handler !== 'function')
|
|
3703
|
+
throw new TypeError('The "handler" must be a function.');
|
|
3704
|
+
if (!__eventRegistry.has(elem))
|
|
3705
|
+
return false;
|
|
3706
|
+
const events = __eventRegistry.get(elem);
|
|
3707
|
+
if (!events || !Array.isArray(events[event]))
|
|
3708
|
+
return false;
|
|
3709
|
+
return events[event].some((item) => item.handler === handler);
|
|
3710
|
+
}
|
|
3711
|
+
/**
|
|
3712
|
+
* Checks if the element has the exact handler registered for a specific event.
|
|
3713
|
+
*
|
|
3714
|
+
* @param {string} event - The event name to check.
|
|
3715
|
+
* @param {EventListenerOrEventListenerObject} handler - The handler function to check.
|
|
3716
|
+
* @returns {boolean}
|
|
3717
|
+
*/
|
|
3718
|
+
hasExactEventListener(event, handler) {
|
|
3719
|
+
return TinyHtml.hasExactEventListener(this, event, handler);
|
|
3720
|
+
}
|
|
3449
3721
|
/**
|
|
3450
3722
|
* Registers an event listener on the specified element.
|
|
3451
3723
|
*
|
|
3452
3724
|
* @param {TinyEventTarget|TinyEventTarget[]} el - The target to listen on.
|
|
3453
3725
|
* @param {string} event - The event type (e.g. 'click', 'keydown').
|
|
3454
|
-
* @param {
|
|
3726
|
+
* @param {EventListenerOrEventListenerObject|null} handler - The callback function to run on event.
|
|
3455
3727
|
* @param {EventRegistryOptions} [options] - Optional event listener options.
|
|
3456
3728
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
3457
3729
|
*/
|
|
@@ -3475,7 +3747,7 @@ class TinyHtml {
|
|
|
3475
3747
|
* Registers an event listener on the specified element.
|
|
3476
3748
|
*
|
|
3477
3749
|
* @param {string} event - The event type (e.g. 'click', 'keydown').
|
|
3478
|
-
* @param {
|
|
3750
|
+
* @param {EventListenerOrEventListenerObject|null} handler - The callback function to run on event.
|
|
3479
3751
|
* @param {EventRegistryOptions} [options] - Optional event listener options.
|
|
3480
3752
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
3481
3753
|
*/
|
|
@@ -3487,7 +3759,7 @@ class TinyHtml {
|
|
|
3487
3759
|
*
|
|
3488
3760
|
* @param {TinyEventTarget|TinyEventTarget[]} el - The target to listen on.
|
|
3489
3761
|
* @param {string} event - The event type (e.g. 'click', 'keydown').
|
|
3490
|
-
* @param {
|
|
3762
|
+
* @param {EventListenerOrEventListenerObject} handler - The callback function to run on event.
|
|
3491
3763
|
* @param {EventRegistryOptions} [options={}] - Optional event listener options.
|
|
3492
3764
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
3493
3765
|
*/
|
|
@@ -3495,10 +3767,11 @@ class TinyHtml {
|
|
|
3495
3767
|
if (typeof event !== 'string')
|
|
3496
3768
|
throw new TypeError('The event name must be a string.');
|
|
3497
3769
|
TinyHtml._preEventTargetElems(el, 'once').forEach((elem) => {
|
|
3498
|
-
/** @type {
|
|
3770
|
+
/** @type {EventListenerOrEventListenerObject} */
|
|
3499
3771
|
const wrapped = (e) => {
|
|
3500
3772
|
TinyHtml.off(elem, event, wrapped);
|
|
3501
|
-
handler
|
|
3773
|
+
if (typeof handler === 'function')
|
|
3774
|
+
handler(e);
|
|
3502
3775
|
};
|
|
3503
3776
|
TinyHtml.on(elem, event, wrapped, typeof options === 'boolean' ? options : { ...options, once: true });
|
|
3504
3777
|
});
|
|
@@ -3508,7 +3781,7 @@ class TinyHtml {
|
|
|
3508
3781
|
* Registers an event listener that runs only once, then is removed.
|
|
3509
3782
|
*
|
|
3510
3783
|
* @param {string} event - The event type (e.g. 'click', 'keydown').
|
|
3511
|
-
* @param {
|
|
3784
|
+
* @param {EventListenerOrEventListenerObject} handler - The callback function to run on event.
|
|
3512
3785
|
* @param {EventRegistryOptions} [options={}] - Optional event listener options.
|
|
3513
3786
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
3514
3787
|
*/
|
|
@@ -3520,7 +3793,7 @@ class TinyHtml {
|
|
|
3520
3793
|
*
|
|
3521
3794
|
* @param {TinyEventTarget|TinyEventTarget[]} el - The target element.
|
|
3522
3795
|
* @param {string} event - The event type.
|
|
3523
|
-
* @param {
|
|
3796
|
+
* @param {EventListenerOrEventListenerObject|null} handler - The function originally bound to the event.
|
|
3524
3797
|
* @param {boolean|EventListenerOptions} [options] - Optional listener options.
|
|
3525
3798
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
3526
3799
|
*/
|
|
@@ -3542,7 +3815,7 @@ class TinyHtml {
|
|
|
3542
3815
|
* Removes a specific event listener from an element.
|
|
3543
3816
|
*
|
|
3544
3817
|
* @param {string} event - The event type.
|
|
3545
|
-
* @param {
|
|
3818
|
+
* @param {EventListenerOrEventListenerObject|null} handler - The function originally bound to the event.
|
|
3546
3819
|
* @param {boolean|EventListenerOptions} [options] - Optional listener options.
|
|
3547
3820
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
3548
3821
|
*/
|
|
@@ -3583,7 +3856,7 @@ class TinyHtml {
|
|
|
3583
3856
|
* Removes all event listeners of all types from the element.
|
|
3584
3857
|
*
|
|
3585
3858
|
* @param {TinyEventTarget|TinyEventTarget[]} el - The target element.
|
|
3586
|
-
* @param {((handler: EventListenerOrEventListenerObject, event: string) => boolean)|null} [filterFn=null] -
|
|
3859
|
+
* @param {((handler: EventListenerOrEventListenerObject|null, event: string) => boolean)|null} [filterFn=null] -
|
|
3587
3860
|
* Optional filter function to selectively remove specific handlers.
|
|
3588
3861
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
3589
3862
|
*/
|
|
@@ -3608,7 +3881,7 @@ class TinyHtml {
|
|
|
3608
3881
|
/**
|
|
3609
3882
|
* Removes all event listeners of all types from the element.
|
|
3610
3883
|
*
|
|
3611
|
-
* @param {((handler: EventListenerOrEventListenerObject, event: string) => boolean)|null} [filterFn=null] -
|
|
3884
|
+
* @param {((handler: EventListenerOrEventListenerObject|null, event: string) => boolean)|null} [filterFn=null] -
|
|
3612
3885
|
* Optional filter function to selectively remove specific handlers.
|
|
3613
3886
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
3614
3887
|
*/
|