@@ -409,10 +409,20 @@ export default class SnippetItemMultiFiles extends React.Component {
409
409
this . setState ( { editingFiles : newEditingFiles } )
410
410
}
411
411
// prevent reading deleted snippet
412
- if ( selectedFile > editingFiles . length - 1 ) {
413
- this . handleChangeFileClick ( editingFiles . length - 1 , ( ) => {
414
- this . resetSnippetHeight ( )
415
- } )
412
+ if ( fileIndex !== selectedFile ) {
413
+ // shift the selected file by 1 to replace to deleted file
414
+ if ( fileIndex < selectedFile ) {
415
+ // by shifting 1 index, the content will changed but we want to use the
416
+ // old selected file content
417
+ this . handleChangeFileClick ( selectedFile - 1 , selectedFile )
418
+ }
419
+ } else {
420
+ // the selected file is deleted
421
+ if ( fileIndex === 0 ) {
422
+ this . handleChangeFileClick ( 0 , fileIndex + 1 )
423
+ } else {
424
+ this . handleChangeFileClick ( fileIndex - 1 )
425
+ }
416
426
}
417
427
} else {
418
428
toast . error ( i18n . __ ( 'The snippet must have at least 1 file' ) )
@@ -446,34 +456,37 @@ export default class SnippetItemMultiFiles extends React.Component {
446
456
this . applyEditorStyle ( )
447
457
}
448
458
449
- handleChangeFileClick ( index , callback ) {
459
+ handleChangeFileClick ( index , useFileAtIndex , callback ) {
450
460
const { snippet } = this . props
451
461
const { editingFiles, isEditing } = this . state
452
462
// set the new selected file index
453
463
this . setState ( { selectedFile : index } , ( ) => {
454
464
// if the snippet is in the editing mode, interact with the state instead
455
465
// of the snippet in prop
456
- const file = isEditing ? editingFiles [ index ] : snippet . files [ index ]
457
- const fileExtension = getExtension ( file . name )
458
- const resultMode = CodeMirror . findModeByExtension ( fileExtension )
459
- // if the mode for that language exists then use it otherwise use text
460
- if ( resultMode ) {
461
- const snippetMode = resultMode . mode
462
- if ( snippetMode === 'htmlmixed' ) {
463
- require ( `codemirror/mode/xml/xml` )
464
- this . editor . setOption ( 'mode' , 'xml' )
465
- this . editor . setOption ( 'htmlMode' , true )
466
+ const fileIndex = useFileAtIndex || index
467
+ const file = isEditing ? editingFiles [ fileIndex ] : snippet . files [ index ]
468
+ if ( file ) {
469
+ const fileExtension = getExtension ( file . name )
470
+ const resultMode = CodeMirror . findModeByExtension ( fileExtension )
471
+ // if the mode for that language exists then use it otherwise use text
472
+ if ( resultMode ) {
473
+ const snippetMode = resultMode . mode
474
+ if ( snippetMode === 'htmlmixed' ) {
475
+ require ( `codemirror/mode/xml/xml` )
476
+ this . editor . setOption ( 'mode' , 'xml' )
477
+ this . editor . setOption ( 'htmlMode' , true )
478
+ } else {
479
+ require ( `codemirror/mode/${ snippetMode } /${ snippetMode } ` )
480
+ this . editor . setOption ( 'mode' , snippetMode )
481
+ }
466
482
} else {
467
- require ( `codemirror/mode/${ snippetMode } /${ snippetMode } ` )
468
- this . editor . setOption ( 'mode' , snippetMode )
483
+ this . editor . setOption ( 'mode' , 'null' )
484
+ }
485
+ this . editor . setValue ( file . value )
486
+ this . resetSnippetHeight ( )
487
+ if ( callback && typeof callback === 'function' ) {
488
+ callback ( )
469
489
}
470
- } else {
471
- this . editor . setOption ( 'mode' , 'null' )
472
- }
473
- this . editor . setValue ( file . value )
474
- this . resetSnippetHeight ( )
475
- if ( callback && typeof callback === 'function' ) {
476
- callback ( )
477
490
}
478
491
} )
479
492
}
0 commit comments