Skip to content

Commit c372d6d

Browse files
authored
Merge pull request #234 from wordpress-mobile/issue/229-place-cursor-next-to-image
Only click the Clickable span if inside it
2 parents cd306bb + 2501e61 commit c372d6d

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback, Vi
202202
aztec.imageGetter = PicassoImageLoader(this, aztec)
203203
// aztec.imageGetter = GlideImageLoader(this)
204204

205+
aztec.setOnMediaTappedListener(this)
206+
205207
source = findViewById(R.id.source) as SourceViewEditText
206208

207209
formattingToolbar = findViewById(R.id.formatting_toolbar) as AztecToolbar
@@ -218,8 +220,6 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback, Vi
218220
aztec.setOnTouchListener(this)
219221
source.setOnImeBackListener(this)
220222
source.setOnTouchListener(this)
221-
222-
aztec.setOnMediaTappedListener(this)
223223
}
224224

225225
override fun onPause() {

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.wordpress.aztec
22

3+
import android.graphics.Rect
34
import android.text.Selection
45
import android.text.Spannable
56
import android.text.method.ArrowKeyMovementMethod
67
import android.text.style.ClickableSpan
8+
import android.util.Log
79
import android.view.MotionEvent
810
import android.widget.TextView
911
import org.wordpress.aztec.spans.AztecMediaClickableSpan
@@ -30,17 +32,29 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
3032
val line = layout.getLineForVertical(y)
3133
val off = layout.getOffsetForHorizontal(line, x.toFloat())
3234

33-
val link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull()
35+
// get the character's position. This may be the left or the right edge of the character so, find the
36+
// other edge by inspecting nearby characters (if they exist)
37+
val charX = layout.getPrimaryHorizontal(off)
38+
val charPrevX = if (off > 0) layout.getPrimaryHorizontal(off - 1) else charX
39+
val charNextX = if (off < text.length) layout.getPrimaryHorizontal(off + 1) else charX
3440

35-
// Only react to AztecMediaClickableSpan and UnknownClickableSpan; not to regular links.
36-
if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan)) {
37-
if (action == MotionEvent.ACTION_UP) {
38-
link.onClick(widget)
39-
} else {
40-
Selection.setSelection(text, text.getSpanStart(link), text.getSpanEnd(link))
41-
}
41+
val lineRect = Rect()
42+
layout.getLineBounds(line, lineRect)
43+
44+
if (((x >= charPrevX && x <= charX) || (x >= charX && x <= charNextX))
45+
&& y >= lineRect.top && y <= lineRect.bottom) {
46+
val link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull()
4247

43-
return true
48+
// Only react to AztecMediaClickableSpan and UnknownClickableSpan; not to regular links.
49+
if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan)) {
50+
if (action == MotionEvent.ACTION_UP) {
51+
link.onClick(widget)
52+
} else {
53+
Selection.setSelection(text, text.getSpanStart(link), text.getSpanEnd(link))
54+
}
55+
56+
return true
57+
}
4458
}
4559
}
4660

0 commit comments

Comments
 (0)