@vdegenne/highlight-manager 0.1.1 → 0.1.3

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;
@@ -100,11 +104,20 @@ export class HighLightManager {
100
104
  // const highlightIndexStart = elements.findIndex((el) =>
101
105
  // el.hasAttribute('highlight'),
102
106
  // );
103
- if (!highlightElements || highlightElements.length === 0) {
104
- throw Error("The highlighted element couldn't be found");
105
- }
106
- const highlightIndexStart = elements.indexOf(highlightElements[0]);
107
- const highlightIndexEnd = elements.indexOf(highlightElements[highlightElements.length - 1]);
107
+ // if (!highlightElements || highlightElements.length === 0) {
108
+ // console.warn("The highlighted element couldn't be found")
109
+ // return {
110
+ // highlightIndexStart: -1,
111
+ // highlightIndexEnd: -1,
112
+ // elements: []
113
+ // }
114
+ // }
115
+ const highlightIndexStart = highlightElements.length
116
+ ? elements.indexOf(highlightElements[0])
117
+ : -1;
118
+ const highlightIndexEnd = highlightElements.length
119
+ ? elements.indexOf(highlightElements[highlightElements.length - 1])
120
+ : -1;
108
121
  if (highlightElements.length === 1) {
109
122
  // const highlightElement = elements[highlightIndex];
110
123
  }
@@ -152,6 +165,7 @@ export class HighLightManager {
152
165
  return false;
153
166
  }
154
167
  // console.log(highlightIndexStart, highlightIndexEnd, start, end)
168
+ globalBeforeHighlight?.();
155
169
  this.#options.beforeHighlight?.();
156
170
  // playClick()
157
171
  if (unhighlightAll) {
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.1",
3
+ "version": "0.1.3",
4
4
  "description": "helper to navigate/highlight elements in a page based on a css selector",
5
5
  "type": "module",
6
6
  "exports": {