Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit f88f138

Browse files
committed
fixed create multi file snippet delete file bug
1 parent f4be8d2 commit f88f138

File tree

1 file changed

+24
-6
lines changed
  • browser/render/modals/create-multi-files-snippet

1 file changed

+24
-6
lines changed

browser/render/modals/create-multi-files-snippet/index.jsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,35 @@ export default class CreateMultiFilesSnippetModal extends React.Component {
169169
newEditingFiles.splice(fileIndex, 1)
170170
this.setState({ files: newEditingFiles })
171171
// prevent reading deleted snippet
172-
this.handleChangeFileClick(selectedFile - 1)
172+
if (fileIndex !== selectedFile) {
173+
// shift the selected file by 1 to replace to deleted file
174+
if (fileIndex < selectedFile) {
175+
// by shifting 1 index, the content will changed but we want to use the
176+
// old selected file content
177+
this.handleChangeFileClick(selectedFile - 1, selectedFile)
178+
}
179+
} else {
180+
// the selected file is deleted
181+
if (fileIndex === 0) {
182+
this.handleChangeFileClick(0, fileIndex + 1)
183+
} else {
184+
this.handleChangeFileClick(fileIndex - 1)
185+
}
186+
}
173187
}
174188

175-
handleChangeFileClick (index) {
189+
handleChangeFileClick (index, useFileAtIndex) {
176190
const { files } = this.state
177191
// set the new selected file index
178192
this.setState({ selectedFile: index }, () => {
179193
// if the snippet is in the editing mode, interact with the state instead
180194
// of the snippet in prop
181-
if (index !== -1) {
182-
const file = files[index]
195+
// Sometime, we want the current index file to change but use the content
196+
// of another file (look at the function above)
197+
const fileIndex = useFileAtIndex || index
198+
const file = files[fileIndex]
199+
// first time loaded there is no file
200+
if (file) {
183201
// if the mode for that language exists then use it otherwise use text
184202
this.applyEditorLanguage(file.name)
185203
this.editor.setValue(file.value)
@@ -229,8 +247,8 @@ export default class CreateMultiFilesSnippetModal extends React.Component {
229247

230248
handleEditingFileValueChange () {
231249
const { selectedFile, files } = this.state
232-
if (files.length > 0) {
233-
const newEditingFiles = _.clone(files)
250+
const newEditingFiles = _.clone(files)
251+
if (files.length > 0 && newEditingFiles[selectedFile]) {
234252
newEditingFiles[selectedFile].value = this.editor.getValue()
235253
this.setState({ files: newEditingFiles })
236254
}

0 commit comments

Comments
 (0)