fix: fix cursor position jumping on autocomplete (#1490)

fixes #56
This commit is contained in:
Nolan Lawson 2019-09-15 16:33:49 -07:00 committed by GitHub
parent 1a8de05083
commit 4256a790fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -91,7 +91,20 @@
this.observe('text', text => { this.observe('text', text => {
const { rawText } = this.get() const { rawText } = this.get()
if (rawText !== text) { if (rawText !== text) {
let newSelectionStart
if (!firstTime) {
const { selectionStart, selectionEnd } = textarea
if (selectionStart > 0 && selectionStart === selectionEnd) {
// Preserve cursor position when doing an autosuggest.
// Note that we don't need to do anything special to measure length here, because
// the selectionStart value is not emoji-aware.
newSelectionStart = (text.length - rawText.length) + selectionStart
}
}
this.set({ rawText: text }) this.set({ rawText: text })
if (typeof newSelectionStart === 'number' && newSelectionStart > 0) {
textarea.selectionStart = textarea.selectionEnd = newSelectionStart
}
// this next autosize is required to resize after // this next autosize is required to resize after
// the user clicks the "toot" button // the user clicks the "toot" button
mark('autosize.update()') mark('autosize.update()')