11package com.swmansion.enriched.textinput.styles
22
33import android.text.Editable
4+ import android.text.Selection
45import android.text.Spannable
56import android.text.SpannableStringBuilder
67import android.text.Spanned
@@ -18,6 +19,7 @@ class ParametrizedStyles(
1819 private val view : EnrichedTextInputView ,
1920) {
2021 private var mentionStart: Int? = null
22+ private var mentionEnd: Int? = null
2123 private var isSettingLinkSpan = false
2224
2325 var mentionIndicators: Array <String > = emptyArray<String >()
@@ -297,6 +299,9 @@ class ParametrizedStyles(
297299 indicator = mentionIndicatorRegex.find(currentWord.text)?.value ? : " "
298300 }
299301
302+ mentionStart = finalStart
303+ mentionEnd = finalEnd
304+
300305 // Mirror iOS conflicting-styles behaviour: check the full candidate range for
301306 // a finalized mention span. If the span's stored text still matches what is in
302307 // the buffer the mention is intact — block the event (covers HTML-loaded
@@ -314,6 +319,7 @@ class ParametrizedStyles(
314319 }
315320 spannable.removeSpan(span)
316321 mentionStart = spanStart
322+ mentionEnd = spanEnd
317323 }
318324
319325 // Extract text without indicator
@@ -322,6 +328,7 @@ class ParametrizedStyles(
322328 // Means we are starting mention
323329 if (text.isEmpty()) {
324330 mentionStart = finalStart
331+ mentionEnd = finalEnd
325332 }
326333
327334 mentionHandler.onMention(indicator, text)
@@ -383,9 +390,12 @@ class ParametrizedStyles(
383390 }
384391
385392 val start = mentionStart ? : selectionStart
393+ val end = mentionEnd ? : selectionEnd
386394
387395 view.runAsATransaction {
388- spannable.replace(start, selectionEnd, text)
396+ Selection .setSelection(spannable, end)
397+
398+ spannable.replace(start, end, text)
389399
390400 val span = EnrichedInputMentionSpan (text, indicator, attributes, view.htmlStyle)
391401 val spanEnd = start + text.length
@@ -401,6 +411,7 @@ class ParametrizedStyles(
401411 view.mentionHandler?.reset()
402412 view.selection.validateStyles()
403413 mentionStart = null
414+ mentionEnd = null
404415 }
405416
406417 fun getStyleRange (): Pair <Int , Int > = view.selection?.getInlineSelection() ? : Pair (0 , 0 )
0 commit comments