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" ;
1212import { Platform , View } from "react-native" ;
1313import Svg , { Defs , LinearGradient , Rect , Stop } from "react-native-svg" ;
1414import { 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+
205236function 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