|
| 1 | +package dev.typetype.android.feature.player.components |
| 2 | + |
| 3 | +import androidx.compose.material3.MaterialTheme |
| 4 | +import androidx.compose.material3.Text |
| 5 | +import androidx.compose.runtime.Composable |
| 6 | +import androidx.compose.runtime.State |
| 7 | +import androidx.compose.runtime.remember |
| 8 | +import androidx.compose.runtime.rememberUpdatedState |
| 9 | +import androidx.compose.ui.Modifier |
| 10 | +import androidx.compose.ui.graphics.Color |
| 11 | +import androidx.compose.ui.text.AnnotatedString |
| 12 | +import androidx.compose.ui.text.LinkAnnotation |
| 13 | +import androidx.compose.ui.text.SpanStyle |
| 14 | +import androidx.compose.ui.text.TextLinkStyles |
| 15 | +import androidx.compose.ui.text.TextStyle |
| 16 | +import androidx.compose.ui.text.buildAnnotatedString |
| 17 | +import androidx.compose.ui.text.font.FontFamily |
| 18 | +import androidx.compose.ui.text.font.FontStyle |
| 19 | +import androidx.compose.ui.text.font.FontWeight |
| 20 | +import androidx.compose.ui.text.style.TextDecoration |
| 21 | +import androidx.compose.ui.text.style.TextOverflow |
| 22 | +import androidx.compose.ui.text.withLink |
| 23 | +import androidx.compose.ui.text.withStyle |
| 24 | + |
| 25 | +@Composable |
| 26 | +internal fun CommentRichText( |
| 27 | + text: String, |
| 28 | + style: TextStyle, |
| 29 | + linkColor: Color, |
| 30 | + onUrlClick: (String) -> Unit, |
| 31 | + onTimestampClick: (Long) -> Unit, |
| 32 | + modifier: Modifier = Modifier, |
| 33 | + maxLines: Int = Int.MAX_VALUE, |
| 34 | + overflow: TextOverflow = TextOverflow.Clip, |
| 35 | +) { |
| 36 | + val latestOnUrlClick = rememberUpdatedState(onUrlClick) |
| 37 | + val latestOnTimestampClick = rememberUpdatedState(onTimestampClick) |
| 38 | + val nodes = remember(text) { parseRichMarkup(text) } |
| 39 | + val linkStyles = remember(linkColor) { |
| 40 | + TextLinkStyles( |
| 41 | + style = SpanStyle( |
| 42 | + color = linkColor, |
| 43 | + textDecoration = TextDecoration.Underline, |
| 44 | + ), |
| 45 | + ) |
| 46 | + } |
| 47 | + val highlightBackground = MaterialTheme.colorScheme.surfaceVariant |
| 48 | + val formatStyles = remember(highlightBackground) { |
| 49 | + mapOf( |
| 50 | + RichMarkupFormat.Strong to SpanStyle(fontWeight = FontWeight.Bold), |
| 51 | + RichMarkupFormat.Emphasized to SpanStyle(fontStyle = FontStyle.Italic), |
| 52 | + RichMarkupFormat.Underline to SpanStyle(textDecoration = TextDecoration.Underline), |
| 53 | + RichMarkupFormat.Strikethrough to SpanStyle(textDecoration = TextDecoration.LineThrough), |
| 54 | + RichMarkupFormat.Code to SpanStyle(fontFamily = FontFamily.Monospace), |
| 55 | + RichMarkupFormat.Keyboard to SpanStyle(fontFamily = FontFamily.Monospace), |
| 56 | + RichMarkupFormat.Highlight to SpanStyle(background = highlightBackground), |
| 57 | + ) |
| 58 | + } |
| 59 | + val annotated = remember(nodes, linkStyles, formatStyles) { |
| 60 | + buildAnnotatedString { |
| 61 | + appendMarkup( |
| 62 | + nodes = nodes, |
| 63 | + style = SpanStyle(), |
| 64 | + linkStyles = linkStyles, |
| 65 | + formatStyles = formatStyles, |
| 66 | + interactive = true, |
| 67 | + onUrlClick = latestOnUrlClick, |
| 68 | + onTimestampClick = latestOnTimestampClick, |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
| 72 | + Text( |
| 73 | + text = annotated, |
| 74 | + style = style, |
| 75 | + modifier = modifier, |
| 76 | + maxLines = maxLines, |
| 77 | + overflow = overflow, |
| 78 | + ) |
| 79 | +} |
| 80 | + |
| 81 | +private fun AnnotatedString.Builder.appendMarkup( |
| 82 | + nodes: List<RichMarkupNode>, |
| 83 | + style: SpanStyle, |
| 84 | + linkStyles: TextLinkStyles, |
| 85 | + formatStyles: Map<RichMarkupFormat, SpanStyle>, |
| 86 | + interactive: Boolean, |
| 87 | + onUrlClick: State<(String) -> Unit>, |
| 88 | + onTimestampClick: State<(Long) -> Unit>, |
| 89 | +) { |
| 90 | + nodes.forEach { node -> |
| 91 | + when (node) { |
| 92 | + is RichMarkupNode.Text -> appendText( |
| 93 | + value = node.value, |
| 94 | + style = style, |
| 95 | + linkStyles = linkStyles, |
| 96 | + interactive = interactive, |
| 97 | + onUrlClick = onUrlClick, |
| 98 | + onTimestampClick = onTimestampClick, |
| 99 | + ) |
| 100 | + is RichMarkupNode.Break -> append('\n') |
| 101 | + is RichMarkupNode.Link -> withLink( |
| 102 | + LinkAnnotation.Url( |
| 103 | + url = node.href, |
| 104 | + styles = linkStyles, |
| 105 | + linkInteractionListener = { onUrlClick.value(node.href) }, |
| 106 | + ), |
| 107 | + ) { |
| 108 | + appendMarkup( |
| 109 | + nodes = node.children, |
| 110 | + style = style, |
| 111 | + linkStyles = linkStyles, |
| 112 | + formatStyles = formatStyles, |
| 113 | + interactive = false, |
| 114 | + onUrlClick = onUrlClick, |
| 115 | + onTimestampClick = onTimestampClick, |
| 116 | + ) |
| 117 | + } |
| 118 | + is RichMarkupNode.Format -> appendMarkup( |
| 119 | + nodes = node.children, |
| 120 | + style = style.merge(formatStyles.getValue(node.format)), |
| 121 | + linkStyles = linkStyles, |
| 122 | + formatStyles = formatStyles, |
| 123 | + interactive = interactive, |
| 124 | + onUrlClick = onUrlClick, |
| 125 | + onTimestampClick = onTimestampClick, |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +private fun AnnotatedString.Builder.appendText( |
| 132 | + value: String, |
| 133 | + style: SpanStyle, |
| 134 | + linkStyles: TextLinkStyles, |
| 135 | + interactive: Boolean, |
| 136 | + onUrlClick: State<(String) -> Unit>, |
| 137 | + onTimestampClick: State<(Long) -> Unit>, |
| 138 | +) { |
| 139 | + if (!interactive) { |
| 140 | + withStyle(style) { append(value) } |
| 141 | + return |
| 142 | + } |
| 143 | + withStyle(style) { |
| 144 | + var cursor = 0 |
| 145 | + interactiveTextRanges(value).forEach { range -> |
| 146 | + append(value.substring(cursor, range.start)) |
| 147 | + when (range) { |
| 148 | + is InteractiveTextRange.Url -> withLink( |
| 149 | + LinkAnnotation.Url( |
| 150 | + url = range.value, |
| 151 | + styles = linkStyles, |
| 152 | + linkInteractionListener = { onUrlClick.value(range.value) }, |
| 153 | + ), |
| 154 | + ) { |
| 155 | + append(value.substring(range.start, range.endExclusive)) |
| 156 | + } |
| 157 | + is InteractiveTextRange.Timestamp -> withLink( |
| 158 | + LinkAnnotation.Clickable( |
| 159 | + tag = range.positionMillis.toString(), |
| 160 | + styles = linkStyles, |
| 161 | + linkInteractionListener = { onTimestampClick.value(range.positionMillis) }, |
| 162 | + ), |
| 163 | + ) { |
| 164 | + append(value.substring(range.start, range.endExclusive)) |
| 165 | + } |
| 166 | + } |
| 167 | + cursor = range.endExclusive |
| 168 | + } |
| 169 | + append(value.substring(cursor)) |
| 170 | + } |
| 171 | +} |
0 commit comments