From 4256a790fc24e161098e07414c02728068924575 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 15 Sep 2019 16:33:49 -0700 Subject: [PATCH] fix: fix cursor position jumping on autocomplete (#1490) fixes #56 --- src/routes/_components/compose/ComposeInput.html | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/routes/_components/compose/ComposeInput.html b/src/routes/_components/compose/ComposeInput.html index c5b4693a..f271efe6 100644 --- a/src/routes/_components/compose/ComposeInput.html +++ b/src/routes/_components/compose/ComposeInput.html @@ -91,7 +91,20 @@ this.observe('text', text => { const { rawText } = this.get() 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 }) + if (typeof newSelectionStart === 'number' && newSelectionStart > 0) { + textarea.selectionStart = textarea.selectionEnd = newSelectionStart + } // this next autosize is required to resize after // the user clicks the "toot" button mark('autosize.update()')