@@ -596,10 +596,38 @@ const openFile = async (
596596 } ) ;
597597 editorLanguages ! [ filename ] = language ;
598598 editors [ filename ] = editor ;
599- editorIds . push ( filename ) ;
599+ if ( ! editorIds . includes ( filename ) ) {
600+ editorIds . push ( filename ) ;
601+ }
600602 handleChangeContent ( editor ) ;
601603} ;
602604
605+ const closeFile = ( filename : string ) => {
606+ if ( editorLanguages && editorLanguages [ filename ] ) {
607+ delete editorLanguages [ filename ] ;
608+ }
609+ if ( editors [ filename ] ) {
610+ editors [ filename ] . destroy ( ) ;
611+ delete editors [ filename ] ;
612+ }
613+ const id = editorIds . findIndex ( ( editorId ) => editorId === filename ) ;
614+ if ( id > - 1 ) {
615+ if ( editorIds . length > 1 ) {
616+ const editorIdToShow = id === 0 ? 1 : id - 1 ;
617+ showEditor ( editorIds [ editorIdToShow ] ) ;
618+ }
619+ editorIds . splice ( id , 1 ) ;
620+ }
621+ UI . getEditorDivs ( ) . forEach ( ( editorDiv ) => {
622+ if ( editorDiv . dataset . editorId === filename ) {
623+ editorDiv . remove ( ) ;
624+ }
625+ } ) ;
626+ UI . getEditorTab ( filename ) ?. remove ( ) ;
627+ setSavedStatus ( ) ;
628+ dispatchChangeEvent ( ) ;
629+ } ;
630+
603631const renameFile = ( filename : string , newName : string ) => {
604632 const config = getConfig ( ) ;
605633 const validName = checkFileName ( newName , config , filename ) ;
@@ -660,30 +688,10 @@ const deleteFile = (filename: string) => {
660688 ...config ,
661689 files : config . files . filter ( ( f ) => f . filename !== filename ) ,
662690 } ) ;
663- if ( editorLanguages && editorLanguages [ filename ] ) {
664- delete editorLanguages [ filename ] ;
665- }
666- if ( editors [ filename ] ) {
667- editors [ filename ] . destroy ( ) ;
668- delete editors [ filename ] ;
669- }
670- const id = editorIds . findIndex ( ( editorId ) => editorId === filename ) ;
671- if ( id > - 1 ) {
672- const editorIdToShow = id === 0 ? 1 : id - 1 ;
673- showEditor ( editorIds [ editorIdToShow ] ) ;
674- editorIds . splice ( id , 1 ) ;
675- }
676- UI . getEditorDivs ( ) . forEach ( ( editorDiv ) => {
677- if ( editorDiv . dataset . editorId === filename ) {
678- editorDiv . remove ( ) ;
679- }
680- } ) ;
681- UI . getEditorTab ( filename ) ?. remove ( ) ;
691+ closeFile ( filename ) ;
682692 if ( config . autoupdate ) {
683693 run ( ) ;
684694 }
685- setSavedStatus ( ) ;
686- dispatchChangeEvent ( ) ;
687695 if ( sidebar ?. files ) {
688696 const newConfig = getConfig ( ) ;
689697 sidebar ?. files ?. update ( {
@@ -709,10 +717,12 @@ const createEditorUI = (title: string, isHidden = false) => {
709717 showEditor,
710718 renameFile,
711719 deleteFile,
720+ closeFile,
712721 isMainFile : title === getMainFile ( config ) ,
713722 isNewFile : false ,
714723 isHidden,
715724 isLocked : config . lockFiles || config . readonly ,
725+ isFilesSectionEnabled : sidebar ?. isEnabled ( 'files' ) ,
716726 } ) ;
717727 return container ;
718728} ;
@@ -840,9 +850,11 @@ const createEditors = async (config: Config) => {
840850 addFile : async ( filename : string ) => addFile ( filename ) ,
841851 renameFile,
842852 deleteFile,
853+ closeFile,
843854 isMainFile : false ,
844855 isNewFile : true ,
845856 isLocked : false ,
857+ isFilesSectionEnabled : sidebar ?. isEnabled ( 'files' ) ,
846858 } ) ,
847859 } ) ;
848860 }
@@ -1048,9 +1060,8 @@ const showEditor = (
10481060 { isUpdate = false , source } : { isUpdate ?: boolean ; source ?: 'sidebar-files' } = { } ,
10491061) => {
10501062 const config = getConfig ( ) ;
1051-
10521063 if ( getSource ( editorId , config ) ?. hidden ) return ;
1053- if ( ! editors [ editorId ] ) {
1064+ if ( ! editorIds . includes ( editorId ) ) {
10541065 if ( config . files . find ( ( f ) => f . filename === editorId ) ) {
10551066 openFile ( editorId , getEditorOptions ( editorId ) ) . then ( ( ) => showEditor ( editorId ) ) ;
10561067 }
0 commit comments