Skip to content

Commit 732ce40

Browse files
committed
Bring the check for hitting enter at the end of the text
1 parent d269bff commit 732ce40

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/handlers/HeadingHandler.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.wordpress.aztec.handlers
22

33
import android.text.Spannable
4+
import org.wordpress.aztec.Constants
45
import org.wordpress.aztec.spans.AztecHeadingSpan
56
import org.wordpress.aztec.watchers.TextDeleter
67

@@ -31,7 +32,22 @@ class HeadingHandler : BlockHandler<AztecHeadingSpan>(AztecHeadingSpan::class.ja
3132
// 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-
cloneHeading(text, block.span, newlineIndex + 1, block.end)
35+
36+
// if the new newline is the second-last character of the block and the last one is a newline (which
37+
// is a visual newline) or the end-of-buffer marker, or it's the last character of the text then it's the last
38+
// actual character of the block
39+
val atEndOfBlock = (newlineIndex == block.end - 2 &&
40+
(text[block.end - 1] == Constants.NEWLINE || text[block.end - 1] == Constants.END_OF_BUFFER_MARKER)) ||
41+
newlineIndex == text.length - 1
42+
43+
if (atEndOfBlock) {
44+
// newline added at the end of the block (right before its visual newline) so, just end the block and
45+
// not add a new block after it
46+
} else {
47+
// newline added at some position inside the block. Let's split the block into two
48+
cloneHeading(text, block.span, newlineIndex + 1, block.end)
49+
}
50+
3551
block.end = newlineIndex + 1
3652
}
3753

0 commit comments

Comments
 (0)