Allow tab completion in middle of input

This commit is contained in:
Jordan Day 2019-10-04 09:32:52 -05:00
parent c39f0d01e6
commit 037fa6d114
1 changed files with 11 additions and 8 deletions

View File

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