mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
Close autocomplete window when selection moved outside active term by click
This commit is contained in:
parent
f31fcf86b3
commit
f1aec2fd58
1 changed files with 9 additions and 4 deletions
|
@ -66,6 +66,13 @@ function changeSelected(firstOrLast, current, sibling) {
|
|||
}
|
||||
}
|
||||
|
||||
function isSelectionOutsideCurrentTerm() {
|
||||
const selectionIndex = Math.min(inputField.selectionStart, inputField.selectionEnd);
|
||||
const [startIndex, endIndex] = selectedTerm[0];
|
||||
|
||||
return startIndex > selectionIndex || endIndex < selectionIndex;
|
||||
}
|
||||
|
||||
function keydownHandler(event) {
|
||||
const selected = document.querySelector('.autocomplete__item--selected'),
|
||||
firstItem = document.querySelector('.autocomplete__item:first-of-type'),
|
||||
|
@ -78,10 +85,7 @@ function keydownHandler(event) {
|
|||
// Close autocompletion popup when text cursor is outside current tag
|
||||
if (selectedTerm && firstItem && (event.keyCode === 37 || event.keyCode === 39)) { // ArrowLeft || ArrowRight
|
||||
requestAnimationFrame(() => {
|
||||
const selectionIndex = Math.min(inputField.selectionStart, inputField.selectionEnd);
|
||||
const [startIndex, endIndex] = selectedTerm[0];
|
||||
|
||||
if (startIndex > selectionIndex || endIndex < selectionIndex) removeParent();
|
||||
if (isSelectionOutsideCurrentTerm()) removeParent();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -247,6 +251,7 @@ function listenAutocomplete() {
|
|||
// If there's a click outside the inputField, remove autocomplete
|
||||
document.addEventListener('click', event => {
|
||||
if (event.target && event.target !== inputField) removeParent();
|
||||
if (event.target === inputField && isSearchField() && isSelectionOutsideCurrentTerm()) removeParent();
|
||||
});
|
||||
|
||||
function fetchLocalAutocomplete(event) {
|
||||
|
|
Loading…
Reference in a new issue