@salesforcedevs/dx-components 0.19.3-alpha.5 → 0.19.3-alpha.50

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.5",
3
+ "version": "0.19.3-alpha.50",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -122,7 +122,10 @@ export default class Sidebar extends LightningElement {
122
122
  this.assignFilteredTrees();
123
123
  this.dispatchEvent(
124
124
  new CustomEvent("highlightedtermchange", {
125
- detail: this.searchText,
125
+ detail: {
126
+ query: this.searchText,
127
+ termsToHighlight: [this.searchText]
128
+ },
126
129
  composed: true,
127
130
  bubbles: true
128
131
  })
@@ -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
 
@@ -88,6 +89,25 @@ export default class SidebarSearch extends LightningElement {
88
89
  private fetchingMoreResults: boolean = false;
89
90
  private didRender = false;
90
91
 
92
+ private get termsToHighlight(): string[] | null {
93
+ const { termsToHighlight, phrasesToHighlight } =
94
+ this.engine?.state?.search?.response;
95
+ if (!termsToHighlight || !phrasesToHighlight) {
96
+ return null;
97
+ }
98
+ return Array.from(
99
+ new Set(
100
+ [
101
+ this.value,
102
+ Object.keys(termsToHighlight),
103
+ Object.keys(phrasesToHighlight),
104
+ Object.values(termsToHighlight),
105
+ Object.values(phrasesToHighlight)
106
+ ].flat(2)
107
+ )
108
+ ) as string[];
109
+ }
110
+
91
111
  private get hasCorrectCoveoConfig(): boolean {
92
112
  const coveoConfigurations = [
93
113
  this.coveoOrganizationId,
@@ -140,12 +160,6 @@ export default class SidebarSearch extends LightningElement {
140
160
  );
141
161
  }
142
162
 
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
163
  this.didRender = true;
150
164
  }
151
165
 
@@ -217,10 +231,6 @@ export default class SidebarSearch extends LightningElement {
217
231
  this.onResultListChange
218
232
  );
219
233
 
220
- if (this.value) {
221
- this.dispatchHighlightedTermChange();
222
- }
223
-
224
234
  if (!this.preloadedState) {
225
235
  if (this.value) {
226
236
  this.submitSearch();
@@ -323,6 +333,7 @@ export default class SidebarSearch extends LightningElement {
323
333
 
324
334
  private dispatchOnChangeEvent(results: Result[]) {
325
335
  this.updateSessionStorage();
336
+ this.dispatchHighlightedTermChange();
326
337
  this.dispatchEvent(
327
338
  new CustomEvent("change", {
328
339
  detail: {
@@ -337,15 +348,21 @@ export default class SidebarSearch extends LightningElement {
337
348
  this.dispatchEvent(new CustomEvent("loading", { detail: loading }));
338
349
  }
339
350
 
340
- private dispatchHighlightedTermChange() {
341
- this.dispatchEvent(
342
- new CustomEvent("highlightedtermchange", {
343
- detail: this.value,
344
- composed: true,
345
- bubbles: true
346
- })
347
- );
348
- }
351
+ private dispatchHighlightedTermChange = () => {
352
+ if (this.termsToHighlight) {
353
+ console.log('dispatching terms to highlight')
354
+ this.dispatchEvent(
355
+ new CustomEvent("highlightedtermchange", {
356
+ detail: {
357
+ query: this.value,
358
+ termsToHighlight: this.termsToHighlight
359
+ },
360
+ composed: true,
361
+ bubbles: true
362
+ })
363
+ );
364
+ }
365
+ };
349
366
 
350
367
  private updateRecentSearches = () =>
351
368
  (this.recentSearches = UserRecentSearches.get().map((item: string) => ({
@@ -400,7 +417,6 @@ export default class SidebarSearch extends LightningElement {
400
417
  this.value = e.detail;
401
418
 
402
419
  this.handleValueChange(false);
403
- this.dispatchHighlightedTermChange();
404
420
  }
405
421
 
406
422
  private onInputFocus() {
@@ -1,15 +1,17 @@
1
1
  import { createSearchRegExp } from "utils/regexps";
2
2
 
3
- const MARK_TAGS = /<\/?mark>/gi;
3
+ const MARK_TAGS = /<\/?mark[^>]*>/gi;
4
+
5
+ // mark tags dif color on ref page
4
6
 
5
7
  export const highlightTerms = (
6
8
  // eslint-disable-next-line no-undef
7
- elements: NodeListOf<Element>,
8
- searchTerm: string
9
+ elements: NodeListOf<HTMLElement>,
10
+ { query, termsToHighlight }: { query: string; termsToHighlight: string[] }
9
11
  ): void => {
10
- searchTerm = searchTerm.trim();
11
- const searchExp = createSearchRegExp(searchTerm);
12
+ const searchExp = createSearchRegExp(termsToHighlight);
12
13
  let scrolled = false;
14
+ console.log({ elements, query, termsToHighlight, searchExp });
13
15
  elements.forEach((element) => {
14
16
  if (
15
17
  element?.innerHTML.includes("<dx-") ||
@@ -18,24 +20,27 @@ export const highlightTerms = (
18
20
  return;
19
21
  }
20
22
 
21
- let innerHtml = element.innerHTML.replace(
22
- // Remove previously added mark tags
23
- MARK_TAGS,
24
- ""
25
- );
26
-
27
- if (searchTerm) {
28
- innerHtml = innerHtml.replace(searchExp, "<mark>$&</mark>");
23
+ try {
24
+ if (element.innerHTML.match(MARK_TAGS)) {
25
+ // Remove previously added mark tags
26
+ element.innerHTML = element.innerHTML.replace(MARK_TAGS, "");
27
+ }
28
+ if (element.innerHTML.match(searchExp) && query) {
29
+ element.innerHTML = element.innerHTML.replace(
30
+ searchExp,
31
+ '<mark style="background-color:var(--dx-g-yellow-vibrant-90)">$&</mark>'
32
+ );
33
+ }
34
+ } catch (e) {
35
+ console.error(
36
+ `${element} could not be parsed by the highlight utility: ${e}`
37
+ );
29
38
  }
30
39
 
31
40
  const mark = element.querySelector("mark");
32
-
33
41
  if (mark && !scrolled) {
34
- console.log(mark)
35
42
  scrolled = true;
36
- mark.scrollIntoView();
43
+ mark.scrollIntoView({ block: "nearest" });
37
44
  }
38
-
39
- element.innerHTML = innerHtml;
40
45
  });
41
46
  };
@@ -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
+ };