Skip to content

Commit 07d3744

Browse files
authored
Merge pull request #879 from wordpress-mobile/issue/implement_list_reverse_start
Implement start and reversed in lists
2 parents 9f2cb0c + 005c849 commit 07d3744

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,46 @@ open class MainActivity : AppCompatActivity(),
9191
private val UNDERLINE = "<u style=\"color:lime\">Underline</u><br>"
9292
private val STRIKETHROUGH = "<s style=\"color:#ff666666\" class=\"test\">Strikethrough</s><br>" // <s> or <strike> or <del>
9393
private val ORDERED = "<ol style=\"color:green\"><li>Ordered</li><li>should have color</li></ol>"
94+
private val ORDERED_WITH_START = "<h4>Start in 10 List:</h4>" +
95+
"<ol start=\"10\">\n" +
96+
" <li>Ten</li>\n" +
97+
" <li>Eleven</li>\n" +
98+
" <li>Twelve</li>\n" +
99+
"</ol>"
100+
private val ORDERED_REVERSED = "<h4>Reversed List:</h4>" +
101+
"<ol reversed>\n" +
102+
" <li>Three</li>\n" +
103+
" <li>Two</li>\n" +
104+
" <li>One</li>\n" +
105+
"</ol>"
106+
private val ORDERED_REVERSED_WITH_START = "<h4>Reversed Start in 10 List:</h4>" +
107+
"<ol reversed start=\"10\">\n" +
108+
" <li>Ten</li>\n" +
109+
" <li>Nine</li>\n" +
110+
" <li>Eight</li>\n" +
111+
"</ol>"
112+
private val ORDERED_REVERSED_NEGATIVE_WITH_START = "<h4>Reversed Start in 1 List:</h4>" +
113+
"<ol reversed start=\"1\">\n" +
114+
" <li>One</li>\n" +
115+
" <li>Zero</li>\n" +
116+
" <li>Minus One</li>\n" +
117+
"</ol>"
118+
private val ORDERED_REVERSED_WITH_START_IDENT = "<h4>Reversed Start in 6 List:</h4>" +
119+
"<ol reversed>" +
120+
" <li>Six</li>" +
121+
" <li>Five</li>" +
122+
" <li>Four</li>" +
123+
" <li>Three</li>" +
124+
" <li>Two</li>" +
125+
" <li>One<ol>" +
126+
" <li>One</li>" +
127+
" <li>Two</li>" +
128+
" <li>Three</li>" +
129+
" <li>Four</li>" +
130+
" <li>Five</li>" +
131+
" <li>Six</li>" +
132+
" <li>Seven</li> " +
133+
" </ol></li></ol>"
94134
private val LINE = "<hr />"
95135
private val UNORDERED = "<ul><li style=\"color:darkred\">Unordered</li><li>Should not have color</li></ul>"
96136
private val QUOTE = "<blockquote>Quote</blockquote>"
@@ -147,6 +187,11 @@ open class MainActivity : AppCompatActivity(),
147187
UNDERLINE +
148188
STRIKETHROUGH +
149189
ORDERED +
190+
ORDERED_WITH_START +
191+
ORDERED_REVERSED +
192+
ORDERED_REVERSED_WITH_START +
193+
ORDERED_REVERSED_NEGATIVE_WITH_START +
194+
ORDERED_REVERSED_WITH_START_IDENT +
150195
LINE +
151196
UNORDERED +
152197
QUOTE +

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class AztecListSpan(override var nestingLevel: Int,
3434
}
3535
}
3636

37-
fun getIndexOfProcessedLine(text: CharSequence, end: Int): Int {
37+
fun getIndexOfProcessedLine(text: CharSequence, end: Int): Int? {
3838
val spanStart = (text as Spanned).getSpanStart(this)
3939
val spanEnd = text.getSpanEnd(this)
4040

@@ -44,7 +44,7 @@ abstract class AztecListSpan(override var nestingLevel: Int,
4444
val hasSublist = listText.getSpans(end - spanStart - 1, end - spanStart, AztecListSpan::class.java)
4545
.any { it.nestingLevel > nestingLevel }
4646
if (hasSublist) {
47-
return -1
47+
return null
4848
}
4949
}
5050

@@ -55,7 +55,7 @@ abstract class AztecListSpan(override var nestingLevel: Int,
5555
.any { it.nestingLevel == nestingLevel + 1 && listText.getSpanStart(it) == startOfLine }
5656

5757
if (!isValidListItem) {
58-
return -1
58+
return null
5959
}
6060

6161
// count the list item spans up to the current line with the expected nesting level => item number
@@ -65,6 +65,17 @@ abstract class AztecListSpan(override var nestingLevel: Int,
6565
.size
6666
}
6767

68+
fun getNumberOfItemsInProcessedLine(text: CharSequence): Int {
69+
val spanStart = (text as Spanned).getSpanStart(this)
70+
val spanEnd = text.getSpanEnd(this)
71+
72+
val listText = text.subSequence(spanStart, spanEnd) as Spanned
73+
74+
return listText.getSpans(0, listText.length, AztecListItemSpan::class.java)
75+
.filter { it.nestingLevel == nestingLevel + 1 }
76+
.size
77+
}
78+
6879
fun nestingDepth(text: Spanned, index: Int, nextIndex: Int): Int {
6980
val finalNextIndex = if (nextIndex > text.length) index else nextIndex
7081
return IAztecNestable.getNestingLevelAt(text, index, finalNextIndex)

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,26 @@ class AztecOrderedListSpan(
5555
p.color = listStyle.indicatorColor
5656
p.style = Paint.Style.FILL
5757

58-
val lineIndex = getIndexOfProcessedLine(text, end)
59-
val textToDraw = if (lineIndex > -1) {
60-
if (dir >= 0) lineIndex.toString() + "."
61-
else "." + lineIndex.toString()
58+
val start = if (attributes.hasAttribute("start") == true) {
59+
attributes.getValue("start").toInt()
6260
} else {
63-
""
61+
0
62+
}
63+
64+
var textToDraw = ""
65+
getIndexOfProcessedLine(text, end)?.let {
66+
val isReversed = attributes.hasAttribute("reversed")
67+
val lineIndex = if (start > 0) {
68+
if (isReversed) start - (it - 1)
69+
else start + (it - 1)
70+
} else {
71+
val number = getNumberOfItemsInProcessedLine(text)
72+
if (isReversed) number - (it - 1)
73+
else it
74+
}
75+
76+
textToDraw = if (dir >= 0) lineIndex.toString() + "."
77+
else "." + lineIndex.toString()
6478
}
6579

6680
val width = p.measureText(textToDraw)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class AztecUnorderedListSpan(
5353
p.style = Paint.Style.FILL
5454

5555
val lineIndex = getIndexOfProcessedLine(text, end)
56-
val textToDraw = if (lineIndex > -1) "\u2022" else ""
56+
val textToDraw = if (lineIndex != null) "\u2022" else ""
5757

5858
val width = p.measureText(textToDraw)
5959

0 commit comments

Comments
 (0)