2021-02-14 15:50:53 +00:00
|
|
|
(function () {
|
2022-08-01 19:01:12 +00:00
|
|
|
let searchBar, results;
|
|
|
|
let shift = false;
|
|
|
|
const keymap = {
|
|
|
|
ArrowUp: goUp,
|
|
|
|
ArrowDown: goDown,
|
|
|
|
ShiftTab: goUp,
|
|
|
|
Tab: goDown,
|
|
|
|
k: goUp,
|
|
|
|
j: goDown,
|
|
|
|
'/': focusSearch,
|
|
|
|
};
|
|
|
|
let activeIdx = -1;
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
searchBar = document.querySelector('#search-bar');
|
|
|
|
results = document.querySelectorAll('#main>div>div>div>a');
|
|
|
|
});
|
|
|
|
|
|
|
|
document.addEventListener('keydown', (e) => {
|
|
|
|
if (e.key === 'Shift') {
|
|
|
|
shift = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.target.tagName === 'INPUT') return true;
|
|
|
|
if (typeof keymap[e.key] === 'function') {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
keymap[`${shift && e.key == 'Tab' ? 'Shift' : ''}${e.key}`]();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
document.addEventListener('keyup', (e) => {
|
|
|
|
if (e.key === 'Shift') {
|
|
|
|
shift = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function goUp () {
|
|
|
|
if (activeIdx > 0) focusResult(activeIdx - 1);
|
|
|
|
else focusSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
function goDown () {
|
|
|
|
if (activeIdx < results.length - 1) focusResult(activeIdx + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function focusResult (idx) {
|
|
|
|
activeIdx = idx;
|
|
|
|
results[activeIdx].scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' });
|
|
|
|
results[activeIdx].focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function focusSearch () {
|
2023-02-21 16:36:38 +00:00
|
|
|
if (window.usingCalculator) {
|
|
|
|
// if this function exists, it means the calculator widget has been displayed
|
|
|
|
if (usingCalculator()) return;
|
|
|
|
}
|
2022-08-01 19:01:12 +00:00
|
|
|
activeIdx = -1;
|
|
|
|
searchBar.focus();
|
|
|
|
}
|
2021-02-14 15:50:53 +00:00
|
|
|
}());
|