@@ -10,6 +10,16 @@ import { EmptyState } from './empty-state'
1010import { FolderView } from './folder'
1111import { UnsupportedFile } from './unsupported-file'
1212
13+ // 常量:扩展名到类型的映射(避免每次渲染时重新创建)
14+ const MARKDOWN_EXTENSIONS = new Set ( [
15+ 'md' , 'txt' , 'markdown' , 'py' , 'js' , 'ts' , 'jsx' , 'tsx' , 'css' , 'scss' , 'less' ,
16+ 'html' , 'xml' , 'json' , 'yaml' , 'yml' , 'sh' , 'bash' , 'java' , 'c' , 'cpp' , 'h' , 'go' ,
17+ 'rs' , 'sql' , 'rb' , 'php' , 'vue' , 'svelte' , 'astro' , 'toml' , 'ini' , 'conf' , 'cfg' ,
18+ 'gitignore' , 'env' , 'example' , 'template'
19+ ] )
20+
21+ const IMAGE_EXTENSIONS = new Set ( [ 'jpg' , 'jpeg' , 'png' , 'gif' , 'bmp' , 'webp' , 'svg' ] )
22+
1323export function EditorLayout ( ) {
1424 const {
1525 activeFilePath,
@@ -68,10 +78,10 @@ export function EditorLayout() {
6878 const extension = path . split ( '.' ) . pop ( ) ?. toLowerCase ( )
6979 if ( ! extension ) return 'unknown'
7080
71- if ( [ 'md' , 'txt' , 'markdown' , 'py' , 'js' , 'ts' , 'jsx' , 'tsx' , 'css' , 'scss' , 'less' , 'html' , 'xml' , 'json' , 'yaml' , 'yml' , 'sh' , 'bash' , 'java' , 'c' , 'cpp' , 'h' , 'go' , 'rs' , 'sql' , 'rb' , 'php' , 'vue' , 'svelte' , 'astro' , 'toml' , 'ini' , 'conf' , 'cfg' , 'gitignore' , 'env' , 'example' , 'template' ] . includes ( extension ) ) {
81+ if ( MARKDOWN_EXTENSIONS . has ( extension ) ) {
7282 return 'markdown'
7383 }
74- if ( [ 'jpg' , 'jpeg' , 'png' , 'gif' , 'bmp' , 'webp' , 'svg' ] . includes ( extension ) ) {
84+ if ( IMAGE_EXTENSIONS . has ( extension ) ) {
7585 return 'image'
7686 }
7787 return 'unknown'
@@ -237,20 +247,28 @@ export function EditorLayout() {
237247 emitter . emit ( 'editor-file-close' , { path : closedPath } )
238248 delete tabContentsRef . current [ closedPath ]
239249
240- // Bug fix: Get closedTab from the current ref value (updated synchronously in useEffect)
250+ // Get closedTab from the current ref value
241251 const closedTab = tabsRef . current . find ( t => t . path === closedPath )
242- if ( closedTab ) {
243- removeTab ( closedTab . id )
244- }
245-
246- // Bug fix: Only switch active tab if we're closing the currently active tab
247- // Use localActiveTabId which is kept in sync via useEffect
248- if ( closedTab && localActiveTabId === closedTab . id ) {
249- if ( tabsRef . current . length > 1 ) {
250- const currentIndex = tabsRef . current . findIndex ( t => t . id === closedTab . id )
251- const targetTab = tabsRef . current [ Math . max ( 0 , currentIndex - 1 ) ] || tabsRef . current [ tabsRef . current . length - 1 ]
252- setActiveTabId ( targetTab . id )
253- setActiveFilePath ( targetTab . path )
252+ if ( ! closedTab ) return
253+
254+ // Save the current tabs count before removing
255+ const tabsCountBeforeRemove = tabsRef . current . length
256+
257+ // Remove the tab
258+ removeTab ( closedTab . id )
259+
260+ // Only switch active tab if we're closing the currently active tab
261+ if ( localActiveTabId === closedTab . id ) {
262+ if ( tabsCountBeforeRemove > 1 ) {
263+ // Find the new target tab from the updated tabsRef after removal
264+ const remainingTabs = tabsRef . current . filter ( t => t . id !== closedTab . id )
265+ if ( remainingTabs . length > 0 ) {
266+ // Try to select the tab to the left, otherwise select the last one
267+ const currentIndex = tabsRef . current . findIndex ( t => t . id === closedTab . id )
268+ const targetTab = remainingTabs [ Math . max ( 0 , currentIndex - 1 ) ] || remainingTabs [ remainingTabs . length - 1 ]
269+ setActiveTabId ( targetTab . id )
270+ setActiveFilePath ( targetTab . path )
271+ }
254272 } else {
255273 setActiveTabId ( '' )
256274 setActiveFilePath ( '' )
@@ -381,8 +399,8 @@ export function EditorLayout() {
381399 onCloseRightTabs = { handleCloseRightTabs }
382400 />
383401
384- { /* Content panels - all rendered, only active one visible */ }
385- { tabs . map ( tab => renderContentPanel ( tab , tab . id === localActiveTabId ) ) }
402+ { /* Only render active tab content - improves performance with many tabs */ }
403+ { tabs . filter ( tab => tab . id === localActiveTabId ) . map ( tab => renderContentPanel ( tab , true ) ) }
386404 </ div >
387405 )
388406}
0 commit comments