@vdegenne/highlight-manager 0.1.4 → 0.1.6
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 +67 -9
- 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 = [];
|
|
@@ -197,12 +198,42 @@ 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
|
+
if (currIndex === -1) {
|
|
205
|
+
if (this.#options.fastTravel) {
|
|
206
|
+
const found = [...elements].reverse().find((el) => isInViewport(el));
|
|
207
|
+
if (found) {
|
|
208
|
+
const i = elements.indexOf(found);
|
|
209
|
+
this.highlight(i, i, true, cache);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
this.highlight(len - 1, len - 1, true, cache);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const currEl = elements[currIndex];
|
|
217
|
+
const currIsVisible = currEl ? isInViewport(currEl) : false;
|
|
218
|
+
const currIsBelow = currEl
|
|
219
|
+
? currEl.getBoundingClientRect().top > window.innerHeight
|
|
220
|
+
: false;
|
|
221
|
+
let prevIndex = -1;
|
|
222
|
+
if (this.#options.fastTravel && !currIsVisible && currIsBelow) {
|
|
223
|
+
const found = elements
|
|
224
|
+
.slice(0, currIndex)
|
|
225
|
+
.reverse()
|
|
226
|
+
.find((el) => isInViewport(el));
|
|
227
|
+
if (found) {
|
|
228
|
+
prevIndex = elements.indexOf(found);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (prevIndex === -1) {
|
|
232
|
+
prevIndex = this.#options.loop
|
|
233
|
+
? (currIndex - step + len) % len
|
|
234
|
+
: Math.max(0, currIndex - step);
|
|
235
|
+
}
|
|
236
|
+
this.highlight(prevIndex, prevIndex, true, cache);
|
|
206
237
|
}
|
|
207
238
|
next(step = 1, cache = false) {
|
|
208
239
|
const { elements, highlightIndexStart, highlightIndexEnd } = this.getInfo(cache);
|
|
@@ -211,11 +242,38 @@ export class HighLightManager {
|
|
|
211
242
|
this.highlight(-1, -1, true, cache);
|
|
212
243
|
return;
|
|
213
244
|
}
|
|
214
|
-
|
|
245
|
+
const currIndex = highlightIndexStart !== highlightIndexEnd
|
|
215
246
|
? highlightIndexEnd
|
|
216
|
-
:
|
|
217
|
-
|
|
218
|
-
|
|
247
|
+
: highlightIndexEnd;
|
|
248
|
+
if (currIndex === -1) {
|
|
249
|
+
if (this.#options.fastTravel) {
|
|
250
|
+
const found = elements.find((el) => isInViewport(el));
|
|
251
|
+
if (found) {
|
|
252
|
+
const i = elements.indexOf(found);
|
|
253
|
+
this.highlight(i, i, true, cache);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
this.highlight(0, 0, true, cache);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const currEl = elements[currIndex];
|
|
261
|
+
const currIsVisible = currEl ? isInViewport(currEl) : false;
|
|
262
|
+
const currIsAbove = currEl
|
|
263
|
+
? currEl.getBoundingClientRect().bottom < 0
|
|
264
|
+
: false;
|
|
265
|
+
let nextIndex = -1;
|
|
266
|
+
if (this.#options.fastTravel && !currIsVisible && currIsAbove) {
|
|
267
|
+
const found = elements.slice(currIndex + 1).find((el) => isInViewport(el));
|
|
268
|
+
if (found) {
|
|
269
|
+
nextIndex = elements.indexOf(found);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (nextIndex === -1) {
|
|
273
|
+
nextIndex = this.#options.loop
|
|
274
|
+
? (currIndex + step) % len
|
|
275
|
+
: Math.min(len - 1, currIndex + step);
|
|
276
|
+
}
|
|
219
277
|
this.highlight(nextIndex, nextIndex, true, cache);
|
|
220
278
|
}
|
|
221
279
|
extendLeftHighlight(step = 1, cache = false) {
|