tiny-essentials 1.14.0 → 1.15.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.
@@ -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
+ }
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var objFilter = require('./objFilter.cjs');
4
+ var collision = require('./collision.cjs');
4
5
 
5
6
  /**
6
7
  * Checks if two DOM elements are colliding on the screen.
@@ -12,13 +13,55 @@ var objFilter = require('./objFilter.cjs');
12
13
  function areHtmlElsColliding(elem1, elem2) {
13
14
  const rect1 = elem1.getBoundingClientRect();
14
15
  const rect2 = elem2.getBoundingClientRect();
16
+ return collision.areElsColliding(rect1, rect2);
17
+ }
15
18
 
16
- return !(
17
- rect1.right < rect2.left ||
18
- rect1.left > rect2.right ||
19
- rect1.bottom < rect2.top ||
20
- rect1.top > rect2.bottom
21
- );
19
+ /**
20
+ * Checks if two DOM elements are colliding on the screen, and locks the collision
21
+ * until the element exits through the same side it entered.
22
+ *
23
+ * @param {Element} elem1 - First DOM element (e.g. draggable or moving element).
24
+ * @param {Element} elem2 - Second DOM element (e.g. a container or boundary element).
25
+ * @param {'top'|'bottom'|'left'|'right'} lockDirection - Direction that must be respected to unlock the collision.
26
+ * @param {WeakMap<Element, string>} stateMap - A shared WeakMap to track persistent entry direction per element.
27
+ * @returns {boolean} True if collision is still active.
28
+ */
29
+ function areHtmlElsCollidingWithLock(elem1, elem2, lockDirection, stateMap) {
30
+ const rect1 = elem1.getBoundingClientRect();
31
+ const rect2 = elem2.getBoundingClientRect();
32
+ const isColliding = collision.areElsColliding(rect1, rect2);
33
+
34
+ if (isColliding) {
35
+ // Save entry direction
36
+ if (!stateMap.has(elem1)) {
37
+ stateMap.set(elem1, lockDirection);
38
+ }
39
+ return true;
40
+ }
41
+
42
+ // Handle unlock logic
43
+ if (stateMap.has(elem1)) {
44
+ const lastDirection = stateMap.get(elem1);
45
+
46
+ switch (lastDirection) {
47
+ case 'top':
48
+ if (collision.areElsCollTop(rect1, rect2)) stateMap.delete(elem1); // exited from top
49
+ break;
50
+ case 'bottom':
51
+ if (collision.areElsCollBottom(rect1, rect2)) stateMap.delete(elem1); // exited from bottom
52
+ break;
53
+ case 'left':
54
+ if (collision.areElsCollLeft(rect1, rect2)) stateMap.delete(elem1); // exited from left
55
+ break;
56
+ case 'right':
57
+ if (collision.areElsCollRight(rect1, rect2)) stateMap.delete(elem1); // exited from right
58
+ break;
59
+ }
60
+
61
+ return stateMap.has(elem1); // still colliding (locked)
62
+ }
63
+
64
+ return false;
22
65
  }
23
66
 
24
67
  /**
@@ -447,13 +490,48 @@ function installWindowHiddenScript({
447
490
  return uninstall;
448
491
  }
449
492
 
493
+ /**
494
+ * Checks if the given element is at least partially visible in the viewport.
495
+ *
496
+ * @param {HTMLElement} element - The DOM element to check.
497
+ * @returns {boolean} True if the element is partially in the viewport, false otherwise.
498
+ */
499
+ function isInViewport(element) {
500
+ const elementTop = element.offsetTop;
501
+ const elementBottom = elementTop + element.offsetHeight;
502
+
503
+ const viewportTop = window.scrollY;
504
+ const viewportBottom = viewportTop + window.innerHeight;
505
+
506
+ return elementBottom > viewportTop && elementTop < viewportBottom;
507
+ }
508
+
509
+ /**
510
+ * Checks if the given element is fully visible in the viewport (top and bottom).
511
+ *
512
+ * @param {HTMLElement} element - The DOM element to check.
513
+ * @returns {boolean} True if the element is fully visible in the viewport, false otherwise.
514
+ */
515
+ function isScrolledIntoView(element) {
516
+ const viewportTop = window.scrollY;
517
+ const viewportBottom = viewportTop + window.innerHeight;
518
+
519
+ const elemTop = element.offsetTop;
520
+ const elemBottom = elemTop + element.offsetHeight;
521
+
522
+ return elemBottom <= viewportBottom && elemTop >= viewportTop;
523
+ }
524
+
450
525
  exports.areHtmlElsColliding = areHtmlElsColliding;
