@salesforcedevs/dx-components 0.19.3-alpha.4 → 0.19.3-alpha.43
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
|
@@ -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:
|
|
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() {
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { createSearchRegExp } from "utils/regexps";
|
|
2
2
|
|
|
3
|
-
const MARK_TAGS = /<\/?mark
|
|
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<
|
|
8
|
-
|
|
9
|
+
elements: NodeListOf<HTMLElement>,
|
|
10
|
+
{ query, termsToHighlight }: { query: string; termsToHighlight: string[] }
|
|
9
11
|
): void => {
|
|
10
|
-
|
|
11
|
-
|
|
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,43 @@ export const highlightTerms = (
|
|
|
17
20
|
return;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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, element.innerHTML);
|
|
35
|
+
element.innerHTML = element.innerHTML.replace(
|
|
36
|
+
searchExp,
|
|
37
|
+
"<mark>$&</mark>"
|
|
38
|
+
);
|
|
39
|
+
console.log("matching modified", element.innerHTML);
|
|
40
|
+
}
|
|
41
|
+
} catch (e) {
|
|
42
|
+
console.error(
|
|
43
|
+
`${element} could not be parsed by the highlight utility: ${e}`
|
|
44
|
+
);
|
|
28
45
|
}
|
|
29
46
|
|
|
30
|
-
element.
|
|
47
|
+
console.log(element.querySelectorAll("mark"));
|
|
48
|
+
|
|
49
|
+
element
|
|
50
|
+
.querySelectorAll("mark")
|
|
51
|
+
.forEach(
|
|
52
|
+
(el) =>
|
|
53
|
+
(el.style.backgroundColor = "var(--dx-g-yellow-vibrant-90)")
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const mark = element.querySelector("mark");
|
|
57
|
+
if (mark && !scrolled) {
|
|
58
|
+
scrolled = true;
|
|
59
|
+
mark.scrollIntoView({ block: "nearest" });
|
|
60
|
+
}
|
|
31
61
|
});
|
|
32
|
-
if (elements.length > 0) {
|
|
33
|
-
console.log(elements[0], 'scrolling')
|
|
34
|
-
elements[0].scrollIntoView(true);
|
|
35
|
-
}
|
|
36
62
|
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const escapeRegExp = (text: string) =>
|
|
2
2
|
text.replace(/[-[\]{}()*+?.,\\^$|]/g, "\\$&");
|
|
3
3
|
|
|
4
|
-
export const createSearchRegExp = (
|
|
5
|
-
|
|
4
|
+
export const createSearchRegExp = (searchTerms: string[]): RegExp => {
|
|
5
|
+
const escaped = searchTerms.map((term) => escapeRegExp(term.trim()));
|
|
6
|
+
return new RegExp(`(?:${escaped.join("|")})(?![^<>]*>)`, "gi");
|
|
7
|
+
};
|