Skip to content

Commit

Permalink
Merge pull request #199 from mikepenz/fix/188
Browse files Browse the repository at this point in the history
Rework link click detection
  • Loading branch information
mikepenz authored Aug 23, 2024
2 parents 6dc3918 + 0081dac commit 701bf2f
Showing 1 changed file with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -145,3 +151,4 @@ fun MarkdownText(
}
)
}

0 comments on commit 701bf2f

Please sign in to comment.