refactor using takeWhile

Fix edge case with white space before images
This commit is contained in:
davotoula
2025-09-11 21:03:06 +02:00
parent 8cb9d13567
commit 4dace5aaf8

View File

@@ -358,11 +358,8 @@ private fun renderParagraphWithFlowRow(
): Int { ): Int {
val paragraph = paragraphs[paragraphIndex] val paragraph = paragraphs[paragraphIndex]
// Check if this paragraph contains only images // Check if this paragraph contains only images (ignoring whitespace)
val isImageOnlyParagraph = val isImageOnlyParagraph = isImageOnlyParagraph(paragraph)
paragraph.words.all { word ->
word is ImageSegment || word is Base64Segment
}
if (isImageOnlyParagraph && paragraph.words.isNotEmpty()) { if (isImageOnlyParagraph && paragraph.words.isNotEmpty()) {
// Collect consecutive image-only paragraphs for gallery // Collect consecutive image-only paragraphs for gallery
@@ -415,6 +412,22 @@ private fun RenderSingleParagraphWithFlowRow(
} }
} }
private fun isImageOnlyParagraph(paragraph: ParagraphState): Boolean {
// A paragraph is "image only" if all non-whitespace words are images
val nonWhitespaceWords =
paragraph.words.filter { word ->
when (word) {
is RegularTextSegment -> word.segmentText.isNotBlank()
else -> true // All other word types (images, links, etc.) are considered non-whitespace
}
}
return nonWhitespaceWords.isNotEmpty() &&
nonWhitespaceWords.all { word ->
word is ImageSegment || word is Base64Segment
}
}
private fun collectConsecutiveImageParagraphs( private fun collectConsecutiveImageParagraphs(
paragraphs: ImmutableList<ParagraphState>, paragraphs: ImmutableList<ParagraphState>,
startIndex: Int, startIndex: Int,
@@ -435,10 +448,7 @@ private fun collectConsecutiveImageParagraphs(
) )
val isCurrentImageOnly = val isCurrentImageOnly =
currentParagraph.words.isNotEmpty() && currentParagraph.words.isNotEmpty() && isImageOnlyParagraph(currentParagraph)
currentParagraph.words.all { word ->
word is ImageSegment || word is Base64Segment
}
if (isCurrentImageOnly) { if (isCurrentImageOnly) {
imageParagraphs.add(currentParagraph) imageParagraphs.add(currentParagraph)
@@ -556,13 +566,15 @@ private fun RenderWordsWithImageGallery(
val word = words[i] val word = words[i]
if (word is ImageSegment || word is Base64Segment) { if (word is ImageSegment || word is Base64Segment) {
// Collect consecutive images // Collect consecutive images (skipping whitespace) using takeWhile
val imageSegments = mutableListOf<Segment>() fun isImageOrWhitespace(segment: Segment): Boolean =
var j = i segment is ImageSegment ||
while (j < words.size && (words[j] is ImageSegment || words[j] is Base64Segment)) { segment is Base64Segment ||
imageSegments.add(words[j]) (segment is RegularTextSegment && segment.segmentText.isBlank())
j++
} val consecutiveSegments = words.drop(i).takeWhile { isImageOrWhitespace(it) }
val imageSegments = consecutiveSegments.filter { it is ImageSegment || it is Base64Segment }
val j = i + consecutiveSegments.size
if (imageSegments.size > 1) { if (imageSegments.size > 1) {
// Multiple images - render as gallery // Multiple images - render as gallery