Skip to content

Commit

Permalink
Merge pull request #329 from MohamedRejeb/fix-crash
Browse files Browse the repository at this point in the history
Fix StringIndexOutOfBoundsException on add new line
  • Loading branch information
MohamedRejeb authored Aug 20, 2024
2 parents 5e75ead + 686dba3 commit 9d7dac7
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public class RichTextState internal constructor(
if (textRange.collapsed) {
val richSpan = getRichSpanByTextIndex(textIndex = textRange.min - 1)

richSpan
richSpan
?.fullSpanStyle
?: RichSpanStyle.DefaultSpanStyle
} else {
Expand Down Expand Up @@ -1850,7 +1850,8 @@ public class RichTextState internal constructor(
parent?.children?.indexOf(richSpan) ?: richSpan.paragraph.children.indexOf(richSpan)
var isRemoved = false

val isRichSpanStylingEmpty = richSpan.spanStyle == SpanStyle() && richSpan.richSpanStyle is RichSpanStyle.Default
val isRichSpanStylingEmpty =
richSpan.spanStyle == SpanStyle() && richSpan.richSpanStyle is RichSpanStyle.Default

if (middleText.isNotEmpty()) {
if (
Expand Down Expand Up @@ -2250,15 +2251,23 @@ public class RichTextState internal constructor(
else
startIndex - richSpan.textRange.min


newRichParagraph.type.startRichSpan.paragraph = newRichParagraph
newRichParagraph.type.startRichSpan.textRange = TextRange(
0,
newRichParagraph.type.startRichSpan.text.length
)

val beforeText = if (textStartIndex > 0) richSpan.text.substring(0, textStartIndex) else "" // + ' '
val afterText = richSpan.text.substring(textStartIndex + 1)
val beforeText =
richSpan.text.substring(
startIndex = 0,
endIndex = textStartIndex
.coerceIn(0, richSpan.text.length)
)
val afterText =
richSpan.text.substring(
startIndex = (textStartIndex + 1)
.coerceIn(0, richSpan.text.length)
)

richSpan.text = beforeText
richSpan.textRange = TextRange(
Expand Down

0 comments on commit 9d7dac7

Please sign in to comment.