|
1 | 1 | package org.wordpress.aztec.handlers |
2 | 2 |
|
3 | 3 | import android.text.Spannable |
| 4 | +import org.wordpress.aztec.Constants |
4 | 5 | import org.wordpress.aztec.spans.AztecHeadingSpan |
5 | 6 | import org.wordpress.aztec.watchers.TextDeleter |
6 | 7 |
|
@@ -31,7 +32,22 @@ class HeadingHandler : BlockHandler<AztecHeadingSpan>(AztecHeadingSpan::class.ja |
31 | 32 | // for the end-of-text marker event in order to attach the new list item to it when that happens. |
32 | 33 |
|
33 | 34 | 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 | + |
35 | 51 | block.end = newlineIndex + 1 |
36 | 52 | } |
37 | 53 |
|
|
0 commit comments