Skip to content

Commit 1b80ae2

Browse files
authored
android - fix auto correct removes styles (#95)
1 parent 7280da0 commit 1b80ae2

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

android/src/main/java/com/swmansion/reactnativerichtexteditor/ReactNativeRichTextEditorView.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
5252
val listStyles: ListStyles? = ListStyles(this)
5353
val parametrizedStyles: ParametrizedStyles? = ParametrizedStyles(this)
5454
var isSettingValue: Boolean = false
55+
var isRemovingMany: Boolean = false
5556

5657
val mentionHandler: MentionHandler? = MentionHandler(this)
5758
var richTextStyle: RichTextStyle = RichTextStyle(this, null)

android/src/main/java/com/swmansion/reactnativerichtexteditor/utils/EditorSelection.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class EditorSelection(private val editorView: ReactNativeRichTextEditorView) {
3535

3636
if (isZeroWidthSelection() && !editorView.isSettingValue) {
3737
editorView.setSelection(start + 1)
38-
return
38+
shouldValidateStyles = false
3939
}
4040

4141
if (!shouldValidateStyles) return
@@ -62,8 +62,15 @@ class EditorSelection(private val editorView: ReactNativeRichTextEditorView) {
6262
fun validateStyles() {
6363
val state = editorView.spanState ?: return
6464

65-
for ((style, config) in EditorSpans.inlineSpans) {
66-
state.setStart(style, getInlineStyleStart(config.clazz))
65+
// We don't validate inline styles when removing many characters at once
66+
// We don't want to remove styles on auto-correction
67+
// If user removes many characters at once, we want to keep the styles config
68+
if (!editorView.isRemovingMany) {
69+
for ((style, config) in EditorSpans.inlineSpans) {
70+
state.setStart(style, getInlineStyleStart(config.clazz))
71+
}
72+
} else {
73+
editorView.isRemovingMany = false
6774
}
6875

6976
for ((style, config) in EditorSpans.paragraphSpans) {

android/src/main/java/com/swmansion/reactnativerichtexteditor/watchers/EditorTextWatcher.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class EditorTextWatcher(private val editorView: ReactNativeRichTextEditorView) :
1818
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
1919
endCursorPosition = start + count
2020
editorView.layoutManager.measureSize(s ?: "")
21+
editorView.isRemovingMany = !editorView.isSettingValue && before > count + 1
2122
}
2223

2324
override fun afterTextChanged(s: Editable?) {

0 commit comments

Comments
 (0)