@vdegenne/highlight-manager 0.1.6 → 0.1.8
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 +21 -3
- package/lib/index.js +19 -7
- package/lib/utils.d.ts +2 -1
- package/lib/utils.js +21 -11
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IsInViewportPartCheck } from './utils.js';
|
|
1
2
|
interface Info {
|
|
2
3
|
elements: HTMLElement[];
|
|
3
4
|
highlightIndexStart: number;
|
|
@@ -9,6 +10,20 @@ interface Info {
|
|
|
9
10
|
highlightElement: HTMLElement | undefined;
|
|
10
11
|
highlightContent: string | undefined;
|
|
11
12
|
}
|
|
13
|
+
interface ScrollStrategy {
|
|
14
|
+
/**
|
|
15
|
+
* @default 'center'
|
|
16
|
+
*/
|
|
17
|
+
whenWhatPartIsHidden: IsInViewportPartCheck;
|
|
18
|
+
/**
|
|
19
|
+
* @default 'smooth'
|
|
20
|
+
*/
|
|
21
|
+
behavior: ScrollBehavior;
|
|
22
|
+
/**
|
|
23
|
+
* @default 'start'
|
|
24
|
+
*/
|
|
25
|
+
logicalPosition: ScrollLogicalPosition;
|
|
26
|
+
}
|
|
12
27
|
interface Options {
|
|
13
28
|
css: string;
|
|
14
29
|
highlightTextColor: string;
|
|
@@ -32,9 +47,9 @@ interface Options {
|
|
|
32
47
|
*/
|
|
33
48
|
applyStyleSheetTo: Document | HTMLElement | ShadowRoot;
|
|
34
49
|
/**
|
|
35
|
-
* @default
|
|
50
|
+
* @default undefined
|
|
36
51
|
*/
|
|
37
|
-
|
|
52
|
+
scrollStrategy: ScrollStrategy | undefined;
|
|
38
53
|
/**
|
|
39
54
|
* If true, will select the next visible candidate if the highlight is offscreen.
|
|
40
55
|
*
|
|
@@ -43,6 +58,9 @@ interface Options {
|
|
|
43
58
|
fastTravel: boolean;
|
|
44
59
|
}
|
|
45
60
|
export declare function setGlobalBeforeHighlight(fct: () => void): void;
|
|
61
|
+
interface HighlightOptions {
|
|
62
|
+
scrollStrategy: ScrollStrategy | undefined;
|
|
63
|
+
}
|
|
46
64
|
export declare class HighLightManager {
|
|
47
65
|
#private;
|
|
48
66
|
protected selector: string;
|
|
@@ -60,7 +78,7 @@ export declare class HighLightManager {
|
|
|
60
78
|
/**
|
|
61
79
|
* @returns {boolean} true if the highlight succeeded, false otherwise.
|
|
62
80
|
*/
|
|
63
|
-
highlight(start: number, end?: number, unhighlightAll?: boolean, cache?: boolean): boolean;
|
|
81
|
+
highlight(start: number, end?: number, unhighlightAll?: boolean, cache?: boolean, options?: HighlightOptions): boolean;
|
|
64
82
|
previous(step?: number, cache?: boolean): void;
|
|
65
83
|
next(step?: number, cache?: boolean): void;
|
|
66
84
|
extendLeftHighlight(step?: number, cache?: boolean): void;
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { querySelectorAll } from 'html-vision';
|
|
2
2
|
import { isInViewport, sleep } from './utils.js';
|
|
3
|
+
const scrollStrategyDefaults = {
|
|
4
|
+
whenWhatPartIsHidden: 'center',
|
|
5
|
+
behavior: 'smooth',
|
|
6
|
+
logicalPosition: 'start',
|
|
7
|
+
};
|
|
3
8
|
const defaults = {
|
|
4
9
|
atomicSelection(_element) {
|
|
5
10
|
return true;
|
|
@@ -14,7 +19,7 @@ const defaults = {
|
|
|
14
19
|
beforeHighlight: undefined,
|
|
15
20
|
onSelectionChange: undefined,
|
|
16
21
|
applyStyleSheetTo: document,
|
|
17
|
-
|
|
22
|
+
scrollStrategy: undefined,
|
|
18
23
|
fastTravel: false,
|
|
19
24
|
};
|
|
20
25
|
// Local array of all declared highlighters for id control.
|
|
@@ -150,7 +155,7 @@ export class HighLightManager {
|
|
|
150
155
|
/**
|
|
151
156
|
* @returns {boolean} true if the highlight succeeded, false otherwise.
|
|
152
157
|
*/
|
|
153
|
-
highlight(start, end, unhighlightAll = true, cache = false) {
|
|
158
|
+
highlight(start, end, unhighlightAll = true, cache = false, options) {
|
|
154
159
|
if (end === undefined) {
|
|
155
160
|
end = start;
|
|
156
161
|
}
|
|
@@ -166,6 +171,13 @@ export class HighLightManager {
|
|
|
166
171
|
return false;
|
|
167
172
|
}
|
|
168
173
|
// console.log(highlightIndexStart, highlightIndexEnd, start, end)
|
|
174
|
+
const _options = {
|
|
175
|
+
scrollStrategy: {
|
|
176
|
+
...scrollStrategyDefaults,
|
|
177
|
+
...this.#options.scrollStrategy,
|
|
178
|
+
...options?.scrollStrategy,
|
|
179
|
+
},
|
|
180
|
+
};
|
|
169
181
|
globalBeforeHighlight?.();
|
|
170
182
|
this.#options.beforeHighlight?.();
|
|
171
183
|
// playClick()
|
|
@@ -176,12 +188,12 @@ export class HighLightManager {
|
|
|
176
188
|
if (elementsToHighlight.length === 0) {
|
|
177
189
|
return false;
|
|
178
190
|
}
|
|
179
|
-
if (
|
|
180
|
-
!isInViewport(elementsToHighlight[0])) {
|
|
191
|
+
if (_options.scrollStrategy &&
|
|
192
|
+
!isInViewport(elementsToHighlight[0], _options.scrollStrategy.whenWhatPartIsHidden)) {
|
|
181
193
|
elementsToHighlight[0]?.scrollIntoView({
|
|
182
|
-
behavior:
|
|
183
|
-
block:
|
|
184
|
-
inline:
|
|
194
|
+
behavior: _options.scrollStrategy.behavior,
|
|
195
|
+
block: _options.scrollStrategy.logicalPosition,
|
|
196
|
+
inline: _options.scrollStrategy.logicalPosition,
|
|
185
197
|
});
|
|
186
198
|
}
|
|
187
199
|
elementsToHighlight.forEach((el) => el.setAttribute(`highlight${this.#id}`, ''));
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export declare function sleep(milli?: number): Promise<unknown>;
|
|
2
|
-
export
|
|
2
|
+
export type IsInViewportPartCheck = 'top' | 'center' | 'bounding-rect' | 'bottom';
|
|
3
|
+
export declare function isInViewport(el: HTMLElement, partCheck?: IsInViewportPartCheck): boolean;
|
|
3
4
|
//# sourceMappingURL=utils.d.ts.map
|
package/lib/utils.js
CHANGED
|
@@ -1,19 +1,29 @@
|
|
|
1
1
|
export function sleep(milli = 1000) {
|
|
2
2
|
return new Promise((r) => setTimeout(r, milli));
|
|
3
3
|
}
|
|
4
|
-
|
|
5
|
-
// return (
|
|
6
|
-
// el.getBoundingClientRect().top >= 0 &&
|
|
7
|
-
// el.getBoundingClientRect().bottom <= window.innerHeight
|
|
8
|
-
// )
|
|
9
|
-
// }
|
|
10
|
-
export function isInViewport(el) {
|
|
4
|
+
export function isInViewport(el, partCheck = 'center') {
|
|
11
5
|
const rect = el.getBoundingClientRect();
|
|
12
6
|
const viewHeight = window.innerHeight || document.documentElement.clientHeight;
|
|
13
7
|
const viewWidth = window.innerWidth || document.documentElement.clientWidth;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
switch (partCheck) {
|
|
9
|
+
case 'top':
|
|
10
|
+
return rect.top >= 0 && rect.top <= viewHeight;
|
|
11
|
+
case 'bottom':
|
|
12
|
+
return rect.bottom >= 0 && rect.bottom <= viewHeight;
|
|
13
|
+
case 'center': {
|
|
14
|
+
const centerY = rect.top + rect.height / 2;
|
|
15
|
+
const centerX = rect.left + rect.width / 2;
|
|
16
|
+
return (centerY >= 0 &&
|
|
17
|
+
centerY <= viewHeight &&
|
|
18
|
+
centerX >= 0 &&
|
|
19
|
+
centerX <= viewWidth);
|
|
20
|
+
}
|
|
21
|
+
case 'bounding-rect':
|
|
22
|
+
default:
|
|
23
|
+
return (rect.bottom > 0 &&
|
|
24
|
+
rect.right > 0 &&
|
|
25
|
+
rect.top < viewHeight &&
|
|
26
|
+
rect.left < viewWidth);
|
|
27
|
+
}
|
|
18
28
|
}
|
|
19
29
|
//# sourceMappingURL=utils.js.map
|