|
1 | 1 | package com.richtext |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import android.graphics.Color |
| 5 | +import android.text.method.LinkMovementMethod |
4 | 6 | import android.util.AttributeSet |
5 | | -import android.view.View |
| 7 | +import androidx.appcompat.widget.AppCompatTextView |
| 8 | +import com.facebook.react.common.ReactConstants |
| 9 | +import com.facebook.react.views.text.ReactTypefaceUtils.applyStyles |
| 10 | +import com.facebook.react.views.text.ReactTypefaceUtils.parseFontStyle |
| 11 | +import com.facebook.react.views.text.ReactTypefaceUtils.parseFontWeight |
| 12 | +import com.richtext.parser.Parser |
| 13 | +import com.richtext.renderer.Renderer |
6 | 14 |
|
7 | | -class RichTextView : View { |
8 | | - constructor(context: Context?) : super(context) |
9 | | - constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) |
10 | | - constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super( |
| 15 | +class RichTextView : AppCompatTextView { |
| 16 | + |
| 17 | + private val parser = Parser() |
| 18 | + private val renderer = Renderer() |
| 19 | + private var onLinkPressCallback: ((String) -> Unit)? = null |
| 20 | + |
| 21 | + private var typefaceDirty = false |
| 22 | + private var didAttachToWindow = false |
| 23 | + |
| 24 | + var fontSize: Float? = null |
| 25 | + private var fontFamily: String? = null |
| 26 | + private var fontStyle: Int = ReactConstants.UNSET |
| 27 | + private var fontWeight: Int = ReactConstants.UNSET |
| 28 | + |
| 29 | + constructor(context: Context) : super(context) { |
| 30 | + prepareComponent() |
| 31 | + } |
| 32 | + |
| 33 | + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { |
| 34 | + prepareComponent() |
| 35 | + } |
| 36 | + |
| 37 | + constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super( |
11 | 38 | context, |
12 | 39 | attrs, |
13 | 40 | defStyleAttr |
14 | | - ) |
| 41 | + ) { |
| 42 | + prepareComponent() |
| 43 | + } |
| 44 | + |
| 45 | + private fun prepareComponent() { |
| 46 | + movementMethod = LinkMovementMethod.getInstance() |
| 47 | + setPadding(0, 0, 0, 0) |
| 48 | + setBackgroundColor(Color.TRANSPARENT) |
| 49 | + } |
| 50 | + |
| 51 | + fun setMarkdownContent(markdown: String) { |
| 52 | + try { |
| 53 | + val document = parser.parseMarkdown(markdown) |
| 54 | + if (document != null) { |
| 55 | + val styledText = renderer.renderDocument(document, onLinkPressCallback) |
| 56 | + text = styledText |
| 57 | + movementMethod = LinkMovementMethod.getInstance() |
| 58 | + } else { |
| 59 | + android.util.Log.e("RichTextView", "Failed to parse markdown - Document is null") |
| 60 | + text = "" |
| 61 | + } |
| 62 | + } catch (e: Exception) { |
| 63 | + android.util.Log.e("RichTextView", "Error parsing markdown: ${e.message}") |
| 64 | + text = "" |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + fun setOnLinkPressCallback(callback: (String) -> Unit) { |
| 70 | + onLinkPressCallback = callback |
| 71 | + } |
| 72 | + |
| 73 | + fun emitOnLinkPress(url: String) { |
| 74 | + val context = this.context as? com.facebook.react.bridge.ReactContext ?: return |
| 75 | + val surfaceId = com.facebook.react.uimanager.UIManagerHelper.getSurfaceId(context) |
| 76 | + val dispatcher = |
| 77 | + com.facebook.react.uimanager.UIManagerHelper.getEventDispatcherForReactTag(context, id) |
| 78 | + |
| 79 | + dispatcher?.dispatchEvent( |
| 80 | + com.richtext.events.LinkPressEvent( |
| 81 | + surfaceId, |
| 82 | + id, |
| 83 | + url |
| 84 | + ) |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + fun setFontSize(size: Float) { |
| 89 | + fontSize = size |
| 90 | + textSize = size |
| 91 | + typefaceDirty = true |
| 92 | + updateTypeface() |
| 93 | + } |
| 94 | + |
| 95 | + fun setFontFamily(family: String?) { |
| 96 | + fontFamily = family |
| 97 | + typefaceDirty = true |
| 98 | + updateTypeface() |
| 99 | + } |
| 100 | + |
| 101 | + fun setFontWeight(weight: String?) { |
| 102 | + val parsedWeight = parseFontWeight(weight) |
| 103 | + if (parsedWeight != fontWeight) { |
| 104 | + fontWeight = parsedWeight |
| 105 | + typefaceDirty = true |
| 106 | + updateTypeface() |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + fun setFontStyle(style: String?) { |
| 111 | + val parsedStyle = parseFontStyle(style) |
| 112 | + if (parsedStyle != fontStyle) { |
| 113 | + fontStyle = parsedStyle |
| 114 | + typefaceDirty = true |
| 115 | + updateTypeface() |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fun setColor(color: Int?) { |
| 120 | + if (color != null) { |
| 121 | + setTextColor(color) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + fun updateTypeface() { |
| 126 | + if (!typefaceDirty) return |
| 127 | + typefaceDirty = false |
| 128 | + |
| 129 | + val newTypeface = applyStyles(typeface, fontStyle, fontWeight, fontFamily, context.assets) |
| 130 | + setTypeface(newTypeface) |
| 131 | + } |
| 132 | + |
| 133 | + override fun onAttachedToWindow() { |
| 134 | + super.onAttachedToWindow() |
| 135 | + didAttachToWindow = true |
| 136 | + updateTypeface() |
| 137 | + } |
15 | 138 | } |
0 commit comments