526
+ exports.areHtmlElsCollidingWithLock = areHtmlElsCollidingWithLock;
451
527
  exports.fetchJson = fetchJson;
452
528
  exports.getHtmlElBorders = getHtmlElBorders;
453
529
  exports.getHtmlElBordersWidth = getHtmlElBordersWidth;
454
530
  exports.getHtmlElMargin = getHtmlElMargin;
455
531
  exports.getHtmlElPadding = getHtmlElPadding;
456
532
  exports.installWindowHiddenScript = installWindowHiddenScript;
533
+ exports.isInViewport = isInViewport;
534
+ exports.isScrolledIntoView = isScrolledIntoView;
457
535
  exports.readBase64Blob = readBase64Blob;
458
536
  exports.readFileBlob = readFileBlob;
459
537
  exports.readJsonBlob = readJsonBlob;
@@ -6,6 +6,17 @@
6
6
  * @returns {boolean} - Returns true if the elements are colliding.
7
7
  */
8
8
  export function areHtmlElsColliding(elem1: Element, elem2: Element): boolean;
9
+ /**
10
+ * Checks if two DOM elements are colliding on the screen, and locks the collision
11
+ * until the element exits through the same side it entered.
12
+ *
13
+ * @param {Element} elem1 - First DOM element (e.g. draggable or moving element).
14
+ * @param {Element} elem2 - Second DOM element (e.g. a container or boundary element).
15
+ * @param {'top'|'bottom'|'left'|'right'} lockDirection - Direction that must be respected to unlock the collision.
16
+ * @param {WeakMap<Element, string>} stateMap - A shared WeakMap to track persistent entry direction per element.
17
+ * @returns {boolean} True if collision is still active.
18
+ */
19
+ export function areHtmlElsCollidingWithLock(elem1: Element, elem2: Element, lockDirection: "top" | "bottom" | "left" | "right", stateMap: WeakMap<Element, string>): boolean;
9
20
  /**
10
21
  * Reads the contents of a file using the specified FileReader method.
11
22
  *
@@ -96,6 +107,20 @@ export function installWindowHiddenScript({ element, hiddenClass, visibleClass,
96
107
  onVisible?: (() => void) | undefined;
97
108
  onHidden?: (() => void) | undefined;
98
109
  }): () => void;
110
+ /**
111
+ * Checks if the given element is at least partially visible in the viewport.
112
+ *
113
+ * @param {HTMLElement} element - The DOM element to check.
114
+ * @returns {boolean} True if the element is partially in the viewport, false otherwise.
115
+ */
116
+ export function isInViewport(element: HTMLElement): boolean;
117
+ /**
118
+ * Checks if the given element is fully visible in the viewport (top and bottom).
119
+ *
120
+ * @param {HTMLElement} element - The DOM element to check.
121
+ * @returns {boolean} True if the element is fully visible in the viewport, false otherwise.
122
+ */
123
+ export function isScrolledIntoView(element: HTMLElement): boolean;
99
124
  export function getHtmlElBordersWidth(el: Element): HtmlElBoxSides;
100
125
  export function getHtmlElBorders(el: Element): HtmlElBoxSides;
101
126
  export function getHtmlElMargin(el: Element): HtmlElBoxSides;
@@ -1,4 +1,5 @@
1
1
  import { isJsonObject } from './objFilter.mjs';
