Skip to content

Commit 651a237

Browse files
authored
fix(android): merging and splitting paragraph styles (#428)
# Summary Fixes: #402 ## Test Plan - enter 3 line code block text - toggle style in second line - notice that style is removed only for second line ## Screenshots / Videos https://github.com/user-attachments/assets/62b25441-5a78-4512-9303-cbc8d7f5f3e9 ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ❌ | | Android | ✅ |
1 parent 15034d1 commit 651a237

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

android/src/main/java/com/swmansion/enriched/textinput/styles/ParagraphStyles.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,34 @@ class ParagraphStyles(
108108
spannable.setSpan(span, safeStart, safeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
109109
}
110110

111+
// Removes spans of the given type in the specified range.
112+
// If the removed span intersects with the range, it will be split and the remaining part will be re-applied after the removal
113+
// Returns true if any spans were removed, false otherwise
111114
private fun <T> removeSpansForRange(
112115
spannable: Spannable,
113116
start: Int,
114117
end: Int,
115118
clazz: Class<T>,
116119
): Boolean {
117120
val ssb = spannable as SpannableStringBuilder
118-
var finalStart = start
119-
var finalEnd = end
120-
121121
val spans = ssb.getSpans(start, end, clazz)
122122
if (spans.isEmpty()) return false
123123

124124
for (span in spans) {
125-
finalStart = ssb.getSpanStart(span).coerceAtMost(finalStart)
126-
finalEnd = ssb.getSpanEnd(span).coerceAtLeast(finalEnd)
127-
125+
val spanStart = ssb.getSpanStart(span)
126+
val spanEnd = ssb.getSpanEnd(span)
128127
ssb.removeSpan(span)
129-
}
130128

131-
ssb.removeZWS(finalStart, finalEnd)
129+
if (spanStart < start) {
130+
setSpan(ssb, clazz, spanStart, start - 1)
131+
}
132+
133+
if (spanEnd > end) {
134+
setSpan(ssb, clazz, end + 1, spanEnd)
135+
}
136+
}
132137

138+
ssb.removeZWS(start, end)
133139
return true
134140
}
135141

apps/example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ const htmlStyle: HtmlStyle = {
437437
codeblock: {
438438
color: 'green',
439439
borderRadius: 8,
440-
backgroundColor: 'aquamarine',
440+
backgroundColor: 'grey',
441441
},
442442
code: {
443443
color: 'purple',

0 commit comments

Comments
 (0)