@vdegenne/highlight-manager 0.1.11 → 0.1.13
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/lib/index.d.ts +6 -3
- package/lib/index.js +11 -9
- package/lib/utils.d.ts +7 -2
- package/lib/utils.js +31 -19
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CheckIf } from './utils.js';
|
|
2
2
|
interface Info {
|
|
3
3
|
elements: HTMLElement[];
|
|
4
4
|
highlightIndexStart: number;
|
|
@@ -12,9 +12,12 @@ interface Info {
|
|
|
12
12
|
}
|
|
13
13
|
interface ScrollStrategy {
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* The visibility check to use to determine
|
|
16
|
+
* whether or not the scroll should be issued.
|
|
17
|
+
*
|
|
18
|
+
* @default when top is not visible
|
|
16
19
|
*/
|
|
17
|
-
|
|
20
|
+
when: CheckIf;
|
|
18
21
|
/**
|
|
19
22
|
* @default 'smooth'
|
|
20
23
|
*/
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { querySelectorAll } from 'html-vision';
|
|
2
|
-
import {
|
|
2
|
+
import { checkVisibility, sleep } from './utils.js';
|
|
3
3
|
const scrollStrategyDefaults = {
|
|
4
|
-
|
|
4
|
+
when: (is) => !is('top-visible'),
|
|
5
5
|
behavior: 'smooth',
|
|
6
6
|
block: undefined,
|
|
7
7
|
inline: undefined,
|
|
@@ -192,7 +192,7 @@ export class HighLightManager {
|
|
|
192
192
|
return false;
|
|
193
193
|
}
|
|
194
194
|
if (_options.scrollStrategy &&
|
|
195
|
-
!
|
|
195
|
+
!checkVisibility(elementsToHighlight[0], _options.scrollStrategy.when)) {
|
|
196
196
|
elementsToHighlight[0]?.scrollIntoView({
|
|
197
197
|
behavior: _options.scrollStrategy.behavior,
|
|
198
198
|
block: _options.scrollStrategy.block,
|
|
@@ -218,7 +218,7 @@ export class HighLightManager {
|
|
|
218
218
|
: highlightIndexStart;
|
|
219
219
|
if (currIndex === -1) {
|
|
220
220
|
if (this.#options.fastTravel) {
|
|
221
|
-
const found = [...elements].reverse().find((el) =>
|
|
221
|
+
const found = [...elements].reverse().find((el) => checkVisibility(el));
|
|
222
222
|
if (found) {
|
|
223
223
|
const i = elements.indexOf(found);
|
|
224
224
|
this.highlight(i, i, true, cache);
|
|
@@ -229,7 +229,7 @@ export class HighLightManager {
|
|
|
229
229
|
return;
|
|
230
230
|
}
|
|
231
231
|
const currEl = elements[currIndex];
|
|
232
|
-
const currIsVisible = currEl ?
|
|
232
|
+
const currIsVisible = currEl ? checkVisibility(currEl) : false;
|
|
233
233
|
const currIsBelow = currEl
|
|
234
234
|
? currEl.getBoundingClientRect().top > window.innerHeight
|
|
235
235
|
: false;
|
|
@@ -238,7 +238,7 @@ export class HighLightManager {
|
|
|
238
238
|
const found = elements
|
|
239
239
|
.slice(0, currIndex)
|
|
240
240
|
.reverse()
|
|
241
|
-
.find((el) =>
|
|
241
|
+
.find((el) => checkVisibility(el));
|
|
242
242
|
if (found) {
|
|
243
243
|
prevIndex = elements.indexOf(found);
|
|
244
244
|
}
|
|
@@ -262,7 +262,7 @@ export class HighLightManager {
|
|
|
262
262
|
: highlightIndexEnd;
|
|
263
263
|
if (currIndex === -1) {
|
|
264
264
|
if (this.#options.fastTravel) {
|
|
265
|
-
const found = elements.find((el) =>
|
|
265
|
+
const found = elements.find((el) => checkVisibility(el));
|
|
266
266
|
if (found) {
|
|
267
267
|
const i = elements.indexOf(found);
|
|
268
268
|
this.highlight(i, i, true, cache);
|
|
@@ -273,13 +273,15 @@ export class HighLightManager {
|
|
|
273
273
|
return;
|
|
274
274
|
}
|
|
275
275
|
const currEl = elements[currIndex];
|
|
276
|
-
const currIsVisible = currEl ?
|
|
276
|
+
const currIsVisible = currEl ? checkVisibility(currEl) : false;
|
|
277
277
|
const currIsAbove = currEl
|
|
278
278
|
? currEl.getBoundingClientRect().bottom < 0
|
|
279
279
|
: false;
|
|
280
280
|
let nextIndex = -1;
|
|
281
281
|
if (this.#options.fastTravel && !currIsVisible && currIsAbove) {
|
|
282
|
-
const found = elements
|
|
282
|
+
const found = elements
|
|
283
|
+
.slice(currIndex + 1)
|
|
284
|
+
.find((el) => checkVisibility(el));
|
|
283
285
|
if (found) {
|
|
284
286
|
nextIndex = elements.indexOf(found);
|
|
285
287
|
}
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export declare function sleep(milli?: number): Promise<unknown>;
|
|
2
|
-
export type
|
|
3
|
-
export
|
|
2
|
+
export type VisibilityCheck = 'top-visible' | 'center-visible' | 'bottom-visible' | 'partially-visible' | 'fully-visible';
|
|
3
|
+
export type CheckIf = (is: (visibility: VisibilityCheck) => boolean) => boolean;
|
|
4
|
+
export declare function checkVisibility(el: HTMLElement,
|
|
5
|
+
/**
|
|
6
|
+
* @default top is not visible
|
|
7
|
+
*/
|
|
8
|
+
checkIf?: CheckIf): boolean;
|
|
4
9
|
//# sourceMappingURL=utils.d.ts.map
|
package/lib/utils.js
CHANGED
|
@@ -1,29 +1,41 @@
|
|
|
1
1
|
export function sleep(milli = 1000) {
|
|
2
2
|
return new Promise((r) => setTimeout(r, milli));
|
|
3
3
|
}
|
|
4
|
-
export function
|
|
4
|
+
export function checkVisibility(el,
|
|
5
|
+
/**
|
|
6
|
+
* @default top is not visible
|
|
7
|
+
*/
|
|
8
|
+
checkIf = (is) => !is('top-visible')) {
|
|
5
9
|
const rect = el.getBoundingClientRect();
|
|
6
10
|
const viewHeight = window.innerHeight || document.documentElement.clientHeight;
|
|
7
11
|
const viewWidth = window.innerWidth || document.documentElement.clientWidth;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
function is(visibilityCheck) {
|
|
13
|
+
switch (visibilityCheck) {
|
|
14
|
+
case 'top-visible':
|
|
15
|
+
return rect.top >= 0 && rect.top <= viewHeight;
|
|
16
|
+
case 'center-visible': {
|
|
17
|
+
const centerY = rect.top + rect.height / 2;
|
|
18
|
+
const centerX = rect.left + rect.width / 2;
|
|
19
|
+
return (centerY >= 0 &&
|
|
20
|
+
centerY <= viewHeight &&
|
|
21
|
+
centerX >= 0 &&
|
|
22
|
+
centerX <= viewWidth);
|
|
23
|
+
}
|
|
24
|
+
case 'bottom-visible':
|
|
25
|
+
return rect.bottom >= 0 && rect.bottom <= viewHeight;
|
|
26
|
+
case 'fully-visible':
|
|
27
|
+
return (rect.top >= 0 &&
|
|
28
|
+
rect.left >= 0 &&
|
|
29
|
+
rect.bottom <= viewHeight &&
|
|
30
|
+
rect.right <= viewWidth);
|
|
31
|
+
case 'partially-visible':
|
|
32
|
+
default:
|
|
33
|
+
return (rect.bottom > 0 &&
|
|
34
|
+
rect.right > 0 &&
|
|
35
|
+
rect.top < viewHeight &&
|
|
36
|
+
rect.left < viewWidth);
|
|
20
37
|
}
|
|
21
|
-
case 'bounding-rect':
|
|
22
|
-
default:
|
|
23
|
-
return (rect.bottom > 0 &&
|
|
24
|
-
rect.right > 0 &&
|
|
25
|
-
rect.top < viewHeight &&
|
|
26
|
-
rect.left < viewWidth);
|
|
27
38
|
}
|
|
39
|
+
return checkIf(is);
|
|
28
40
|
}
|
|
29
41
|
//# sourceMappingURL=utils.js.map
|