@salesforcedevs/dx-components 0.19.3-alpha.6 → 0.19.3-alpha.60

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.6",
3
+ "version": "0.19.3-alpha.60",
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
  })
@@ -88,6 +88,30 @@ export default class SidebarSearch extends LightningElement {
88
88
  private fetchingMoreResults: boolean = false;
89
89
  private didRender = false;
90
90
 
91
+ private get termsToHighlight(): string[] | null {
92
+ const { termsToHighlight, phrasesToHighlight } =
93
+ this.engine?.state?.search?.response;
94
+ if (!termsToHighlight || !phrasesToHighlight) {
95
+ return null;
96
+ }
97
+
98
+ const flattenTerms = (terms: Object): string[] =>
99
+ Object.entries(terms).reduce((acc, [key, value]) => {
100
+ if (Array.isArray(value)) {
101
+ return [...acc, key, ...value];
102
+ }
103
+ const values = flattenTerms(value);
104
+ return [...acc, key, ...values];
105
+ }, []);
106
+
107
+ const terms = flattenTerms({
108
+ ...termsToHighlight,
109
+ ...phrasesToHighlight
110
+ });
111
+
112
+ return Array.from(new Set([this.value, ...terms].flat(2))) as string[];
113
+ }
114
+
91
115
  private get hasCorrectCoveoConfig(): boolean {
92
116
  const coveoConfigurations = [
93
117
  this.coveoOrganizationId,
@@ -140,12 +164,6 @@ export default class SidebarSearch extends LightningElement {
140
164
  );
141
165
  }
142
166
 
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
167
  this.didRender = true;
150
168
  }
151
169
 
@@ -217,10 +235,6 @@ export default class SidebarSearch extends LightningElement {
217
235
  this.onResultListChange
218
236
  );
219
237
 
220
- if (this.value) {
221
- this.dispatchHighlightedTermChange();
222
- }
223
-
224
238
  if (!this.preloadedState) {
225
239
  if (this.value) {
226
240
  this.submitSearch();
@@ -323,6 +337,7 @@ export default class SidebarSearch extends LightningElement {
323
337
 
324
338
  private dispatchOnChangeEvent(results: Result[]) {
325
339
  this.updateSessionStorage();
340
+ this.dispatchHighlightedTermChange();
326
341
  this.dispatchEvent(
327
342
  new CustomEvent("change", {
328
343
  detail: {
@@ -337,15 +352,20 @@ export default class SidebarSearch extends LightningElement {
337
352
  this.dispatchEvent(new CustomEvent("loading", { detail: loading }));
338
353
  }
339
354
 
340
- private dispatchHighlightedTermChange() {
341
- this.dispatchEvent(
342
- new CustomEvent("highlightedtermchange", {
343
- detail: this.value,
344
- composed: true,
345
- bubbles: true
346
- })
347
- );
348
- }
355
+ private dispatchHighlightedTermChange = () => {
356
+ if (this.termsToHighlight) {
357
+ this.dispatchEvent(
358
+ new CustomEvent("highlightedtermchange", {
359
+ detail: {
360
+ query: this.value,
361
+ termsToHighlight: this.termsToHighlight
362
+ },
363
+ composed: true,
364
+ bubbles: true
365
+ })
366
+ );
367
+ }
368
+ };
349
369
 
350
370
  private updateRecentSearches = () =>
351
371
  (this.recentSearches = UserRecentSearches.get().map((item: string) => ({
@@ -400,7 +420,6 @@ export default class SidebarSearch extends LightningElement {
400
420
  this.value = e.detail;
401
421
 
402
422
  this.handleValueChange(false);
403
- this.dispatchHighlightedTermChange();
404
423
  }
405
424
 
406
425
  private onInputFocus() {
@@ -1,15 +1,18 @@
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
+ console.log({ elements, query, termsToHighlight });
13
+ const searchExp = createSearchRegExp(termsToHighlight);
12
14
  let scrolled = false;
15
+ console.log({ searchExp });
13
16
  elements.forEach((element) => {
14
17
  if (
15
18
  element?.innerHTML.includes("<dx-") ||
@@ -18,23 +21,27 @@ export const highlightTerms = (
18
21
  return;
19
22
  }
20
23
 
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>");
24
+ try {
25
+ if (element.innerHTML.match(MARK_TAGS)) {
26
+ // Remove previously added mark tags
27
+ element.innerHTML = element.innerHTML.replace(MARK_TAGS, "");
28
+ }
29
+ if (element.innerHTML.match(searchExp) && query) {
30
+ element.innerHTML = element.innerHTML.replace(
31
+ searchExp,
32
+ '<mark style="background-color:var(--dx-g-yellow-vibrant-90)">$&</mark>'
33
+ );
34
+ }
35
+ } catch (e) {
36
+ console.error(
37
+ `${element} could not be parsed by the highlight utility: ${e}`
38
+ );
29
39
  }
30
40
 
31
41
  const mark = element.querySelector("mark");
32
-
33
42
  if (mark && !scrolled) {
34
43
  scrolled = true;
35
- element.scrollIntoView({ block: 'nearest', inline: 'start' })
44
+ mark.scrollIntoView({ block: "nearest" });
36
45
  }
37
-
38
- element.innerHTML = innerHtml;
39
46
  });
40
47
  };
@@ -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(`(?:\\b|[^A-Za-z0-9]\\K)(?:${escaped.join("|")})(?![^<>]*>)(?=\\b|[^A-Za-z0-9])`, "gi");
7
+ };