Skip to content

Commit

Permalink
search: fix oddities in keyboard handling #463
Browse files Browse the repository at this point in the history
  • Loading branch information
McShelby committed Feb 3, 2023
1 parent 00faf15 commit fc040e7
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions static/js/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- delete search term when suggestions are closed
McShelby/hugo-theme-relearn#452
- register focus event ignoring minChars because that doesn't make sense
McShelby/hugo-theme-relearn#452
- on ESC, close overlay without deleting search term if overlay is open
- on ESC, delete search term if overlay is closed
- on UP, preventDefault to keep cursor in position
Copyright (c) 2014 Simon Steinberger / Pixabay
GitHub: https://github.com/Pixabay/JavaScript-autoComplete
Expand Down Expand Up @@ -162,6 +166,7 @@ var autoComplete = (function(){
var key = window.event ? e.keyCode : e.which;
// down (40), up (38)
if ((key == 40 || key == 38) && that.sc.innerHTML) {
e.preventDefault();
var next, sel = that.sc.querySelector('.autocomplete-suggestion.selected');
if (!sel) {
next = (key == 40) ? that.sc.querySelector('.autocomplete-suggestion') : that.sc.childNodes[that.sc.childNodes.length - 1]; // first : last
Expand All @@ -185,22 +190,20 @@ var autoComplete = (function(){
}
// esc
else if (key == 27) {
if( that.sc.style.display != 'none' ){
if (that.sc.style.display != 'none') {
// just close the overlay if it's open, and prevent other listeners
// from recognizing it; this is not for you!
e.preventDefault();
e.stopImmediatePropagation();
that.sc.style.display = 'none';
var sel = that.sc.querySelector('.autocomplete-suggestion.selected');
if (sel) {
e.preventDefault();
setTimeout(function(){
that.value = that.last_val;
that.focus();
that.sc.style.display = 'none';
}, 0);
}
else{
that.value = '';
that.sc.style.display = 'none';
that.focus();
}
}
else{
else {
// if no overlay is open, we want to remove the search term and also
// want other listeners to recognize it
that.value = '';
}
}
Expand Down

0 comments on commit fc040e7

Please sign in to comment.