mirror of
https://github.com/tcsenpai/obsidiangarden_netlify.git
synced 2025-06-06 20:55:21 +00:00
Handle search better
This commit is contained in:
parent
85660bc077
commit
e93d772910
@ -6,22 +6,25 @@
|
|||||||
if(document.getElementById('globalsearch').classList.contains('active')){
|
if(document.getElementById('globalsearch').classList.contains('active')){
|
||||||
document.getElementById('globalsearch').classList.remove('active');
|
document.getElementById('globalsearch').classList.remove('active');
|
||||||
}else{
|
}else{
|
||||||
|
|
||||||
document.getElementById('globalsearch').classList.add('active');
|
document.getElementById('globalsearch').classList.add('active');
|
||||||
document.getElementById('term').focus();
|
document.getElementById('term').focus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function debounce(func, timeout = 300) {
|
function debounce(func, wait, immediate) {
|
||||||
let timer;
|
var timeout;
|
||||||
return(...args) => {
|
return function() {
|
||||||
clearTimeout(timer);
|
var context = this, args = arguments;
|
||||||
timer = setTimeout(() => {
|
var later = function() {
|
||||||
func.apply();
|
timeout = null;
|
||||||
}, timeout);
|
if (!immediate) func.apply(context, args);
|
||||||
|
};
|
||||||
|
var callNow = immediate && !timeout;
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = setTimeout(later, wait);
|
||||||
|
if (callNow) func.apply(context, args);
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
function setCorrectShortcut(){
|
function setCorrectShortcut(){
|
||||||
if(navigator.platform.toUpperCase().indexOf('MAC')>=0){
|
if(navigator.platform.toUpperCase().indexOf('MAC')>=0){
|
||||||
@ -111,7 +114,7 @@
|
|||||||
field = document.querySelector('#term');
|
field = document.querySelector('#term');
|
||||||
field.addEventListener('keydown', (e) => {
|
field.addEventListener('keydown', (e) => {
|
||||||
if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') {
|
if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') {
|
||||||
debounce(()=>search())();
|
debounce(search, 500, false)();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
resultsDiv = document.querySelector('#search-results');
|
resultsDiv = document.querySelector('#search-results');
|
||||||
@ -119,16 +122,20 @@
|
|||||||
const params = new URL(location.href).searchParams;
|
const params = new URL(location.href).searchParams;
|
||||||
if (params.get('q')) {
|
if (params.get('q')) {
|
||||||
field.setAttribute('value', params.get('q'));
|
field.setAttribute('value', params.get('q'));
|
||||||
|
toggleSearch();
|
||||||
search();
|
search();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
window.lastSearch = '';
|
||||||
async function search() {
|
async function search() {
|
||||||
let search = field
|
let search = field
|
||||||
.value
|
.value
|
||||||
.trim();
|
.trim();
|
||||||
if (!search)
|
if (!search)
|
||||||
return;
|
return;
|
||||||
|
if(search == lastSearch) return;
|
||||||
console.log(`search for ${search}`);
|
console.log(`search for ${search}`);
|
||||||
|
window.lastSearch = search;
|
||||||
let searchRequest = await fetch(`/api/search?term=${encodeURIComponent(search)}`);
|
let searchRequest = await fetch(`/api/search?term=${encodeURIComponent(search)}`);
|
||||||
let results = await searchRequest.json();
|
let results = await searchRequest.json();
|
||||||
let resultsHTML = '';
|
let resultsHTML = '';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user