From 9f0d441538f5480e3f76167df7bd37fdaa4d9f49 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Mon, 12 Jun 2023 16:48:56 +0200 Subject: [PATCH] MarkdownText: improve link formatting --- .../ui/component/markdown/MarkdownEditor.kt | 8 ++++-- .../ui/component/markdown/MarkdownText.kt | 3 ++- .../component/markdown/StringAnnotations.kt | 26 ++++++++++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/MarkdownEditor.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/MarkdownEditor.kt index dc9ae132..001a43df 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/MarkdownEditor.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/MarkdownEditor.kt @@ -5,6 +5,7 @@ import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Box import androidx.compose.foundation.text.BasicTextField +import androidx.compose.material3.ColorScheme import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.ProvideTextStyle @@ -43,6 +44,7 @@ fun MarkdownEditor( placeholder: (@Composable () -> Unit)? = null ) { val typography = MaterialTheme.typography + val colorScheme = MaterialTheme.colorScheme val delimiterColor = MaterialTheme.colorScheme.secondary val interactionSource = remember { MutableInteractionSource() } @@ -116,8 +118,9 @@ fun MarkdownEditor( cursorBrush = SolidColor(MaterialTheme.colorScheme.primary), visualTransformation = remember( typography, + colorScheme, delimiterColor - ) { MarkdownTransformation(typography, delimiterColor) }, + ) { MarkdownTransformation(colorScheme, typography, delimiterColor) }, interactionSource = interactionSource, ) @@ -156,6 +159,7 @@ fun MarkdownEditor( class MarkdownTransformation( + private val colorScheme: ColorScheme, private val typography: Typography, delimiterColor: Color, ) : VisualTransformation { @@ -170,7 +174,7 @@ class MarkdownTransformation( return TransformedText( buildAnnotatedString { append(text) - applyStyles(tree, typography, delimiterStyle) + applyStyles(tree, colorScheme, typography, delimiterStyle) }, OffsetMapping.Identity, ) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/MarkdownText.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/MarkdownText.kt index 524b4575..33b3dea0 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/MarkdownText.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/MarkdownText.kt @@ -132,11 +132,12 @@ fun ParagraphNode(node: ASTNode, text: String) { val start = node.startOffset val end = node.endOffset val substring = text.substring(start, end) + val colorScheme = MaterialTheme.colorScheme val typography = MaterialTheme.typography Text( text = buildAnnotatedString { append(substring) - applyStyles(node, typography, SpanStyle(fontSize = 0.sp), node.startOffset) + applyStyles(node, colorScheme, typography, SpanStyle(fontSize = 0.sp), node.startOffset) }, ) } diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/StringAnnotations.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/StringAnnotations.kt index 37ec8df2..8f36ac02 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/StringAnnotations.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/component/markdown/StringAnnotations.kt @@ -1,11 +1,14 @@ package de.mm20.launcher2.ui.component.markdown +import androidx.compose.material3.ColorScheme +import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Typography import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextDecoration import org.intellij.markdown.MarkdownElementTypes import org.intellij.markdown.MarkdownTokenTypes import org.intellij.markdown.ast.ASTNode @@ -13,6 +16,7 @@ import kotlin.math.min fun AnnotatedString.Builder.applyStyles( node: ASTNode, + colorScheme: ColorScheme, typography: Typography, delimiterStyle: SpanStyle, rootOffset: Int = 0, @@ -121,9 +125,29 @@ fun AnnotatedString.Builder.applyStyles( min(node.endOffset + 1 - rootOffset, length) ) } + MarkdownElementTypes.INLINE_LINK -> { + val text = node.children.firstOrNull { it.type == MarkdownElementTypes.LINK_TEXT } + val destination = node.children.firstOrNull { it.type == MarkdownElementTypes.LINK_DESTINATION } + + if (text != null && destination != null) { + addStyle( + SpanStyle( + color = colorScheme.primary, + textDecoration = TextDecoration.Underline + ), + text.startOffset - rootOffset, + text.endOffset - rootOffset, + ) + addStyle( + delimiterStyle, + destination.startOffset - rootOffset, + destination.endOffset - rootOffset, + ) + } + } } for (child in node.children) { - applyStyles(child, typography, delimiterStyle, rootOffset) + applyStyles(child, colorScheme, typography, delimiterStyle, rootOffset) } if (node.children.isEmpty() &&