Skip to content

Commit 8ecb19d

Browse files
committed
Fix block-closing with a newline even when there is another block right above
1 parent ad4dc3c commit 8ecb19d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ abstract class BlockHandler<SpanType : IAztecBlockSpan>(val clazz: Class<SpanTyp
2424
var newlineIndex: Int = -1
2525
var nestingLevel = 0
2626
var markerIndex: Int = -1
27+
var isReplay = false
2728

28-
override fun handleTextChanged(text: Spannable, inputStart: Int, count: Int, nestingLevel: Int) {
29+
override fun handleTextChanged(text: Spannable, inputStart: Int, count: Int, nestingLevel: Int, isReplay: Boolean) {
2930
this.text = text
30-
31+
this.isReplay = isReplay
3132
this.nestingLevel = nestingLevel
3233

3334
// use charsNew to get the spans at the input point. It appears to be more reliable vs the whole Editable.
@@ -92,9 +93,10 @@ abstract class BlockHandler<SpanType : IAztecBlockSpan>(val clazz: Class<SpanTyp
9293
}
9394

9495
// prev newline needs to be at the same nesting level to account for "double-enter"
96+
val prevNewlineNesting = IAztecNestable.getNestingLevelAt(text, newlineIndex - 1, newlineIndex)
97+
val currentNewlineNesting = IAztecNestable.getNestingLevelAt(text, newlineIndex, newlineIndex + 1)
9598
if (text[newlineIndex - 1] == Constants.NEWLINE
96-
&& IAztecNestable.getNestingLevelAt(text, newlineIndex - 1, newlineIndex) ==
97-
IAztecNestable.getNestingLevelAt(text, newlineIndex, newlineIndex + 1)
99+
&& (prevNewlineNesting == currentNewlineNesting || prevNewlineNesting > currentNewlineNesting && !isReplay)
98100
&& atEndOfBlock) {
99101
return PositionType.EMPTY_LINE_AT_BLOCK_END
100102
}

aztec/src/main/kotlin/org/wordpress/aztec/watchers/BlockElementWatcher.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ open class BlockElementWatcher(aztecText: AztecText) : TextWatcher {
1717
val handlers = ArrayList<TextChangeHandler>()
1818

1919
interface TextChangeHandler {
20-
fun handleTextChanged(text: Spannable, inputStart: Int, count: Int, nestingLevel: Int)
20+
fun handleTextChanged(text: Spannable, inputStart: Int, count: Int, nestingLevel: Int, isReplay: Boolean)
2121
}
2222

2323
private val aztecTextRef: WeakReference<AztecText?> = WeakReference(aztecText)
@@ -64,6 +64,7 @@ open class BlockElementWatcher(aztecText: AztecText) : TextWatcher {
6464
var startIndex = start
6565
var charCount = count
6666

67+
var hasReplay = false
6768
do {
6869
val nestingLevelToProcess = IAztecNestable.getNestingLevelAt(s as Spanned, startIndex, startIndex + charCount)
6970

@@ -73,11 +74,12 @@ open class BlockElementWatcher(aztecText: AztecText) : TextWatcher {
7374
s as Spannable,
7475
startIndex,
7576
charCount,
76-
nestingLevelToProcess)
77+
nestingLevelToProcess,
78+
hasReplay)
7779
}
7880

7981
// check for a replay marker and use its bounds
80-
val hasReplay = aztecTextRef.get()?.text?.let text@ { text ->
82+
hasReplay = aztecTextRef.get()?.text?.let text@ { text ->
8183
text.getSpans(0, s.length, MarkForReplay::class.java).firstOrNull { mark ->
8284
if (mark != null) {
8385
startIndex = text.getSpanStart(mark)

0 commit comments

Comments
 (0)