Skip to content

Commit 5cebfd8

Browse files
authored
Merge pull request #973 from wordpress-mobile/fix/task-list-click-issues
Fix task list click issues
2 parents e9c357d + 1d2f51d commit 5cebfd8

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,14 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
413413
styles.getColor(R.styleable.AztecText_codeColor, 0)),
414414
InlineFormatter.HighlightStyle(styles.getResourceId(R.styleable.AztecText_highlightColor, R.color.grey_lighten_10)))
415415

416+
val listStyle = BlockFormatter.ListStyle(
417+
styles.getColor(R.styleable.AztecText_bulletColor, 0),
418+
styles.getDimensionPixelSize(R.styleable.AztecText_bulletMargin, 0),
419+
styles.getDimensionPixelSize(R.styleable.AztecText_bulletPadding, 0),
420+
styles.getDimensionPixelSize(R.styleable.AztecText_bulletWidth, 0),
421+
verticalParagraphMargin)
416422
blockFormatter = BlockFormatter(this,
417-
BlockFormatter.ListStyle(
418-
styles.getColor(R.styleable.AztecText_bulletColor, 0),
419-
styles.getDimensionPixelSize(R.styleable.AztecText_bulletMargin, 0),
420-
styles.getDimensionPixelSize(R.styleable.AztecText_bulletPadding, 0),
421-
styles.getDimensionPixelSize(R.styleable.AztecText_bulletWidth, 0),
422-
verticalParagraphMargin),
423+
listStyle,
423424
BlockFormatter.QuoteStyle(
424425
styles.getColor(R.styleable.AztecText_quoteBackground, 0),
425426
styles.getColor(R.styleable.AztecText_quoteColor, 0),
@@ -437,6 +438,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
437438
alignmentRendering,
438439
BlockFormatter.ExclusiveBlockStyles(styles.getBoolean(R.styleable.AztecText_exclusiveBlocks, false))
439440
)
441+
EnhancedMovementMethod.taskListClickHandler = TaskListClickHandler(listStyle)
440442

441443
linkFormatter = LinkFormatter(this, LinkFormatter.LinkStyle(styles.getColor(
442444
R.styleable.AztecText_linkColor, 0),

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import android.text.method.ArrowKeyMovementMethod
66
import android.text.style.ClickableSpan
77
import android.view.MotionEvent
88
import android.widget.TextView
9-
import org.wordpress.aztec.spans.AztecListItemSpan
109
import org.wordpress.aztec.spans.AztecMediaClickableSpan
11-
import org.wordpress.aztec.spans.AztecTaskListSpan
1210
import org.wordpress.aztec.spans.AztecURLSpan
1311
import org.wordpress.aztec.spans.UnknownClickableSpan
1412

1513
/**
1614
* http://stackoverflow.com/a/23566268/569430
1715
*/
1816
object EnhancedMovementMethod : ArrowKeyMovementMethod() {
17+
var taskListClickHandler: TaskListClickHandler? = null
1918
var isLinkTapEnabled = false
2019
var linkTappedListener: AztecText.OnLinkTappedListener? = null
2120

@@ -38,6 +37,9 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
3837
val line = layout.getLineForVertical(y)
3938
val off = layout.getOffsetForHorizontal(line, x.toFloat())
4039

40+
// This handles the case when the task list checkbox is clicked
41+
if (taskListClickHandler?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true) return true
42+
4143
// get the character's position. This may be the left or the right edge of the character so, find the
4244
// other edge by inspecting nearby characters (if they exist)
4345
val charX = layout.getPrimaryHorizontal(off)
@@ -64,8 +66,6 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
6466

6567
var link: ClickableSpan? = null
6668

67-
if (handleTaskListClick(text, off)) return true
68-
6969
if (clickedOnSpan) {
7070
if (isClickedSpanAmbiguous) {
7171
if (clickedOnSpanToTheLeftOfCursor) {
@@ -93,16 +93,4 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
9393

9494
return super.onTouchEvent(widget, text, event)
9595
}
96-
97-
private fun handleTaskListClick(text: Spannable, off: Int): Boolean {
98-
val clickedList = text.getSpans(off, off, AztecTaskListSpan::class.java).firstOrNull()
99-
val clickedLine = text.getSpans(off, off, AztecListItemSpan::class.java).lastOrNull()
100-
val spanStart = text.getSpanStart(clickedLine)
101-
if (spanStart == off && clickedList != null && clickedLine != null && clickedList.canToggle()) {
102-
clickedLine.toggleCheck()
103-
clickedList.refresh()
104-
return true
105-
}
106-
return false
107-
}
10896
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.wordpress.aztec
2+
3+
import android.text.Spannable
4+
import org.wordpress.aztec.formatting.BlockFormatter
5+
import org.wordpress.aztec.spans.AztecListItemSpan
6+
import org.wordpress.aztec.spans.AztecTaskListSpan
7+
8+
class TaskListClickHandler(val listStyle: BlockFormatter.ListStyle) {
9+
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()) return false
12+
val clickedList = text.getSpans(off, off, AztecTaskListSpan::class.java).firstOrNull()
13+
val clickedLines = text.getSpans(off, off, AztecListItemSpan::class.java)
14+
val clickedLine = clickedLines.find {
15+
val spanStart = text.getSpanStart(it)
16+
spanStart == off || (spanStart == off - 1 && text.getSpanEnd(it) == off)
17+
}
18+
if (clickedList != null && clickedLine != null && clickedList.canToggle()) {
19+
clickedLine.toggleCheck()
20+
clickedList.refresh()
21+
return true
22+
}
23+
return false
24+
}
25+
}
26+

aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ class BlockFormatter(editor: AztecText,
4949
private val alignmentRendering: AlignmentRendering,
5050
private val exclusiveBlockStyles: ExclusiveBlockStyles
5151
) : AztecFormatter(editor) {
52-
data class ListStyle(val indicatorColor: Int, val indicatorMargin: Int, val indicatorPadding: Int, val indicatorWidth: Int, val verticalPadding: Int)
52+
data class ListStyle(val indicatorColor: Int, val indicatorMargin: Int, val indicatorPadding: Int, val indicatorWidth: Int, val verticalPadding: Int) {
53+
fun leadingMargin(): Int {
54+
return indicatorMargin + 2 * indicatorWidth + indicatorPadding
55+
}
56+
}
57+
5358
data class QuoteStyle(val quoteBackground: Int, val quoteColor: Int, val quoteBackgroundAlpha: Float, val quoteMargin: Int, val quotePadding: Int, val quoteWidth: Int, val verticalPadding: Int)
5459
data class PreformatStyle(val preformatBackground: Int, val preformatBackgroundAlpha: Float, val preformatColor: Int, val verticalPadding: Int)
5560
data class HeaderStyle(val verticalPadding: Int)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ open class AztecTaskListSpan(
7575
}
7676

7777
override fun getLeadingMargin(first: Boolean): Int {
78-
return listStyle.indicatorMargin + 2 * listStyle.indicatorWidth + listStyle.indicatorPadding
78+
return listStyle.leadingMargin()
7979
}
8080

8181
override fun drawLeadingMargin(c: Canvas, p: Paint, x: Int, dir: Int,
@@ -96,7 +96,7 @@ open class AztecTaskListSpan(
9696

9797
p.color = listStyle.indicatorColor
9898
p.style = Paint.Style.FILL
99-
val drawableHeight = (0.9 * (p.fontMetrics.bottom - p.fontMetrics.top))
99+
val drawableHeight = (0.8 * (p.fontMetrics.bottom - p.fontMetrics.top))
100100
// Make sure the marker is correctly aligned on RTL languages
101101
val markerStartPosition: Float = x + (listStyle.indicatorMargin * dir) * 1f
102102
val d: Drawable = context.resources.getDrawable(R.drawable.ic_checkbox, null)

0 commit comments

Comments
 (0)