Repository navigation
Add message typography and inline link styling theme keys #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
c5deba2
74ae7c3
80f7bdc
0673815
1e0084e
43dec3a
5af0515
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,7 +99,9 @@ internal object ConciergeStyles { | |
| return this.copy( | ||
| // Note: Font family would require loading custom fonts, which is not implemented yet | ||
| // fontFamily = typography.fontFamily?.let { FontFamily(...) }, | ||
| lineHeight = typography.lineHeight?.let { (this.fontSize.value * it).sp } ?: this.lineHeight | ||
| fontSize = typography.fontSize?.sp ?: this.fontSize, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [LOW] |
||
| fontWeight = typography.fontWeight?.let { FontWeight(it) } ?: this.fontWeight, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [HIGH] |
||
| lineHeight = typography.lineHeight?.let { ((typography.fontSize?.toFloat() ?: this.fontSize.value) * it).sp } ?: this.lineHeight | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,15 @@ data class ConciergeThemeTokens( | |
| */ | ||
| data class ConciergeTypography( | ||
| val fontFamily: String? = null, | ||
| val lineHeight: Double? = null | ||
| val lineHeight: Double? = null, | ||
| /** Message body font size in sp. When null, the Material default for the text style is used. */ | ||
| val fontSize: Double? = null, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [LOW] |
||
| /** Message body font weight (100-900). When null, the text style default is used. */ | ||
| val fontWeight: Int? = null, | ||
| /** Font weight applied to inline hyperlinks in message text. When null, links inherit [fontWeight]. */ | ||
| val linkFontWeight: Int? = null, | ||
| /** Whether inline hyperlinks in message text are underlined. Defaults to true (WCAG 1.4.1). */ | ||
| val linkUnderline: Boolean = true | ||
| ) | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ package com.adobe.marketing.mobile.concierge.utils.markdown | |
|
|
||
| import androidx.compose.foundation.isSystemInDarkTheme | ||
| import androidx.compose.ui.text.AnnotatedString | ||
| import androidx.compose.ui.text.font.FontWeight | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import com.adobe.marketing.mobile.concierge.ConciergeConstants | ||
|
|
@@ -44,6 +45,9 @@ internal object MarkdownParser { | |
| val messageTextStyle = messageBubbleStyle.textStyle.copy(color = messageBubbleStyle.botMessageTextColor) | ||
| val darkTheme = isSystemInDarkTheme() | ||
| val linkColor = ConciergeTheme.colors.messageConciergeLink ?: ConciergeTheme.colors.onSurface | ||
| val typography = ConciergeTheme.tokens?.typography | ||
| val linkFontWeight = typography?.linkFontWeight?.let { FontWeight(it) } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [HIGH] Same range problem as |
||
| val linkUnderline = typography?.linkUnderline ?: true | ||
|
|
||
| // Memoize the rendered AnnotatedString. Without this, MarkdownRenderer.render() re-runs on | ||
| // every recomposition for every visible message, rebuilding the full AnnotatedString. During | ||
|
|
@@ -57,13 +61,13 @@ internal object MarkdownParser { | |
| // itself is NOT used as a key — ConciergeTheme.colorScheme builds a fresh ColorScheme on every | ||
| // read and ColorScheme has only reference equality, so keying on it would invalidate the cache | ||
| // every recomposition and defeat the memoization entirely. | ||
| return remember(markdown, linkHints, messageTextStyle, linkColor, darkTheme) { | ||
| return remember(markdown, linkHints, messageTextStyle, linkColor, linkFontWeight, linkUnderline, darkTheme) { | ||
| Log.debug( | ||
| ConciergeConstants.EXTENSION_NAME, | ||
| TAG, | ||
| "Parsed ${tokens.size} tokens, starting rendering." | ||
| ) | ||
| MarkdownRenderer.render(markdown, tokens, colorScheme, messageTextStyle, linkHints, linkColor) | ||
| MarkdownRenderer.render(markdown, tokens, colorScheme, messageTextStyle, linkHints, linkColor, linkFontWeight, linkUnderline) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,7 +74,9 @@ internal object MarkdownRenderer { | |
| colorScheme: ColorScheme, | ||
| baseTextStyle: TextStyle, | ||
| linkHints: List<LinkHint> = emptyList(), | ||
| linkColor: Color = Color.Unspecified | ||
| linkColor: Color = Color.Unspecified, | ||
| linkFontWeight: FontWeight? = null, | ||
| linkUnderline: Boolean = true | ||
| ): AnnotatedString { | ||
| Log.debug( | ||
| ConciergeConstants.EXTENSION_NAME, | ||
|
|
@@ -91,7 +93,7 @@ internal object MarkdownRenderer { | |
| if (token.start < currentIndex) return@forEach | ||
|
|
||
| appendGapText(markdown, currentIndex, token.start, builder, colorScheme, baseTextStyle) | ||
| renderToken(token, builder, colorScheme, baseTextStyle, resolvedLinkColor, linkHintByHref) | ||
| renderToken(token, builder, colorScheme, baseTextStyle, resolvedLinkColor, linkHintByHref, linkFontWeight, linkUnderline) | ||
| currentIndex = token.end | ||
| } | ||
|
|
||
|
|
@@ -134,14 +136,16 @@ internal object MarkdownRenderer { | |
| colorScheme: ColorScheme, | ||
| baseTextStyle: TextStyle, | ||
| linkColor: Color, | ||
| linkHintByHref: Map<String, String> = emptyMap() | ||
| linkHintByHref: Map<String, String> = emptyMap(), | ||
| linkFontWeight: FontWeight? = null, | ||
| linkUnderline: Boolean = true | ||
| ) { | ||
| when (token.type) { | ||
| TokenType.CODE_BLOCK -> renderCodeBlock(token, builder, colorScheme, baseTextStyle) | ||
| TokenType.INLINE_CODE -> renderInlineCode(token, builder, colorScheme, baseTextStyle) | ||
| TokenType.BOLD_LINK -> renderBoldLink(token, builder, colorScheme, baseTextStyle, linkColor, linkHintByHref) | ||
| TokenType.ITALIC_LINK -> renderItalicLink(token, builder, colorScheme, baseTextStyle, linkColor, linkHintByHref) | ||
| TokenType.LINK -> renderLink(token, builder, colorScheme, baseTextStyle, linkColor, linkHintByHref) | ||
| TokenType.BOLD_LINK -> renderBoldLink(token, builder, colorScheme, baseTextStyle, linkColor, linkHintByHref, linkFontWeight, linkUnderline) | ||
| TokenType.ITALIC_LINK -> renderItalicLink(token, builder, colorScheme, baseTextStyle, linkColor, linkHintByHref, linkFontWeight, linkUnderline) | ||
| TokenType.LINK -> renderLink(token, builder, colorScheme, baseTextStyle, linkColor, linkHintByHref, linkFontWeight, linkUnderline) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [HIGH] |
||
| TokenType.CITATION -> renderCitation(token, builder, colorScheme, baseTextStyle) | ||
| TokenType.BOLD -> renderBold(token, builder, colorScheme, baseTextStyle) | ||
| TokenType.ITALIC -> renderItalic(token, builder, colorScheme, baseTextStyle) | ||
|
|
@@ -203,7 +207,9 @@ internal object MarkdownRenderer { | |
| colorScheme: ColorScheme, | ||
| baseTextStyle: TextStyle, | ||
| linkColor: Color, | ||
| linkHintByHref: Map<String, String> = emptyMap() | ||
| linkHintByHref: Map<String, String> = emptyMap(), | ||
| linkFontWeight: FontWeight? = null, | ||
| linkUnderline: Boolean = true | ||
| ) { | ||
| val (linkText, linkUrl) = token.groups | ||
| val styleStart = builder.length | ||
|
|
@@ -212,7 +218,8 @@ internal object MarkdownRenderer { | |
| builder.addStyle( | ||
| baseTextStyle.toSpanStyle().copy( | ||
| color = linkColor, | ||
| textDecoration = TextDecoration.Underline | ||
| textDecoration = if (linkUnderline) TextDecoration.Underline else null, | ||
| fontWeight = linkFontWeight ?: baseTextStyle.fontWeight | ||
| ), | ||
| styleStart, | ||
| builder.length | ||
|
|
@@ -234,7 +241,9 @@ internal object MarkdownRenderer { | |
| colorScheme: ColorScheme, | ||
| baseTextStyle: TextStyle, | ||
| linkColor: Color, | ||
| linkHintByHref: Map<String, String> = emptyMap() | ||
| linkHintByHref: Map<String, String> = emptyMap(), | ||
| linkFontWeight: FontWeight? = null, | ||
| linkUnderline: Boolean = true | ||
| ) { | ||
| val (linkText, linkUrl) = token.groups | ||
| val styleStart = builder.length | ||
|
|
@@ -243,8 +252,8 @@ internal object MarkdownRenderer { | |
| builder.addStyle( | ||
| baseTextStyle.toSpanStyle().copy( | ||
| color = linkColor, | ||
| textDecoration = TextDecoration.Underline, | ||
| fontWeight = FontWeight.Bold | ||
| textDecoration = if (linkUnderline) TextDecoration.Underline else null, | ||
| fontWeight = linkFontWeight ?: FontWeight.Bold | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [LOW] With |
||
| ), | ||
| styleStart, | ||
| builder.length | ||
|
|
@@ -266,7 +275,9 @@ internal object MarkdownRenderer { | |
| colorScheme: ColorScheme, | ||
| baseTextStyle: TextStyle, | ||
| linkColor: Color, | ||
| linkHintByHref: Map<String, String> = emptyMap() | ||
| linkHintByHref: Map<String, String> = emptyMap(), | ||
| linkFontWeight: FontWeight? = null, | ||
| linkUnderline: Boolean = true | ||
| ) { | ||
| val (linkText, linkUrl) = token.groups | ||
| val styleStart = builder.length | ||
|
|
@@ -275,7 +286,8 @@ internal object MarkdownRenderer { | |
| builder.addStyle( | ||
| baseTextStyle.toSpanStyle().copy( | ||
| color = linkColor, | ||
| textDecoration = TextDecoration.Underline, | ||
| textDecoration = if (linkUnderline) TextDecoration.Underline else null, | ||
| fontWeight = linkFontWeight ?: baseTextStyle.fontWeight, | ||
| fontStyle = FontStyle.Italic | ||
| ), | ||
| styleStart, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[LOW]
parseFontWeightturns an unreadable value (e.g."heavy") into400instead ofnull. For link weight, that 400 then overridesFontWeight.Boldon**[text](url)**links.parseFontWeightOrNullplus a 1–1000 range check here (and formessage-font-weightabove) would fall back to the default look and also fix the crash.