diff --git a/docs/rich_text_state.md b/docs/rich_text_state.md index 45a141c4..469d35a9 100644 --- a/docs/rich_text_state.md +++ b/docs/rich_text_state.md @@ -26,4 +26,12 @@ richTextState.setConfig( codeBackgroundColor = Color.Transparent, codeStrokeColor = Color.LightGray, ) +``` + +### Changing the editor's selection + +The editor's selection can be changed using the `RichTextState.selection` property. + +```kotlin +richTextState.selection = TextRange(0, 5) ``` \ No newline at end of file diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt index d161c554..19e7eddf 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/model/RichTextState.kt @@ -48,7 +48,18 @@ class RichTextState internal constructor( var annotatedString by mutableStateOf(AnnotatedString(text = "")) private set - val selection get() = textFieldValue.selection + /** + * The selection of the rich text. + */ + var selection + get() = textFieldValue.selection + set(value) { + if (value.min >= 0 && value.max <= textFieldValue.text.length) { + val newTextFieldValue = textFieldValue.copy(selection = value) + updateTextFieldValue(newTextFieldValue) + } + } + val composition get() = textFieldValue.composition internal var singleParagraphMode by mutableStateOf(false)