Merge pull request #3437 from FryDay/issue-1801

Allow tab completion in middle of input
This commit is contained in:
Pavel Djundik 2019-10-04 18:53:03 +03:00 committed by GitHub
commit 8c19613bce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -202,13 +202,13 @@ function enableAutocomplete(inputRef) {
e.preventDefault();
const text = input.val();
if (input.get(0).selectionStart !== text.length) {
return;
}
const element = input.get(0);
if (tabCount === 0) {
lastMatch = text.split(/\s/).pop();
lastMatch = text
.substring(0, element.selectionStart)
.split(/\s/)
.pop();
if (lastMatch.length === 0) {
return;
@ -221,16 +221,19 @@ function enableAutocomplete(inputRef) {
}
}
const position = input.get(0).selectionStart - lastMatch.length;
const position = element.selectionStart - lastMatch.length;
const newMatch = nicksStrategy.replace(
[0, currentMatches[tabCount % currentMatches.length]],
position
);
const remainder = text.substr(element.selectionStart);
input.val(text.substr(0, position) + newMatch);
input.val(text.substr(0, position) + newMatch + remainder);
element.selectionStart -= remainder.length;
element.selectionEnd = element.selectionStart;
// Propagate change to Vue model
input.get(0).dispatchEvent(
element.dispatchEvent(
new CustomEvent("input", {
detail: "autocomplete",
})