2
+ import { areElsColliding, areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, } from './collision.mjs';
2
3
  /**
3
4
  * Checks if two DOM elements are colliding on the screen.
4
5
  *
@@ -9,10 +10,53 @@ import { isJsonObject } from './objFilter.mjs';
9
10
  export function areHtmlElsColliding(elem1, elem2) {
10
11
  const rect1 = elem1.getBoundingClientRect();
11
12
  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);
13
+ return areElsColliding(rect1, rect2);
14
+ }
15
+ /**
16
+ * Checks if two DOM elements are colliding on the screen, and locks the collision
17
+ * until the element exits through the same side it entered.
18
+ *
19
+ * @param {Element} elem1 - First DOM element (e.g. draggable or moving element).
20
+ * @param {Element} elem2 - Second DOM element (e.g. a container or boundary element).
21
+ * @param {'top'|'bottom'|'left'|'right'} lockDirection - Direction that must be respected to unlock the collision.
22
+ * @param {WeakMap<Element, string>} stateMap - A shared WeakMap to track persistent entry direction per element.
23
+ * @returns {boolean} True if collision is still active.
24
+ */
25
+ export function areHtmlElsCollidingWithLock(elem1, elem2, lockDirection, stateMap) {
26
+ const rect1 = elem1.getBoundingClientRect();
27
+ const rect2 = elem2.getBoundingClientRect();
28
+ const isColliding = areElsColliding(rect1, rect2);
29
+ if (isColliding) {
30
+ // Save entry direction
31
+ if (!stateMap.has(elem1)) {
32
+ stateMap.set(elem1, lockDirection);
33
+ }
34
+ return true;
35
+ }
36
+ // Handle unlock logic
37
+ if (stateMap.has(elem1)) {
38
+ const lastDirection = stateMap.get(elem1);
39
+ switch (lastDirection) {
40
+ case 'top':
41
+ if (areElsCollTop(rect1, rect2))
42
+ stateMap.delete(elem1); // exited from top
43
+ break;
44
+ case 'bottom':
45
+ if (areElsCollBottom(rect1, rect2))
46
+ stateMap.delete(elem1); // exited from bottom
47
+ break;
48
+ case 'left':
49
+ if (areElsCollLeft(rect1, rect2))
50
+ stateMap.delete(elem1); // exited from left
51
+ break;
52
+ case 'right':
53
+ if (areElsCollRight(rect1, rect2))
54
+ stateMap.delete(elem1); // exited from right
55
+ break;
56
+ }
57
+ return stateMap.has(elem1); // still colliding (locked)
58
+ }
59
+ return false;
16
60
  }
17
61
  /**
18
62
  * Reads the contents of a file using the specified FileReader method.
@@ -385,3 +429,29 @@ export function installWindowHiddenScript({ element = document.body, hiddenClass
385
429
  handler(simulatedEvent);
386
430
  return uninstall;
387
431
  }
432
+ /**
433
+ * Checks if the given element is at least partially visible in the viewport.
434
+ *
435
+ * @param {HTMLElement} element - The DOM element to check.
436
+ * @returns {boolean} True if the element is partially in the viewport, false otherwise.
437
+ */
438
+ export function isInViewport(element) {
439
+ const elementTop = element.offsetTop;
440
+ const elementBottom = elementTop + element.offsetHeight;
441
+ const viewportTop = window.scrollY;
442
+ const viewportBottom = viewportTop + window.innerHeight;
443
+ return elementBottom > viewportTop && elementTop < viewportBottom;
444
+ }
445
+ /**
446
+ * Checks if the given element is fully visible in the viewport (top and bottom).
447
+ *
448
+ * @param {HTMLElement} element - The DOM element to check.
449
+ * @returns {boolean} True if the element is fully visible in the viewport, false otherwise.
450
+ */
451
+ export function isScrolledIntoView(element) {
452
+ const viewportTop = window.scrollY;
453
+ const viewportBottom = viewportTop + window.innerHeight;
454
+ const elemTop = element.offsetTop;
455
+ const elemBottom = elemTop + element.offsetHeight;
456
+ return elemBottom <= viewportBottom && elemTop >= viewportTop;
457
+ }
@@ -9,6 +9,7 @@ var objFilter = require('./objFilter.cjs');
9
9
  var fullScreen = require('./fullScreen.cjs');
10
10
  var simpleMath = require('./simpleMath.cjs');
11
11
  var text = require('./text.cjs');
12
+ var collision = require('./collision.cjs');
12
13
 
13
14
 
14
15
 
@@ -26,6 +27,8 @@ exports.getHtmlElBordersWidth = html.getHtmlElBordersWidth;
26
27
  exports.getHtmlElMargin = html.getHtmlElMargin;
27
28
  exports.getHtmlElPadding = html.getHtmlElPadding;
28
29
  exports.installWindowHiddenScript = html.installWindowHiddenScript;
30
+ exports.isInViewport = html.isInViewport;
31
+ exports.isScrolledIntoView = html.isScrolledIntoView;
29
32
  exports.readBase64Blob = html.readBase64Blob;
30
33
  exports.readFileBlob = html.readFileBlob;
31
34
  exports.readJsonBlob = html.readJsonBlob;
