@@ -57,10 +57,12 @@ import { MediaActions, type MediaActionSource } from "~/components/media/MediaAc
5757import { useRemoteOpenState } from "~/remoteOpen" ;
5858import { useClientSettings } from "~/hooks/useSettings" ;
5959import { useTheme } from "~/hooks/useTheme" ;
60+ import { useStableCallback } from "~/hooks/useStableCallback" ;
6061import { getLocalStorageItem , setLocalStorageItem , useLocalStorage } from "~/hooks/useLocalStorage" ;
6162import { resolveDiffThemeName } from "~/lib/diffRendering" ;
6263import { PREFERRED_HIGHLIGHTER } from "~/lib/syntaxHighlighting" ;
6364import { cn } from "~/lib/utils" ;
65+ import { requestWorkspaceFocus } from "~/lib/workspaceFocus" ;
6466import type { ChatFileAttachment } from "~/types" ;
6567import { isHostPath } from "@t3tools/shared/path" ;
6668import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks" ;
@@ -598,6 +600,7 @@ const createFileEditor: EditorFactory<FileCommentAnnotationGroup, undefined> = (
598600 options ,
599601 editStateKey ,
600602) => new Editor ( editorType , options , editStateKey ) ;
603+ type FileEditor = Editor < "file" , FileCommentAnnotationGroup , undefined > ;
601604
602605function editableFileContents (
603606 environmentId : EnvironmentId ,
@@ -661,6 +664,7 @@ interface EditableFileSurfaceProps {
661664 wordWrap : boolean ;
662665 onPostRender : FilePostRender ;
663666 onPendingChange : ( relativePath : string , pending : boolean ) => void ;
667+ onEditorChange : ( editor : FileEditor | null ) => void ;
664668}
665669
666670interface FileSelectionOverride {
@@ -680,6 +684,7 @@ function EditableFileSurface({
680684 wordWrap,
681685 onPostRender,
682686 onPendingChange,
687+ onEditorChange,
683688} : EditableFileSurfaceProps ) {
684689 const addReviewComment = useComposerDraftStore ( ( store ) => store . addReviewComment ) ;
685690 const removeReviewComment = useComposerDraftStore ( ( store ) => store . removeReviewComment ) ;
@@ -705,13 +710,15 @@ function EditableFileSurface({
705710 }
706711 const { ready : editable , onPostRender : onEditablePostRender } =
707712 useEditableAfterHighlight ( externalFile ) ;
708- const editorRef = useRef < Editor < "file" , FileCommentAnnotationGroup , undefined > | null > ( null ) ;
713+ const editorRef = useRef < FileEditor | null > ( null ) ;
709714 const editorOptions : EditorOptions < "file" , FileCommentAnnotationGroup , undefined > = {
710715 onAttach : ( editor ) => {
711716 editorRef . current = editor ;
717+ onEditorChange ( editor ) ;
712718 } ,
713719 onComplete : ( ) => {
714720 editorRef . current = null ;
721+ onEditorChange ( null ) ;
715722 } ,
716723 } ;
717724
@@ -943,6 +950,7 @@ function RenderedMarkdownSurface({
943950 | "revealRequestId"
944951 | "wordWrap"
945952 | "onPostRender"
953+ | "onEditorChange"
946954> & {
947955 threadRef : ScopedThreadRef ;
948956 readOnly : boolean ;
@@ -1142,6 +1150,55 @@ export default function FilePreviewPanel({
11421150 ! ( tableDelimiter && renderTable ) &&
11431151 ! renderBrowserFile &&
11441152 ! isDocument ;
1153+ const mayOpenEditor =
1154+ previewPath !== null &&
1155+ ! isHostFile &&
1156+ canWriteFiles &&
1157+ file . error === null &&
1158+ ! file . data ?. truncated &&
1159+ ! isMedia &&
1160+ ! isDocument &&
1161+ ! renderBrowserFile &&
1162+ ! renderMermaid &&
1163+ ! ( isMarkdown && renderMarkdown ) &&
1164+ ! ( tableDelimiter && renderTable ) ;
1165+ const contentFocusRef = useRef < HTMLDivElement > ( null ) ;
1166+ const fileEditorRef = useRef < FileEditor | null > ( null ) ;
1167+ const focusEditorFromContent = ( ) => {
1168+ const editor = fileEditorRef . current ;
1169+ if ( ! editor || document . activeElement !== contentFocusRef . current ) return ;
1170+ // Without a caret the editor focuses with no selection, so no caret shows and typing
1171+ // lands on whatever the browser selection held.
1172+ const hasCaret = Boolean ( editor . getViewState ( ) . selections ?. length ) ;
1173+ editor . focus (
1174+ hasCaret
1175+ ? { preventScroll : true }
1176+ : { preventScroll : true , lineNumber : revealLine ?? "first-visible" } ,
1177+ ) ;
1178+ } ;
1179+ const handleEditorChange = ( editor : FileEditor | null ) => {
1180+ fileEditorRef . current = editor ;
1181+ focusEditorFromContent ( ) ;
1182+ } ;
1183+ const handleEditorChangeStable = useStableCallback ( handleEditorChange ) ;
1184+ const focusEditorOnTreeRowClickOrEnter = ( event : {
1185+ nativeEvent : Event ;
1186+ altKey : boolean ;
1187+ ctrlKey : boolean ;
1188+ metaKey : boolean ;
1189+ shiftKey : boolean ;
1190+ } ) => {
1191+ if ( event . altKey || event . ctrlKey || event . metaKey || event . shiftKey ) return ;
1192+ const row = event . nativeEvent
1193+ . composedPath ( )
1194+ . find (
1195+ ( node ) : node is Element =>
1196+ node instanceof Element && node . matches ( '[role="treeitem"][data-item-path]' ) ,
1197+ ) ;
1198+ const path = row ?. getAttribute ( "data-item-path" ) ;
1199+ if ( ! path || path . endsWith ( "/" ) ) return ;
1200+ requestWorkspaceFocus ( { area : "panel" , selector : "[data-file-content-focus]" } ) ;
1201+ } ;
11451202 const rendered = isMermaid
11461203 ? renderMermaid
11471204 : isMarkdown
@@ -1364,6 +1421,20 @@ export default function FilePreviewPanel({
13641421 < div
13651422 className = { cn ( "min-w-0 flex-1 flex-col overflow-hidden" , previewPath ? "flex" : "hidden" ) }
13661423 >
1424+ { mayOpenEditor ? (
1425+ // Panel focus lands here and passes to the editor once it attaches. sr-only keeps the
1426+ // 1px box workspace focus needs, and clips it so clicks in the file never reach it.
1427+ // The hand-off waits a microtask: workspace focus checks that focus stayed here.
1428+ < div
1429+ ref = { contentFocusRef }
1430+ tabIndex = { - 1 }
1431+ data-workspace-focus-default
1432+ data-file-content-focus
1433+ aria-label = "File contents"
1434+ className = "sr-only"
1435+ onFocus = { ( ) => queueMicrotask ( focusEditorFromContent ) }
1436+ />
1437+ ) : null }
13671438 { isFolder ? null : relativePath && attachment ? (
13681439 < AttachmentFilePreview
13691440 key = { `${ environmentId } :${ attachment . id } ` }
@@ -1539,36 +1610,45 @@ export default function FilePreviewPanel({
15391610 wordWrap = { wordWrap }
15401611 onPostRender = { onFilePostRender }
15411612 onPendingChange = { onPendingChange }
1613+ onEditorChange = { handleEditorChangeStable }
15421614 />
15431615 </ DiffWorkerPoolProvider >
15441616 )
15451617 ) : null }
15461618 </ div >
15471619 { showExplorer ? (
15481620 < FileExplorerPane resizable = { Boolean ( previewPath ) } >
1549- < FileBrowserPanel
1550- key = { `${ environmentId } :${ hostFolderPath ?? cwd } ` }
1551- environmentId = { environmentId }
1552- cwd = { hostFolderPath ?? cwd }
1553- projectName = { hostFolderPath ?? projectName }
1554- hostFolder = { hostFolderPath !== null }
1555- selectedPath = {
1556- hostFolderPath === null
1557- ? relativePath
1558- : trailChildOf ( hostFolderPath , breadcrumbTrail ?? hostFolderPath )
1559- }
1560- selectedPathRevealId = { revealRequestId }
1561- onOpenFile = { onOpenFile }
1562- workspaceMutationId = { workspaceMutationId }
1563- { ...( previewPath && ! isMedia && ! isPdf
1564- ? {
1565- onRefreshSelectedFile : ( ) => {
1566- if ( ! isDocument ) file . refresh ( ) ;
1567- setFileRefreshRevision ( ( value ) => value + 1 ) ;
1568- } ,
1569- }
1570- : { } ) }
1571- />
1621+ < div
1622+ className = "contents"
1623+ onClick = { focusEditorOnTreeRowClickOrEnter }
1624+ onKeyDown = { ( event ) => {
1625+ if ( event . key === "Enter" ) focusEditorOnTreeRowClickOrEnter ( event ) ;
1626+ } }
1627+ >
1628+ < FileBrowserPanel
1629+ key = { `${ environmentId } :${ hostFolderPath ?? cwd } ` }
1630+ environmentId = { environmentId }
1631+ cwd = { hostFolderPath ?? cwd }
1632+ projectName = { hostFolderPath ?? projectName }
1633+ hostFolder = { hostFolderPath !== null }
1634+ selectedPath = {
1635+ hostFolderPath === null
1636+ ? relativePath
1637+ : trailChildOf ( hostFolderPath , breadcrumbTrail ?? hostFolderPath )
1638+ }
1639+ selectedPathRevealId = { revealRequestId }
1640+ onOpenFile = { onOpenFile }
1641+ workspaceMutationId = { workspaceMutationId }
1642+ { ...( previewPath && ! isMedia && ! isPdf
1643+ ? {
1644+ onRefreshSelectedFile : ( ) => {
1645+ if ( ! isDocument ) file . refresh ( ) ;
1646+ setFileRefreshRevision ( ( value ) => value + 1 ) ;
1647+ } ,
1648+ }
1649+ : { } ) }
1650+ />
1651+ </ div >
15721652 </ FileExplorerPane >
15731653 ) : null }
15741654 </ div >
0 commit comments