@vdegenne/highlight-manager 0.1.6 → 0.1.7

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
@@ -9,6 +9,7 @@ interface Info {
9
9
  highlightElement: HTMLElement | undefined;
10
10
  highlightContent: string | undefined;
11
11
  }
12
+ type ScrollWhenOffscreenOption = 'never' | 'bounding-rect' | 'top';
12
13
  interface Options {
13
14
  css: string;
14
15
  highlightTextColor: string;
@@ -32,9 +33,9 @@ interface Options {
32
33
  */
33
34
  applyStyleSheetTo: Document | HTMLElement | ShadowRoot;
34
35
  /**
35
- * @default false
36
+ * @default 'none'
36
37
  */
37
- scrollWhenOffscreen: boolean;
38
+ scrollWhenOffscreen: ScrollWhenOffscreenOption;
38
39
  /**
39
40
  * If true, will select the next visible candidate if the highlight is offscreen.
40
41
  *
@@ -43,6 +44,9 @@ interface Options {
43
44
  fastTravel: boolean;
44
45
  }
45
46
  export declare function setGlobalBeforeHighlight(fct: () => void): void;
47
+ interface HighlightOptions {
48
+ scrollWhenOffscreen: ScrollWhenOffscreenOption;
49
+ }
46
50
  export declare class HighLightManager {
47
51
  #private;
48
52
  protected selector: string;
@@ -60,7 +64,7 @@ export declare class HighLightManager {
60
64
  /**
61
65
  * @returns {boolean} true if the highlight succeeded, false otherwise.
62
66
  */
63
- highlight(start: number, end?: number, unhighlightAll?: boolean, cache?: boolean): boolean;
67
+ highlight(start: number, end?: number, unhighlightAll?: boolean, cache?: boolean, options?: HighlightOptions): boolean;
64
68
  previous(step?: number, cache?: boolean): void;
65
69
  next(step?: number, cache?: boolean): void;
66
70
  extendLeftHighlight(step?: number, cache?: boolean): void;
package/lib/index.js CHANGED
@@ -14,7 +14,7 @@ const defaults = {
14
14
  beforeHighlight: undefined,
15
15
  onSelectionChange: undefined,
16
16
  applyStyleSheetTo: document,
17
- scrollWhenOffscreen: false,
17
+ scrollWhenOffscreen: 'never',
18
18
  fastTravel: false,
19
19
  };
20
20
  // Local array of all declared highlighters for id control.
@@ -75,7 +75,9 @@ export class HighLightManager {
75
75
  const els = querySelectorAll(this.selector);
76
76
  const el = els[index];
77
77
  if (el) {
78
- this.highlight(index, index, true, false);
78
+ this.highlight(index, index, true, false, {
79
+ scrollWhenOffscreen: 'never',
80
+ });
79
81
  wr.resolve(el);
80
82
  this.#highlightWhenAvailablePromiseWR = undefined;
81
83
  return;
@@ -150,7 +152,7 @@ export class HighLightManager {
150
152
  /**
151
153
  * @returns {boolean} true if the highlight succeeded, false otherwise.
152
154
  */
153
- highlight(start, end, unhighlightAll = true, cache = false) {
155
+ highlight(start, end, unhighlightAll = true, cache = false, options) {
154
156
  if (end === undefined) {
155
157
  end = start;
156
158
  }
@@ -166,6 +168,9 @@ export class HighLightManager {
166
168
  return false;
167
169
  }
168
170
  // console.log(highlightIndexStart, highlightIndexEnd, start, end)
171
+ const _options = {
172
+ scrollWhenOffscreen: options?.scrollWhenOffscreen ?? this.#options.scrollWhenOffscreen,
173
+ };
169
174
  globalBeforeHighlight?.();
170
175
  this.#options.beforeHighlight?.();
171
176
  // playClick()
@@ -176,12 +181,12 @@ export class HighLightManager {
176
181
  if (elementsToHighlight.length === 0) {
177
182
  return false;
178
183
  }
179
- if (this.#options.scrollWhenOffscreen &&
180
- !isInViewport(elementsToHighlight[0])) {
184
+ if (_options.scrollWhenOffscreen !== 'never' &&
185
+ !isInViewport(elementsToHighlight[0], _options.scrollWhenOffscreen === 'top')) {
181
186
  elementsToHighlight[0]?.scrollIntoView({
182
187
  behavior: 'smooth',
183
- block: 'center',
184
- inline: 'center',
188
+ block: _options.scrollWhenOffscreen === 'top' ? 'start' : 'center',
189
+ inline: _options.scrollWhenOffscreen === 'top' ? 'start' : 'center',
185
190
  });
186
191
  }
187
192
  elementsToHighlight.forEach((el) => el.setAttribute(`highlight${this.#id}`, ''));
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: HTMLElement): boolean;
2
+ export declare function isInViewport(el: HTMLElement, onlyTop?: boolean): boolean;
3
3
  //# sourceMappingURL=utils.d.ts.map
package/lib/utils.js CHANGED
@@ -7,10 +7,13 @@ export function sleep(milli = 1000) {
7
7
  // el.getBoundingClientRect().bottom <= window.innerHeight
8
8
  // )
9
9
  // }
10
- export function isInViewport(el) {
10
+ export function isInViewport(el, onlyTop = false) {
11
11
  const rect = el.getBoundingClientRect();
12
12
  const viewHeight = window.innerHeight || document.documentElement.clientHeight;
13
13
  const viewWidth = window.innerWidth || document.documentElement.clientWidth;
14
+ if (onlyTop) {
15
+ return rect.top >= 0 && rect.top <= viewHeight;
16
+ }
14
17
  return (rect.bottom > 0 &&
15
18
  rect.right > 0 &&
16
19
  rect.top < viewHeight &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vdegenne/highlight-manager",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "helper to navigate/highlight elements in a page based on a css selector",
5
5
  "type": "module",
6
6
  "exports": {