From 80b098765ccf84e031134276a4138c11aa88c4d3 Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Sat, 30 Mar 2024 18:30:25 +0100 Subject: [PATCH] Fix getBoundingBoxes returning emptyList when the last line is empty --- .../richeditor/utils/TextLayoutResultExt.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/utils/TextLayoutResultExt.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/utils/TextLayoutResultExt.kt index 118f19c5..ea06de87 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/utils/TextLayoutResultExt.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/utils/TextLayoutResultExt.kt @@ -31,13 +31,19 @@ fun TextLayoutResult.getBoundingBoxes( if (multiParagraph.lineCount == 0) return emptyList() - val lastLinePosition = - Offset( - x = multiParagraph.getLineRight(multiParagraph.lineCount - 1), - y = multiParagraph.getLineTop(multiParagraph.lineCount - 1) - ) + var lastOffset = 0 + var lastNonEmptyLineIndex = multiParagraph.lineCount - 1 + + while (lastOffset == 0 && lastNonEmptyLineIndex >= 0) { + val lastLinePosition = + Offset( + x = multiParagraph.getLineRight(lastNonEmptyLineIndex), + y = multiParagraph.getLineTop(lastNonEmptyLineIndex) + ) - val lastOffset = multiParagraph.getOffsetForPosition(lastLinePosition) + lastOffset = multiParagraph.getOffsetForPosition(lastLinePosition) + lastNonEmptyLineIndex-- + } if (startOffset >= lastOffset) return emptyList()