-
-
Notifications
You must be signed in to change notification settings - Fork 797
Expand file tree
/
Copy pathHtmlText.kt
More file actions
92 lines (88 loc) · 3.27 KB
/
HtmlText.kt
File metadata and controls
92 lines (88 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
91
92
package org.wikipedia.compose.components
import androidx.compose.foundation.text.TextAutoSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.LinkInteractionListener
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLinkStyles
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import androidx.core.net.toUri
import org.wikipedia.WikipediaApp
import org.wikipedia.compose.extensions.composeFromHtml
import org.wikipedia.compose.theme.BaseTheme
import org.wikipedia.compose.theme.WikipediaTheme
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.theme.Theme
import org.wikipedia.util.UriUtil
@Composable
fun HtmlText(
text: String,
modifier: Modifier = Modifier,
linkStyle: TextLinkStyles = TextLinkStyles(
style = SpanStyle(
color = WikipediaTheme.colors.progressiveColor,
fontSize = 14.sp
)
),
style: TextStyle = TextStyle(
color = WikipediaTheme.colors.primaryColor,
fontSize = 14.sp
),
color: Color = Color.Unspecified,
maxLines: Int = Int.MAX_VALUE,
overflow: TextOverflow = TextOverflow.Ellipsis,
lineHeight: TextUnit = 1.6.em,
linkInteractionListener: LinkInteractionListener = defaultLinkInteractionListener(),
textAlign: TextAlign = TextAlign.Start,
autoSize: TextAutoSize? = null
) {
Text(
modifier = modifier,
text = AnnotatedString.composeFromHtml(
htmlString = text,
linkStyles = linkStyle,
linkInteractionListener = linkInteractionListener
),
lineHeight = lineHeight,
style = style,
color = color,
maxLines = maxLines,
overflow = overflow,
textAlign = textAlign,
autoSize = autoSize
)
}
/**
* Provides a default [LinkInteractionListener] that opens links in an external browser, and
* accounts for links that don't have an explicit protocol (links that start with "//...").
* @param wikiSite Optional [WikiSite] to also handle fully relative URLs.
*/
fun defaultLinkInteractionListener(wikiSite: WikiSite? = null): LinkInteractionListener {
return LinkInteractionListener { linkAnnotation ->
(linkAnnotation as? LinkAnnotation.Url)?.url?.let { url ->
UriUtil.visitInExternalBrowser(WikipediaApp.instance,
(if (wikiSite == null) UriUtil.resolveProtocolRelativeUrl(url) else
UriUtil.resolveProtocolRelativeUrl(wikiSite, url)).toUri()
)
}
}
}
@Preview
@Composable
private fun HtmlTextPreview() {
BaseTheme(currentTheme = Theme.LIGHT) {
HtmlText("This is an <em>example</em> of <strong>text</strong><br />with " +
"<a href=\"#foo\">html</a>, with nonstandard stuff<br />like <code>monospace</code>" +
" and <sup>superscript</sup>, too!")
}
}