|
1 |
| -// The following is for integrating with https://github.com/cmfcmf/docusaurus-search-local |
| 1 | +import ExecutionEnvironment from "@docusaurus/core/lib/client/exports/ExecutionEnvironment"; |
2 | 2 |
|
3 |
| -let searchInput, timeout: NodeJS.Timeout | undefined; |
| 3 | +if (ExecutionEnvironment.canUseDOM) { |
| 4 | + // The following is for integrating with https://github.com/cmfcmf/docusaurus-search-local |
| 5 | + let searchInput, timeout: NodeJS.Timeout | undefined; |
4 | 6 |
|
5 |
| -function processEvent(event: Event): void { |
6 |
| - clearTimeout(timeout); |
7 |
| - timeout = setTimeout(() => { |
8 |
| - const query = (event?.target as HTMLInputElement)?.value.trim(); |
9 |
| - if (query) { |
10 |
| - window.analytics.track("Searched", { |
11 |
| - category: "Search", |
12 |
| - label: window.location.pathname, |
13 |
| - properties: { |
14 |
| - query, |
15 |
| - }, |
16 |
| - }); |
| 7 | + function processEvent(event: Event): void { |
| 8 | + clearTimeout(timeout); |
| 9 | + timeout = setTimeout(() => { |
| 10 | + const query = (event?.target as HTMLInputElement)?.value.trim(); |
| 11 | + if (query) { |
| 12 | + window.analytics.track("Searched", { |
| 13 | + category: "Search", |
| 14 | + label: window.location.pathname, |
| 15 | + properties: { |
| 16 | + query, |
| 17 | + }, |
| 18 | + }); |
| 19 | + } |
| 20 | + }, 1000); |
| 21 | + } |
| 22 | + |
| 23 | + function checkSearchInput() { |
| 24 | + searchInput = document.querySelector("input[type=search]"); |
| 25 | + if (searchInput) { |
| 26 | + searchInput.addEventListener("input", processEvent); |
| 27 | + return true; |
17 | 28 | }
|
18 |
| - }, 1000); |
19 |
| -} |
20 | 29 |
|
21 |
| -function checkSearchInput() { |
22 |
| - searchInput = document.querySelector("input[type=search]"); |
23 |
| - if (searchInput) { |
24 |
| - searchInput.addEventListener("input", processEvent); |
25 |
| - return true; |
| 30 | + return false; |
26 | 31 | }
|
27 | 32 |
|
28 |
| - return false; |
29 |
| -} |
| 33 | + if (!checkSearchInput()) { |
| 34 | + // We need to wait until it gets added to the DOM |
| 35 | + const observer = new MutationObserver(() => { |
| 36 | + if (checkSearchInput()) { |
| 37 | + observer.disconnect(); |
| 38 | + } |
| 39 | + }); |
30 | 40 |
|
31 |
| -if (!checkSearchInput()) { |
32 |
| - // We need to wait until it gets added to the DOM |
33 |
| - const observer = new MutationObserver(() => { |
34 |
| - if (checkSearchInput()) { |
35 |
| - observer.disconnect(); |
36 |
| - } |
37 |
| - }); |
38 |
| - |
39 |
| - observer.observe(document.body, { |
40 |
| - subtree: false, |
41 |
| - childList: true, |
42 |
| - }); |
| 41 | + observer.observe(document.body, { |
| 42 | + subtree: false, |
| 43 | + childList: true, |
| 44 | + }); |
| 45 | + } |
43 | 46 | }
|
44 | 47 |
|
45 | 48 | export {};
|
0 commit comments