8000 Asking for confirmation to remove and overwrite files · arduino/lab-micropython-editor@8894beb · GitHub
[go: up one dir, main page]

Skip to content

Commit 8894beb

Browse files
committed
Asking for confirmation to remove and overwrite files
1 parent 301b04e commit 8894beb

File tree

1 file changed

+81
-32
lines changed

1 file changed

+81
-32
lines changed

ui/arduino/store.js

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function store(state, emitter) {
6565
state.serialPath = null
6666
state.isTerminalOpen = false
6767
state.serialFiles = []
68-
emitter.emit('close-panel')
68+
emitter.emit('render')
6969
})
7070
emitter.on('connect', async (path) => {
7171
log('connect')
@@ -150,17 +150,20 @@ function store(state, emitter) {
150150
}
151151

152152
emitter.emit('update-files')
153+
emitter.emit('message', `${filename} is saved on ${state.selectedDevice}.`, 1000)
153154
})
154155
emitter.on('remove', async () => {
155156
log('remove')
156-
if (state.selectedDevice === 'serial') {
157-
await serial.removeFile(state.selectedFile)
158-
}
159-
if (state.selectedDevice === 'disk') {
160-
await disk.removeFile(state.diskPath, state.selectedFile)
157+
if (confirm(`Do you want to remove ${state.selectedFile} from ${state.selectedDevice}?`)) {
158+
if (state.selectedDevice === 'serial') {
159+
await serial.removeFile(state.selectedFile)
160+
}
161+
if (state.selectedDevice === 'disk') {
162+
await disk.removeFile(state.diskPath, state.selectedFile)
163+
}
164+
emitter.emit('update-files')
165+
emitter.emit('render')
161166
}
162-
emitter.emit('update-files')
163-
emitter.emit('render')
164167
})
165168
emitter.on('select-file', async (device, filename) => {
166169
log('select-file')
@@ -226,19 +229,37 @@ function store(state, emitter) {
226229
})
227230
emitter.on('upload', async () => {
228231
log('upload')
229-
emitter.emit('message', 'Uploading file... Please wait')
230-
await serial.uploadFile(state.diskPath, state.selectedFile)
231-
emitter.emit('message', 'File uploaded!', 500)
232-
emitter.emit('update-files')
233-
emitter.emit('render')
232+
let confirmation = true
233+
if (state.serialFiles.indexOf(state.selectedFile) !== -1) {
234+
confirmation = confirm(`Do you want to overwrite ${state.selectedFile} on board?`)
235+
}
236+
if (confirmation) {
237+
emitter.emit('message', 'Uploading file... Please wait')
238+
let editor = state.cache(AceEditor, 'editor').editor
239+
let contents = editor.getValue()
240+
await disk.saveFileContent(state.diskPath, state.selectedFile, contents)
241+
await serial.uploadFile(state.diskPath, state.selectedFile)
242+
emitter.emit('message', 'File uploaded!', 500)
243+
emitter.emit('update-files')
244+
emitter.emit('render')
245+
}
234246
})
235247
emitter.on('download', async () => {
236248
log('download')
237-
emitter.emit('message', 'Downloading file... Please wait')
238-
await serial.downloadFile(state.diskPath, state.selectedFile)
239-
emitter.emit('message', 'File downloaded!', 500)
240-
emitter.emit('update-files')
241-
emitter.emit('render')
249+
let confirmation = true
250+
if (state.diskFiles.indexOf(state.selectedFile) !== -1) {
251+
confirmation = confirm(`Do you want to overwrite ${state.selectedFile} on disk?`)
252+
}
253+
if (confirmation) {
254+
emitter.emit('message', 'Downloading file... Please wait')
255+
let editor = state.cache(AceEditor, 'editor').editor
256+
let contents = editor.getValue()
257+
await serial.saveFileContent(state.selectedFile, contents)
258+
await serial.downloadFile(state.diskPath, state.selectedFile)
259+
emitter.emit('message', 'File downloaded!', 500)
260+
emitter.emit('update-files')
261+
emitter.emit('render')
262+
}
242263
})
243264

244265
// PANEL MANAGEMENT
@@ -298,31 +319,59 @@ function store(state, emitter) {
298319
let contents = editor.getValue()
299320

300321
if (state.selectedDevice === 'serial') {
301-
if (state.serialFiles.indexOf(oldFilename) !== -1) {
302-
// If old name exists, rename file
303-
await serial.renameFile(oldFilename, filename)
322+
// Ask for confirmation to overwrite existing file
323+
let confirmation = true
324+
if (state.serialFiles.indexOf(filename) !== -1) {
325+
confirmation = confirm(`Do you want to overwrite ${filename} on ${state.selectedDevice}?`)
326+
}
327+
328+
if (confirmation) {
329+
if (state.serialFiles.indexOf(oldFilename) !== -1) {
330+
// If old name exists, save old file and rename
331+
await serial.saveFileContent(oldFilename, contents)
332+
await serial.renameFile(oldFilename, filename)
333+
} else {
334+
// If old name doesn't exist create new file
335+
await serial.saveFileContent(filename, contents)
336+
}
337+
state.isEditingFilename = false
338+
emitter.emit('update-files')
339+
emitter.emit('render')
340+
341+
emitter.emit('message', `${filename} is saved on ${state.selectedDevice}.`, 1000)
304342
} else {
305-
// If old name doesn't exist create new file
306-
await serial.saveFileContent(filename, contents)
343+
state.isEditingFilename = false
344+
emitter.emit('render')
307345
}
308346
}
309347

310348
if (state.diskPath !== null && state.selectedDevice === 'disk') {
349+
// Ask for confirmation to overwrite existing file
350+
let confirmation = true
351+
if (state.diskFiles.indexOf(filename) !== -1) {
352+
confirmation = confirm(`Do you want to overwrite ${filename} on ${state.selectedDevice}?`)
353+
}
354+
if (confirmation) {
355+
if (state.diskFiles.indexOf(oldFilename) !== -1) {
356+
// If old name exists, save old file and rename
357+
await disk.saveFileContent(state.diskPath, oldFilename, contents)
358+
await disk.renameFile(state.diskPath, oldFilename, filename)
359+
} else {
360+
// If old name doesn't exist create new file
361+
await disk.saveFileContent(state.diskPath, filename, contents)
362+
}
363+
state.isEditingFilename = false
364+
emitter.emit('update-files')
365+
emitter.emit('render')
311366

312-
if (state.diskFiles.indexOf(oldFilename) !== -1) {
313-
// If old name exists, rename file
314-
await disk.renameFile(state.diskPath, oldFilename, filename)
367+
emitter.emit('message', `${filename} is saved on ${state.selectedDevice}.`, 1000)
315368
} else {
316-
// If old name doesn't exist create new file
317-
await disk.saveFileContent(state.diskPath, filename, contents)
369+
state.isEditingFilename = false
370+
emitter.emit('render')
318371
}
319372
}
320373

321-
state.isEditingFilename = false
322-
emitter.emit('update-files')
323-
emitter.emit('render')
324374

325-
emitter.emit('message', "Filename is saved.", 1000)
326375
})
327376

328377
emitter.on('message', (text, timeout) => {

0 commit comments

Comments
 (0)
0