Skip to content

Commit 373bb37

Browse files
shivamhwpclaude
andcommitted
refactor(mobile): keep file text selection state with the source view
Selection state now lives in a wrapper rendered only for source text, so switching file, line or Preview/Source resets it by unmounting instead of a scoped key and manual clears. The native long-press handler no longer needs a conditional spread, and the WebView drops an unused testID and a default. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 20c0018 commit 373bb37

2 files changed

Lines changed: 41 additions & 60 deletions

File tree

‎apps/mobile/src/features/files/SourceFileSurface.tsx‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,11 @@ function NativeSourceFileSurface(
247247
styleJson={styleJson}
248248
themeJson={themeJson}
249249
tokensJson={tokensJson}
250-
{...(onLongPressLine
251-
? {
252-
onPressLine: ({ nativeEvent }) => {
253-
if (
254-
nativeEvent.gesture === "longPress" &&
255-
nativeEvent.newLineNumber !== undefined
256-
) {
257-
onLongPressLine(nativeEvent.newLineNumber);
258-
}
259-
},
260-
}
261-
: {})}
250+
onPressLine={({ nativeEvent }) => {
251+
if (nativeEvent.gesture === "longPress" && nativeEvent.newLineNumber !== undefined) {
252+
onLongPressLine?.(nativeEvent.newLineNumber);
253+
}
254+
}}
262255
{...(onRefresh
263256
? {
264257
refreshing: isPullRefreshing,
@@ -367,7 +360,6 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
367360

368361
const webSelection = usesWebSelection ? (
369362
<WebView
370-
testID="source-selectable-web-view"
371363
source={{
372364
html: selectableSourceHtml(normalizedContents, {
373365
foreground,
@@ -378,7 +370,6 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
378370
}),
379371
}}
380372
originWhitelist={["*"]}
381-
javaScriptEnabled
382373
// Android pastes copied HTML as styled text with collapsed whitespace, so copy plain text.
383374
// Wrapped text reflows once the WebView reaches its final width, so scroll again then.
384375
injectedJavaScript={`addEventListener("copy",(event)=>{event.clipboardData.setData("text/plain",getSelection().toString());event.preventDefault();});${

‎apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx‎

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
usePreventRemove,
99
type StaticScreenProps,
1010
} from "@react-navigation/native";
11-
import { type ReactNode, useId, useState } from "react";
11+
import { type ComponentProps, type ReactNode, useId, useState } from "react";
1212
import { Platform, View } from "react-native";
1313
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
1414
import { EnvironmentId, type ProjectReadFileResult, ThreadId } from "@t3tools/contracts";
@@ -202,6 +202,37 @@ function defaultViewMode(path: string | null): FileViewMode {
202202
: "source";
203203
}
204204

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 from: number | null | undefined;
211+
readonly line: number;
212+
readonly active: boolean;
213+
} | null>(null);
214+
const current = selection?.from === props.initialLine ? selection : null;
215+
usePreventRemove(current?.active === true, ({ data }) => {
216+
setSelection((value) => value && { ...value, active: false });
217+
// The re-dispatched action skips this guard, so "Return to chat" still leaves in one tap.
218+
if (data.action.type !== "GO_BACK" && data.action.type !== "POP") {
219+
navigation.dispatch(data.action);
220+
}
221+
});
222+
return (
223+
<SourceFileSurface
224+
{...props}
225+
initialLine={current?.line ?? props.initialLine}
226+
selectable={current?.active === true}
227+
onLongPressLine={
228+
Platform.OS === "android"
229+
? (line) => setSelection({ from: props.initialLine, line, active: true })
230+
: undefined
231+
}
232+
/>
233+
);
234+
}
235+
205236
function FileContent(props: {
206237
readonly activeMode: FileViewMode;
207238
readonly cwd: string;
@@ -218,8 +249,6 @@ function FileContent(props: {
218249
readonly threadId: ThreadId | null;
219250
readonly initialLine: number | null;
220251
readonly truncated: boolean;
221-
readonly selectable: boolean;
222-
readonly onLongPressLine?: (line: number) => void;
223252
readonly onRefresh?: () => Promise<void> | void;
224253
}) {
225254
// Reopening a mutable host file must not reuse a poster from an earlier visit.
@@ -315,13 +344,11 @@ function FileContent(props: {
315344
onRefresh={props.onRefresh}
316345
/>
317346
) : (
318-
<SourceFileSurface
347+
<FileSourceSurface
319348
contents={props.fileContents}
320349
path={props.relativePath}
321350
initialLine={props.initialLine}
322351
headerInsetTop={props.truncated ? 0 : undefined}
323-
selectable={props.selectable}
324-
onLongPressLine={props.onLongPressLine}
325352
onRefresh={props.onRefresh}
326353
/>
327354
)}
@@ -720,31 +747,6 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
720747
: null,
721748
);
722749
const fileData = fileQuery.data as ProjectReadFileResult | null;
723-
// Android numbered rows are separate text views, so a long-press opens the whole file as
724-
// one selectable text at that line. Back returns to the numbered rows at the same line.
725-
const selectionScope = JSON.stringify([
726-
environmentId,
727-
cwd,
728-
relativePath,
729-
targetLine,
730-
resolvedActiveMode,
731-
]);
732-
const [textSelection, setTextSelection] = useState<{
733-
readonly scope: string;
734-
readonly line: number;
735-
readonly active: boolean;
736-
} | null>(null);
737-
const scopedTextSelection = textSelection?.scope === selectionScope ? textSelection : null;
738-
const canSelectText =
739-
Platform.OS === "android" && resolvedActiveMode === "source" && fileData?.contents != null;
740-
const selectingText = canSelectText && scopedTextSelection?.active === true;
741-
usePreventRemove(selectingText, ({ data }) => {
742-
setTextSelection((current) => current && { ...current, active: false });
743-
// The re-dispatched action skips this guard, so "Return to chat" still leaves in one tap.
744-
if (data.action.type !== "GO_BACK" && data.action.type !== "POP") {
745-
navigation.dispatch(data.action);
746-
}
747-
});
748750

749751
const handleSelectFile = (path: string) => {
750752
const targetPath =
@@ -812,10 +814,7 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
812814
title: filePreviewDelimiter({ name: relativePath }) ? "Table" : "Preview",
813815
icon: filePreviewDelimiter({ name: relativePath }) ? "tablecells" : "eye",
814816
inline: true,
815-
onPress: () => {
816-
setTextSelection(null);
817-
setModeOverride({ path: relativePath, mode: "preview" });
818-
},
817+
onPress: () => setModeOverride({ path: relativePath, mode: "preview" }),
819818
} as const)
820819
: null,
821820
canToggleMode
@@ -824,10 +823,7 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
824823
title: "Source",
825824
icon: "doc.text",
826825
inline: true,
827-
onPress: () => {
828-
setTextSelection(null);
829-
setModeOverride({ path: relativePath, mode: "source" });
830-
},
826+
onPress: () => setModeOverride({ path: relativePath, mode: "source" }),
831827
} as const)
832828
: null,
833829
resolvedActiveMode === "source"
@@ -1044,16 +1040,10 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
10441040
resolveVideoUri={assetPreview.refresh}
10451041
fileContents={fileData?.contents ?? null}
10461042
fileError={fileQuery.error}
1047-
initialLine={scopedTextSelection?.line ?? targetLine}
1043+
initialLine={targetLine}
10481044
relativePath={relativePath}
10491045
threadId={threadId}
10501046
truncated={fileData?.truncated ?? false}
1051-
selectable={selectingText}
1052-
onLongPressLine={
1053-
canSelectText
1054-
? (line) => setTextSelection({ scope: selectionScope, line, active: true })
1055-
: undefined
1056-
}
10571047
onRefresh={() => fileQuery.refresh()}
10581048
/>
10591049
</ScreenContent>

0 commit comments

Comments
 (0)