Skip to content

Commit

Permalink
Trigger search event when user presses Ctrl + K (#804)
Browse files Browse the repository at this point in the history
Co-authored-by: Mimi <[email protected]>
  • Loading branch information
penxian and stevenjoezhang authored Sep 20, 2024
1 parent 0708dc7 commit 1b2b4f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions source/js/third-party/search/algolia-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ document.addEventListener('DOMContentLoaded', () => {
});
document.querySelector('.popup-btn-close').addEventListener('click', onPopupClose);
document.addEventListener('pjax:success', onPopupClose);
window.addEventListener('keydown', event => {
if ((event.ctrlKey || event.metaKey) && event.key === 'k') {
event.preventDefault();
document.body.classList.add('search-active');
setTimeout(() => input.focus(), 500);
}
});
window.addEventListener('keyup', event => {
if (event.key === 'Escape') {
onPopupClose();
Expand Down
8 changes: 8 additions & 0 deletions source/js/third-party/search/local-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ document.addEventListener('DOMContentLoaded', () => {
localSearch.highlightSearchWords(document.querySelector('.post-body'));
onPopupClose();
});
window.addEventListener('keydown', event => {
if ((event.ctrlKey || event.metaKey) && event.key === 'k') {
event.preventDefault();
document.body.classList.add('search-active');
setTimeout(() => input.focus(), 500);
if (!localSearch.isfetched) localSearch.fetchData();
}
});
window.addEventListener('keyup', event => {
if (event.key === 'Escape') {
onPopupClose();
Expand Down

0 comments on commit 1b2b4f2

Please sign in to comment.