@salesforcedevs/dx-components 0.19.3-alpha.3 → 0.19.3-alpha.33

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dx-components",
3
- "version": "0.19.3-alpha.3",
3
+ "version": "0.19.3-alpha.33",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -27,6 +27,7 @@ export const SESSION_KEY = "dx-sidebar-search-state";
27
27
 
28
28
  const DEFAULT_RESULT_COUNT = 20;
29
29
  const SEARCH_DEBOUNCE_DELAY = 780;
30
+ const HIGHLIGHT_DEBOUNCE_DELAY = 200;
30
31
 
31
32
  const UserRecentSearches = new RecentSearches();
32
33
 
@@ -140,12 +141,6 @@ export default class SidebarSearch extends LightningElement {
140
141
  );
141
142
  }
142
143
 
143
- // If this is the first render and there is a search value, trigger
144
- // term highlighting
145
- if (!this.didRender && this.value) {
146
- this.dispatchHighlightedTermChange();
147
- }
148
-
149
144
  this.didRender = true;
150
145
  }
151
146
 
@@ -217,10 +212,6 @@ export default class SidebarSearch extends LightningElement {
217
212
  this.onResultListChange
218
213
  );
219
214
 
220
- if (this.value) {
221
- this.dispatchHighlightedTermChange();
222
- }
223
-
224
215
  if (!this.preloadedState) {
225
216
  if (this.value) {
226
217
  this.submitSearch();
@@ -323,6 +314,7 @@ export default class SidebarSearch extends LightningElement {
323
314
 
324
315
  private dispatchOnChangeEvent(results: Result[]) {
325
316
  this.updateSessionStorage();
317
+ this.dispatchHighlightedTermChange();
326
318
  this.dispatchEvent(
327
319
  new CustomEvent("change", {
328
320
  detail: {
@@ -337,15 +329,32 @@ export default class SidebarSearch extends LightningElement {
337
329
  this.dispatchEvent(new CustomEvent("loading", { detail: loading }));
338
330
  }
339
331
 
340
- private dispatchHighlightedTermChange() {
332
+ private dispatchHighlightedTermChange = () => {
333
+ const { termsToHighlight, phrasesToHighlight } =
334
+ this.engine?.state?.search?.response;
335
+ if (!termsToHighlight || !phrasesToHighlight) {
336
+ return;
337
+ }
338
+ const normalizedTermsToHighlight = Array.from(
339
+ new Set(
340
+ [
341
+ this.value,
342
+ Object.values(termsToHighlight),
343
+ Object.values(phrasesToHighlight)
344
+ ].flat(2)
345
+ )
346
+ );
341
347
  this.dispatchEvent(
342
348
  new CustomEvent("highlightedtermchange", {
343
- detail: this.value,
349
+ detail: {
350
+ query: this.value,
351
+ termsToHighlight: normalizedTermsToHighlight
352
+ },
344
353
  composed: true,
345
354
  bubbles: true
346
355
  })
347
356
  );
348
- }
357
+ };
349
358
 
350
359
  private updateRecentSearches = () =>
351
360
  (this.recentSearches = UserRecentSearches.get().map((item: string) => ({
@@ -400,7 +409,6 @@ export default class SidebarSearch extends LightningElement {
400
409
  this.value = e.detail;
401
410
 
402
411
  this.handleValueChange(false);
403
- this.dispatchHighlightedTermChange();
404
412
  }
405
413
 
406
414
  private onInputFocus() {
@@ -2,13 +2,16 @@ import { createSearchRegExp } from "utils/regexps";
2
2
 
3
3
  const MARK_TAGS = /<\/?mark>/gi;
4
4
 
5
+ // mark tags dif color on ref page
6
+
5
7
  export const highlightTerms = (
6
8
  // eslint-disable-next-line no-undef
7
9
  elements: NodeListOf<Element>,
8
- searchTerm: string
10
+ { query, termsToHighlight }: { query: string; termsToHighlight: string[] }
9
11
  ): void => {
10
- searchTerm = searchTerm.trim();
11
- const searchExp = createSearchRegExp(searchTerm);
12
+ const searchExp = createSearchRegExp(termsToHighlight);
13
+ let scrolled = false;
14
+ console.log({ elements, query, termsToHighlight, searchExp });
12
15
  elements.forEach((element) => {
13
16
  if (
14
17
  element?.innerHTML.includes("<dx-") ||
@@ -17,20 +20,33 @@ export const highlightTerms = (
17
20
  return;
18
21
  }
19
22
 
20
- let innerHtml = element.innerHTML.replace(
21
- // Remove previously added mark tags
22
- MARK_TAGS,
23
- ""
24
- );
23
+ try {
24
+ let innerHtml = element.innerHTML.replace(
25
+ // Remove previously added mark tags
26
+ MARK_TAGS,
27
+ ""
28
+ );
29
+
30
+ if (query) {
31
+ innerHtml = innerHtml.replace(searchExp, "<mark>$&</mark>");
32
+ }
25
33
 
26
- if (searchTerm) {
27
- innerHtml = innerHtml.replace(searchExp, "<mark>$&</mark>");
34
+ element.innerHTML = innerHtml;
35
+ } catch (e) {
36
+ console.error(e, element);
28
37
  }
29
38
 
30
- element.innerHTML = innerHtml;
39
+ // element
40
+ // .querySelectorAll("mark")
41
+ // .forEach(
42
+ // (el) =>
43
+ // (el.style.backgroundColor = "var(--dx-g-yellow-vibrant-90)")
44
+ // );
45
+
46
+ const mark = element.querySelector("mark");
47
+ if (mark && !scrolled) {
48
+ scrolled = true;
49
+ mark.scrollIntoView({ block: "nearest" });
50
+ }
31
51
  });
32
- if (elements.length > 0) {
33
- console.log(elements[0], 'scrolling')
34
- elements[0].scrollIntoView(true);
35
- }
36
52
  };
@@ -1,5 +1,7 @@
1
1
  const escapeRegExp = (text: string) =>
2
2
  text.replace(/[-[\]{}()*+?.,\\^$|]/g, "\\$&");
3
3
 
4
- export const createSearchRegExp = (searchTerm: string): RegExp =>
5
- new RegExp(`(?:${escapeRegExp(searchTerm)})(?![^<>]*>)`, "gi");
4
+ export const createSearchRegExp = (searchTerms: string[]): RegExp => {
5
+ const escaped = searchTerms.map((term) => escapeRegExp(term.trim()));
6
+ return new RegExp(`(?:${escaped.join("|")})(?![^<>]*>)`, "gi");
7
+ };