|
| 1 | +import Fuse from "https://esm.sh/[email protected]"; |
| 2 | + |
| 3 | +const searchBtn = document.querySelector("#search-btn"); |
| 4 | + |
| 5 | +function hideSearchBtn() { |
| 6 | + // Hide search icon if we can't search in the first place. |
| 7 | + searchBtn.style.display = 'none'; |
| 8 | +} |
| 9 | + |
| 10 | +function debounce(mainFunction, delay) { |
| 11 | + // Declare a variable called 'timer' to store the timer ID |
| 12 | + let timer; |
| 13 | + |
| 14 | + // Return an anonymous function that takes in any number of arguments |
| 15 | + return function (...args) { |
| 16 | + // Clear the previous timer to prevent the execution of 'mainFunction' |
| 17 | + clearTimeout(timer); |
| 18 | + |
| 19 | + // Set a new timer that will execute 'mainFunction' after the specified delay |
| 20 | + timer = setTimeout(() => { |
| 21 | + mainFunction(...args); |
| 22 | + }, delay); |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +const root = document.documentElement.getAttribute("data-root"); |
| 27 | +if (root && searchBtn) { |
| 28 | + let fuse = null; |
| 29 | + const searchIndexUrl = `${root}/index.json`; |
| 30 | + fetch(searchIndexUrl, {}) |
| 31 | + .then(response => response.json()) |
| 32 | + .then(index => { |
| 33 | + fuse = new Fuse(index, { |
| 34 | + includeScore: true, |
| 35 | + keys: ['uri', 'title', 'content', 'headings'], |
| 36 | + includeMatches: true, |
| 37 | + limit: 20, |
| 38 | + ignoreLocation: true, |
| 39 | + threshold: 0.6, |
| 40 | + minMatchCharLength: 2, |
| 41 | + ignoreFieldNorm: true, |
| 42 | + shouldSort: true |
| 43 | + }); |
| 44 | + }) |
| 45 | + .catch(() => { |
| 46 | + hideSearchBtn(); |
| 47 | + }) |
| 48 | + |
| 49 | + const searchDialog = document.querySelector("dialog"); |
| 50 | + const empty = document.querySelector("dialog .empty"); |
| 51 | + const resultsElement = document.querySelector("dialog ul"); |
| 52 | + const searchBox = document.querySelector("dialog input[type=search]"); |
| 53 | + |
| 54 | + searchBtn.addEventListener("click", () => { |
| 55 | + searchDialog.showModal(); |
| 56 | + }) |
| 57 | + |
| 58 | + searchDialog.addEventListener("click", ev => { |
| 59 | + if (ev.target.tagName === "DIALOG") { |
| 60 | + searchBox.value = ''; |
| 61 | + searchDialog.close() |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + function searchAux(searchTerm) { |
| 66 | + if (!fuse) return; |
| 67 | + |
| 68 | + const results = fuse.search(searchTerm); |
| 69 | + if (results.length === 0) { |
| 70 | + clearResults(); |
| 71 | + empty.textContent = "No results were found"; |
| 72 | + } else { |
| 73 | + if (location.hostname === 'localhost'){ |
| 74 | + console.table(results); |
| 75 | + } |
| 76 | + |
| 77 | + empty.style.display = 'none'; |
| 78 | + const newResultNodes = |
| 79 | + results |
| 80 | + .map(result => { |
| 81 | + const item = result.item; |
| 82 | + const li = document.createElement("li"); |
| 83 | + const a = document.createElement("a"); |
| 84 | + a.setAttribute("href", item.uri); |
| 85 | + const icon = document.createElement("iconify-icon"); |
| 86 | + icon.setAttribute("width", "24"); |
| 87 | + icon.setAttribute("height", "24"); |
| 88 | + icon.setAttribute("icon", item.type === "content" ? "iconoir:page" : "bxs:file-doc") |
| 89 | + a.append(icon, item.title); |
| 90 | + li.appendChild(a); |
| 91 | + return li; |
| 92 | + }); |
| 93 | + resultsElement.replaceChildren(...newResultNodes); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + const search = debounce(searchAux, 250); |
| 98 | + |
| 99 | + function clearResults() { |
| 100 | + empty.style.display = 'block'; |
| 101 | + resultsElement.replaceChildren(); |
| 102 | + } |
| 103 | + |
| 104 | + searchBox.addEventListener('keyup', ev => { |
| 105 | + ev.stopPropagation(); |
| 106 | + const searchTerm = ev.target.value; |
| 107 | + if (!searchTerm) { |
| 108 | + empty.textContent = "Type something to start searching."; |
| 109 | + clearResults(); |
| 110 | + } else { |
| 111 | + search(searchTerm); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + window.addEventListener('keyup', ev => { |
| 116 | + if (ev.key === 'Escape' && searchDialog.open) { |
| 117 | + searchDialog.close(); |
| 118 | + } |
| 119 | + |
| 120 | + if (ev.key === '/' && !searchDialog.open) { |
| 121 | + searchDialog.showModal(); |
| 122 | + } |
| 123 | + }) |
| 124 | +} else { |
| 125 | + hideSearchBtn(); |
| 126 | +} |
0 commit comments