@vdegenne/highlight-manager 0.1.3 → 0.1.5
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 -0
- package/lib/index.js +59 -10
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -35,6 +35,12 @@ interface Options {
|
|
|
35
35
|
* @default false
|
|
36
36
|
*/
|
|
37
37
|
scrollWhenOffscreen: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* If true, will select the next visible candidate if the highlight is offscreen.
|
|
40
|
+
*
|
|
41
|
+
* @default false
|
|
42
|
+
*/
|
|
43
|
+
fastTravel: boolean;
|
|
38
44
|
}
|
|
39
45
|
export declare function setGlobalBeforeHighlight(fct: () => void): void;
|
|
40
46
|
export declare class HighLightManager {
|
package/lib/index.js
CHANGED
|
@@ -15,6 +15,7 @@ const defaults = {
|
|
|
15
15
|
onSelectionChange: undefined,
|
|
16
16
|
applyStyleSheetTo: document,
|
|
17
17
|
scrollWhenOffscreen: false,
|
|
18
|
+
fastTravel: false,
|
|
18
19
|
};
|
|
19
20
|
// Local array of all declared highlighters for id control.
|
|
20
21
|
const highlighters = [];
|
|
@@ -175,7 +176,7 @@ export class HighLightManager {
|
|
|
175
176
|
if (elementsToHighlight.length === 0) {
|
|
176
177
|
return false;
|
|
177
178
|
}
|
|
178
|
-
if (this.#options.scrollWhenOffscreen
|
|
179
|
+
if (this.#options.scrollWhenOffscreen &&
|
|
179
180
|
!isInViewport(elementsToHighlight[0])) {
|
|
180
181
|
elementsToHighlight[0]?.scrollIntoView({
|
|
181
182
|
behavior: 'smooth',
|
|
@@ -197,12 +198,36 @@ export class HighLightManager {
|
|
|
197
198
|
this.highlight(-1, -1, true, cache);
|
|
198
199
|
return;
|
|
199
200
|
}
|
|
200
|
-
|
|
201
|
+
const currIndex = highlightIndexStart !== highlightIndexEnd
|
|
201
202
|
? highlightIndexStart
|
|
202
|
-
:
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
: highlightIndexStart;
|
|
204
|
+
const currEl = elements[currIndex];
|
|
205
|
+
const currIsVisible = currEl ? isInViewport(currEl) : false;
|
|
206
|
+
const currIsBelow = currEl
|
|
207
|
+
? currEl.getBoundingClientRect().top > window.innerHeight
|
|
208
|
+
: false;
|
|
209
|
+
let prevIndex = -1;
|
|
210
|
+
if (this.#options.fastTravel && !currIsVisible && currIsBelow) {
|
|
211
|
+
for (let i = currIndex - 1; i >= 0; i--) {
|
|
212
|
+
const el = elements[i];
|
|
213
|
+
if (el && isInViewport(el)) {
|
|
214
|
+
prevIndex = i;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (prevIndex === -1) {
|
|
220
|
+
const base = highlightIndexStart !== highlightIndexEnd
|
|
221
|
+
? highlightIndexStart
|
|
222
|
+
: highlightIndexStart;
|
|
223
|
+
if (this.#options.loop) {
|
|
224
|
+
prevIndex = (base - step + len) % len;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
prevIndex = Math.max(0, base - step);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
this.highlight(prevIndex, prevIndex, true, cache);
|
|
206
231
|
}
|
|
207
232
|
next(step = 1, cache = false) {
|
|
208
233
|
const { elements, highlightIndexStart, highlightIndexEnd } = this.getInfo(cache);
|
|
@@ -211,11 +236,35 @@ export class HighLightManager {
|
|
|
211
236
|
this.highlight(-1, -1, true, cache);
|
|
212
237
|
return;
|
|
213
238
|
}
|
|
214
|
-
|
|
239
|
+
const currIndex = highlightIndexStart !== highlightIndexEnd
|
|
215
240
|
? highlightIndexEnd
|
|
216
|
-
:
|
|
217
|
-
|
|
218
|
-
|
|
241
|
+
: highlightIndexEnd;
|
|
242
|
+
const currEl = elements[currIndex];
|
|
243
|
+
const currIsVisible = currEl ? isInViewport(currEl) : false;
|
|
244
|
+
const currIsAbove = currEl
|
|
245
|
+
? currEl.getBoundingClientRect().bottom < 0
|
|
246
|
+
: false;
|
|
247
|
+
let nextIndex = -1;
|
|
248
|
+
if (this.#options.fastTravel && !currIsVisible && currIsAbove) {
|
|
249
|
+
for (let i = currIndex + 1; i < len; i++) {
|
|
250
|
+
const el = elements[i];
|
|
251
|
+
if (el && isInViewport(el)) {
|
|
252
|
+
nextIndex = i;
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (nextIndex === -1) {
|
|
258
|
+
const base = highlightIndexStart !== highlightIndexEnd
|
|
259
|
+
? highlightIndexEnd
|
|
260
|
+
: highlightIndexEnd;
|
|
261
|
+
if (this.#options.loop) {
|
|
262
|
+
nextIndex = (base + step) % len;
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
nextIndex = Math.min(len - 1, base + step);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
219
268
|
this.highlight(nextIndex, nextIndex, true, cache);
|
|
220
269
|
}
|
|
221
270
|
extendLeftHighlight(step = 1, cache = false) {
|