11package org.wordpress.aztec
22
33import android.graphics.Rect
4- import android.text.Selection
54import android.text.Spannable
65import android.text.method.ArrowKeyMovementMethod
76import android.text.style.ClickableSpan
@@ -29,7 +28,11 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
2928
3029 val layout = widget.layout
3130 val line = layout.getLineForVertical(y)
32- val off = layout.getOffsetForHorizontal(line, x.toFloat())
31+ var off = layout.getOffsetForHorizontal(line, x.toFloat())
32+
33+ if (text.length > off) {
34+ off++
35+ }
3336
3437 // get the character's position. This may be the left or the right edge of the character so, find the
3538 // other edge by inspecting nearby characters (if they exist)
@@ -40,18 +43,15 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
4043 val lineRect = Rect ()
4144 layout.getLineBounds(line, lineRect)
4245
43- if (((x >= charPrevX && x <= charX) || (x >= charX && x <= charNextX))
46+ if (((x in charPrevX.. charX) || (x in charX.. charNextX))
4447 && y >= lineRect.top && y <= lineRect.bottom) {
4548 val link = text.getSpans(off, off, ClickableSpan ::class .java).firstOrNull()
4649
4750 // Only react to AztecMediaClickableSpan and UnknownClickableSpan; not to regular links.
4851 if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan )) {
4952 if (action == MotionEvent .ACTION_UP ) {
5053 link.onClick(widget)
51- } else {
52- Selection .setSelection(text, text.getSpanStart(link), text.getSpanEnd(link))
5354 }
54-
5555 return true
5656 }
5757 }
0 commit comments