From 7be177e9ab51f3b33fd60833ee3a7dd0aa30522b Mon Sep 17 00:00:00 2001 From: Lirusaito Date: Tue, 26 Mar 2019 05:09:21 -0400 Subject: [PATCH] Fix a text editor bug with selection being lost when url segments updated By doing it the right way, and calculating where the selection should be after the change is applied. Also no longer consider text editor modified if nothing was changed. Also update cursor position so it doesn't move to the wrong place on update --- indra/llui/lltexteditor.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/indra/llui/lltexteditor.cpp b/indra/llui/lltexteditor.cpp index 3996470be..e2e34538b 100644 --- a/indra/llui/lltexteditor.cpp +++ b/indra/llui/lltexteditor.cpp @@ -4477,9 +4477,22 @@ void LLTextEditor::replaceUrl(const std::string &url, { S32 start = seg->getStart(); S32 end = seg->getEnd(); - text = text.substr(0, start) + wlabel + text.substr(end, text.size() - end + 1); - seg->setEnd(start + wlabel.size()); - modified = true; + const auto& old_label = text.substr(start, end - start); + if (wlabel != old_label) + { + const auto difference = std::abs((S32)wlabel.length() - (S32)old_label.length()); + if (mSelectionEnd >= end) // Selection stays at/after end + { + mSelectionEnd += difference; + if (mSelectionStart >= end) + mSelectionStart += difference; + } + if (mCursorPos >= end) // Cursor stays at/after end + mCursorPos += difference; + text.replace(start, end - start, wlabel); + seg->setEnd(start + wlabel.size()); + modified = true; + } } /* Singu TODO: Icons with Urls? @@ -4508,7 +4521,6 @@ void LLTextEditor::replaceUrl(const std::string &url, { mWText = text; mTextIsUpToDate = FALSE; - deselect(); setCursorPos(mCursorPos); needsReflow(); }