react-native-advanced-text 0.1.20 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -42,52 +42,51 @@ class AdvancedTextView : TextView {
|
|
|
42
42
|
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { init() }
|
|
43
43
|
|
|
44
44
|
private fun init() {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
45
|
+
Log.d(TAG, "AdvancedTextView initialized")
|
|
46
|
+
|
|
47
|
+
textSize = 16f
|
|
48
|
+
setPadding(16, 16, 16, 16)
|
|
49
|
+
|
|
50
|
+
movementMethod = ClickableMovementMethod.getInstance()
|
|
51
|
+
isClickable = false
|
|
52
|
+
isLongClickable = false
|
|
53
|
+
|
|
54
|
+
setTextIsSelectable(true)
|
|
55
|
+
|
|
56
|
+
customSelectionActionModeCallback = object : ActionMode.Callback {
|
|
57
|
+
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
|
|
58
|
+
customActionMode = mode
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
|
|
63
|
+
menu?.clear()
|
|
64
|
+
val selectionStart = selectionStart
|
|
65
|
+
val selectionEnd = selectionEnd
|
|
66
|
+
|
|
67
|
+
if (selectionStart >= 0 && selectionEnd >= 0 && selectionStart != selectionEnd) {
|
|
68
|
+
lastSelectedText = text.subSequence(selectionStart, selectionEnd).toString()
|
|
69
|
+
menuOptions.forEachIndexed { index, option ->
|
|
70
|
+
menu?.add(0, index, index, option)
|
|
71
|
+
}
|
|
72
|
+
sendSelectionEvent(lastSelectedText, "selection")
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
|
|
79
|
+
item?.let {
|
|
80
|
+
sendSelectionEvent(lastSelectedText, it.title.toString())
|
|
81
|
+
mode?.finish()
|
|
82
|
+
return true
|
|
83
|
+
}
|
|
84
|
+
return false
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
override fun onDestroyActionMode(mode: ActionMode?) {
|
|
88
|
+
customActionMode = null
|
|
89
|
+
}
|
|
91
90
|
}
|
|
92
91
|
}
|
|
93
92
|
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
package com.advancedtext
|
|
2
|
+
|
|
3
|
+
import android.text.Layout
|
|
4
|
+
import android.text.Selection
|
|
5
|
+
import android.text.Spannable
|
|
6
|
+
import android.text.method.BaseMovementMethod
|
|
7
|
+
import android.text.style.ClickableSpan
|
|
8
|
+
import android.view.MotionEvent
|
|
9
|
+
import android.widget.TextView
|
|
10
|
+
|
|
11
|
+
class ClickableMovementMethod : BaseMovementMethod() {
|
|
12
|
+
|
|
13
|
+
companion object {
|
|
14
|
+
private var sInstance: ClickableMovementMethod? = null
|
|
15
|
+
|
|
16
|
+
fun getInstance(): ClickableMovementMethod {
|
|
17
|
+
if (sInstance == null) {
|
|
18
|
+
sInstance = ClickableMovementMethod()
|
|
19
|
+
}
|
|
20
|
+
return sInstance!!
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
override fun canSelectArbitrarily(): Boolean = false
|
|
25
|
+
|
|
26
|
+
override fun onTouchEvent(widget: TextView, buffer: Spannable, event: MotionEvent): Boolean {
|
|
27
|
+
val action = event.actionMasked
|
|
28
|
+
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
|
|
29
|
+
|
|
30
|
+
var x = (event.x - widget.totalPaddingLeft + widget.scrollX).toInt()
|
|
31
|
+
var y = (event.y - widget.totalPaddingTop + widget.scrollY).toInt()
|
|
32
|
+
|
|
33
|
+
val layout: Layout = widget.layout
|
|
34
|
+
val links: Array<ClickableSpan>? = if (y < 0 || y > layout.height) {
|
|
35
|
+
null
|
|
36
|
+
} else {
|
|
37
|
+
val line = layout.getLineForVertical(y)
|
|
38
|
+
if (x < layout.getLineLeft(line) || x > layout.getLineRight(line)) null
|
|
39
|
+
else {
|
|
40
|
+
val offset = layout.getOffsetForHorizontal(line, x.toFloat())
|
|
41
|
+
buffer.getSpans(offset, offset, ClickableSpan::class.java)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!links.isNullOrEmpty()) {
|
|
46
|
+
if (action == MotionEvent.ACTION_UP) links[0].onClick(widget)
|
|
47
|
+
else Selection.setSelection(buffer, buffer.getSpanStart(links[0]), buffer.getSpanEnd(links[0]))
|
|
48
|
+
return true
|
|
49
|
+
} else {
|
|
50
|
+
Selection.removeSelection(buffer)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
override fun initialize(widget: TextView, text: Spannable) {
|
|
57
|
+
Selection.removeSelection(text)
|
|
58
|
+
}
|
|
59
|
+
}
|
package/package.json
CHANGED