Skip to content

Commit 66e9443

Browse files
committed
fix(mobile): preserve Android markdown spacing and clean selection copies
1 parent 8e520d3 commit 66e9443

6 files changed

Lines changed: 109 additions & 20 deletions

File tree

‎apps/mobile/modules/t3-markdown-text/android/build.gradle‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ android {
88
namespace 'expo.modules.t3markdowntext'
99
compileSdk rootProject.ext.compileSdkVersion
1010

11+
testOptions {
12+
unitTests.includeAndroidResources = true
13+
}
14+
1115
defaultConfig {
1216
minSdkVersion rootProject.ext.minSdkVersion
1317
targetSdkVersion rootProject.ext.targetSdkVersion
@@ -17,4 +21,12 @@ android {
1721
dependencies {
1822
implementation project(':expo-modules-core')
1923
implementation 'com.facebook.react:react-android'
24+
testImplementation 'junit:junit:4.13.2'
25+
testImplementation 'org.robolectric:robolectric:4.16.1'
26+
}
27+
28+
tasks.withType(Test).configureEach {
29+
javaLauncher = javaToolchains.launcherFor {
30+
languageVersion = JavaLanguageVersion.of(21)
31+
}
2032
}

‎apps/mobile/modules/t3-markdown-text/android/src/main/java/expo/modules/t3markdowntext/T3MarkdownTextSelectionModule.kt‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ import kotlin.math.min
1818

1919
private const val OBJECT_REPLACEMENT_CHARACTER = "\uFFFC"
2020

21-
private fun copyTextWithoutInlineImages(
21+
internal fun copyTextWithoutInlineImages(
2222
text: CharSequence,
2323
start: Int,
2424
end: Int
2525
): String {
2626
if (text !is Spanned) return text.subSequence(start, end).toString()
2727

28+
fun isInlineImage(index: Int): Boolean =
29+
index >= 0 && text[index].toString() == OBJECT_REPLACEMENT_CHARACTER &&
30+
text.getSpans(index, index + 1, ReplacementSpan::class.java).isNotEmpty()
31+
2832
return buildString {
2933
for (index in start until end) {
30-
val isInlineImage =
31-
text[index].toString() == OBJECT_REPLACEMENT_CHARACTER &&
32-
text.getSpans(index, index + 1, ReplacementSpan::class.java).isNotEmpty()
33-
if (!isInlineImage) append(text[index])
34+
// The renderer inserts one NBSP after each image to keep its label on the same line.
35+
// Inspect the original text even when selection starts after the image.
36+
val isIconSpacer = text[index] == '\u00A0' && isInlineImage(index - 1)
37+
if (!isInlineImage(index) && !isIconSpacer) append(text[index])
3438
}
3539
}
3640
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package expo.modules.t3markdowntext
2+
3+
import android.graphics.drawable.ColorDrawable
4+
import android.text.SpannableString
5+
import android.text.Spanned
6+
import android.text.style.ImageSpan
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.robolectric.RobolectricTestRunner
11+
import org.robolectric.annotation.Config
12+
13+
@RunWith(RobolectricTestRunner::class)
14+
@Config(sdk = [36], manifest = Config.NONE)
15+
class MarkdownSelectionCopyTest {
16+
private fun withIcon(value: String): SpannableString = SpannableString(value).apply {
17+
val index = value.indexOf('\uFFFC')
18+
setSpan(ImageSpan(ColorDrawable()), index, index + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
19+
}
20+
21+
@Test
22+
fun removesIconAndInjectedSpacer() {
23+
val text = withIcon("\uFFFC\u00A0main.go:12 starts the server.")
24+
assertEquals("main.go:12 starts the server.", copyTextWithoutInlineImages(text, 0, text.length))
25+
}
26+
27+
@Test
28+
fun removesSpacerWhenSelectionStartsAfterIcon() {
29+
val text = withIcon("\uFFFC\u00A0main.go:12 starts the server.")
30+
assertEquals("main.go:12", copyTextWithoutInlineImages(text, 1, 12))
31+
}
32+
33+
@Test
34+
fun preservesAuthoredWhitespaceAndLiteralObjectCharacters() {
35+
val text = withIcon("before\u00A0 \uFFFC\u00A0\u00A0 main.go after\u00A0\uFFFC\u00A0")
36+
assertEquals(
37+
"before\u00A0 \u00A0 main.go after\u00A0\uFFFC\u00A0",
38+
copyTextWithoutInlineImages(text, 0, text.length)
39+
)
40+
}
41+
42+
@Test
43+
fun preservesTextWithoutImageSpans() {
44+
val text = "\uFFFC\u00A0main.go"
45+
assertEquals(text, copyTextWithoutInlineImages(text, 0, text.length))
46+
assertEquals(text, copyTextWithoutInlineImages(SpannableString(text), 0, text.length))
47+
}
48+
}

‎apps/mobile/modules/t3-markdown-text/src/NativeMarkdownBlock.ios.tsx‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type { MarkdownNode } from "react-native-nitro-markdown/headless";
44

55
import { CopyTextButton } from "./CopyTextButton";
66
import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive";
7-
import { nativeMarkdownDocumentRuns, nativeMarkdownListItemBlocks } from "./nativeMarkdownText";
7+
import {
8+
nativeMarkdownBlockSpacing,
9+
nativeMarkdownDocumentRuns,
10+
nativeMarkdownListItemBlocks,
11+
} from "./nativeMarkdownText";
812
import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText.ios";
913
import type {
1014
MarkdownCodeHighlighter,
@@ -595,17 +599,28 @@ export function NativeMarkdownBlock(props: {
595599
switch (props.node.type) {
596600
case "document":
597601
return (
598-
<View style={{ gap: 8 }}>
602+
<View>
599603
{(props.node.children ?? []).map((child, index) => (
600-
<NativeMarkdownBlock
604+
<View
601605
key={nodeKey(child, index)}
602-
node={child}
603-
skills={props.skills}
604-
textStyle={props.textStyle}
605-
highlightCode={props.highlightCode}
606-
onLinkPress={props.onLinkPress}
607-
depth={depth}
608-
/>
606+
style={{
607+
paddingTop:
608+
Platform.OS === "android"
609+
? nativeMarkdownBlockSpacing(props.node.children?.[index - 1], child)
610+
: index > 0
611+
? 8
612+
: 0,
613+
}}
614+
>
615+
<NativeMarkdownBlock
616+
node={child}
617+
skills={props.skills}
618+
textStyle={props.textStyle}
619+
highlightCode={props.highlightCode}
620+
onLinkPress={props.onLinkPress}
621+
depth={depth}
622+
/>
623+
</View>
609624
))}
610625
</View>
611626
);

‎apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,14 @@ function appendTable(
589589
return runs;
590590
}
591591

592+
export function nativeMarkdownBlockSpacing(
593+
previous: MarkdownNode | undefined,
594+
current: MarkdownNode,
595+
): number {
596+
if (!previous) return 0;
597+
return current.type === "heading" ? 20 : previous.type === "heading" ? 10 : 12;
598+
}
599+
592600
function appendDocumentBlock(
593601
runs: NativeMarkdownTextRun[],
594602
node: MarkdownNode,
@@ -599,11 +607,7 @@ function appendDocumentBlock(
599607
const children = node.children ?? [];
600608
for (const [index, child] of children.entries()) {
601609
if (index > 0) {
602-
const previous = children[index - 1];
603-
appendSpacer(
604-
runs,
605-
child.type === "heading" ? 20 : previous?.type === "heading" ? 10 : 12,
606-
);
610+
appendSpacer(runs, nativeMarkdownBlockSpacing(children[index - 1], child));
607611
}
608612
appendDocumentBlock(runs, child, depth);
609613
}

‎apps/mobile/src/lib/nativeMarkdownText.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";
22
import type { MarkdownNode } from "react-native-nitro-markdown/headless";
33

44
import {
5+
nativeMarkdownBlockSpacing,
56
nativeMarkdownChunkSpacing,
67
nativeMarkdownDocumentChunks,
78
nativeMarkdownDocumentRuns,
@@ -381,6 +382,11 @@ describe("nativeMarkdownDocumentRuns", () => {
381382
.filter((run) => run.role === "spacer")
382383
.map((run) => run.spacing),
383384
).toEqual([20, 10, 12]);
385+
expect(
386+
node.children?.map((child, index) =>
387+
nativeMarkdownBlockSpacing(node.children?.[index - 1], child),
388+
),
389+
).toEqual([0, 20, 10, 12]);
384390
});
385391

386392
it("renders tight list items whose inline nodes are direct children", () => {

0 commit comments

Comments
 (0)