From 829d24535ab2ad625851e8b22bb765e795fab2f3 Mon Sep 17 00:00:00 2001 From: abarrafo Date: Wed, 16 Oct 2024 09:13:13 +0200 Subject: [PATCH 1/6] Fix PR #396 --- .../richeditor/model/RichTextState.kt | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) 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 f9e030d9..e880b785 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 @@ -2756,21 +2756,29 @@ public class RichTextState internal constructor( var pressY = pressPosition.y val textLayoutResult = this.textLayoutResult ?: return var index = 0 + + // Ensure pressY is within valid bounds + pressY = pressY.coerceIn(0f, textLayoutResult.size.height.toFloat()) + for (i in 0 until textLayoutResult.lineCount) { index = i 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 = i - 1 @@ -2791,12 +2799,12 @@ public class RichTextState internal constructor( val lineParagraphStart = lineParagraph.getFirstNonEmptyChild()?.textRange?.min ?: return - index = richParagraphList.indexOf(lineParagraph) - + // Get bounding box for the paragraph start, ensuring it's within valid line bounds val lineParagraphStartBounds = textLayoutResult.getBoundingBox(lineParagraphStart) - if (index > 0 && lineParagraphStartBounds.top > pressY) + if (index > 0 && lineParagraphStartBounds.top > pressY) { index-- + } } val selectedParagraph = richParagraphList.getOrNull(index) ?: return @@ -2804,32 +2812,34 @@ public class RichTextState internal constructor( val nextParagraphStart = nextParagraph?.getFirstNonEmptyChild()?.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) ) ) - 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) ) ) - else if (newSelection != null) + } else if (newSelection != null) { updateTextFieldValue( textFieldValue.copy( selection = newSelection ) ) + } } private var registerLastPressPositionJob: Job? = null From c40a1e8aeb3d0bd29dc870b4c1481b50f31a9148 Mon Sep 17 00:00:00 2001 From: abarrafo Date: Wed, 16 Oct 2024 09:18:30 +0200 Subject: [PATCH 2/6] Fix #396 --- .../richeditor/model/RichTextState.kt | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) 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 e880b785..4323f653 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 @@ -2757,6 +2757,9 @@ public class RichTextState internal constructor( val textLayoutResult = this.textLayoutResult ?: return var index = 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()) @@ -2797,9 +2800,14 @@ public class RichTextState internal constructor( val lineParagraph = getRichParagraphByTextIndex(lineTextStartIndex) ?: return - val lineParagraphStart = lineParagraph.getFirstNonEmptyChild()?.textRange?.min ?: return + var lineParagraphStart = lineParagraph.getFirstNonEmptyChild()?.textRange?.min ?: return + + // Ensure lineParagraphStart is within valid bounds before accessing bounding box + if (lineParagraphStart >= textLength) { + // Adjust lineParagraphStart to be within valid range + lineParagraphStart = (textLength - 1).coerceAtLeast(0) + } - // Get bounding box for the paragraph start, ensuring it's within valid line bounds val lineParagraphStartBounds = textLayoutResult.getBoundingBox(lineParagraphStart) if (index > 0 && lineParagraphStartBounds.top > pressY) { @@ -2819,7 +2827,7 @@ public class RichTextState internal constructor( ) { 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 ( @@ -2830,13 +2838,18 @@ public class RichTextState internal constructor( ) { 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) { + // 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 ) ) } From 524b433a57dd121953cf071469d0d01106a58fe8 Mon Sep 17 00:00:00 2001 From: Saurabh Sandav Date: Tue, 29 Oct 2024 13:26:09 +0530 Subject: [PATCH 3/6] Fix material reference in material3 code --- .../com/mohamedrejeb/richeditor/ui/material3/RichTextEditor.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From a0941a9179964761eae45f32e017257bfabf958e Mon Sep 17 00:00:00 2001 From: Michal Srutek Date: Wed, 13 Nov 2024 08:18:15 +0100 Subject: [PATCH 4/6] Add INTERNET permission to sample --- sample/android/src/main/AndroidManifest.xml | 2 ++ 1 file changed, 2 insertions(+) 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 @@ + + Date: Wed, 13 Nov 2024 08:44:08 +0100 Subject: [PATCH 5/6] Update sample to use RC10 --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d9ea440f..61d04fd9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" From 4323b9e51e89804ea9a48797f19e83d74ec41db4 Mon Sep 17 00:00:00 2001 From: Michal Srutek Date: Wed, 13 Nov 2024 08:54:03 +0100 Subject: [PATCH 6/6] Compose 1.7.1 --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d9ea440f..85fde577 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"