Skip to content

Commit

Permalink
Add NexT.utils.debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed May 7, 2024
1 parent e2ede14 commit 6e89bb9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
7 changes: 4 additions & 3 deletions source/css/_common/components/third-party/search.styl
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ if (hexo-config('algolia_search.enable')) {
}

.pagination.algolia-pagination {
// Override default style of pagination
margin: 40px 0;
padding: 0;
// Override the default style of pagination
// Put pagination at the bottom when there is sufficient height
margin-top: auto;
padding: 40px 0;
// Override motion
// https://github.com/theme-next/hexo-theme-next/issues/537
visibility: visible;
Expand Down
20 changes: 18 additions & 2 deletions source/js/third-party/search/algolia-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ document.addEventListener('DOMContentLoaded', () => {
return result;
};

let isSearching = false;
let pendingQuery = null;

const searchAlgolia = async(searchText, page = 0) => {
if (isSearching) {
pendingQuery = { searchText, page };
return;
}
isSearching = true;
const startTime = Date.now();
const data = await index.search(searchText, {
page,
attributesToRetrieve : ['permalink'],
Expand All @@ -34,7 +43,7 @@ document.addEventListener('DOMContentLoaded', () => {
} else {
const stats = CONFIG.i18n.hits_time
.replace('${hits}', data.nbHits)
.replace('${time}', data.processingTimeMS);
.replace('${time}', Date.now() - startTime);
let pagination = '';
if (data.nbPages > 1) {
pagination += '<nav class="pagination algolia-pagination">';
Expand Down Expand Up @@ -63,6 +72,12 @@ document.addEventListener('DOMContentLoaded', () => {
});
});
}
isSearching = false;
if (pendingQuery !== null && (pendingQuery.searchText !== searchText || pendingQuery.page !== page)) {
const { searchText, page } = pendingQuery;
pendingQuery = null;
searchAlgolia(searchText, page);
}
};

const inputEventFunction = async() => {
Expand All @@ -75,7 +90,8 @@ document.addEventListener('DOMContentLoaded', () => {
await searchAlgolia(searchText, 0);
};

input.addEventListener('input', inputEventFunction);
const debouncedSearch = NexT.utils.debounce(inputEventFunction, 500);

Check failure on line 93 in source/js/third-party/search/algolia-search.js

View workflow job for this annotation

GitHub Actions / linter

'NexT' is not defined

Check failure on line 93 in source/js/third-party/search/algolia-search.js

View workflow job for this annotation

GitHub Actions / linter

'NexT' is not defined
input.addEventListener('input', debouncedSearch);

// Handle and trigger popup window
document.querySelectorAll('.popup-trigger').forEach(element => {
Expand Down
9 changes: 9 additions & 0 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,5 +498,14 @@ NexT.utils = {
});
intersectionObserver.observe(element);
});
},

debounce: function(func, wait) {
let timeout;
return function(...args) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
}
};

0 comments on commit 6e89bb9

Please sign in to comment.