@salesforcedevs/dx-components 0.19.3-alpha.4 → 0.19.3-alpha.40

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.4",
3
+ "version": "0.19.3-alpha.40",
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,34 @@ 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.keys(termsToHighlight),
343
+ Object.keys(phrasesToHighlight),
344
+ Object.values(termsToHighlight),
345
+ Object.values(phrasesToHighlight)
346
+ ].flat(2)
347
+ )
348
+ );
341
349
  this.dispatchEvent(
342
350
  new CustomEvent("highlightedtermchange", {
343
- detail: this.value,
351
+ detail: {
352
+ query: this.value,
353
+ termsToHighlight: normalizedTermsToHighlight
354
+ },
344
355
  composed: true,
345
356
  bubbles: true
346
357
  })
347
358
  );
348
- }
359
+ };
349
360
 
350
361
  private updateRecentSearches = () =>
351
362
  (this.recentSearches = UserRecentSearches.get().map((item: string) => ({
@@ -400,7 +411,6 @@ export default class SidebarSearch extends LightningElement {
400
411
  this.value = e.detail;
401
412
 
402
413
  this.handleValueChange(false);
403
- this.dispatchHighlightedTermChange();
404
414
  }
405
415
 
406
416
  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
- 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);
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,42 @@ 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
- );
25
-
26
- if (searchTerm) {
27
- innerHtml = innerHtml.replace(searchExp, "<mark>$&</mark>");
23
+ try {
24
+ if (element.innerHTML.match(MARK_TAGS)) {
25
+ // Remove previously added mark tags
26
+ console.log(
27
+ "matching marks",
28
+ element,
29
+ element.style.backgroundColor
30
+ );
31
+ element.innerHTML = element.innerHTML.replace(MARK_TAGS, "");
32
+ }
33
+ if (element.innerHTML.match(searchExp) && query) {
34
+ console.log("matching", element);
35
+ element.innerHTML = element.innerHTML.replace(
36
+ searchExp,
37
+ "<mark>$&</mark>"
38
+ );
39
+ }
40
+ } catch (e) {
41
+ console.error(
42
+ `${element} could not be parsed by the highlight utility: ${e}`
43
+ );
28
44
  }
29
45
 
30
- element.innerHTML = innerHtml;
46
+ console.log(element.querySelectorAll("mark"))
47
+
48
+ element
49
+ .querySelectorAll("mark")
50
+ .forEach(
51
+ (el) =>
52
+ (el.style.backgroundColor = "var(--dx-g-yellow-vibrant-90)")
53
+ );
54
+
55
+ const mark = element.querySelector("mark");
56
+ if (mark && !scrolled) {
57
+ scrolled = true;
58
+ mark.scrollIntoView({ block: "nearest" });
59
+ }
31
60
  });
32
- if (elements.length > 0) {
33
- console.log(elements[0], 'scrolling')
34
- elements[0].scrollIntoView(true);
35
- }
36
61
  };
@@ -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
+ };