Skip to content

Commit 6c7e8ac

Browse files
committed
Better heading boundary calculation
1 parent 761c705 commit 6c7e8ac

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,16 +399,15 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
399399
private fun applyHeadingBlock(headingSpan: AztecHeadingSpan, start: Int, end: Int) {
400400
val lines = TextUtils.split(editableText.substring(start, end), "\n")
401401
for (i in lines.indices) {
402-
val lineLength = lines[i].length
402+
val splitLength = lines[i].length
403403

404-
val lineStart = (0..i - 1).sumBy { lines[it].length + 1 }
405-
val lineEnd = (lineStart + lineLength).let {
406-
if ((start + it) != editableText.length) it + 1 else it // include the newline or not
407-
}
404+
val lineStart = start + (0..i - 1).sumBy { lines[it].length + 1 }
405+
val lineEnd = Math.min(lineStart + splitLength + 1, end) // +1 to include the newline
408406

407+
val lineLength = lineEnd - lineStart
409408
if (lineLength == 0) continue
410409

411-
HeadingHandler.cloneHeading(editableText, headingSpan, start + lineStart, start + lineEnd)
410+
HeadingHandler.cloneHeading(editableText, headingSpan, lineStart, lineEnd)
412411
}
413412
}
414413

0 commit comments

Comments
 (0)