mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2024-11-23 10:21:46 +01:00
feat(search): return results at typing
This commit is contained in:
parent
8aeb562bb3
commit
84a15e1604
1 changed files with 16 additions and 6 deletions
|
@ -63,7 +63,8 @@ const tagsToReplace = {
|
|||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"'
|
||||
'"': '"',
|
||||
'…': '…'
|
||||
};
|
||||
|
||||
function replaceTag(tag) {
|
||||
|
@ -84,7 +85,7 @@ async function getData() {
|
|||
return data;
|
||||
}
|
||||
|
||||
function updateQueryString(keywords: string) {
|
||||
function updateQueryString(keywords: string, replaceState = false) {
|
||||
const pageURL = new URL(window.location.toString());
|
||||
|
||||
if (keywords === '') {
|
||||
|
@ -94,7 +95,12 @@ function updateQueryString(keywords: string) {
|
|||
pageURL.searchParams.set('keyword', keywords);
|
||||
}
|
||||
|
||||
window.history.pushState('', '', pageURL.toString());
|
||||
if (replaceState) {
|
||||
window.history.replaceState('', '', pageURL.toString());
|
||||
}
|
||||
else {
|
||||
window.history.pushState('', '', pageURL.toString());
|
||||
}
|
||||
}
|
||||
|
||||
function bindQueryStringChange() {
|
||||
|
@ -118,11 +124,12 @@ function handleQueryString() {
|
|||
|
||||
function bindSearchForm() {
|
||||
let lastSearch = '';
|
||||
searchForm.addEventListener('submit', async (e) => {
|
||||
|
||||
const eventHandler = (e) => {
|
||||
e.preventDefault();
|
||||
const keywords = searchInput.value;
|
||||
|
||||
updateQueryString(keywords);
|
||||
updateQueryString(keywords, true);
|
||||
|
||||
if (keywords === '') {
|
||||
return clear();
|
||||
|
@ -132,7 +139,10 @@ function bindSearchForm() {
|
|||
lastSearch = keywords;
|
||||
|
||||
doSearch(keywords.split(' '));
|
||||
})
|
||||
}
|
||||
|
||||
searchInput.addEventListener('input', eventHandler);
|
||||
searchInput.addEventListener('compositionend', eventHandler);
|
||||
}
|
||||
|
||||
function clear() {
|
||||
|
|
Loading…
Reference in a new issue