Skip to content

Commit 4e9627e

Browse files
authored
Merge pull request #793 from adrielcafe/develop
Clickable URL Spans
2 parents c2c22b5 + 2ec4807 commit 4e9627e

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ open class Aztec private constructor(val visualEditor: AztecText, val toolbar: I
2424
private var onAudioTappedListener: AztecText.OnAudioTappedListener? = null
2525
private var onMediaDeletedListener: AztecText.OnMediaDeletedListener? = null
2626
private var onVideoInfoRequestedListener: AztecText.OnVideoInfoRequestedListener? = null
27+
private var onLinkTappedListener: AztecText.OnLinkTappedListener? = null
28+
private var isLinkTapEnabled: Boolean = false
2729
private var plugins: ArrayList<IAztecPlugin> = visualEditor.plugins
2830
var sourceEditor: SourceViewEditText? = null
2931

@@ -134,6 +136,18 @@ open class Aztec private constructor(val visualEditor: AztecText, val toolbar: I
134136
return this
135137
}
136138

139+
fun setOnLinkTappedListener(onLinkTappedListener: AztecText.OnLinkTappedListener): Aztec {
140+
this.onLinkTappedListener = onLinkTappedListener
141+
initLinkTappedListener()
142+
return this
143+
}
144+
145+
fun setLinkTapEnabled(isLinkTapEnabled: Boolean): Aztec {
146+
this.isLinkTapEnabled = isLinkTapEnabled
147+
initLinkTapEnabled()
148+
return this
149+
}
150+
137151
fun addPlugin(plugin: IAztecPlugin): Aztec {
138152
plugins.add(plugin)
139153

@@ -219,4 +233,14 @@ open class Aztec private constructor(val visualEditor: AztecText, val toolbar: I
219233
visualEditor.setOnVideoInfoRequestedListener(onVideoInfoRequestedListener!!)
220234
}
221235
}
236+
237+
private fun initLinkTappedListener() {
238+
if (onLinkTappedListener != null) {
239+
visualEditor.setOnLinkTappedListener(onLinkTappedListener!!)
240+
}
241+
}
242+
243+
private fun initLinkTapEnabled() {
244+
visualEditor.setLinkTapEnabled(isLinkTapEnabled)
245+
}
222246
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
324324
fun onBackspaceKey() : Boolean
325325
}
326326

327+
interface OnLinkTappedListener {
328+
fun onLinkTapped(widget: View, url: String)
329+
}
330+
327331
constructor(context: Context) : super(context) {
328332
init(null)
329333
}
@@ -833,6 +837,14 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
833837
this.onVideoInfoRequestedListener = listener
834838
}
835839

840+
fun setOnLinkTappedListener(listener: OnLinkTappedListener) {
841+
EnhancedMovementMethod.linkTappedListener = listener
842+
}
843+
844+
fun setLinkTapEnabled(isLinkTapEnabled: Boolean) {
845+
EnhancedMovementMethod.isLinkTapEnabled = isLinkTapEnabled
846+
}
847+
836848
override fun onKeyPreIme(keyCode: Int, event: KeyEvent): Boolean {
837849
if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
838850
onImeBackListener?.onImeBack()
@@ -1845,4 +1857,5 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
18451857
override fun dispatchHoverEvent(event: MotionEvent): Boolean {
18461858
return if (accessibilityDelegate.onHoverEvent(event)) true else super.dispatchHoverEvent(event)
18471859
}
1860+
18481861
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import android.text.style.ClickableSpan
77
import android.view.MotionEvent
88
import android.widget.TextView
99
import org.wordpress.aztec.spans.AztecMediaClickableSpan
10+
import org.wordpress.aztec.spans.AztecURLSpan
1011
import org.wordpress.aztec.spans.UnknownClickableSpan
1112

1213
/**
1314
* http://stackoverflow.com/a/23566268/569430
1415
*/
1516
object EnhancedMovementMethod : ArrowKeyMovementMethod() {
17+
var isLinkTapEnabled = false
18+
var linkTappedListener: AztecText.OnLinkTappedListener? = null
19+
1620
override fun onTouchEvent(widget: TextView, text: Spannable, event: MotionEvent): Boolean {
1721
val action = event.action
1822

@@ -72,11 +76,14 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
7276
link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull { text.getSpanStart(it) == off }
7377
}
7478

75-
if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan)) {
76-
if (action == MotionEvent.ACTION_UP) {
79+
if (link != null) {
80+
if (link is AztecMediaClickableSpan || link is UnknownClickableSpan) {
7781
link.onClick(widget)
82+
return true
83+
} else if (link is AztecURLSpan && isLinkTapEnabled) {
84+
linkTappedListener?.onLinkTapped(widget, link.url) ?: link.onClick(widget)
85+
return true
7886
}
79-
return true
8087
}
8188
}
8289

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
picassoVersion = '2.5.2'
1010
robolectricVersion = '3.5.1'
1111
jUnitVersion = '4.12'
12-
jSoupVersion = '1.10.3'
12+
jSoupVersion = '1.11.3'
1313
wordpressUtilsVersion = '1.21'
1414
espressoVersion = '3.0.1'
1515
}

0 commit comments

Comments
 (0)