Skip to content

Commit

Permalink
Fix issue with android auto-fill feature (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedRejeb authored Sep 21, 2024
1 parent a507cec commit 30e695a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ public class RichTextState internal constructor(
*/
private fun handleAddingCharacters() {
val typedCharsCount = tempTextFieldValue.text.length - textFieldValue.text.length
var startTypeIndex = tempTextFieldValue.selection.max - typedCharsCount
var startTypeIndex = textFieldValue.selection.min
val typedText = tempTextFieldValue.text.substring(
startIndex = startTypeIndex,
endIndex = startTypeIndex + typedCharsCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,83 @@ class RichTextStateTest {
assertEquals("Testing some text\nTesting some text", richTextState.toText())
}

@OptIn(ExperimentalRichTextApi::class)
@Test
fun testAddNewTextToFirstParagraphWithSelectionOnSecondParagraph() {
// https://github.com/MohamedRejeb/compose-rich-editor/issues/311
val richTextState = RichTextState(
initialRichParagraphList = listOf(
RichParagraph(
key = 1,
).also {
it.children.add(
RichSpan(
text = "G",
paragraph = it,
),
)
},
RichParagraph(
key = 2,
).also {
it.children.add(
RichSpan(
text = "b",
paragraph = it,
),
)
}
)
)

richTextState.selection = TextRange(1)
richTextState.onTextFieldValueChange(
TextFieldValue(
text = "Good b",
selection = TextRange(5),
)
)

assertEquals("Good\nb", richTextState.toText())
}

@OptIn(ExperimentalRichTextApi::class)
@Test
fun testTextCorrection() {
val richTextState = RichTextState(
initialRichParagraphList = listOf(
RichParagraph(
key = 1,
).also {
it.children.add(
RichSpan(
text = "Hilo",
paragraph = it,
),
)
},
RichParagraph(
key = 2,
).also {
it.children.add(
RichSpan(
text = "b",
paragraph = it,
),
)
}
)
)

richTextState.selection = TextRange(2)
richTextState.onTextFieldValueChange(
TextFieldValue(
text = "Hello b",
selection = TextRange(5),
)
)

assertEquals("Hello\nb", richTextState.toText())
}

}

0 comments on commit 30e695a

Please sign in to comment.