Fix cursor jumping to wrong position after insert

This commit is contained in:
Dominik Reh
2022-10-12 14:21:36 +02:00
parent a782f951a6
commit 5fa179dde1

View File

@@ -172,13 +172,13 @@ function insertTextAtCursor(text, tagword) {
let direction = prompt.substring(cursorPos, cursorPos + tagword.length) === tagword ? 1 : -1;
if (direction === 1) {
promptTextbox.value = prompt.substring(0, cursorPos) + sanitizedText + optionalComma + prompt.substring(cursorPos + tagword.length)
// Update cursor position to after the inserted text
promptTextbox.selectionStart = cursorPos + sanitizedText.length + optionalComma.length;
} else {
promptTextbox.value = prompt.substring(0, cursorPos - tagword.length) + sanitizedText + optionalComma + prompt.substring(cursorPos)
promptTextbox.selectionStart = cursorPos - tagword.length + sanitizedText.length + optionalComma.length;
}
prompt = promptTextbox.value;
// Update cursor position to after the inserted text
promptTextbox.selectionStart = cursorPos + sanitizedText.length;
promptTextbox.selectionEnd = promptTextbox.selectionStart;
// Hide results after inserting