perf: optimize local search
This commit is contained in:
parent
0abad9b9a8
commit
fd2c99910a
|
@ -1,8 +1,8 @@
|
||||||
<div class="search-pop-overlay">
|
<div class="search-pop-overlay">
|
||||||
<div class="popup search-popup">
|
<div class="popup search-popup">
|
||||||
<div class="search-header">
|
<div class="search-header">
|
||||||
<span class="search-icon">
|
<span class="search-input-field-pre">
|
||||||
<i class="fas fa-search"></i>
|
<i class="fas fa-keyboard"></i>
|
||||||
</span>
|
</span>
|
||||||
<div class="search-input-container">
|
<div class="search-input-container">
|
||||||
<input autocomplete="off"
|
<input autocomplete="off"
|
||||||
|
|
|
@ -37,14 +37,15 @@ $icon-size = 1.2rem;
|
||||||
display: flex;
|
display: flex;
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
|
|
||||||
.search-icon, .popup-btn-close {
|
.search-input-field-pre, .popup-btn-close {
|
||||||
font-size: $icon-size;
|
font-size: $icon-size;
|
||||||
padding: 0 10px;
|
padding: 0 10px;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.search-icon {
|
.search-input-field-pre {
|
||||||
|
cursor: pointer;
|
||||||
color: var(--third-text-color);
|
color: var(--third-text-color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,24 +17,9 @@ KEEP.initLocalSearch = () => {
|
||||||
} else if (searchPath.endsWith('json')) {
|
} else if (searchPath.endsWith('json')) {
|
||||||
isXml = false;
|
isXml = false;
|
||||||
}
|
}
|
||||||
const input = document.querySelector('.search-input');
|
const searchInputDom = document.querySelector('.search-input');
|
||||||
const resultContent = document.getElementById('search-result');
|
const resultContent = document.getElementById('search-result');
|
||||||
|
|
||||||
// Ref: https://github.com/ForbesLindesay/unescape-html
|
|
||||||
const unescapeHtml = html => {
|
|
||||||
return String(html)
|
|
||||||
.replace(/"/g, '"')
|
|
||||||
.replace(/'/g, '\'')
|
|
||||||
.replace(/:/g, ':')
|
|
||||||
// Replace all the other &#x; chars
|
|
||||||
.replace(/&#(\d+);/g, (m, p) => {
|
|
||||||
return String.fromCharCode(p);
|
|
||||||
})
|
|
||||||
.replace(/</g, '<')
|
|
||||||
.replace(/>/g, '>')
|
|
||||||
.replace(/&/g, '&');
|
|
||||||
};
|
|
||||||
|
|
||||||
const getIndexByWord = (word, text, caseSensitive) => {
|
const getIndexByWord = (word, text, caseSensitive) => {
|
||||||
let wordLen = word.length;
|
let wordLen = word.length;
|
||||||
if (wordLen === 0) return [];
|
if (wordLen === 0) return [];
|
||||||
|
@ -105,7 +90,7 @@ KEEP.initLocalSearch = () => {
|
||||||
|
|
||||||
const inputEventFunction = () => {
|
const inputEventFunction = () => {
|
||||||
if (!isfetched) return;
|
if (!isfetched) return;
|
||||||
let searchText = input.value.trim().toLowerCase();
|
let searchText = searchInputDom.value.trim().toLowerCase();
|
||||||
let keywords = searchText.split(/[-\s]+/);
|
let keywords = searchText.split(/[-\s]+/);
|
||||||
if (keywords.length > 1) {
|
if (keywords.length > 1) {
|
||||||
keywords.push(searchText);
|
keywords.push(searchText);
|
||||||
|
@ -243,9 +228,6 @@ KEEP.initLocalSearch = () => {
|
||||||
datas = datas.filter(data => data.title).map(data => {
|
datas = datas.filter(data => data.title).map(data => {
|
||||||
data.title = data.title.trim();
|
data.title = data.title.trim();
|
||||||
data.content = data.content ? data.content.trim().replace(/<[^>]+>/g, '') : '';
|
data.content = data.content ? data.content.trim().replace(/<[^>]+>/g, '') : '';
|
||||||
if (KEEP.theme_config.local_search.unescape) {
|
|
||||||
data.content = unescapeHtml(data.content);
|
|
||||||
}
|
|
||||||
data.url = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
|
data.url = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
|
||||||
return data;
|
return data;
|
||||||
});
|
});
|
||||||
|
@ -259,18 +241,8 @@ KEEP.initLocalSearch = () => {
|
||||||
fetchData();
|
fetchData();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (KEEP.theme_config.local_search.trigger === 'auto') {
|
if (searchInputDom) {
|
||||||
if (input) {
|
searchInputDom.addEventListener('input', inputEventFunction);
|
||||||
input.addEventListener('input', inputEventFunction);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
document.querySelector('.search-icon').addEventListener('click', inputEventFunction);
|
|
||||||
input.addEventListener('keypress', event => {
|
|
||||||
if (event.key === 'Enter') {
|
|
||||||
inputEventFunction();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle and trigger popup window
|
// Handle and trigger popup window
|
||||||
|
@ -278,7 +250,7 @@ KEEP.initLocalSearch = () => {
|
||||||
element.addEventListener('click', () => {
|
element.addEventListener('click', () => {
|
||||||
document.body.style.overflow = 'hidden';
|
document.body.style.overflow = 'hidden';
|
||||||
document.querySelector('.search-pop-overlay').style.display = 'block';
|
document.querySelector('.search-pop-overlay').style.display = 'block';
|
||||||
input.focus();
|
searchInputDom.focus();
|
||||||
if (!isfetched) fetchData();
|
if (!isfetched) fetchData();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -294,6 +266,11 @@ KEEP.initLocalSearch = () => {
|
||||||
onPopupClose();
|
onPopupClose();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
document.querySelector('.search-input-field-pre').addEventListener('click', () => {
|
||||||
|
searchInputDom.value = '';
|
||||||
|
searchInputDom.focus();
|
||||||
|
inputEventFunction();
|
||||||
|
});
|
||||||
document.querySelector('.popup-btn-close').addEventListener('click', onPopupClose);
|
document.querySelector('.popup-btn-close').addEventListener('click', onPopupClose);
|
||||||
window.addEventListener('pjax:success', onPopupClose);
|
window.addEventListener('pjax:success', onPopupClose);
|
||||||
window.addEventListener('keyup', event => {
|
window.addEventListener('keyup', event => {
|
||||||
|
|
Loading…
Reference in New Issue