diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d9ea440f..972d219e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] agp = "8.5.2" kotlin = "2.0.21" -compose = "1.7.0" +compose = "1.7.1" dokka = "1.9.10" ksoup = "0.4.0" @@ -14,7 +14,7 @@ nexus-publish = "2.0.0" androidx-appcompat = "1.7.0" activity-compose = "1.9.3" voyager = "1.1.0-beta03" -richeditor = "1.0.0-rc05" +richeditor = "1.0.0-rc10" coroutines = "1.9.0" ktor = "3.0.1" android-minSdk = "21" 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 25efa6c9..4259cc7e 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 @@ -2770,22 +2770,31 @@ public class RichTextState internal constructor( var pressY = pressPosition.y val textLayoutResult = this.textLayoutResult ?: return var index = 0 - var lastIndex = 0 + + // Get the length of the text + val textLength = textLayoutResult.layoutInput.text.length + + // Ensure pressY is within valid bounds + pressY = pressY.coerceIn(0f, textLayoutResult.size.height.toFloat()) for (i in 0 until textLayoutResult.lineCount) { val start = textLayoutResult.getLineStart(i) val top = textLayoutResult.getLineTop(i) if (i == 0) { - if (start > 0f) + if (start > 0f) { pressX += start + } - if (top > 0f) + if (top > 0f) { pressY += top + } } - if (i == 0 && top > pressY) + // Make sure pressY is within the current line's top position + if (i == 0 && top > pressY) { break + } if (top > pressY) { index = lastIndex @@ -2826,32 +2835,39 @@ public class RichTextState internal constructor( (nextParagraph.getFirstNonEmptyChild() ?: nextParagraph.type.startRichSpan) .textRange.min.minus(nextParagraph.type.startText.length) + // Handle selection adjustments if ( selection.collapsed && selection.min == nextParagraphStart - ) + ) { updateTextFieldValue( textFieldValue.copy( - selection = TextRange(selection.min - 1, selection.min - 1) + selection = TextRange((selection.min - 1).coerceAtLeast(0), (selection.min - 1).coerceAtLeast(0)) ) ) - else if ( + } else if ( selection.collapsed && index == richParagraphList.lastIndex && selectedParagraph.isEmpty() && selection.min == selectedParagraph.getFirstNonEmptyChild()?.textRange?.min?.minus(1) - ) + ) { updateTextFieldValue( textFieldValue.copy( - selection = TextRange(selection.min + 1, selection.min + 1) + selection = TextRange((selection.min + 1).coerceAtMost(textLength - 1), (selection.min + 1).coerceAtMost(textLength - 1)) ) ) - else if (newSelection != null) + } else if (newSelection != null) { + // Ensure newSelection is within valid bounds + val adjustedSelection = TextRange( + newSelection.start.coerceIn(0, textLength), + newSelection.end.coerceIn(0, textLength) + ) updateTextFieldValue( textFieldValue.copy( - selection = newSelection + selection = adjustedSelection ) ) + } } private var registerLastPressPositionJob: Job? = null diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/material3/RichTextEditor.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/material3/RichTextEditor.kt index d611d1f2..f9ae4908 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/material3/RichTextEditor.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/material3/RichTextEditor.kt @@ -7,7 +7,7 @@ import androidx.compose.foundation.layout.* import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.selection.LocalTextSelectionColors -import androidx.compose.material.LocalTextStyle +import androidx.compose.material3.LocalTextStyle import androidx.compose.material3.* import androidx.compose.material3.Typography import androidx.compose.runtime.Composable diff --git a/sample/android/src/main/AndroidManifest.xml b/sample/android/src/main/AndroidManifest.xml index 5972afab..080feda0 100644 --- a/sample/android/src/main/AndroidManifest.xml +++ b/sample/android/src/main/AndroidManifest.xml @@ -1,5 +1,7 @@ + +