Skip to content

Commit

Permalink
Add onTextClick to MarkdownBlockRenderer
Browse files Browse the repository at this point in the history
This is a workaround to the fact that ClickableText swallows all clicks,
since its onClick lambda has no way of saying if it did handle a click.

Hopefully this won't be needed anymore when we move to Compose 1.7,
which brings in a proper LinkAnnotation.
  • Loading branch information
rock3r committed Apr 25, 2024
1 parent 241117d commit c7f4646
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public open class DefaultMarkdownBlockRenderer(
private val rendererExtensions: List<MarkdownRendererExtension>,
private val inlineRenderer: InlineMarkdownRenderer,
private val onUrlClick: (String) -> Unit,
private val onTextClick: () -> Unit,
) : MarkdownBlockRenderer {

@Composable
Expand Down Expand Up @@ -115,6 +116,7 @@ public open class DefaultMarkdownBlockRenderer(
text = renderedContent,
textStyle = styling.inlinesStyling.textStyle,
color = styling.inlinesStyling.textStyle.color.takeOrElse { LocalContentColor.current },
onUnhandledClick = onTextClick,
)
}

Expand Down Expand Up @@ -158,6 +160,7 @@ public open class DefaultMarkdownBlockRenderer(
text = renderedContent,
textStyle = textStyle,
color = textStyle.color.takeOrElse { LocalContentColor.current },
onUnhandledClick = onTextClick,
)

if (underlineWidth > 0.dp && underlineColor.isSpecified) {
Expand Down Expand Up @@ -380,6 +383,7 @@ public open class DefaultMarkdownBlockRenderer(
textStyle: TextStyle,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
onUnhandledClick: () -> Unit,
) {
var pointerIcon by remember { mutableStateOf(PointerIcon.Default) }

Expand All @@ -400,8 +404,12 @@ public open class DefaultMarkdownBlockRenderer(
}
},
) { offset ->
val span = text.getUrlAnnotations(offset, offset).firstOrNull() ?: return@ClickableText
onUrlClick(span.item.url)
val span = text.getUrlAnnotations(offset, offset).firstOrNull()
if (span != null) {
onUrlClick(span.item.url)
} else {
onUnhandledClick()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ public fun MarkdownBlockRenderer.Companion.light(
rendererExtensions: List<MarkdownRendererExtension> = emptyList(),
inlineRenderer: InlineMarkdownRenderer = InlineMarkdownRenderer.default(),
onUrlClick: (String) -> Unit = {},
): MarkdownBlockRenderer = DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick)
onTextClick: () -> Unit = {},
): MarkdownBlockRenderer =
DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick, onTextClick)

public fun MarkdownBlockRenderer.Companion.dark(
styling: MarkdownStyling = MarkdownStyling.dark(),
rendererExtensions: List<MarkdownRendererExtension> = emptyList(),
inlineRenderer: InlineMarkdownRenderer = InlineMarkdownRenderer.default(),
onUrlClick: (String) -> Unit = {},
): MarkdownBlockRenderer = DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick)
onTextClick: () -> Unit = {},
): MarkdownBlockRenderer =
DefaultMarkdownBlockRenderer(styling, rendererExtensions, inlineRenderer, onUrlClick, onTextClick)
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,15 @@ internal fun MarkdownPreview(
styling = markdownStyling,
rendererExtensions = listOf(GitHubAlertRendererExtension(AlertStyling.dark(), markdownStyling)),
inlineRenderer = InlineMarkdownRenderer.default(extensions),
) { url ->
Desktop.getDesktop().browse(URI.create(url))
}
onUrlClick = { url -> Desktop.getDesktop().browse(URI.create(url)) },
)
} else {
MarkdownBlockRenderer.light(
styling = markdownStyling,
rendererExtensions = listOf(GitHubAlertRendererExtension(AlertStyling.light(), markdownStyling)),
inlineRenderer = InlineMarkdownRenderer.default(extensions),
) { url ->
Desktop.getDesktop().browse(URI.create(url))
}
onUrlClick = { url -> Desktop.getDesktop().browse(URI.create(url)) },
)
}
}

Expand Down

0 comments on commit c7f4646

Please sign in to comment.