Skip to content

Commit a115935

Browse files
authored
Merge pull request #1118 from wordpress-mobile/dodroid-954-checklist-issue
Fix checkbox interaction for indented (nested) task list items
2 parents e91bf74 + 59b2013 commit a115935

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -751,16 +751,11 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
751751
x += scrollX
752752
y += scrollY
753753

754-
// We check whether the user tap on a checkbox of a task list. The aztec text field should be enabled and we
755-
// check whether the tap event was on the leading margin which is where the checkbox is located plus a padding
756-
// space used to increase the target size for the tap event.
757-
if (isEnabled &&
758-
x + totalPaddingStart <= (blockFormatter.listStyleLeadingMargin() + AztecTaskListSpan.PADDING_SPACE)) {
754+
// We check whether the user tap on a checkbox of a task list. The handler performs its
755+
// own nesting-aware x-coordinate check, so we only need a rough gate here.
756+
if (isEnabled) {
759757
val line = layout.getLineForVertical(y)
760758
val off = layout.getOffsetForHorizontal(line, x.toFloat())
761-
// If the tap event was on the leading margin, we double check whether we are tapping on a task list item.
762-
// If that is true, then we return false because we don't want to propagate the tap event to stop moving
763-
// the cursor to the item tapped.
764759
if (getTaskListHandler()?.handleTaskListClick(
765760
text,
766761
off,

aztec/src/main/kotlin/org/wordpress/aztec/TaskListClickHandler.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ import org.wordpress.aztec.spans.AztecTaskListSpan
77

88
class TaskListClickHandler(val listStyle: BlockFormatter.ListStyle) {
99
fun handleTaskListClick(text: Spannable, off: Int, x: Int, startMargin: Int): Boolean {
10-
// We want to make sure that text click will not trigger the checked change
11-
if (x + startMargin > (listStyle.leadingMargin() + AztecTaskListSpan.PADDING_SPACE)) return false
12-
val clickedList = text.getSpans(off, off, AztecTaskListSpan::class.java).firstOrNull()
10+
val clickedList = text.getSpans(off, off, AztecTaskListSpan::class.java).maxByOrNull { it.nestingLevel }
11+
?: return false
12+
// Account for nested leading margins: each nesting level adds one leadingMargin width
13+
val depthMultiplier = (clickedList.nestingLevel / 2) + 1
14+
val effectiveMargin = listStyle.leadingMargin() * depthMultiplier
15+
if (x + startMargin > (effectiveMargin + AztecTaskListSpan.PADDING_SPACE)) return false
1316
val clickedLines = text.getSpans(off, off, AztecListItemSpan::class.java)
1417
val clickedLine = clickedLines.find {
1518
val spanStart = text.getSpanStart(it)
1619
spanStart == off || (spanStart == off - 1 && text.getSpanEnd(it) == off)
1720
}
18-
if (clickedList != null && clickedLine != null && clickedList.canToggle()) {
21+
if (clickedLine != null && clickedList.canToggle()) {
1922
clickedLine.toggleCheck()
2023
clickedList.refresh()
2124
return true

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ open class AztecTaskListSpan(
139139
private fun isChecked(text: CharSequence, lineIndex: Int): Boolean {
140140
val spanStart = (text as Spanned).getSpanStart(this)
141141
val spanEnd = text.getSpanEnd(this)
142-
val sortedSpans = text.getSpans(spanStart, spanEnd, AztecListItemSpan::class.java).sortedBy {
143-
text.getSpanStart(it)
144-
}
142+
val sortedSpans = text.getSpans(spanStart, spanEnd, AztecListItemSpan::class.java)
143+
.filter { it.nestingLevel == nestingLevel + 1 }
144+
.sortedBy { text.getSpanStart(it) }
145145
return sortedSpans.getOrNull(lineIndex - 1)?.attributes?.getValue("checked") == "true"
146146
}
147147

0 commit comments

Comments
 (0)