Skip to content

Commit 171712d

Browse files
committed
refactor(android): enhance LinkRenderer to utilize configurable link styles
1 parent 231b601 commit 171712d

3 files changed

Lines changed: 37 additions & 21 deletions

File tree

android/src/main/java/com/richtext/renderer/NodeRenderer.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.richtext.renderer
22

33
import android.text.SpannableStringBuilder
4-
import android.text.style.UnderlineSpan
54
import com.richtext.spans.RichTextLinkSpan
65
import com.richtext.spans.RichTextHeadingSpan
76
import com.richtext.spans.RichTextParagraphSpan
@@ -128,7 +127,9 @@ class TextRenderer : NodeRenderer {
128127
}
129128
}
130129

131-
class LinkRenderer : NodeRenderer {
130+
class LinkRenderer(
131+
private val config: RendererConfig? = null
132+
) : NodeRenderer {
132133
override fun render(
133134
node: Node,
134135
builder: SpannableStringBuilder,
@@ -140,21 +141,14 @@ class LinkRenderer : NodeRenderer {
140141

141142
var child = link.firstChild
142143
while (child != null) {
143-
NodeRendererFactory.getRenderer(child).render(child, builder, onLinkPress)
144+
NodeRendererFactory.getRenderer(child, config).render(child, builder, onLinkPress)
144145
child = child.next
145146
}
146147

147148
val contentLength = builder.length - start
148-
if (contentLength > 0) {
149-
builder.setSpan(
150-
RichTextLinkSpan(url, onLinkPress),
151-
start,
152-
start + contentLength,
153-
android.text.SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
154-
)
155-
149+
if (contentLength > 0 && config != null) {
156150
builder.setSpan(
157-
UnderlineSpan(),
151+
RichTextLinkSpan(url, onLinkPress, config.style),
158152
start,
159153
start + contentLength,
160154
android.text.SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
@@ -180,7 +174,7 @@ object NodeRendererFactory {
180174
is Paragraph -> ParagraphRenderer(config)
181175
is Heading -> HeadingRenderer(config)
182176
is Text -> TextRenderer()
183-
is Link -> LinkRenderer()
177+
is Link -> LinkRenderer(config)
184178
is HardLineBreak, is SoftLineBreak -> LineBreakRenderer()
185179
else -> {
186180
android.util.Log.w(

android/src/main/java/com/richtext/spans/RichTextLinkSpan.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import android.text.TextPaint
44
import android.text.style.ClickableSpan
55
import android.view.View
66
import com.richtext.RichTextView
7+
import com.richtext.styles.RichTextStyle
78

89
class RichTextLinkSpan(
910
private val url: String,
10-
private val onLinkPress: ((String) -> Unit)?
11+
private val onLinkPress: ((String) -> Unit)?,
12+
private val style: RichTextStyle
1113
) : ClickableSpan() {
1214

1315
override fun onClick(widget: View) {
@@ -21,7 +23,7 @@ class RichTextLinkSpan(
2123

2224
override fun updateDrawState(textPaint: TextPaint) {
2325
super.updateDrawState(textPaint)
24-
textPaint.isUnderlineText = true
25-
textPaint.color = textPaint.linkColor
26+
textPaint.color = style.getLinkColor()
27+
textPaint.isUnderlineText = style.getLinkUnderline()
2628
}
2729
}

android/src/main/java/com/richtext/styles/RichTextStyle.kt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ data class HeadingStyle(
88
val fontFamily: String?
99
)
1010

11+
data class LinkStyle(
12+
val color: Int,
13+
val underline: Boolean
14+
)
15+
1116
class RichTextStyle(style: ReadableMap) {
1217
private val headingStyles = arrayOfNulls<HeadingStyle>(7)
18+
private lateinit var linkStyle: LinkStyle
1319

1420
init {
1521
parseStyles(style)
@@ -24,21 +30,35 @@ class RichTextStyle(style: ReadableMap) {
2430
return headingStyles[level]?.fontFamily
2531
}
2632

33+
fun getLinkColor(): Int {
34+
return linkStyle.color
35+
}
36+
37+
fun getLinkUnderline(): Boolean {
38+
return linkStyle.underline
39+
}
40+
2741
private fun parseStyles(style: ReadableMap) {
2842
(1..6).forEach { level ->
2943
val levelKey = "h$level"
30-
val levelStyle = style.getMap(levelKey)
31-
requireNotNull(levelStyle) { "Style for $levelKey not found. JS should always provide defaults." }
32-
33-
require(levelStyle.hasKey("fontSize") && !levelStyle.isNull("fontSize")) {
34-
"fontSize not found for $levelKey. JS should always provide defaults."
44+
val levelStyle = requireNotNull(style.getMap(levelKey)) {
45+
"Style for $levelKey not found. JS should always provide defaults."
3546
}
3647

3748
val fontSize = PixelUtil.toPixelFromSP(levelStyle.getDouble("fontSize").toFloat())
3849
val fontFamily = levelStyle.getString("fontFamily")
3950

4051
headingStyles[level] = HeadingStyle(fontSize, fontFamily)
4152
}
53+
54+
val linkStyleMap = requireNotNull(style.getMap("link")) {
55+
"Link style not found. JS should always provide defaults."
56+
}
57+
58+
val color = linkStyleMap.getInt("color")
59+
val underline = linkStyleMap.getBoolean("underline")
60+
61+
linkStyle = LinkStyle(color, underline)
4262
}
4363
}
4464

0 commit comments

Comments
 (0)