git.iko.soy/opencode-android
on e37d1bb help sign in
43 lines 1.7 kB history raw
1package soy.iko.opencode.ui.components
3import android.content.ClipData
4import android.content.ClipboardManager
5import android.content.Context
6import android.widget.Toast
7import androidx.compose.foundation.ExperimentalFoundationApi
8import androidx.compose.foundation.combinedClickable
9import androidx.compose.material3.MaterialTheme
10import androidx.compose.runtime.Composable
11import androidx.compose.ui.Modifier
12import androidx.compose.ui.platform.LocalContext
13import androidx.compose.ui.text.TextStyle
14import com.mikepenz.markdown.m3.Markdown
16/**
17 * Renders markdown using [multiplatform-markdown-renderer](https://github.com/mikepenz/multiplatform-markdown-renderer)
18 * (commonmark-java under the hood). Keeps a local API so callers don't import the library directly.
19 * Long-press copies the raw markdown to the system clipboard.
20 */
21@OptIn(ExperimentalFoundationApi::class)
22@Composable
23fun MarkdownText(
24 markdown: String,
25 modifier: Modifier = Modifier,
26 style: TextStyle = MaterialTheme.typography.bodyLarge,
27) {
28 val context = LocalContext.current
29 Markdown(
30 content = markdown,
31 modifier = modifier.combinedClickable(
32 onClick = {},
33 onLongClick = { copyToClipboard(context, markdown) },
34 ),
35 )
38/** Copy [text] to the system clipboard and show a confirmation toast. */
39internal fun copyToClipboard(context: Context, label: String, text: String = label) {
40 val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
41 clipboard.setPrimaryClip(ClipData.newPlainText(label.take(40), text))
42 Toast.makeText(context, "Copied", Toast.LENGTH_SHORT).show()