@vdegenne/highlight-manager 0.1.5 → 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.
Files changed (2) hide show
  1. package/lib/index.js +39 -30
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -201,6 +201,18 @@ export class HighLightManager {
201
201
  const currIndex = highlightIndexStart !== highlightIndexEnd
202
202
  ? highlightIndexStart
203
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
+ }
204
216
  const currEl = elements[currIndex];
205
217
  const currIsVisible = currEl ? isInViewport(currEl) : false;
206
218
  const currIsBelow = currEl
@@ -208,24 +220,18 @@ export class HighLightManager {
208
220
  : false;
209
221
  let prevIndex = -1;
210
222
  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
- }
223
+ const found = elements
224
+ .slice(0, currIndex)
225
+ .reverse()
226
+ .find((el) => isInViewport(el));
227
+ if (found) {
228
+ prevIndex = elements.indexOf(found);
217
229
  }
218
230
  }
219
231
  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
- }
232
+ prevIndex = this.#options.loop
233
+ ? (currIndex - step + len) % len
234
+ : Math.max(0, currIndex - step);
229
235
  }
230
236
  this.highlight(prevIndex, prevIndex, true, cache);
231
237
  }
@@ -239,6 +245,18 @@ export class HighLightManager {
239
245
  const currIndex = highlightIndexStart !== highlightIndexEnd
240
246
  ? highlightIndexEnd
241
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
+ }
242
260
  const currEl = elements[currIndex];
243
261
  const currIsVisible = currEl ? isInViewport(currEl) : false;
244
262
  const currIsAbove = currEl
@@ -246,24 +264,15 @@ export class HighLightManager {
246
264
  : false;
247
265
  let nextIndex = -1;
248
266
  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
- }
267
+ const found = elements.slice(currIndex + 1).find((el) => isInViewport(el));
268
+ if (found) {
269
+ nextIndex = elements.indexOf(found);
255
270
  }
256
271
  }
257
272
  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
- }
273
+ nextIndex = this.#options.loop
274
+ ? (currIndex + step) % len
275
+ : Math.min(len - 1, currIndex + step);
267
276
  }
268
277
  this.highlight(nextIndex, nextIndex, true, cache);
269
278
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vdegenne/highlight-manager",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "helper to navigate/highlight elements in a page based on a css selector",
5
5
  "type": "module",
6
6
  "exports": {