11package org.wordpress.aztec.handlers
22
33import android.text.Spannable
4+ import org.wordpress.aztec.Constants
45import org.wordpress.aztec.spans.AztecHeadingSpan
56import org.wordpress.aztec.watchers.TextDeleter
67
@@ -26,13 +27,25 @@ class HeadingHandler : BlockHandler<AztecHeadingSpan>(AztecHeadingSpan::class.ja
2627 TextDeleter .mark(text, newlineIndex, newlineIndex + 1 )
2728 }
2829
29- override fun handleNewlineAtTextEnd () {
30- block. end = newlineIndex + 1
31- }
30+ // fun handleNewlineAtTextEnd()
31+ // got a newline while being at the end-of-text. We'll let the current list item engulf it and will wait
32+ // for the end-of-text marker event in order to attach the new list item to it when that happens.
3233
3334 override fun handleNewlineInBody () {
34- // newline added at some position inside the block. Let's split the block into two
35- cloneHeading(text, block.span, newlineIndex + 1 , block.end)
35+ // if the new newline is the second-last character of the block and the last one is a newline (which
36+ // is a visual newline) or the end-of-buffer marker, or it's the last character of the text then it's the last
37+ // actual character of the block
38+ val atEndOfBlock = (newlineIndex == block.end - 2 &&
39+ (text[block.end - 1 ] == Constants .NEWLINE || text[block.end - 1 ] == Constants .END_OF_BUFFER_MARKER )) ||
40+ newlineIndex == text.length - 1
41+
42+ if (atEndOfBlock) {
43+ // newline added at the end of the block (right before its visual newline) so, just end the block and
44+ // not add a new block after it
45+ } else {
46+ // newline added at some position inside the block. Let's split the block into two
47+ cloneHeading(text, block.span, newlineIndex + 1 , block.end)
48+ }
3649
3750 block.end = newlineIndex + 1
3851 }
0 commit comments