Skip to content

Commit

Permalink
Merge pull request #318 from MohamedRejeb/1.x
Browse files Browse the repository at this point in the history
Fix bug when managing first line
  • Loading branch information
MohamedRejeb authored Aug 4, 2024
2 parents 6e025e0 + 7cd2c90 commit 67494e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ public class RichTextState internal constructor(
)
val previousIndex = startTypeIndex - 1

val activeRichSpan = getRichSpanByTextIndex(previousIndex)
val activeRichSpan = getOrCreateRichSpanByTextIndex(previousIndex)

if (activeRichSpan != null) {
if (startTypeIndex < activeRichSpan.textRange.min) {
Expand Down Expand Up @@ -2714,6 +2714,29 @@ public class RichTextState internal constructor(
return richParagraphList
}

private fun getOrCreateRichSpanByTextIndex(
textIndex: Int,
ignoreCustomFiltering: Boolean = false,
): RichSpan? {
val richSpan =
getRichSpanByTextIndex(
textIndex = textIndex,
ignoreCustomFiltering = ignoreCustomFiltering,
)

if (richSpan == null && textIndex < 0) {
val firstParagraph = richParagraphList.firstOrNull() ?: return null
val newRichSpan = RichSpan(
paragraph = firstParagraph,
text = "",
)
firstParagraph.children.add(0, newRichSpan)
return newRichSpan
}

return richSpan
}

/**
* Returns the [RichSpan] that contains the given [textIndex].
* If no [RichSpan] contains the given [textIndex], null is returned.
Expand All @@ -2728,12 +2751,6 @@ public class RichTextState internal constructor(
// If the text index is equal or less than 0, we can return the first non-empty child of the first paragraph.
if (textIndex < 0) {
val firstParagraph = richParagraphList.firstOrNull() ?: return null

// if (textIndex == 0 && firstParagraph.isEmpty() && richParagraphList.size > 1) {
// val secondParagraph = richParagraphList[1]
// return secondParagraph.getFirstNonEmptyChild(secondParagraph.type.startText.length)
// }

return firstParagraph.getFirstNonEmptyChild(firstParagraph.type.startText.length)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ internal class RichParagraph(
fun isNotBlank(ignoreStartRichSpan: Boolean = true): Boolean = !isBlank(ignoreStartRichSpan)

fun getFirstNonEmptyChild(offset: Int = -1): RichSpan? {
println("children: $children")
children.fastForEach { richSpan ->
if (richSpan.text.isNotEmpty()) {
if (offset != -1)
Expand Down

0 comments on commit 67494e9

Please sign in to comment.