|
1 | 1 | package com.halilibo.richtext.ui |
2 | 2 |
|
3 | | -import androidx.compose.foundation.gestures.detectTapGestures |
4 | 3 | import androidx.compose.foundation.text.BasicText |
5 | 4 | import androidx.compose.foundation.text.InlineTextContent |
6 | 5 | import androidx.compose.runtime.Composable |
7 | 6 | import androidx.compose.runtime.compositionLocalOf |
8 | 7 | import androidx.compose.runtime.mutableStateOf |
9 | 8 | import androidx.compose.runtime.remember |
10 | 9 | import androidx.compose.ui.Modifier |
| 10 | +import androidx.compose.ui.geometry.Offset |
11 | 11 | import androidx.compose.ui.graphics.Color |
12 | 12 | import androidx.compose.ui.graphics.takeOrElse |
13 | 13 | import androidx.compose.ui.input.pointer.pointerInput |
14 | 14 | import androidx.compose.ui.text.AnnotatedString |
15 | 15 | import androidx.compose.ui.text.TextLayoutResult |
16 | 16 | import androidx.compose.ui.text.TextStyle |
17 | 17 | import androidx.compose.ui.text.style.TextOverflow |
| 18 | +import com.halilibo.richtext.ui.util.detectTapGesturesIf |
18 | 19 |
|
19 | 20 | /** |
20 | 21 | * Carries the text style in Composition tree. [Heading], [CodeBlock], |
@@ -111,11 +112,15 @@ internal fun RichTextScope.ClickableText( |
111 | 112 | maxLines: Int = Int.MAX_VALUE, |
112 | 113 | onTextLayout: (TextLayoutResult) -> Unit = {}, |
113 | 114 | inlineContent: Map<String, InlineTextContent> = mapOf(), |
| 115 | + isOffsetClickable: (Int) -> Boolean, |
114 | 116 | onClick: (Int) -> Unit |
115 | 117 | ) { |
116 | 118 | val layoutResult = remember { mutableStateOf<TextLayoutResult?>(null) } |
| 119 | + val shouldHandle = { pos: Offset -> |
| 120 | + layoutResult.value?.getOffsetForPosition(pos)?.let { isOffsetClickable(it) } ?: false |
| 121 | + } |
117 | 122 | val pressIndicator = Modifier.pointerInput(onClick) { |
118 | | - detectTapGestures { pos -> |
| 123 | + detectTapGesturesIf(predicate = shouldHandle) { pos -> |
119 | 124 | layoutResult.value?.let { layoutResult -> |
120 | 125 | onClick(layoutResult.getOffsetForPosition(pos)) |
121 | 126 | } |
|
0 commit comments