diff --git a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownText.kt b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownText.kt index a87c065..6b5b88a 100644 --- a/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownText.kt +++ b/multiplatform-markdown-renderer/src/commonMain/kotlin/com/mikepenz/markdown/compose/elements/MarkdownText.kt @@ -2,7 +2,9 @@ package com.mikepenz.markdown.compose.elements import androidx.compose.animation.animateContentSize import androidx.compose.foundation.Image -import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.gestures.awaitEachGesture +import androidx.compose.foundation.gestures.awaitFirstDown +import androidx.compose.foundation.gestures.waitForUpOrCancellation import androidx.compose.foundation.text.InlineTextContent import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -12,17 +14,9 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.onPlaced import androidx.compose.ui.platform.LocalUriHandler -import androidx.compose.ui.text.AnnotatedString -import androidx.compose.ui.text.Placeholder -import androidx.compose.ui.text.PlaceholderVerticalAlign -import androidx.compose.ui.text.TextLayoutResult -import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.* import androidx.compose.ui.unit.sp -import com.mikepenz.markdown.compose.LocalImageTransformer -import com.mikepenz.markdown.compose.LocalMarkdownColors -import com.mikepenz.markdown.compose.LocalMarkdownExtendedSpans -import com.mikepenz.markdown.compose.LocalMarkdownTypography -import com.mikepenz.markdown.compose.LocalReferenceLinkHandler +import com.mikepenz.markdown.compose.* import com.mikepenz.markdown.compose.elements.material.MarkdownBasicText import com.mikepenz.markdown.compose.extendedspans.ExtendedSpans import com.mikepenz.markdown.compose.extendedspans.drawBehind @@ -45,7 +39,7 @@ fun MarkdownText( content: AnnotatedString, modifier: Modifier = Modifier, style: TextStyle = LocalMarkdownTypography.current.text, - extendedSpans: ExtendedSpans? = LocalMarkdownExtendedSpans.current.extendedSpans?.invoke() + extendedSpans: ExtendedSpans? = LocalMarkdownExtendedSpans.current.extendedSpans?.invoke(), ) { // extend the annotated string with extended spans styles if provided val extendedStyledText = if (extendedSpans != null) { @@ -88,18 +82,30 @@ fun MarkdownText( val hasUrl = content.getStringAnnotations(MARKDOWN_TAG_URL, 0, content.length).any() val textModifier = if (hasUrl) modifier.pointerInput(Unit) { - detectTapGestures { pos -> - layoutResult.value?.let { layoutResult -> + awaitEachGesture { + val pointer = awaitFirstDown() + val pos = pointer.position // current position + + val foundReference = layoutResult.value?.let { layoutResult -> val position = layoutResult.getOffsetForPosition(pos) content.getStringAnnotations(MARKDOWN_TAG_URL, position, position).reversed().firstOrNull() - ?.let { - val foundReference = referenceLinkHandler.find(it.item) - try { - uriHandler.openUri(foundReference) - } catch (t: Throwable) { - println("Could not open the provided url: $foundReference") - } + ?.let { referenceLinkHandler.find(it.item) } + } + + if (foundReference != null) { + pointer.consume() // consume if we clicked on a link + + val up = waitForUpOrCancellation() + if (up != null) { + up.consume() + + // wait for finger up to navigate to the link + try { + uriHandler.openUri(foundReference) + } catch (t: Throwable) { + println("Could not open the provided url: $foundReference") } + } } } } else modifier @@ -145,3 +151,4 @@ fun MarkdownText( } ) } +