@vdegenne/highlight-manager 0.1.5 → 0.1.7
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 +7 -3
- package/lib/index.js +51 -37
- package/lib/utils.d.ts +1 -1
- package/lib/utils.js +4 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ interface Info {
|
|
|
9
9
|
highlightElement: HTMLElement | undefined;
|
|
10
10
|
highlightContent: string | undefined;
|
|
11
11
|
}
|
|
12
|
+
type ScrollWhenOffscreenOption = 'never' | 'bounding-rect' | 'top';
|
|
12
13
|
interface Options {
|
|
13
14
|
css: string;
|
|
14
15
|
highlightTextColor: string;
|
|
@@ -32,9 +33,9 @@ interface Options {
|
|
|
32
33
|
*/
|
|
33
34
|
applyStyleSheetTo: Document | HTMLElement | ShadowRoot;
|
|
34
35
|
/**
|
|
35
|
-
* @default
|
|
36
|
+
* @default 'none'
|
|
36
37
|
*/
|
|
37
|
-
scrollWhenOffscreen:
|
|
38
|
+
scrollWhenOffscreen: ScrollWhenOffscreenOption;
|
|
38
39
|
/**
|
|
39
40
|
* If true, will select the next visible candidate if the highlight is offscreen.
|
|
40
41
|
*
|
|
@@ -43,6 +44,9 @@ interface Options {
|
|
|
43
44
|
fastTravel: boolean;
|
|
44
45
|
}
|
|
45
46
|
export declare function setGlobalBeforeHighlight(fct: () => void): void;
|
|
47
|
+
interface HighlightOptions {
|
|
48
|
+
scrollWhenOffscreen: ScrollWhenOffscreenOption;
|
|
49
|
+
}
|
|
46
50
|
export declare class HighLightManager {
|
|
47
51
|
#private;
|
|
48
52
|
protected selector: string;
|
|
@@ -60,7 +64,7 @@ export declare class HighLightManager {
|
|
|
60
64
|
/**
|
|
61
65
|
* @returns {boolean} true if the highlight succeeded, false otherwise.
|
|
62
66
|
*/
|
|
63
|
-
highlight(start: number, end?: number, unhighlightAll?: boolean, cache?: boolean): boolean;
|
|
67
|
+
highlight(start: number, end?: number, unhighlightAll?: boolean, cache?: boolean, options?: HighlightOptions): boolean;
|
|
64
68
|
previous(step?: number, cache?: boolean): void;
|
|
65
69
|
next(step?: number, cache?: boolean): void;
|
|
66
70
|
extendLeftHighlight(step?: number, cache?: boolean): void;
|
package/lib/index.js
CHANGED
|
@@ -14,7 +14,7 @@ const defaults = {
|
|
|
14
14
|
beforeHighlight: undefined,
|
|
15
15
|
onSelectionChange: undefined,
|
|
16
16
|
applyStyleSheetTo: document,
|
|
17
|
-
scrollWhenOffscreen:
|
|
17
|
+
scrollWhenOffscreen: 'never',
|
|
18
18
|
fastTravel: false,
|
|
19
19
|
};
|
|
20
20
|
// Local array of all declared highlighters for id control.
|
|
@@ -75,7 +75,9 @@ export class HighLightManager {
|
|
|
75
75
|
const els = querySelectorAll(this.selector);
|
|
76
76
|
const el = els[index];
|
|
77
77
|
if (el) {
|
|
78
|
-
this.highlight(index, index, true, false
|
|
78
|
+
this.highlight(index, index, true, false, {
|
|
79
|
+
scrollWhenOffscreen: 'never',
|
|
80
|
+
});
|
|
79
81
|
wr.resolve(el);
|
|
80
82
|
this.#highlightWhenAvailablePromiseWR = undefined;
|
|
81
83
|
return;
|
|
@@ -150,7 +152,7 @@ export class HighLightManager {
|
|
|
150
152
|
/**
|
|
151
153
|
* @returns {boolean} true if the highlight succeeded, false otherwise.
|
|
152
154
|
*/
|
|
153
|
-
highlight(start, end, unhighlightAll = true, cache = false) {
|
|
155
|
+
highlight(start, end, unhighlightAll = true, cache = false, options) {
|
|
154
156
|
if (end === undefined) {
|
|
155
157
|
end = start;
|
|
156
158
|
}
|
|
@@ -166,6 +168,9 @@ export class HighLightManager {
|
|
|
166
168
|
return false;
|
|
167
169
|
}
|
|
168
170
|
// console.log(highlightIndexStart, highlightIndexEnd, start, end)
|
|
171
|
+
const _options = {
|
|
172
|
+
scrollWhenOffscreen: options?.scrollWhenOffscreen ?? this.#options.scrollWhenOffscreen,
|
|
173
|
+
};
|
|
169
174
|
globalBeforeHighlight?.();
|
|
170
175
|
this.#options.beforeHighlight?.();
|
|
171
176
|
// playClick()
|
|
@@ -176,12 +181,12 @@ export class HighLightManager {
|
|
|
176
181
|
if (elementsToHighlight.length === 0) {
|
|
177
182
|
return false;
|
|
178
183
|
}
|
|
179
|
-
if (
|
|
180
|
-
!isInViewport(elementsToHighlight[0])) {
|
|
184
|
+
if (_options.scrollWhenOffscreen !== 'never' &&
|
|
185
|
+
!isInViewport(elementsToHighlight[0], _options.scrollWhenOffscreen === 'top')) {
|
|
181
186
|
elementsToHighlight[0]?.scrollIntoView({
|
|
182
187
|
behavior: 'smooth',
|
|
183
|
-
block: 'center',
|
|
184
|
-
inline: 'center',
|
|
188
|
+
block: _options.scrollWhenOffscreen === 'top' ? 'start' : 'center',
|
|
189
|
+
inline: _options.scrollWhenOffscreen === 'top' ? 'start' : 'center',
|
|
185
190
|
});
|
|
186
191
|
}
|
|
187
192
|
elementsToHighlight.forEach((el) => el.setAttribute(`highlight${this.#id}`, ''));
|
|
@@ -201,6 +206,18 @@ export class HighLightManager {
|
|
|
201
206
|
const currIndex = highlightIndexStart !== highlightIndexEnd
|
|
202
207
|
? highlightIndexStart
|
|
203
208
|
: highlightIndexStart;
|
|
209
|
+
if (currIndex === -1) {
|
|
210
|
+
if (this.#options.fastTravel) {
|
|
211
|
+
const found = [...elements].reverse().find((el) => isInViewport(el));
|
|
212
|
+
if (found) {
|
|
213
|
+
const i = elements.indexOf(found);
|
|
214
|
+
this.highlight(i, i, true, cache);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
this.highlight(len - 1, len - 1, true, cache);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
204
221
|
const currEl = elements[currIndex];
|
|
205
222
|
const currIsVisible = currEl ? isInViewport(currEl) : false;
|
|
206
223
|
const currIsBelow = currEl
|
|
@@ -208,24 +225,18 @@ export class HighLightManager {
|
|
|
208
225
|
: false;
|
|
209
226
|
let prevIndex = -1;
|
|
210
227
|
if (this.#options.fastTravel && !currIsVisible && currIsBelow) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
228
|
+
const found = elements
|
|
229
|
+
.slice(0, currIndex)
|
|
230
|
+
.reverse()
|
|
231
|
+
.find((el) => isInViewport(el));
|
|
232
|
+
if (found) {
|
|
233
|
+
prevIndex = elements.indexOf(found);
|
|
217
234
|
}
|
|
218
235
|
}
|
|
219
236
|
if (prevIndex === -1) {
|
|
220
|
-
|
|
221
|
-
?
|
|
222
|
-
:
|
|
223
|
-
if (this.#options.loop) {
|
|
224
|
-
prevIndex = (base - step + len) % len;
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
prevIndex = Math.max(0, base - step);
|
|
228
|
-
}
|
|
237
|
+
prevIndex = this.#options.loop
|
|
238
|
+
? (currIndex - step + len) % len
|
|
239
|
+
: Math.max(0, currIndex - step);
|
|
229
240
|
}
|
|
230
241
|
this.highlight(prevIndex, prevIndex, true, cache);
|
|
231
242
|
}
|
|
@@ -239,6 +250,18 @@ export class HighLightManager {
|
|
|
239
250
|
const currIndex = highlightIndexStart !== highlightIndexEnd
|
|
240
251
|
? highlightIndexEnd
|
|
241
252
|
: highlightIndexEnd;
|
|
253
|
+
if (currIndex === -1) {
|
|
254
|
+
if (this.#options.fastTravel) {
|
|
255
|
+
const found = elements.find((el) => isInViewport(el));
|
|
256
|
+
if (found) {
|
|
257
|
+
const i = elements.indexOf(found);
|
|
258
|
+
this.highlight(i, i, true, cache);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
this.highlight(0, 0, true, cache);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
242
265
|
const currEl = elements[currIndex];
|
|
243
266
|
const currIsVisible = currEl ? isInViewport(currEl) : false;
|
|
244
267
|
const currIsAbove = currEl
|
|
@@ -246,24 +269,15 @@ export class HighLightManager {
|
|
|
246
269
|
: false;
|
|
247
270
|
let nextIndex = -1;
|
|
248
271
|
if (this.#options.fastTravel && !currIsVisible && currIsAbove) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
nextIndex = i;
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
272
|
+
const found = elements.slice(currIndex + 1).find((el) => isInViewport(el));
|
|
273
|
+
if (found) {
|
|
274
|
+
nextIndex = elements.indexOf(found);
|
|
255
275
|
}
|
|
256
276
|
}
|
|
257
277
|
if (nextIndex === -1) {
|
|
258
|
-
|
|
259
|
-
?
|
|
260
|
-
:
|
|
261
|
-
if (this.#options.loop) {
|
|
262
|
-
nextIndex = (base + step) % len;
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
nextIndex = Math.min(len - 1, base + step);
|
|
266
|
-
}
|
|
278
|
+
nextIndex = this.#options.loop
|
|
279
|
+
? (currIndex + step) % len
|
|
280
|
+
: Math.min(len - 1, currIndex + step);
|
|
267
281
|
}
|
|
268
282
|
this.highlight(nextIndex, nextIndex, true, cache);
|
|
269
283
|
}
|
package/lib/utils.d.ts
CHANGED
package/lib/utils.js
CHANGED
|
@@ -7,10 +7,13 @@ export function sleep(milli = 1000) {
|
|
|
7
7
|
// el.getBoundingClientRect().bottom <= window.innerHeight
|
|
8
8
|
// )
|
|
9
9
|
// }
|
|
10
|
-
export function isInViewport(el) {
|
|
10
|
+
export function isInViewport(el, onlyTop = false) {
|
|
11
11
|
const rect = el.getBoundingClientRect();
|
|
12
12
|
const viewHeight = window.innerHeight || document.documentElement.clientHeight;
|
|
13
13
|
const viewWidth = window.innerWidth || document.documentElement.clientWidth;
|
|
14
|
+
if (onlyTop) {
|
|
15
|
+
return rect.top >= 0 && rect.top <= viewHeight;
|
|
16
|
+
}
|
|
14
17
|
return (rect.bottom > 0 &&
|
|
15
18
|
rect.right > 0 &&
|
|
16
19
|
rect.top < viewHeight &&
|