diff --git a/scripts/fuzzy.js b/scripts/fuzzy.js index 89eb821..ea69144 100644 --- a/scripts/fuzzy.js +++ b/scripts/fuzzy.js @@ -1,7 +1,7 @@ -setTimeout(() => { +(() => { let searchField = document.querySelector("#search"); - if (searchField === null) - return; + if (searchField === null) + return; let client = new XMLHttpRequest(); client.open("GET", "/pages.json"); @@ -11,7 +11,7 @@ setTimeout(() => { } client.send(); searchField.focus(); -}, 50); +})(); function fuzzyInit(pagesFileName) { if (pagesFileName == "") @@ -35,49 +35,52 @@ function fuzzyInit(pagesFileName) { searchField.addEventListener("keyup", (e) => { - let searchValue = searchField.value ? searchField.value.toLowerCase() : ""; + let searchValue = searchField.value ? searchField.value.toLowerCase() : ""; - if (e.code === "Enter") { - if (resultBlock.childNodes === null - || resultBlock.childNodes[0] === null - || resultBlock.childNodes[0].href === undefined) - return; - - window.location = resultBlock.childNodes[0].href; + if (e.code === "Enter") { + if (resultBlock.childNodes === null + || resultBlock.childNodes[0] === null + || resultBlock.childNodes[0].href === undefined) return; - } - // help - if (searchValue === "?" || searchValue === "help") { - resultBlock.innerHTML = "

Help

Enter a page or directory name.
If do not know any, clear the search field to list everything.
Using the Enter key would take you to the first page in list, if the list is not empty.
Alternatively, use the Up and Down arrow keys to select the page you want and use the Enter key." - return; - } - - let results = []; - for (const [i, page] of pages.entries()) { - ret = fuzzySearch(page.title, searchValue); - if (ret === null) - continue; - results.push({formatted: ret.formatted, link: page.link, score: ret.score}); - } - - results.sort((x, y) => {return x.score - y.score}); - - resultBlock.innerHTML = ""; - for (const res of results) { - linkBlock = document.createElement("a"); - linkBlock.classList.add("hyperlink"); - linkBlock.href = res.link; - linkBlock.innerHTML = `
${res.formatted}
`; - resultBlock.appendChild(linkBlock); - } - - if (results.length <= 0) - resultBlock.innerHTML = "Unknown command or no matching pages found." + window.location = resultBlock.childNodes[0].href; + return; } - ); + + // help + if (searchValue === "?" || searchValue === "help") { + resultBlock.innerHTML = "

Help

Enter a page or directory name.
If do not know any, clear the search field to list everything.
Using the Enter key would take you to the first page in list, if the list is not empty.
Alternatively, use the Up and Down arrow keys to select the page you want and use the Enter key.
Use Backspace to go back to the search." + return; + } + + let results = []; + for (const [i, page] of pages.entries()) { + ret = fuzzySearch(page.title, searchValue); + if (ret === null) + continue; + results.push({formatted: ret.formatted, link: page.link, score: ret.score}); + } + + results.sort((x, y) => {return x.score - y.score}); + + resultBlock.innerHTML = ""; + for (const res of results) { + linkBlock = document.createElement("a"); + linkBlock.classList.add("hyperlink"); + linkBlock.href = res.link; + linkBlock.innerHTML = `
${res.formatted}
`; + resultBlock.appendChild(linkBlock); + } + + if (results.length <= 0) + resultBlock.innerHTML = "Unknown command or no matching pages found." + }); document.body.addEventListener("keydown", (e) => { + if (e.code === "Backspace") { + searchField.focus(); + } + if (e.code === "ArrowDown" || e.code === "ArrowUp") { if (resultBlock.childNodes === null) return;