Skip to content

Commit 767ed63

Browse files
authored
android - fix conflicting styles logic (#87)
1 parent 26d4f32 commit 767ed63

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,21 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
361361

362362
for (style in conflictingStyles) {
363363
if (spanState?.getStart(style) != null) {
364+
val start = selection?.start ?: 0
365+
val end = selection?.end ?: 0
366+
val lengthBefore = text?.length ?: 0
367+
364368
toggleStyle(style)
369+
370+
val lengthAfter = text?.length ?: 0
371+
val charactersRemoved = lengthBefore - lengthAfter
372+
val finalEnd = if (charactersRemoved > 0 && end > start) {
373+
end - charactersRemoved
374+
} else {
375+
end
376+
}
377+
378+
selection?.onSelection(start, finalEnd)
365379
}
366380
}
367381

android/src/main/java/com/swmansion/reactnativerichtexteditor/spans/EditorSpans.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ object EditorSpans {
9595
conflictingStyles = arrayOf(H1, H2, H3, BOLD, ITALIC, UNDERLINE, STRIKETHROUGH, UNORDERED_LIST, ORDERED_LIST, BLOCK_QUOTE),
9696
),
9797
UNORDERED_LIST to StylesMergingConfig(
98-
conflictingStyles = arrayOf(H1, H2, H3, ORDERED_LIST, CODE_BLOCK),
98+
conflictingStyles = arrayOf(H1, H2, H3, ORDERED_LIST, CODE_BLOCK, BLOCK_QUOTE),
9999
),
100100
ORDERED_LIST to StylesMergingConfig(
101-
conflictingStyles = arrayOf(H1, H2, H3, UNORDERED_LIST, CODE_BLOCK),
101+
conflictingStyles = arrayOf(H1, H2, H3, UNORDERED_LIST, CODE_BLOCK, BLOCK_QUOTE),
102102
),
103103
LINK to StylesMergingConfig(
104104
blockingStyles = arrayOf(INLINE_CODE, CODE_BLOCK, MENTION)

android/src/main/java/com/swmansion/reactnativerichtexteditor/styles/ParagraphStyles.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class ParagraphStyles(private val editorView: ReactNativeRichTextEditorView) {
166166
currentStart = currentEnd + 1
167167
}
168168

169-
editorView.spanState?.setStart(name, currentStart - 1)
169+
editorView.spanState?.setStart(name, start)
170170
setAndMergeSpans(spannable, type, start, currentEnd)
171171
}
172172
}

0 commit comments

Comments
 (0)