Skip to content

Commit 9f8ba17

Browse files
committed
Handle multi-line list items, including nested multi-line blocks
1 parent 45e4caa commit 9f8ba17

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecListSpan.kt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,21 @@ abstract class AztecListSpan(override var nestingLevel: Int,
4545
}
4646
}
4747

48+
// only display a line indicator when it's the first line of a list item
4849
val textBeforeBeforeEnd = listText.subSequence(0, end - spanStart) as Spanned
50+
val startOfLine = textBeforeBeforeEnd.lastIndexOf(Constants.NEWLINE) + 1
51+
val isValidListItem = listText.getSpans(0, listText.length, AztecListItemSpan::class.java)
52+
.any { it.nestingLevel == nestingLevel + 1 && listText.getSpanStart(it) == startOfLine }
4953

50-
// gather the nesting depth for each line
51-
val nestingDepth = ArrayList<Int>()
52-
textBeforeBeforeEnd.forEachIndexed { i, c ->
53-
if (c == Constants.NEWLINE) {
54-
nestingDepth.add(nestingDepth(textBeforeBeforeEnd, i, i + 1))
55-
}
56-
}
57-
58-
// count the lines that have the same nesting depth as us
59-
var otherLinesAtSameNestingLevel = 0
60-
for (maxNestingLevel in nestingDepth) {
61-
// the -1 is because the list is one level up than the list item
62-
// if the item contains one block element, we still want to display number/bullet
63-
if (maxNestingLevel - 1 == nestingLevel || maxNestingLevel - 2 == nestingLevel) {
64-
otherLinesAtSameNestingLevel++
65-
}
54+
if (!isValidListItem) {
55+
return -1
6656
}
6757

68-
return otherLinesAtSameNestingLevel + 1
58+
// count the list item spans up to the current line with the expected nesting level => item number
59+
val checkEnd = Math.min(textBeforeBeforeEnd.length + 1, listText.length)
60+
return listText.getSpans(0, checkEnd, AztecListItemSpan::class.java)
61+
.filter { it.nestingLevel == nestingLevel + 1 }
62+
.size
6963
}
7064

7165
fun nestingDepth(text: Spanned, index: Int, nextIndex: Int): Int {

0 commit comments

Comments
 (0)