@@ -52,3 +55,21 @@ exports.addAiMarkerShortcut = text.addAiMarkerShortcut;
52
55
  exports.safeTextTrim = text.safeTextTrim;
53
56
  exports.toTitleCase = text.toTitleCase;
54
57
  exports.toTitleCaseLowerFirst = text.toTitleCaseLowerFirst;
58
+ exports.areElsCollBottom = collision.areElsCollBottom;
59
+ exports.areElsCollLeft = collision.areElsCollLeft;
60
+ exports.areElsCollPerfBottom = collision.areElsCollPerfBottom;
61
+ exports.areElsCollPerfLeft = collision.areElsCollPerfLeft;
62
+ exports.areElsCollPerfRight = collision.areElsCollPerfRight;
63
+ exports.areElsCollPerfTop = collision.areElsCollPerfTop;
64
+ exports.areElsCollRight = collision.areElsCollRight;
65
+ exports.areElsCollTop = collision.areElsCollTop;
66
+ exports.areElsColliding = collision.areElsColliding;
67
+ exports.areElsPerfColliding = collision.areElsPerfColliding;
68
+ exports.getElsCollDetails = collision.getElsCollDetails;
69
+ exports.getElsCollDirDepth = collision.getElsCollDirDepth;
70
+ exports.getElsCollOverlap = collision.getElsCollOverlap;
71
+ exports.getElsCollOverlapPos = collision.getElsCollOverlapPos;
72
+ exports.getElsColliding = collision.getElsColliding;
73
+ exports.getElsPerfColliding = collision.getElsPerfColliding;
74
+ exports.getElsRelativeCenterOffset = collision.getElsRelativeCenterOffset;
75
+ exports.getRectCenter = collision.getRectCenter;
@@ -1,3 +1,23 @@
1
+ import { areElsCollTop } from './collision.mjs';
2
+ import { areElsCollBottom } from './collision.mjs';
3
+ import { areElsCollLeft } from './collision.mjs';
4
+ import { areElsCollRight } from './collision.mjs';
5
+ import { areElsCollPerfTop } from './collision.mjs';
6
+ import { areElsCollPerfBottom } from './collision.mjs';
7
+ import { areElsCollPerfLeft } from './collision.mjs';
8
+ import { areElsCollPerfRight } from './collision.mjs';
9
+ import { areElsColliding } from './collision.mjs';
10
+ import { areElsPerfColliding } from './collision.mjs';
11
+ import { getElsColliding } from './collision.mjs';
12
+ import { getElsPerfColliding } from './collision.mjs';
13
+ import { getElsCollOverlap } from './collision.mjs';
14
+ import { getElsCollOverlapPos } from './collision.mjs';
15
+ import { getRectCenter } from './collision.mjs';
16
+ import { getElsRelativeCenterOffset } from './collision.mjs';
17
+ import { getElsCollDirDepth } from './collision.mjs';
18
+ import { getElsCollDetails } from './collision.mjs';
19
+ import { isInViewport } from './html.mjs';
20
+ import { isScrolledIntoView } from './html.mjs';
1
21
  import { safeTextTrim } from './text.mjs';
2
22
  import { installWindowHiddenScript } from './html.mjs';
3
23
  import { genFibonacciSeq } from './simpleMath.mjs';
@@ -38,5 +58,5 @@ import { getTimeDuration } from './clock.mjs';
38
58
  import { shuffleArray } from './array.mjs';
39
59
  import { toTitleCase } from './text.mjs';
40
60
  import { toTitleCaseLowerFirst } from './text.mjs';
41
- export { safeTextTrim, installWindowHiddenScript, genFibonacciSeq, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, fetchJson, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst };
61
+ export { areElsCollTop, areElsCollBottom, areElsCollLeft, areElsCollRight, areElsCollPerfTop, areElsCollPerfBottom, areElsCollPerfLeft, areElsCollPerfRight, areElsColliding, areElsPerfColliding, getElsColliding, getElsPerfColliding, getElsCollOverlap, getElsCollOverlapPos, getRectCenter, getElsRelativeCenterOffset, getElsCollDirDepth, getElsCollDetails, isInViewport, isScrolledIntoView, safeTextTrim, installWindowHiddenScript, genFibonacciSeq, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, fetchJson, readJsonBlob, readFileBlob, readBase64Blob, saveJsonFile, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst };
42
62
  //# sourceMappingURL=index.d.mts.map