diff --git a/android/src/main/java/com/swmansion/enriched/EnrichedStyleManipulator.kt b/android/src/main/java/com/swmansion/enriched/EnrichedStyleManipulator.kt index 5233c48f0..eaee258b0 100644 --- a/android/src/main/java/com/swmansion/enriched/EnrichedStyleManipulator.kt +++ b/android/src/main/java/com/swmansion/enriched/EnrichedStyleManipulator.kt @@ -10,7 +10,7 @@ import com.swmansion.enriched.styles.ParagraphStyles import com.swmansion.enriched.styles.ParametrizedStyles class EnrichedStyleManipulator( - val view: EnrichedTextInputView, + private val view: EnrichedTextInputView, ) { val inlineStyles: InlineStyles = InlineStyles(view) val paragraphStyles: ParagraphStyles = ParagraphStyles(view) @@ -29,26 +29,53 @@ class EnrichedStyleManipulator( name: TextStyle, start: Int, end: Int, - ): Boolean = - when (EnrichedSpans.getStyleGroup(name)) { - TextStyleGroup.INLINE -> inlineStyles.removeStyle(name, start, end) - TextStyleGroup.PARAGRAPH -> paragraphStyles.removeStyle(name, start, end) - TextStyleGroup.LIST -> listStyles.removeStyle(name, start, end) - TextStyleGroup.PARAMETRIZED -> parametrizedStyles.removeStyle(name, start, end) - null -> false + ): Boolean { + return when (EnrichedSpans.getStyleGroup(name)) { + TextStyleGroup.INLINE -> { + inlineStyles.removeStyle(name, start, end) + } + + TextStyleGroup.PARAGRAPH -> { + val removed = paragraphStyles.removeStyle(name, start, end) + view.correctScrollPositionIfNeeded() + + return removed + } + + TextStyleGroup.LIST -> { + listStyles.removeStyle(name, start, end) + } + + TextStyleGroup.PARAMETRIZED -> { + parametrizedStyles.removeStyle(name, start, end) + } + + null -> { + false + } } + } internal fun toggleStyle(name: TextStyle) { when (EnrichedSpans.getStyleGroup(name)) { - TextStyleGroup.INLINE -> inlineStyles.toggleStyle(name) + TextStyleGroup.INLINE -> { + inlineStyles.toggleStyle(name) + } - TextStyleGroup.PARAGRAPH -> paragraphStyles.toggleStyle(name) + TextStyleGroup.PARAGRAPH -> { + paragraphStyles.toggleStyle(name) + view.correctScrollPositionIfNeeded() + } - TextStyleGroup.LIST -> listStyles.toggleStyle(name) + TextStyleGroup.LIST -> { + listStyles.toggleStyle(name) + } TextStyleGroup.PARAMETRIZED, null, - -> Log.w("EnrichedTextInputView", "Unknown style: $name") + -> { + Log.w("EnrichedTextInputView", "Unknown style: $name") + } } } diff --git a/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt b/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt index 1453f181d..145616a63 100644 --- a/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt +++ b/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt @@ -280,9 +280,9 @@ class EnrichedTextInputView : AppCompatEditText { return super.onTouchEvent(event) } - override fun canScrollVertically(direction: Int): Boolean = scrollEnabled + override fun canScrollVertically(direction: Int): Boolean = scrollEnabled && super.canScrollVertically(direction) - override fun canScrollHorizontally(direction: Int): Boolean = scrollEnabled + override fun canScrollHorizontally(direction: Int): Boolean = scrollEnabled && super.canScrollHorizontally(direction) override fun onScrollChanged( horiz: Int, @@ -653,7 +653,7 @@ class EnrichedTextInputView : AppCompatEditText { // we need to override isLayoutRequested to force EditText to scroll to the end of the new text // immediately. // Ivan Ihnatsiuk: let android calculate layout to avoid jumping behavior when we insert a new line. -// override fun isLayoutRequested(): Boolean = false + // override fun isLayoutRequested(): Boolean = false fun afterUpdateTransaction() { updateTypeface() @@ -740,6 +740,14 @@ class EnrichedTextInputView : AppCompatEditText { dispatcher?.dispatchEvent(event) } + fun correctScrollPositionIfNeeded() { + if (!scrollEnabled) { + return + } + + forceScrollToSelection() + } + private fun forceScrollToSelection() { val textLayout = layout ?: return val cursorOffset = selectionStart diff --git a/android/src/main/java/com/swmansion/enriched/watchers/EnrichedTextWatcher.kt b/android/src/main/java/com/swmansion/enriched/watchers/EnrichedTextWatcher.kt index 78e85f6e7..5a6ba7a8f 100644 --- a/android/src/main/java/com/swmansion/enriched/watchers/EnrichedTextWatcher.kt +++ b/android/src/main/java/com/swmansion/enriched/watchers/EnrichedTextWatcher.kt @@ -70,6 +70,7 @@ class EnrichedTextWatcher( } } } + view.correctScrollPositionIfNeeded() } private fun applyStyles(s: Editable) {