@vdegenne/highlight-manager 0.1.2 → 0.1.4

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 CHANGED
@@ -36,6 +36,7 @@ interface Options {
36
36
  */
37
37
  scrollWhenOffscreen: boolean;
38
38
  }
39
+ export declare function setGlobalBeforeHighlight(fct: () => void): void;
39
40
  export declare class HighLightManager {
40
41
  #private;
41
42
  protected selector: string;
package/lib/index.js CHANGED
@@ -18,6 +18,10 @@ const defaults = {
18
18
  };
19
19
  // Local array of all declared highlighters for id control.
20
20
  const highlighters = [];
21
+ let globalBeforeHighlight;
22
+ export function setGlobalBeforeHighlight(fct) {
23
+ globalBeforeHighlight = fct;
24
+ }
21
25
  export class HighLightManager {
22
26
  #cache;
23
27
  #options;
@@ -161,6 +165,7 @@ export class HighLightManager {
161
165
  return false;
162
166
  }
163
167
  // console.log(highlightIndexStart, highlightIndexEnd, start, end)
168
+ globalBeforeHighlight?.();
164
169
  this.#options.beforeHighlight?.();
165
170
  // playClick()
166
171
  if (unhighlightAll) {
@@ -170,7 +175,7 @@ export class HighLightManager {
170
175
  if (elementsToHighlight.length === 0) {
171
176
  return false;
172
177
  }
173
- if (this.#options.scrollWhenOffscreen ||
178
+ if (this.#options.scrollWhenOffscreen &&
174
179
  !isInViewport(elementsToHighlight[0])) {
175
180
  elementsToHighlight[0]?.scrollIntoView({
176
181
  behavior: 'smooth',
package/lib/utils.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export declare function sleep(milli?: number): Promise<unknown>;
2
- export declare function isInViewport(el: Element): boolean;
2
+ export declare function isInViewport(el: HTMLElement): boolean;
3
3
  //# sourceMappingURL=utils.d.ts.map
package/lib/utils.js CHANGED
@@ -1,8 +1,19 @@
1
1
  export function sleep(milli = 1000) {
2
2
  return new Promise((r) => setTimeout(r, milli));
3
3
  }
4
+ // export function isInViewport(el: Element) {
5
+ // return (
6
+ // el.getBoundingClientRect().top >= 0 &&
7
+ // el.getBoundingClientRect().bottom <= window.innerHeight
8
+ // )
9
+ // }
4
10
  export function isInViewport(el) {
5
- return (el.getBoundingClientRect().top >= 0 &&
6
- el.getBoundingClientRect().bottom <= window.innerHeight);
11
+ const rect = el.getBoundingClientRect();
12
+ const viewHeight = window.innerHeight || document.documentElement.clientHeight;
13
+ const viewWidth = window.innerWidth || document.documentElement.clientWidth;
14
+ return (rect.bottom > 0 &&
15
+ rect.right > 0 &&
16
+ rect.top < viewHeight &&
17
+ rect.left < viewWidth);
7
18
  }
8
19
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vdegenne/highlight-manager",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "helper to navigate/highlight elements in a page based on a css selector",
5
5
  "type": "module",
6
6
  "exports": {