Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flexsearch arrow key navigation error #509

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,23 @@ Source:

document.addEventListener('keydown',suggestionFocus);

function suggestionFocus(e){
function suggestionFocus(e) {
const suggestionsHidden = suggestions.classList.contains('d-none');
if (suggestionsHidden) return;

const focusableSuggestions= suggestions.querySelectorAll('a');
const focusable= [...focusableSuggestions];
const index = focusable.indexOf(document.activeElement);
const focusableSuggestions= [...suggestions.querySelectorAll('a')];
if (focusableSuggestions.length === 0) return;

const keyDefault = suggestions.classList.contains('d-none');
const index = focusableSuggestions.indexOf(document.activeElement);

let nextIndex = 0;

if ((e.keyCode === 38) && (!keyDefault)) {
if (e.key === "ArrowUp") {
e.preventDefault();
nextIndex= index > 0 ? index-1 : 0;
const nextIndex = index > 0 ? index - 1 : 0;
focusableSuggestions[nextIndex].focus();
}
else if ((e.keyCode === 40) && (!keyDefault)) {
else if (e.key === "ArrowDown") {
e.preventDefault();
nextIndex= index+1 < focusable.length ? index+1 : index;
const nextIndex= index + 1 < focusableSuggestions.length ? index + 1 : index;
focusableSuggestions[nextIndex].focus();
}

Expand Down Expand Up @@ -127,11 +126,9 @@ Source:

//flatSearch now returns results for each index field. create a single list
const flatResults = {}; //keyed by href to dedupe results
results.forEach(result=>{
result.result.forEach(r=>{
flatResults[r.doc.href] = r.doc;
});
});
for (const result of results.flatMap(r => r.result)) {
flatResults[result.doc.href] = result.doc;
}

//construct a list of suggestions list
for(const href in flatResults) {
Expand Down