The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 02:00 UTC
fix(mobile): select file text across lines on Android (#31286)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Nov 9, 2026 at 12:35 UTC
8f144827f680f4064cb3951b4b60d4e24043748d
5 files changed
+158
-16
apps/mobile/src/features/files/SourceFileSurface.tsx
+76
-13
@@ -6,12 +6,14 @@ import { useLayoutEffect, useRef, useState } from "react";
6
import {
7
FlatList,
8
Platform,
9
+ Pressable,
10
RefreshControl,
11
ScrollView,
12
Text as NativeText,
13
useWindowDimensions,
14
View,
15
} from "react-native";
16
+import { WebView } from "react-native-webview";
17
import { useSafeAreaInsets } from "react-native-safe-area-context";
18
import { AppText as Text } from "../../components/ui/AppText";
19
import { LoadingStrip } from "../../components/ui/LoadingStrip";
@@ -35,7 +37,11 @@ import {
37
} from "./nativeSourceFileAdapter";
38
import { SourceText } from "@t3tools/mobile-source-text";
39
38
-import { boundedSelectableSourceTokens, prepareSourceFileDocument } from "./source-file-document";
40
+import {
41
+ boundedSelectableSourceTokens,
42
+ prepareSourceFileDocument,
43
+ selectableSourceHtml,
44
+} from "./source-file-document";
45
import { sourceHighlightAtom } from "./sourceHighlightingState";
46
import { createSourceLineScroll } from "./source-file-scroll";
47
@@ -46,6 +52,7 @@ interface SourceFileSurfaceProps {
52
readonly initialLine?: number | null;
53
readonly headerInsetTop?: number;
54
readonly selectable?: boolean;
55
+ readonly onLongPressLine?: (line: number) => void;
56
readonly onRefresh?: () => Promise<void> | void;
57
}
58
@@ -58,8 +65,10 @@ const HighlightedSourceLine = function HighlightedSourceLine(props: {
65
readonly tokens: ReadonlyArray<HighlightedToken> | null;
66
readonly highlighted: boolean;
67
readonly wordBreak: boolean;
68
+ readonly onLongPress?: () => void;
69
}) {
62
- return (
70
+ const selectable = props.onLongPress === undefined;
71
+ const row = (
72
<View
73
className={cn("flex-row", props.highlighted && "bg-primary/10")}
74
style={{ minHeight: props.codeSurface.rowHeight }}
@@ -76,7 +85,7 @@ const HighlightedSourceLine = function HighlightedSourceLine(props: {
85
{props.index + 1}
86
</NativeText>
87
<NativeText
79
- selectable
88
+ selectable={selectable}
89
selectionColorClassName={Platform.OS === "android" ? "accent-focus/32" : undefined}
90
numberOfLines={props.wordBreak ? undefined : 1}
91
className="flex-1 font-normal text-foreground"
@@ -106,7 +115,7 @@ const HighlightedSourceLine = function HighlightedSourceLine(props: {
115
return (
116
<NativeText
117
key={`${start}:${token.content.length}:${token.color ?? ""}`}
109
- selectable
118
+ selectable={selectable}
119
selectionColorClassName={
120
Platform.OS === "android" ? "accent-focus/32" : undefined
121
}
@@ -126,6 +135,20 @@ const HighlightedSourceLine = function HighlightedSourceLine(props: {
135
</NativeText>
136
</View>
137
);
138
+ const onLongPress = props.onLongPress;
139
+ return onLongPress ? (
140
+ <Pressable
141
+ onLongPress={onLongPress}
142
+ accessibilityActions={[{ name: "longpress", label: "Select text" }]}
143
+ onAccessibilityAction={(event) => {
144
+ if (event.nativeEvent.actionName === "longpress") onLongPress();
145
+ }}
146
+ >
147
+ {row}
148
+ </Pressable>
149
+ ) : (
150
+ row
151
+ );
152
};
153
154
function useSourceFileModel(props: SourceFileSurfaceProps) {
@@ -185,7 +208,7 @@ function NativeSourceFileSurface(
208
readonly NativeView: ComponentType<NativeReviewDiffViewProps>;
209
},
210
) {
188
- const { NativeView, onRefresh } = props;
211
+ const { NativeView, onLongPressLine, onRefresh } = props;
212
const insets = useSafeAreaInsets();
213
const columnMetrics = useNativeColumnLayoutMetrics();
214
const safeArea = columnMetrics?.safeArea ?? insets;
@@ -224,6 +247,11 @@ function NativeSourceFileSurface(
247
styleJson={styleJson}
248
themeJson={themeJson}
249
tokensJson={tokensJson}
250
+ onPressLine={({ nativeEvent }) => {
251
+ if (nativeEvent.gesture === "longPress" && nativeEvent.newLineNumber !== undefined) {
252
+ onLongPressLine?.(nativeEvent.newLineNumber);
253
+ }
254
+ }}
255
{...(onRefresh
256
? {
257
refreshing: isPullRefreshing,
@@ -237,7 +265,8 @@ function NativeSourceFileSurface(
265
266
function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
267
const usesAutomaticInsets = Platform.OS === "ios" && !props.embedded;
240
- const foreground = useUniwindTheme()["--color-foreground"];
268
+ const appTheme = useUniwindTheme();
269
+ const foreground = appTheme["--color-foreground"];
270
const { codeSurface, codeWordBreak } = useAppearanceCodeSurface();
271
const { normalizedContents, lines, status, targetIndex, tokens } = useSourceFileModel(props);
272
const selectableTokens = props.selectable ? boundedSelectableSourceTokens(tokens) : null;
@@ -248,17 +277,23 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
277
<RefreshControl refreshing={isPullRefreshing} onRefresh={() => void handlePullToRefresh()} />
278
) : undefined;
279
280
+ const usesLineList = !props.selectable;
281
+ // Android lays out a full selectable TextView on the UI thread, which can stall large files.
282
+ const usesWebSelection = props.selectable === true && Platform.OS === "android";
283
+ const usesTextSelection = props.selectable === true && !usesWebSelection;
284
+
285
// Layout effect so a pending jump is cancelled in the commit that swaps the file.
286
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- scrolls the FlatList to the target line and cancels a pending jump when the file changes
287
useLayoutEffect(() => {
254
- if (targetIndex === null) {
288
+ if (targetIndex === null || !usesLineList) {
289
return;
290
}
291
lineScroll.scrollToLine(listRef.current, targetIndex);
292
return lineScroll.cancel;
259
- // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Opening another file at the same line index scrolls the new file to it.
260
- }, [lineScroll, props.path, targetIndex]);
293
+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Opening another file at the same line index, or leaving selection mode, scrolls the remounted list to it.
294
+ }, [lineScroll, props.path, targetIndex, usesLineList]);
295
296
+ const { onLongPressLine } = props;
297
const renderLine = ({ item, index }: { item: string; index: number }) => (
298
<HighlightedSourceLine
299
codeSurface={codeSurface}
@@ -267,14 +302,14 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
302
tokens={tokens?.[index] ?? null}
303
highlighted={index === targetIndex}
304
wordBreak={codeWordBreak}
305
+ onLongPress={onLongPressLine ? () => onLongPressLine(index + 1) : undefined}
306
/>
307
);
308
309
// One native text view permits document-wide selection; virtualized rows cannot.
274
- const selectableBlock = props.selectable ? (
310
+ const selectableBlock = usesTextSelection ? (
311
<SourceText
312
selectable
277
- selectionColorClassName={Platform.OS === "android" ? "accent-focus/32" : undefined}
313
style={{
314
color: foreground,
315
fontFamily: REVIEW_MONO_FONT_FAMILY,
@@ -322,6 +357,33 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
357
</SourceText>
358
) : null;
359
360
+ const webSelection = usesWebSelection ? (
361
+ <WebView
362
+ source={{
363
+ html: selectableSourceHtml(normalizedContents, {
364
+ foreground,
365
+ selection: appTheme["--color-focus"],
366
+ fontSize: codeSurface.fontSize,
367
+ rowHeight: codeSurface.rowHeight,
368
+ wordBreak: codeWordBreak,
369
+ }),
370
+ }}
371
+ originWhitelist={["*"]}
372
+ // Android pastes copied HTML as styled text with collapsed whitespace, so copy plain text.
373
+ // Wrapped text reflows once the WebView reaches its final width, so scroll again then.
374
+ injectedJavaScript={`addEventListener("copy",(event)=>{event.clipboardData.setData("text/plain",getSelection().toString());event.preventDefault();});${
375
+ targetIndex === null
376
+ ? ""
377
+ : `const line=document.getElementById("L${targetIndex + 1}");const reveal=()=>line?.scrollIntoView({block:"center"});reveal();addEventListener("resize",reveal,{once:true});`
378
+ }true;`}
379
+ domStorageEnabled={false}
380
+ cacheEnabled={false}
381
+ setSupportMultipleWindows={false}
382
+ onShouldStartLoadWithRequest={(request) => request.url === "about:blank"}
383
+ style={{ flex: 1, backgroundColor: "transparent" }}
384
+ />
385
+ ) : null;
386
+
387
const list = (
388
<FlatList
389
ref={listRef}
@@ -353,7 +415,6 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
415
/>
416
);
417
356
- const usesLineList = !props.selectable;
418
const padded = (
419
<ScrollView
420
refreshControl={refreshControl}
@@ -372,7 +433,7 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
433
434
return (
435
<View className="relative flex-1 bg-sheet">
375
- <SourceHighlightStatusView status={status} />
436
+ {usesWebSelection ? null : <SourceHighlightStatusView status={status} />}
437
{usesLineList ? (
438
codeWordBreak ? (
439
list
@@ -381,6 +442,8 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
442
{list}
443
</ScrollView>
444
)
445
+ ) : usesWebSelection ? (
446
+ webSelection
447
) : codeWordBreak ? (
448
padded
449
) : (
apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx
+36
-3
@@ -2,8 +2,13 @@ import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-action
2
import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
3
import { resolveFilesystemReadAccess } from "@t3tools/client-runtime/state/filesystem";
4
import { environmentSession } from "../../state/session";
5
-import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
6
-import { type ReactNode, useId, useState } from "react";
5
+import {
6
+ StackActions,
7
+ useNavigation,
8
+ usePreventRemove,
9
+ type StaticScreenProps,
10
+} from "@react-navigation/native";
11
+import { type ComponentProps, type ReactNode, useId, useState } from "react";
12
import { Platform, View } from "react-native";
13
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
14
import { EnvironmentId, type ProjectReadFileResult, ThreadId } from "@t3tools/contracts";
@@ -197,6 +202,33 @@ function defaultViewMode(path: string | null): FileViewMode {
202
: "source";
203
}
204
205
+// Android numbered rows are separate text views, so a long-press opens the whole file as one
206
+// selectable text at that line. Back returns to the numbered rows at the same line.
207
+function FileSourceSurface(props: ComponentProps<typeof SourceFileSurface>) {
208
+ const navigation = useNavigation();
209
+ const [selection, setSelection] = useState<{
210
+ readonly line: number;
211
+ readonly active: boolean;
212
+ } | null>(null);
213
+ usePreventRemove(selection?.active === true, ({ data }) => {
214
+ setSelection((value) => value && { ...value, active: false });
215
+ // The re-dispatched action skips this guard, so "Return to chat" still leaves in one tap.
216
+ if (data.action.type !== "GO_BACK" && data.action.type !== "POP") {
217
+ navigation.dispatch(data.action);
218
+ }
219
+ });
220
+ return (
221
+ <SourceFileSurface
222
+ {...props}
223
+ initialLine={selection?.line ?? props.initialLine}
224
+ selectable={selection?.active === true}
225
+ onLongPressLine={
226
+ Platform.OS === "android" ? (line) => setSelection({ line, active: true }) : undefined
227
+ }
228
+ />
229
+ );
230
+}
231
+
232
function FileContent(props: {
233
readonly activeMode: FileViewMode;
234
readonly cwd: string;
@@ -308,7 +340,8 @@ function FileContent(props: {
340
onRefresh={props.onRefresh}
341
/>
342
) : (
311
- <SourceFileSurface
343
+ <FileSourceSurface
344
+ key={props.initialLine}
345
contents={props.fileContents}
346
path={props.relativePath}
347
initialLine={props.initialLine}
apps/mobile/src/features/files/source-file-document.test.ts
new
+20
@@ -0,0 +1,20 @@
1
+import { expect, it } from "vite-plus/test";
2
+import { prepareSourceFileDocument, selectableSourceHtml } from "./source-file-document";
3
+
4
+it("preserves source whitespace and markup as literal selectable text", () => {
5
+ const source = prepareSourceFileDocument(
6
+ "\n\tlead\r\n<script>alert(1)</script> & > 😀\u00a0end\n",
7
+ );
8
+ const html = selectableSourceHtml(source.contents, {
9
+ foreground: "#000000",
10
+ selection: "#3366ff",
11
+ fontSize: 14,
12
+ rowHeight: 24,
13
+ wordBreak: true,
14
+ });
15
+ const page = new DOMParser().parseFromString(html, "text/html");
16
+
17
+ expect(page.querySelector("script")).toBeNull();
18
+ expect(page.body.textContent).toBe("\n\tlead\n<script>alert(1)</script> & > 😀\u00a0end\n");
19
+ expect(page.getElementById("L2")?.textContent).toBe("\tlead");
20
+});
apps/mobile/src/features/files/source-file-document.ts
+25
@@ -66,3 +66,28 @@ export function boundedSelectableSourceTokens(
66
}
67
return tokens;
68
}
69
+
70
+export interface SelectableSourceHtmlStyle {
71
+ readonly foreground: string;
72
+ readonly selection: string;
73
+ readonly fontSize: number;
74
+ readonly rowHeight: number;
75
+ readonly wordBreak: boolean;
76
+}
77
+
78
+// A <pre> would drop a leading newline, so whitespace is preserved on a plain <div>.
79
+export function selectableSourceHtml(contents: string, style: SelectableSourceHtmlStyle): string {
80
+ const text = contents
81
+ .split("\n")
82
+ .map(
83
+ (line, index) =>
84
+ `<span id="L${index + 1}">${line.replace(/[&<>]/g, (char) =>
85
+ char === "&" ? "&" : char === "<" ? "<" : ">",
86
+ )}</span>`,
87
+ )
88
+ .join("\n");
89
+ const wrap = style.wordBreak
90
+ ? "white-space:pre-wrap;overflow-wrap:anywhere"
91
+ : "white-space:pre;width:max-content";
92
+ return `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta http-equiv="Content-Security-Policy" content="default-src 'none';style-src 'unsafe-inline'"><style>html,body{margin:0;background:transparent}div{box-sizing:border-box;padding:8px 12px ${style.rowHeight}px;color:${style.foreground};font-family:monospace;font-size:${style.fontSize}px;line-height:${style.rowHeight / style.fontSize};${wrap}}::selection{background:color-mix(in srgb,${style.selection} 32%,transparent)}</style></head><body><div>${text}</div></body></html>`;
93
+}
apps/mobile/vite.config.ts
+1
@@ -10,6 +10,7 @@ const reactTests = [
10
"src/state/use-thread-pr.demand.test.tsx",
11
"src/features/threads/git/GitBranchesSheet.occupancy.test.tsx",
12
"src/features/files/useFileTreeEntries.test.tsx",
13
+ "src/features/files/source-file-document.test.ts",
14
"src/features/projects/AddProjectScreen.test.ts",
15
"src/lib/useMediaVideoPlayback.test.tsx",
16
"src/features/pull-requests/usePullRequestEnvironmentFallback.test.tsx",