@@ -52,9 +52,6 @@ class EnrichedTextInputView : AppCompatEditText {
5252 val paragraphStyles: ParagraphStyles ? = ParagraphStyles (this )
5353 val listStyles: ListStyles ? = ListStyles (this )
5454 val parametrizedStyles: ParametrizedStyles ? = ParametrizedStyles (this )
55- // Sometimes setting up style triggers many changes in sequence
56- // Eg. removing conflicting styles -> changing text -> applying spans
57- // In such scenario we want to prevent from handling side effects (eg. onTextChanged)
5855 var isDuringTransaction: Boolean = false
5956 var isRemovingMany: Boolean = false
6057
@@ -241,18 +238,17 @@ class EnrichedTextInputView : AppCompatEditText {
241238
242239 fun setValue (value : CharSequence? ) {
243240 if (value == null ) return
244- isDuringTransaction = true
245241
246- val newText = parseText(value)
247- setText(newText)
248-
249- // Assign SpanWatcher one more time as our previous spannable has been replaced
250- addSpanWatcher(EnrichedSpanWatcher (this ))
242+ runAsATransaction {
243+ val newText = parseText(value)
244+ setText(newText)
251245
252- // Scroll to the last line of text
253- setSelection(text?.length ? : 0 )
246+ // Assign SpanWatcher one more time as our previous spannable has been replaced
247+ addSpanWatcher( EnrichedSpanWatcher ( this ) )
254248
255- isDuringTransaction = false
249+ // Scroll to the last line of text
250+ setSelection(text?.length ? : 0 )
251+ }
256252 }
257253
258254 fun setAutoFocus (autoFocus : Boolean ) {
@@ -460,13 +456,13 @@ class EnrichedTextInputView : AppCompatEditText {
460456 val end = selection?.end ? : 0
461457 val lengthBefore = text?.length ? : 0
462458
463- isDuringTransaction = true
464- val targetRange = getTargetRange(name)
465- val removed = removeStyle(style, targetRange.first, targetRange.second)
466- if (removed) {
467- spanState?.setStart(style, null )
459+ runAsATransaction {
460+ val targetRange = getTargetRange(name)
461+ val removed = removeStyle(style, targetRange.first, targetRange.second)
462+ if (removed) {
463+ spanState?.setStart(style, null )
464+ }
468465 }
469- isDuringTransaction = false
470466
471467 val lengthAfter = text?.length ? : 0
472468 val charactersRemoved = lengthBefore - lengthAfter
@@ -524,6 +520,18 @@ class EnrichedTextInputView : AppCompatEditText {
524520 parametrizedStyles?.setMentionSpan(text, indicator, attributes)
525521 }
526522
523+ // Sometimes setting up style triggers many changes in sequence
524+ // Eg. removing conflicting styles -> changing text -> applying spans
525+ // In such scenario we want to prevent from handling side effects (eg. onTextChanged)
526+ fun runAsATransaction (block : () -> Unit ) {
527+ try {
528+ isDuringTransaction = true
529+ block()
530+ } finally {
531+ isDuringTransaction = false
532+ }
533+ }
534+
527535 override fun onAttachedToWindow () {
528536 super .onAttachedToWindow()
529537
0 commit comments