tiny-essentials 1.14.0 → 1.16.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 +4585 -270
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyDragger.js +4386 -471
- package/dist/v1/TinyDragger.min.js +1 -1
- package/dist/v1/TinyEssentials.js +4634 -242
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyHtml.js +4327 -0
- package/dist/v1/TinyHtml.min.js +1 -0
- package/dist/v1/TinyUploadClicker.js +4628 -246
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/basics/collision.cjs +413 -0
- package/dist/v1/basics/collision.d.mts +187 -0
- package/dist/v1/basics/collision.mjs +350 -0
- package/dist/v1/basics/html.cjs +5 -109
- package/dist/v1/basics/html.d.mts +2 -28
- package/dist/v1/basics/html.mjs +5 -91
- package/dist/v1/basics/html_deprecated.cjs +124 -0
- package/dist/v1/basics/html_deprecated.d.mts +40 -0
- package/dist/v1/basics/html_deprecated.mjs +97 -0
- package/dist/v1/basics/index.cjs +27 -5
- package/dist/v1/basics/index.d.mts +26 -6
- package/dist/v1/basics/index.mjs +4 -2
- package/dist/v1/build/TinyHtml.cjs +7 -0
- package/dist/v1/build/TinyHtml.d.mts +3 -0
- package/dist/v1/build/TinyHtml.mjs +2 -0
- package/dist/v1/index.cjs +29 -5
- package/dist/v1/index.d.mts +27 -6
- package/dist/v1/index.mjs +5 -2
- package/dist/v1/libs/TinyDragger.cjs +91 -16
- package/dist/v1/libs/TinyDragger.d.mts +78 -2
- package/dist/v1/libs/TinyDragger.mjs +93 -17
- package/dist/v1/libs/TinyHtml.cjs +3859 -0
- package/dist/v1/libs/TinyHtml.d.mts +2151 -0
- package/dist/v1/libs/TinyHtml.mjs +3457 -0
- package/docs/v1/README.md +2 -0
- package/docs/v1/basics/collision.md +237 -0
- package/docs/v1/basics/html.md +1 -105
- package/docs/v1/basics/html_deprecated.md +127 -0
- package/docs/v1/libs/TinyDragger.md +45 -15
- package/docs/v1/libs/TinyHtml.md +1333 -0
- package/package.json +8 -2
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A direction relative to a rectangle.
|
|
3
|
+
*
|
|
4
|
+
* Represents one of the four cardinal directions from the perspective of the element.
|
|
5
|
+
*
|
|
6
|
+
* @typedef {'top' | 'bottom' | 'left' | 'right'} Dirs
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Represents all directional aspects of a collision.
|
|
10
|
+
*
|
|
11
|
+
* @typedef {Object} CollDirs
|
|
12
|
+
* @property {Dirs | 'center' | null} in - The dominant direction of entry. `'center'` if all sides are equally overlapped. `null` if no collision.
|
|
13
|
+
* @property {Dirs | null} x - The horizontal direction (`'left'` or `'right'`) the collision is biased toward, or `null`.
|
|
14
|
+
* @property {Dirs | null} y - The vertical direction (`'top'` or `'bottom'`) the collision is biased toward, or `null`.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Indicates if a collision is in the negative direction (rect2 is outside rect1).
|
|
18
|
+
*
|
|
19
|
+
* @typedef {Object} NegCollDirs
|
|
20
|
+
* @property {Dirs | null} x - Horizontal direction of negative overlap, if any.
|
|
21
|
+
* @property {Dirs | null} y - Vertical direction of negative overlap, if any.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Collision depth values from each side of rect2 inside rect1.
|
|
25
|
+
*
|
|
26
|
+
* Positive values indicate penetration; negative values indicate gaps.
|
|
27
|
+
*
|
|
28
|
+
* @typedef {Object} CollData
|
|
29
|
+
* @property {number} top - Depth from rect2's top into rect1.
|
|
30
|
+
* @property {number} bottom - Depth from rect2's bottom into rect1.
|
|
31
|
+
* @property {number} left - Depth from rect2's left into rect1.
|
|
32
|
+
* @property {number} right - Depth from rect2's right into rect1.
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* X and Y offset representing center difference between two rectangles.
|
|
36
|
+
*
|
|
37
|
+
* Useful to measure how far one element's center is from another.
|
|
38
|
+
*
|
|
39
|
+
* @typedef {Object} CollCenter
|
|
40
|
+
* @property {number} x - Horizontal distance in pixels from rect1's center to rect2's center.
|
|
41
|
+
* @property {number} y - Vertical distance in pixels from rect1's center to rect2's center.
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* Represents a rectangular area in absolute pixel values.
|
|
45
|
+
*
|
|
46
|
+
* Similar to `DOMRect`, this object describes the dimensions and position of a box
|
|
47
|
+
* in the 2D plane, typically representing an element's bounding box.
|
|
48
|
+
*
|
|
49
|
+
* @typedef {Object} ObjRect
|
|
50
|
+
* @property {number} height - The total height of the rectangle in pixels.
|
|
51
|
+
* @property {number} width - The total width of the rectangle in pixels.
|
|
52
|
+
* @property {number} top - The Y-coordinate of the top edge of the rectangle.
|
|
53
|
+
* @property {number} bottom - The Y-coordinate of the bottom edge of the rectangle.
|
|
54
|
+
* @property {number} left - The X-coordinate of the left edge of the rectangle.
|
|
55
|
+
* @property {number} right - The X-coordinate of the right edge of the rectangle.
|
|
56
|
+
*/
|
|
57
|
+
// Normal collision checks (loose overlap detection)
|
|
58
|
+
/**
|
|
59
|
+
* Checks if rect1 is completely above rect2 (no vertical overlap).
|
|
60
|
+
*
|
|
61
|
+
* @param {ObjRect} rect1 - The bounding rectangle of the first element.
|
|
62
|
+
* @param {ObjRect} rect2 - The bounding rectangle of the second element.
|
|
63
|
+
* @returns {boolean} True if rect1 is entirely above rect2.
|
|
64
|
+
*/
|
|
65
|
+
export const areElsCollTop = (rect1, rect2) => rect1.bottom < rect2.top;
|
|
66
|
+
/**
|
|
67
|
+
* Checks if rect1 is completely below rect2 (no vertical overlap).
|
|
68
|
+
*
|
|
69
|
+
* @param {ObjRect} rect1
|
|
70
|
+
* @param {ObjRect} rect2
|
|
71
|
+
* @returns {boolean} True if rect1 is entirely below rect2.
|
|
72
|
+
*/
|
|
73
|
+
export const areElsCollBottom = (rect1, rect2) => rect1.top > rect2.bottom;
|
|
74
|
+
/**
|
|
75
|
+
* Checks if rect1 is completely to the left of rect2 (no horizontal overlap).
|
|
76
|
+
*
|
|
77
|
+
* @param {ObjRect} rect1
|
|
78
|
+
* @param {ObjRect} rect2
|
|
79
|
+
* @returns {boolean} True if rect1 is entirely to the left of rect2.
|
|
80
|
+
*/
|
|
81
|
+
export const areElsCollLeft = (rect1, rect2) => rect1.right < rect2.left;
|
|
82
|
+
/**
|
|
83
|
+
* Checks if rect1 is completely to the right of rect2 (no horizontal overlap).
|
|
84
|
+
*
|
|
85
|
+
* @param {ObjRect} rect1
|
|
86
|
+
* @param {ObjRect} rect2
|
|
87
|
+
* @returns {boolean} True if rect1 is entirely to the right of rect2.
|
|
88
|
+
*/
|
|
89
|
+
export const areElsCollRight = (rect1, rect2) => rect1.left > rect2.right;
|
|
90
|
+
// Perfect collision checks (touch included)
|
|
91
|
+
/**
|
|
92
|
+
* Checks if rect1 is perfectly above rect2 (no vertical touch or overlap).
|
|
93
|
+
*
|
|
94
|
+
* @param {ObjRect} rect1
|
|
95
|
+
* @param {ObjRect} rect2
|
|
96
|
+
* @returns {boolean} True if rect1 is fully above or touching rect2's top.
|
|
97
|
+
*/
|
|
98
|
+
export const areElsCollPerfTop = (rect1, rect2) => rect1.bottom <= rect2.top;
|
|
99
|
+
/**
|
|
100
|
+
* Checks if rect1 is perfectly below rect2 (no vertical touch or overlap).
|
|
101
|
+
*
|
|
102
|
+
* @param {ObjRect} rect1
|
|
103
|
+
* @param {ObjRect} rect2
|
|
104
|
+
* @returns {boolean} True if rect1 is fully below or touching rect2's bottom.
|
|
105
|
+
*/
|
|
106
|
+
export const areElsCollPerfBottom = (rect1, rect2) => rect1.top >= rect2.bottom;
|
|
107
|
+
/**
|
|
108
|
+
* Checks if rect1 is perfectly to the left of rect2 (no horizontal touch or overlap).
|
|
109
|
+
*
|
|
110
|
+
* @param {ObjRect} rect1
|
|
111
|
+
* @param {ObjRect} rect2
|
|
112
|
+
* @returns {boolean} True if rect1 is fully left or touching rect2's left.
|
|
113
|
+
*/
|
|
114
|
+
export const areElsCollPerfLeft = (rect1, rect2) => rect1.right <= rect2.left;
|
|
115
|
+
/**
|
|
116
|
+
* Checks if rect1 is perfectly to the right of rect2 (no horizontal touch or overlap).
|
|
117
|
+
*
|
|
118
|
+
* @param {ObjRect} rect1
|
|
119
|
+
* @param {ObjRect} rect2
|
|
120
|
+
* @returns {boolean} True if rect1 is fully right or touching rect2's right.
|
|
121
|
+
*/
|
|
122
|
+
export const areElsCollPerfRight = (rect1, rect2) => rect1.left >= rect2.right;
|
|
123
|
+
// Main collision check
|
|
124
|
+
/**
|
|
125
|
+
* Returns true if rect1 and rect2 are colliding (partially or fully overlapping).
|
|
126
|
+
*
|
|
127
|
+
* @param {ObjRect} rect1
|
|
128
|
+
* @param {ObjRect} rect2
|
|
129
|
+
* @returns {boolean} True if there's any collision between rect1 and rect2.
|
|
130
|
+
*/
|
|
131
|
+
export const areElsColliding = (rect1, rect2) => !(areElsCollLeft(rect1, rect2) ||
|
|
132
|
+
areElsCollRight(rect1, rect2) ||
|
|
133
|
+
areElsCollTop(rect1, rect2) ||
|
|
134
|
+
areElsCollBottom(rect1, rect2));
|
|
135
|
+
/**
|
|
136
|
+
* Returns true if rect1 and rect2 are colliding or perfectly touching.
|
|
137
|
+
*
|
|
138
|
+
* @param {ObjRect} rect1
|
|
139
|
+
* @param {ObjRect} rect2
|
|
140
|
+
* @returns {boolean} True if there's any contact or overlap.
|
|
141
|
+
*/
|
|
142
|
+
export const areElsPerfColliding = (rect1, rect2) => !(areElsCollPerfLeft(rect1, rect2) ||
|
|
143
|
+
areElsCollPerfRight(rect1, rect2) ||
|
|
144
|
+
areElsCollPerfTop(rect1, rect2) ||
|
|
145
|
+
areElsCollPerfBottom(rect1, rect2));
|
|
146
|
+
// Collision direction guess (loose and perfect)
|
|
147
|
+
/**
|
|
148
|
+
* Attempts to determine the direction rect1 entered rect2 based on loose overlap rules.
|
|
149
|
+
*
|
|
150
|
+
* @param {ObjRect} rect1
|
|
151
|
+
* @param {ObjRect} rect2
|
|
152
|
+
* @returns {string|null} 'top' | 'bottom' | 'left' | 'right' | null
|
|
153
|
+
*/
|
|
154
|
+
export const getElsColliding = (rect1, rect2) => {
|
|
155
|
+
if (areElsCollLeft(rect1, rect2))
|
|
156
|
+
return 'left';
|
|
157
|
+
else if (areElsCollRight(rect1, rect2))
|
|
158
|
+
return 'right';
|
|
159
|
+
else if (areElsCollTop(rect1, rect2))
|
|
160
|
+
return 'top';
|
|
161
|
+
else if (areElsCollBottom(rect1, rect2))
|
|
162
|
+
return 'bottom';
|
|
163
|
+
return null;
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Attempts to determine the direction rect1 touched or entered rect2 using perfect mode.
|
|
167
|
+
*
|
|
168
|
+
* @param {ObjRect} rect1
|
|
169
|
+
* @param {ObjRect} rect2
|
|
170
|
+
* @returns {'top' | 'bottom' | 'left' | 'right' | null}
|
|
171
|
+
*/
|
|
172
|
+
export const getElsPerfColliding = (rect1, rect2) => {
|
|
173
|
+
if (areElsCollPerfLeft(rect1, rect2))
|
|
174
|
+
return 'left';
|
|
175
|
+
else if (areElsCollPerfRight(rect1, rect2))
|
|
176
|
+
return 'right';
|
|
177
|
+
else if (areElsCollPerfTop(rect1, rect2))
|
|
178
|
+
return 'top';
|
|
179
|
+
else if (areElsCollPerfBottom(rect1, rect2))
|
|
180
|
+
return 'bottom';
|
|
181
|
+
return null;
|
|
182
|
+
};
|
|
183
|
+
// Overlap Calculation
|
|
184
|
+
/**
|
|
185
|
+
* Calculates overlap values between rect1 and rect2 in all directions.
|
|
186
|
+
*
|
|
187
|
+
* @param {ObjRect} rect1
|
|
188
|
+
* @param {ObjRect} rect2
|
|
189
|
+
* @returns {{
|
|
190
|
+
* overlapLeft: number,
|
|
191
|
+
* overlapRight: number,
|
|
192
|
+
* overlapTop: number,
|
|
193
|
+
* overlapBottom: number
|
|
194
|
+
* }} Distance of overlap from each direction (can be negative).
|
|
195
|
+
*/
|
|
196
|
+
export const getElsCollOverlap = (rect1, rect2) => ({
|
|
197
|
+
overlapLeft: rect2.right - rect1.left,
|
|
198
|
+
overlapRight: rect1.right - rect2.left,
|
|
199
|
+
overlapTop: rect2.bottom - rect1.top,
|
|
200
|
+
overlapBottom: rect1.bottom - rect2.top,
|
|
201
|
+
});
|
|
202
|
+
/**
|
|
203
|
+
* Determines directional collision based on overlap depth.
|
|
204
|
+
*
|
|
205
|
+
* @param {Object} [settings={}]
|
|
206
|
+
* @param {number} [settings.overlapLeft]
|
|
207
|
+
* @param {number} [settings.overlapRight]
|
|
208
|
+
* @param {number} [settings.overlapTop]
|
|
209
|
+
* @param {number} [settings.overlapBottom]
|
|
210
|
+
* @returns {{ dirX: Dirs, dirY: Dirs }} Direction of strongest X/Y overlap.
|
|
211
|
+
*/
|
|
212
|
+
export const getElsCollOverlapPos = ({ overlapLeft = -1, overlapRight = -1, overlapTop = -1, overlapBottom = -1, } = {}) => ({
|
|
213
|
+
dirX: overlapLeft < overlapRight ? 'right' : 'left',
|
|
214
|
+
dirY: overlapTop < overlapBottom ? 'bottom' : 'top',
|
|
215
|
+
});
|
|
216
|
+
// Center utils
|
|
217
|
+
/**
|
|
218
|
+
* Calculates the center point (X and Y) of a given Rect.
|
|
219
|
+
*
|
|
220
|
+
* @param {ObjRect} rect - The bounding rectangle of the element.
|
|
221
|
+
* @returns {{ x: number, y: number }} An object with the `x` and `y` coordinates of the center.
|
|
222
|
+
*/
|
|
223
|
+
export const getRectCenter = (rect) => ({
|
|
224
|
+
x: rect.left + rect.width / 2,
|
|
225
|
+
y: rect.top + rect.height / 2,
|
|
226
|
+
});
|
|
227
|
+
/**
|
|
228
|
+
* Calculates the offset between the center of rect2 and the center of rect1.
|
|
229
|
+
*
|
|
230
|
+
* The values will be 0 when rect1 is perfectly centered over rect2.
|
|
231
|
+
*
|
|
232
|
+
* @param {ObjRect} rect1 - The bounding rectangle of the reference element.
|
|
233
|
+
* @param {ObjRect} rect2 - The bounding rectangle of the element being compared.
|
|
234
|
+
* @returns {{
|
|
235
|
+
* x: number,
|
|
236
|
+
* y: number
|
|
237
|
+
* }} An object with the X and Y offset in pixels from rect1's center to rect2's center.
|
|
238
|
+
*/
|
|
239
|
+
export function getElsRelativeCenterOffset(rect1, rect2) {
|
|
240
|
+
const center1X = rect1.left + rect1.width / 2;
|
|
241
|
+
const center1Y = rect1.top + rect1.height / 2;
|
|
242
|
+
const center2X = rect2.left + rect2.width / 2;
|
|
243
|
+
const center2Y = rect2.top + rect2.height / 2;
|
|
244
|
+
return {
|
|
245
|
+
x: center2X - center1X,
|
|
246
|
+
y: center2Y - center1Y,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
// Direction & Depth detection
|
|
250
|
+
/**
|
|
251
|
+
* Detects the direction of the dominant collision between two elements
|
|
252
|
+
* and calculates how deep the overlap is in both x and y axes.
|
|
253
|
+
*
|
|
254
|
+
* @param {ObjRect} rect1 - The bounding rectangle of the first element.
|
|
255
|
+
* @param {ObjRect} rect2 - The bounding rectangle of the second element.
|
|
256
|
+
* @returns {{
|
|
257
|
+
* inDir: Dirs | null;
|
|
258
|
+
* dirX: Dirs | null;
|
|
259
|
+
* dirY: Dirs | null;
|
|
260
|
+
* depthX: number;
|
|
261
|
+
* depthY: number;
|
|
262
|
+
* }} An object containing the collision direction and how deep the overlap is.
|
|
263
|
+
*/
|
|
264
|
+
export function getElsCollDirDepth(rect1, rect2) {
|
|
265
|
+
if (!areElsPerfColliding(rect1, rect2))
|
|
266
|
+
return {
|
|
267
|
+
inDir: null,
|
|
268
|
+
dirX: null,
|
|
269
|
+
dirY: null,
|
|
270
|
+
depthX: 0,
|
|
271
|
+
depthY: 0,
|
|
272
|
+
};
|
|
273
|
+
const { overlapLeft, overlapRight, overlapTop, overlapBottom } = getElsCollOverlap(rect1, rect2);
|
|
274
|
+
const { dirX, dirY } = getElsCollOverlapPos({
|
|
275
|
+
overlapLeft,
|
|
276
|
+
overlapRight,
|
|
277
|
+
overlapTop,
|
|
278
|
+
overlapBottom,
|
|
279
|
+
});
|
|
280
|
+
const depthX = Math.min(overlapLeft, overlapRight);
|
|
281
|
+
const depthY = Math.min(overlapTop, overlapBottom);
|
|
282
|
+
/** @type {Dirs} */
|
|
283
|
+
let inDir;
|
|
284
|
+
if (depthX < depthY)
|
|
285
|
+
inDir = dirX;
|
|
286
|
+
else
|
|
287
|
+
inDir = dirY;
|
|
288
|
+
return { inDir, dirX, dirY, depthX, depthY };
|
|
289
|
+
}
|
|
290
|
+
// Full detail report
|
|
291
|
+
/**
|
|
292
|
+
* Detects the collision direction and depth between two DOMRects.
|
|
293
|
+
*
|
|
294
|
+
* @param {ObjRect} rect1 - The bounding rectangle of the first element.
|
|
295
|
+
* @param {ObjRect} rect2 - The bounding rectangle of the second element.
|
|
296
|
+
* @returns {{ depth: CollData; dirs: CollDirs; isNeg: NegCollDirs; }} Collision info or null if no collision is detected.
|
|
297
|
+
*/
|
|
298
|
+
export function getElsCollDetails(rect1, rect2) {
|
|
299
|
+
const isColliding = areElsPerfColliding(rect1, rect2);
|
|
300
|
+
/** @type {CollDirs} */
|
|
301
|
+
const dirs = { in: null, x: null, y: null };
|
|
302
|
+
/** @type {NegCollDirs} */
|
|
303
|
+
const isNeg = { y: null, x: null };
|
|
304
|
+
/** @type {Record<Dirs, number>} */
|
|
305
|
+
const depth = { top: 0, bottom: 0, left: 0, right: 0 };
|
|
306
|
+
// Depth
|
|
307
|
+
// Yes, it's actually reversed the values orders
|
|
308
|
+
const { overlapLeft, overlapRight, overlapTop, overlapBottom } = getElsCollOverlap(rect2, rect1);
|
|
309
|
+
depth.top = overlapTop;
|
|
310
|
+
depth.bottom = overlapBottom;
|
|
311
|
+
depth.left = overlapLeft;
|
|
312
|
+
depth.right = overlapRight;
|
|
313
|
+
// Dirs
|
|
314
|
+
/**
|
|
315
|
+
* Detect the direction with the smallest positive overlap (entry point)
|
|
316
|
+
* @type {[Dirs, number][]}
|
|
317
|
+
*/
|
|
318
|
+
// @ts-ignore
|
|
319
|
+
const entries = Object.entries(depth)
|
|
320
|
+
.filter(([, val]) => val > 0)
|
|
321
|
+
.sort((a, b) => a[1] - b[1]);
|
|
322
|
+
// Yes, it's actually reversed the values orders here too
|
|
323
|
+
const { dirX, dirY } = getElsCollOverlapPos({
|
|
324
|
+
overlapLeft: overlapRight,
|
|
325
|
+
overlapRight: overlapLeft,
|
|
326
|
+
overlapTop: overlapBottom,
|
|
327
|
+
overlapBottom: overlapTop,
|
|
328
|
+
});
|
|
329
|
+
dirs.y = dirY;
|
|
330
|
+
dirs.x = dirX;
|
|
331
|
+
// isNeg
|
|
332
|
+
if (depth.bottom < 0)
|
|
333
|
+
isNeg.y = 'bottom';
|
|
334
|
+
else if (depth.top < 0)
|
|
335
|
+
isNeg.y = 'top';
|
|
336
|
+
if (depth.left < 0)
|
|
337
|
+
isNeg.x = 'left';
|
|
338
|
+
else if (depth.right < 0)
|
|
339
|
+
isNeg.x = 'right';
|
|
340
|
+
// Inside Dir
|
|
341
|
+
dirs.in = isColliding
|
|
342
|
+
? depth.top === depth.bottom && depth.bottom === depth.left && depth.left === depth.right
|
|
343
|
+
? 'center'
|
|
344
|
+
: entries.length
|
|
345
|
+
? entries[0][0]
|
|
346
|
+
: 'top'
|
|
347
|
+
: null; // fallback in case of exact match
|
|
348
|
+
// Complete
|
|
349
|
+
return { dirs, depth, isNeg };
|
|
350
|
+
}
|
package/dist/v1/basics/html.cjs
CHANGED
|
@@ -2,24 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var objFilter = require('./objFilter.cjs');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
* Checks if two DOM elements are colliding on the screen.
|
|
7
|
-
*
|
|
8
|
-
* @param {Element} elem1 - First DOM element.
|
|
9
|
-
* @param {Element} elem2 - Second DOM element.
|
|
10
|
-
* @returns {boolean} - Returns true if the elements are colliding.
|
|
11
|
-
*/
|
|
12
|
-
function areHtmlElsColliding(elem1, elem2) {
|
|
13
|
-
const rect1 = elem1.getBoundingClientRect();
|
|
14
|
-
const rect2 = elem2.getBoundingClientRect();
|
|
15
|
-
|
|
16
|
-
return !(
|
|
17
|
-
rect1.right < rect2.left ||
|
|
18
|
-
rect1.left > rect2.right ||
|
|
19
|
-
rect1.bottom < rect2.top ||
|
|
20
|
-
rect1.top > rect2.bottom
|
|
21
|
-
);
|
|
22
|
-
}
|
|
5
|
+
/////////////////////////////////////////////////////////////////
|
|
23
6
|
|
|
24
7
|
/**
|
|
25
8
|
* Reads the contents of a file using the specified FileReader method.
|
|
@@ -240,89 +223,7 @@ async function fetchJson(
|
|
|
240
223
|
);
|
|
241
224
|
}
|
|
242
225
|
|
|
243
|
-
|
|
244
|
-
* @typedef {Object} HtmlElBoxSides
|
|
245
|
-
* @property {number} x - Total horizontal size (left + right)
|
|
246
|
-
* @property {number} y - Total vertical size (top + bottom)
|
|
247
|
-
* @property {number} left
|
|
248
|
-
* @property {number} right
|
|
249
|
-
* @property {number} top
|
|
250
|
-
* @property {number} bottom
|
|
251
|
-
*/
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Returns the total border width and individual sides from `border{Side}Width` CSS properties.
|
|
255
|
-
*
|
|
256
|
-
* @param {Element} el - The target DOM element.
|
|
257
|
-
* @returns {HtmlElBoxSides} - Total horizontal (x) and vertical (y) border widths, and each side individually.
|
|
258
|
-
*/
|
|
259
|
-
const getHtmlElBordersWidth = (el) => {
|
|
260
|
-
const styles = getComputedStyle(el);
|
|
261
|
-
const left = parseFloat(styles.borderLeftWidth) || 0;
|
|
262
|
-
const right = parseFloat(styles.borderRightWidth) || 0;
|
|
263
|
-
const top = parseFloat(styles.borderTopWidth) || 0;
|
|
264
|
-
const bottom = parseFloat(styles.borderBottomWidth) || 0;
|
|
265
|
-
const x = left + right;
|
|
266
|
-
const y = top + bottom;
|
|
267
|
-
|
|
268
|
-
return { x, y, left, right, top, bottom };
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Returns the total border size and individual sides from `border{Side}` CSS properties.
|
|
273
|
-
*
|
|
274
|
-
* @param {Element} el - The target DOM element.
|
|
275
|
-
* @returns {HtmlElBoxSides} - Total horizontal (x) and vertical (y) border sizes, and each side individually.
|
|
276
|
-
*/
|
|
277
|
-
const getHtmlElBorders = (el) => {
|
|
278
|
-
const styles = getComputedStyle(el);
|
|
279
|
-
const left = parseFloat(styles.borderLeft) || 0;
|
|
280
|
-
const right = parseFloat(styles.borderRight) || 0;
|
|
281
|
-
const top = parseFloat(styles.borderTop) || 0;
|
|
282
|
-
const bottom = parseFloat(styles.borderBottom) || 0;
|
|
283
|
-
const x = left + right;
|
|
284
|
-
const y = top + bottom;
|
|
285
|
-
|
|
286
|
-
return { x, y, left, right, top, bottom };
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Returns the total margin and individual sides from `margin{Side}` CSS properties.
|
|
291
|
-
*
|
|
292
|
-
* @param {Element} el - The target DOM element.
|
|
293
|
-
* @returns {HtmlElBoxSides} - Total horizontal (x) and vertical (y) margins, and each side individually.
|
|
294
|
-
*/
|
|
295
|
-
const getHtmlElMargin = (el) => {
|
|
296
|
-
const styles = getComputedStyle(el);
|
|
297
|
-
const left = parseFloat(styles.marginLeft) || 0;
|
|
298
|
-
const right = parseFloat(styles.marginRight) || 0;
|
|
299
|
-
const top = parseFloat(styles.marginTop) || 0;
|
|
300
|
-
const bottom = parseFloat(styles.marginBottom) || 0;
|
|
301
|
-
|
|
302
|
-
const x = left + right;
|
|
303
|
-
const y = top + bottom;
|
|
304
|
-
|
|
305
|
-
return { x, y, left, right, top, bottom };
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Returns the total padding and individual sides from `padding{Side}` CSS properties.
|
|
310
|
-
*
|
|
311
|
-
* @param {Element} el - The target DOM element.
|
|
312
|
-
* @returns {HtmlElBoxSides} - Total horizontal (x) and vertical (y) paddings, and each side individually.
|
|
313
|
-
*/
|
|
314
|
-
const getHtmlElPadding = (el) => {
|
|
315
|
-
const styles = getComputedStyle(el);
|
|
316
|
-
const left = parseFloat(styles.paddingLeft) || 0;
|
|
317
|
-
const right = parseFloat(styles.paddingRight) || 0;
|
|
318
|
-
const top = parseFloat(styles.paddingTop) || 0;
|
|
319
|
-
const bottom = parseFloat(styles.paddingBottom) || 0;
|
|
320
|
-
|
|
321
|
-
const x = left + right;
|
|
322
|
-
const y = top + bottom;
|
|
323
|
-
|
|
324
|
-
return { x, y, left, right, top, bottom };
|
|
325
|
-
};
|
|
226
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
326
227
|
|
|
327
228
|
/**
|
|
328
229
|
* Installs a script that toggles CSS classes on a given element
|
|
@@ -330,7 +231,7 @@ const getHtmlElPadding = (el) => {
|
|
|
330
231
|
* triggers callbacks on visibility changes.
|
|
331
232
|
*
|
|
332
233
|
* @param {Object} [settings={}]
|
|
333
|
-
* @param {
|
|
234
|
+
* @param {Element} [settings.element=document.body] - The element to receive visibility classes.
|
|
334
235
|
* @param {string} [settings.hiddenClass='windowHidden'] - CSS class applied when the page is hidden.
|
|
335
236
|
* @param {string} [settings.visibleClass='windowVisible'] - CSS class applied when the page is visible.
|
|
336
237
|
* @param {() => void} [settings.onVisible] - Callback called when page becomes visible.
|
|
@@ -345,8 +246,8 @@ function installWindowHiddenScript({
|
|
|
345
246
|
onVisible,
|
|
346
247
|
onHidden,
|
|
347
248
|
} = {}) {
|
|
348
|
-
if (!(element instanceof
|
|
349
|
-
throw new TypeError(`"element" must be an instance of
|
|
249
|
+
if (!(element instanceof Element))
|
|
250
|
+
throw new TypeError(`"element" must be an instance of Element.`);
|
|
350
251
|
if (typeof hiddenClass !== 'string') throw new TypeError(`"hiddenClass" must be a string.`);
|
|
351
252
|
if (typeof visibleClass !== 'string') throw new TypeError(`"visibleClass" must be a string.`);
|
|
352
253
|
if (onVisible !== undefined && typeof onVisible !== 'function')
|
|
@@ -447,12 +348,7 @@ function installWindowHiddenScript({
|
|
|
447
348
|
return uninstall;
|
|
448
349
|
}
|
|
449
350
|
|
|
450
|
-
exports.areHtmlElsColliding = areHtmlElsColliding;
|
|
451
351
|
exports.fetchJson = fetchJson;
|
|
452
|
-
exports.getHtmlElBorders = getHtmlElBorders;
|
|
453
|
-
exports.getHtmlElBordersWidth = getHtmlElBordersWidth;
|
|
454
|
-
exports.getHtmlElMargin = getHtmlElMargin;
|
|
455
|
-
exports.getHtmlElPadding = getHtmlElPadding;
|
|
456
352
|
exports.installWindowHiddenScript = installWindowHiddenScript;
|
|
457
353
|
exports.readBase64Blob = readBase64Blob;
|
|
458
354
|
exports.readFileBlob = readFileBlob;
|
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks if two DOM elements are colliding on the screen.
|
|
3
|
-
*
|
|
4
|
-
* @param {Element} elem1 - First DOM element.
|
|
5
|
-
* @param {Element} elem2 - Second DOM element.
|
|
6
|
-
* @returns {boolean} - Returns true if the elements are colliding.
|
|
7
|
-
*/
|
|
8
|
-
export function areHtmlElsColliding(elem1: Element, elem2: Element): boolean;
|
|
9
1
|
/**
|
|
10
2
|
* Reads the contents of a file using the specified FileReader method.
|
|
11
3
|
*
|
|
@@ -81,7 +73,7 @@ export function fetchJson(url: string, { method, body, timeout, retries, headers
|
|
|
81
73
|
* triggers callbacks on visibility changes.
|
|
82
74
|
*
|
|
83
75
|
* @param {Object} [settings={}]
|
|
84
|
-
* @param {
|
|
76
|
+
* @param {Element} [settings.element=document.body] - The element to receive visibility classes.
|
|
85
77
|
* @param {string} [settings.hiddenClass='windowHidden'] - CSS class applied when the page is hidden.
|
|
86
78
|
* @param {string} [settings.visibleClass='windowVisible'] - CSS class applied when the page is visible.
|
|
87
79
|
* @param {() => void} [settings.onVisible] - Callback called when page becomes visible.
|
|
@@ -90,28 +82,10 @@ export function fetchJson(url: string, { method, body, timeout, retries, headers
|
|
|
90
82
|
* @throws {TypeError} If any provided setting is invalid.
|
|
91
83
|
*/
|
|
92
84
|
export function installWindowHiddenScript({ element, hiddenClass, visibleClass, onVisible, onHidden, }?: {
|
|
93
|
-
element?:
|
|
85
|
+
element?: Element | undefined;
|
|
94
86
|
hiddenClass?: string | undefined;
|
|
95
87
|
visibleClass?: string | undefined;
|
|
96
88
|
onVisible?: (() => void) | undefined;
|
|
97
89
|
onHidden?: (() => void) | undefined;
|
|
98
90
|
}): () => void;
|
|
99
|
-
export function getHtmlElBordersWidth(el: Element): HtmlElBoxSides;
|
|
100
|
-
export function getHtmlElBorders(el: Element): HtmlElBoxSides;
|
|
101
|
-
export function getHtmlElMargin(el: Element): HtmlElBoxSides;
|
|
102
|
-
export function getHtmlElPadding(el: Element): HtmlElBoxSides;
|
|
103
|
-
export type HtmlElBoxSides = {
|
|
104
|
-
/**
|
|
105
|
-
* - Total horizontal size (left + right)
|
|
106
|
-
*/
|
|
107
|
-
x: number;
|
|
108
|
-
/**
|
|
109
|
-
* - Total vertical size (top + bottom)
|
|
110
|
-
*/
|
|
111
|
-
y: number;
|
|
112
|
-
left: number;
|
|
113
|
-
right: number;
|
|
114
|
-
top: number;
|
|
115
|
-
bottom: number;
|
|
116
|
-
};
|
|
117
91
|
//# sourceMappingURL=html.d.mts.map
|
package/dist/v1/basics/html.mjs
CHANGED
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
import { isJsonObject } from './objFilter.mjs';
|
|
2
|
-
|
|
3
|
-
* Checks if two DOM elements are colliding on the screen.
|
|
4
|
-
*
|
|
5
|
-
* @param {Element} elem1 - First DOM element.
|
|
6
|
-
* @param {Element} elem2 - Second DOM element.
|
|
7
|
-
* @returns {boolean} - Returns true if the elements are colliding.
|
|
8
|
-
*/
|
|
9
|
-
export function areHtmlElsColliding(elem1, elem2) {
|
|
10
|
-
const rect1 = elem1.getBoundingClientRect();
|
|
11
|
-
const rect2 = elem2.getBoundingClientRect();
|
|
12
|
-
return !(rect1.right < rect2.left ||
|
|
13
|
-
rect1.left > rect2.right ||
|
|
14
|
-
rect1.bottom < rect2.top ||
|
|
15
|
-
rect1.top > rect2.bottom);
|
|
16
|
-
}
|
|
2
|
+
/////////////////////////////////////////////////////////////////
|
|
17
3
|
/**
|
|
18
4
|
* Reads the contents of a file using the specified FileReader method.
|
|
19
5
|
*
|
|
@@ -203,86 +189,14 @@ export async function fetchJson(url, { method = 'GET', body, timeout = 0, retrie
|
|
|
203
189
|
}
|
|
204
190
|
throw new Error(`Failed to fetch JSON from "${url}"${lastError ? `: ${lastError.message}` : '.'}`);
|
|
205
191
|
}
|
|
206
|
-
|
|
207
|
-
* @typedef {Object} HtmlElBoxSides
|
|
208
|
-
* @property {number} x - Total horizontal size (left + right)
|
|
209
|
-
* @property {number} y - Total vertical size (top + bottom)
|
|
210
|
-
* @property {number} left
|
|
211
|
-
* @property {number} right
|
|
212
|
-
* @property {number} top
|
|
213
|
-
* @property {number} bottom
|
|
214
|
-
*/
|
|
215
|
-
/**
|
|
216
|
-
* Returns the total border width and individual sides from `border{Side}Width` CSS properties.
|
|
217
|
-
*
|
|
218
|
-
* @param {Element} el - The target DOM element.
|
|
219
|
-
* @returns {HtmlElBoxSides} - Total horizontal (x) and vertical (y) border widths, and each side individually.
|
|
220
|
-
*/
|
|
221
|
-
export const getHtmlElBordersWidth = (el) => {
|
|
222
|
-
const styles = getComputedStyle(el);
|
|
223
|
-
const left = parseFloat(styles.borderLeftWidth) || 0;
|
|
224
|
-
const right = parseFloat(styles.borderRightWidth) || 0;
|
|
225
|
-
const top = parseFloat(styles.borderTopWidth) || 0;
|
|
226
|
-
const bottom = parseFloat(styles.borderBottomWidth) || 0;
|
|
227
|
-
const x = left + right;
|
|
228
|
-
const y = top + bottom;
|
|
229
|
-
return { x, y, left, right, top, bottom };
|
|
230
|
-
};
|
|
231
|
-
/**
|
|
232
|
-
* Returns the total border size and individual sides from `border{Side}` CSS properties.
|
|
233
|
-
*
|
|
234
|
-
* @param {Element} el - The target DOM element.
|
|
235
|
-
* @returns {HtmlElBoxSides} - Total horizontal (x) and vertical (y) border sizes, and each side individually.
|
|
236
|
-
*/
|
|
237
|
-
export const getHtmlElBorders = (el) => {
|
|
238
|
-
const styles = getComputedStyle(el);
|
|
239
|
-
const left = parseFloat(styles.borderLeft) || 0;
|
|
240
|
-
const right = parseFloat(styles.borderRight) || 0;
|
|
241
|
-
const top = parseFloat(styles.borderTop) || 0;
|
|
242
|
-
const bottom = parseFloat(styles.borderBottom) || 0;
|
|
243
|
-
const x = left + right;
|
|
244
|
-
const y = top + bottom;
|
|
245
|
-
return { x, y, left, right, top, bottom };
|
|
246
|
-
};
|
|
247
|
-
/**
|
|
248
|
-
* Returns the total margin and individual sides from `margin{Side}` CSS properties.
|
|
249
|
-
*
|
|
250
|
-
* @param {Element} el - The target DOM element.
|
|
251
|
-
* @returns {HtmlElBoxSides} - Total horizontal (x) and vertical (y) margins, and each side individually.
|
|
252
|
-
*/
|
|
253
|
-
export const getHtmlElMargin = (el) => {
|
|
254
|
-
const styles = getComputedStyle(el);
|
|
255
|
-
const left = parseFloat(styles.marginLeft) || 0;
|
|
256
|
-
const right = parseFloat(styles.marginRight) || 0;
|
|
257
|
-
const top = parseFloat(styles.marginTop) || 0;
|
|
258
|
-
const bottom = parseFloat(styles.marginBottom) || 0;
|
|
259
|
-
const x = left + right;
|
|
260
|
-
const y = top + bottom;
|
|
261
|
-
return { x, y, left, right, top, bottom };
|
|
262
|
-
};
|
|
263
|
-
/**
|
|
264
|
-
* Returns the total padding and individual sides from `padding{Side}` CSS properties.
|
|
265
|
-
*
|
|
266
|
-
* @param {Element} el - The target DOM element.
|
|
267
|
-
* @returns {HtmlElBoxSides} - Total horizontal (x) and vertical (y) paddings, and each side individually.
|
|
268
|
-
*/
|
|
269
|
-
export const getHtmlElPadding = (el) => {
|
|
270
|
-
const styles = getComputedStyle(el);
|
|
271
|
-
const left = parseFloat(styles.paddingLeft) || 0;
|
|
272
|
-
const right = parseFloat(styles.paddingRight) || 0;
|
|
273
|
-
const top = parseFloat(styles.paddingTop) || 0;
|
|
274
|
-
const bottom = parseFloat(styles.paddingBottom) || 0;
|
|
275
|
-
const x = left + right;
|
|
276
|
-
const y = top + bottom;
|
|
277
|
-
return { x, y, left, right, top, bottom };
|
|
278
|
-
};
|
|
192
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
279
193
|
/**
|
|
280
194
|
* Installs a script that toggles CSS classes on a given element
|
|
281
195
|
* based on the page's visibility or focus state, and optionally
|
|
282
196
|
* triggers callbacks on visibility changes.
|
|
283
197
|
*
|
|
284
198
|
* @param {Object} [settings={}]
|
|
285
|
-
* @param {
|
|
199
|
+
* @param {Element} [settings.element=document.body] - The element to receive visibility classes.
|
|
286
200
|
* @param {string} [settings.hiddenClass='windowHidden'] - CSS class applied when the page is hidden.
|
|
287
201
|
* @param {string} [settings.visibleClass='windowVisible'] - CSS class applied when the page is visible.
|
|
288
202
|
* @param {() => void} [settings.onVisible] - Callback called when page becomes visible.
|
|
@@ -291,8 +205,8 @@ export const getHtmlElPadding = (el) => {
|
|
|
291
205
|
* @throws {TypeError} If any provided setting is invalid.
|
|
292
206
|
*/
|
|
293
207
|
export function installWindowHiddenScript({ element = document.body, hiddenClass = 'windowHidden', visibleClass = 'windowVisible', onVisible, onHidden, } = {}) {
|
|
294
|
-
if (!(element instanceof
|
|
295
|
-
throw new TypeError(`"element" must be an instance of
|
|
208
|
+
if (!(element instanceof Element))
|
|
209
|
+
throw new TypeError(`"element" must be an instance of Element.`);
|
|
296
210
|
if (typeof hiddenClass !== 'string')
|
|
297
211
|
throw new TypeError(`"hiddenClass" must be a string.`);
|
|
298
212
|
if (typeof visibleClass !== 'string')
|