From 19b6b924d934dd383113d80facce126903124d76 Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Fri, 31 May 2024 12:49:26 +0200 Subject: [PATCH] Make sure the selection in the plain text editor is inside the text bounds (#2959) * Make sure the selection in the plain text editor is inside the text bounds * Add changelog --- changelog.d/2959.bugfix | 1 + .../textcomposer/components/markdown/MarkdownTextInput.kt | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 changelog.d/2959.bugfix diff --git a/changelog.d/2959.bugfix b/changelog.d/2959.bugfix new file mode 100644 index 0000000000..777c5da212 --- /dev/null +++ b/changelog.d/2959.bugfix @@ -0,0 +1 @@ +Fix crash when restoring the selection values in the plain text editor. diff --git a/libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt b/libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt index a30334f04c..8016fa637e 100644 --- a/libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt +++ b/libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/components/markdown/MarkdownTextInput.kt @@ -90,13 +90,15 @@ fun MarkdownTextInput( tag = TestTags.plainTextEditor.value // Needed for UI tests setPadding(0) setBackgroundColor(Color.TRANSPARENT) - setText(state.text.value()) + val text = state.text.value() + setText(text) inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES or InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_AUTO_CORRECT if (canUpdateState) { - setSelection(state.selection.first, state.selection.last) + val textRange = 0..text.length + setSelection(state.selection.first.coerceIn(textRange), state.selection.last.coerceIn(textRange)) setOnFocusChangeListener { _, hasFocus -> state.hasFocus = hasFocus }