Skip to content

Commit 3be4792

Browse files
committed
Cleanup and covering more edge cases
1 parent cd21d6e commit 3be4792

File tree

1 file changed

+169
-141
lines changed

1 file changed

+169
-141
lines changed

ui/arduino2/store.js

+169-141
Original file line numberDiff line numberDiff line change
@@ -215,82 +215,69 @@ async function store(state, emitter) {
215215
let openFile = state.openFiles.find(f => f.id === state.editingFile)
216216

217217
let willOverwrite = false
218-
if (openFile.parentFolder === null) { // it's a new file
218+
const oldParentFolder = openFile.parentFolder
219+
const isNewFile = oldParentFolder === null
220+
221+
if (isNewFile) {
219222
// Define parent folder
220223
if (openFile.source == 'board') {
221224
openFile.parentFolder = state.boardNavigationPath
222-
// Check for overwrite
223-
willOverwrite = await serial.fileExists(
224-
serial.getFullPath(
225-
state.boardNavigationRoot,
226-
openFile.parentFolder,
227-
openFile.fileName
228-
)
229-
)
230225
} else if (openFile.source == 'disk') {
231226
openFile.parentFolder = state.diskNavigationPath
232-
// Check for overwrite
233-
willOverwrite = await disk.fileExists(
234-
disk.getFullPath(
235-
state.diskNavigationRoot,
236-
openFile.parentFolder,
237-
openFile.fileName
238-
)
239-
)
240227
}
241228

242-
} else if (openFile.parentFolder !== null) {
243-
// Check if the current full path exists
244-
let fullPathExists = false
229+
}
230+
231+
// Check if the current full path exists
232+
let fullPathExists = false
233+
if (openFile.source == 'board') {
234+
fullPathExists = await serial.fileExists(
235+
serial.getFullPath(
236+
state.boardNavigationRoot,
237+
openFile.parentFolder,
238+
openFile.fileName
239+
)
240+
)
241+
} else if (openFile.source == 'disk') {
242+
fullPathExists = await disk.fileExists(
243+
disk.getFullPath(
244+
state.diskNavigationRoot,
245+
openFile.parentFolder,
246+
openFile.fileName
247+
)
248+
)
249+
}
250+
251+
if (isNewFile || !fullPathExists) {
252+
// Redefine parent folder
245253
if (openFile.source == 'board') {
246-
fullPathExists = await serial.fileExists(
254+
openFile.parentFolder = state.boardNavigationPath
255+
// Check for overwrite
256+
willOverwrite = await serial.fileExists(
247257
serial.getFullPath(
248258
state.boardNavigationRoot,
249259
openFile.parentFolder,
250260
openFile.fileName
251261
)
252262
)
253263
} else if (openFile.source == 'disk') {
254-
fullPathExists = await disk.fileExists(
264+
openFile.parentFolder = state.diskNavigationPath
265+
// Check for overwrite
266+
willOverwrite = await disk.fileExists(
255267
disk.getFullPath(
256268
state.diskNavigationRoot,
257269
openFile.parentFolder,
258270
openFile.fileName
259271
)
260272
)
261273
}
262-
263-
if (!fullPathExists) {
264-
// Redefine parent folder
265-
if (openFile.source == 'board') {
266-
openFile.parentFolder = state.boardNavigationPath
267-
// Check for overwrite
268-
willOverwrite = await serial.fileExists(
269-
serial.getFullPath(
270-
state.boardNavigationRoot,
271-
openFile.parentFolder,
272-
openFile.fileName
273-
)
274-
)
275-
} else if (openFile.source == 'disk') {
276-
openFile.parentFolder = state.diskNavigationPath
277-
// Check for overwrite
278-
willOverwrite = await disk.fileExists(
279-
disk.getFullPath(
280-
state.diskNavigationRoot,
281-
openFile.parentFolder,
282-
openFile.fileName
283-
)
284-
)
285-
}
286-
}
287274
}
288275

289276
if (willOverwrite) {
290277
const confirmation = confirm(`You are about to overwrite the file ${openFile.fileName} on your ${openFile.source}.\n\n Are you sure you want to proceed?`, 'Cancel', 'Yes')
291278
if (!confirmation) {
292279
state.isSaving = false
293-
openFile.parentFolder = null
280+
openFile.parentFolder = oldParentFolder
294281
emitter.emit('render')
295282
return
296283
}
@@ -368,7 +355,7 @@ async function store(state, emitter) {
368355
if (state.isConnected) {
369356
state.boardFiles = await getBoardFiles(
370357
serial.getFullPath(
371-
'/',
358+
state.boardNavigationRoot,
372359
state.boardNavigationPath,
373360
''
374361
)
@@ -784,6 +771,17 @@ async function store(state, emitter) {
784771
})
785772
emitter.on('finish-renaming-tab', async (value) => {
786773
log('finish-renaming-tab', value)
774+
775+
// You can only rename one tab, the active one
776+
const openFile = state.openFiles.find(f => f.id === state.renamingTab)
777+
778+
if (!value || openFile.fileName == value) {
779+
state.renamingTab = null
780+
state.isSaving = false
781+
emitter.emit('render')
782+
return
783+
}
784+
787785
let response = canSave({
788786
view: state.view,
789787
isConnected: state.isConnected,
@@ -798,93 +796,59 @@ async function store(state, emitter) {
798796
state.isSaving = true
799797
emitter.emit('render')
800798

801-
// Rename the open file object
802-
let openFile = state.openFiles.find(f => f.id === state.renamingTab)
803-
if (value == openFile.fileName) {
804-
state.renamingTab = null
805-
state.isSaving = false
806-
emitter.emit('render')
807-
return
808-
}
809-
799+
const oldParentFolder = openFile.parentFolder
810800
const oldName = openFile.fileName
811801
openFile.fileName = value
812802

813-
let willOverwrite = false
814-
if (openFile.parentFolder === null) { // it's a new file
815-
// Define parent folder
803+
const isNewFile = oldParentFolder === null
804+
let fullPathExists = false
805+
if (!isNewFile) {
806+
// Check if full path exists
816807
if (openFile.source == 'board') {
817-
openFile.parentFolder = state.boardNavigationPath
818-
// Check for overwrite
819-
willOverwrite = await serial.fileExists(
808+
fullPathExists = await serial.fileExists(
820809
serial.getFullPath(
821810
state.boardNavigationRoot,
822811
openFile.parentFolder,
823-
openFile.fileName
812+
oldName
824813
)
825814
)
826815
} else if (openFile.source == 'disk') {
827-
openFile.parentFolder = state.diskNavigationPath
828-
// Check for overwrite
829-
willOverwrite = await disk.fileExists(
816+
fullPathExists = await disk.fileExists(
830817
disk.getFullPath(
831818
state.diskNavigationRoot,
832819
openFile.parentFolder,
833-
openFile.fileName
820+
oldName
834821
)
835822
)
836823
}
837-
} else if (openFile.parentFolder !== null) {
824+
}
825+
if (isNewFile || !fullPathExists) {
826+
// Define parent folder
838827
if (openFile.source == 'board') {
839-
// Check for overwrite
840-
willOverwrite = await serial.fileExists(
841-
serial.getFullPath(
842-
state.boardNavigationRoot,
843-
openFile.parentFolder,
844-
openFile.fileName
845-
)
846-
)
828+
openFile.parentFolder = state.boardNavigationPath
847829
} else if (openFile.source == 'disk') {
848-
// Check for overwrite
849-
willOverwrite = await disk.fileExists(
850-
disk.getFullPath(
851-
state.diskNavigationRoot,
852-
openFile.parentFolder,
853-
openFile.fileName
854-
)
855-
)
830+
openFile.parentFolder = state.diskNavigationPath
856831
}
857-
}
832+
}
858833

859-
// SAVE FILE CONTENTS
860-
const contents = openFile.editor.editor.state.doc.toString()
861-
try {
862-
if (openFile.source == 'board') {
863-
await serial.get_prompt()
864-
await serial.saveFileContent(
865-
serial.getFullPath(
866-
state.boardNavigationRoot,
867-
openFile.parentFolder,
868-
oldName
869-
),
870-
contents,
871-
(e) => {
872-
state.savingProgress = e
873-
emitter.emit('render')
874-
}
834+
// Check if it will overwrite
835+
let willOverwrite = false
836+
if (openFile.source == 'board') {
837+
willOverwrite = await serial.fileExists(
838+
serial.getFullPath(
839+
state.boardNavigationRoot,
840+
openFile.parentFolder,
841+
openFile.fileName
875842
)
876-
} else if (openFile.source == 'disk') {
877-
await disk.saveFileContent(
878-
disk.getFullPath(
879-
state.diskNavigationRoot,
880-
openFile.parentFolder,
881-
oldName
882-
),
883-
contents
843+
)
844+
} else if (openFile.source == 'disk') {
845+
willOverwrite = await disk.fileExists(
846+
disk.getFullPath(
847+
state.diskNavigationRoot,
848+
openFile.parentFolder,
849+
openFile.fileName
884850
)
885-
}
886-
} catch (e) {
887-
log('error', e)
851+
)
888852
}
889853

890854
if (willOverwrite) {
@@ -898,36 +862,100 @@ async function store(state, emitter) {
898862
}
899863
}
900864

901-
try {
902-
if (openFile.source == 'board') {
903-
await serial.renameFile(
904-
serial.getFullPath(
905-
state.boardNavigationRoot,
906-
openFile.parentFolder,
907-
oldName
908-
),
909-
serial.getFullPath(
910-
state.boardNavigationRoot,
911-
openFile.parentFolder,
912-
openFile.fileName
865+
if (fullPathExists) {
866+
// SAVE FILE CONTENTS
867+
const contents = openFile.editor.editor.state.doc.toString()
868+
try {
869+
if (openFile.source == 'board') {
870+
await serial.get_prompt()
871+
await serial.saveFileContent(
872+
serial.getFullPath(
873+
state.boardNavigationRoot,
874+
openFile.parentFolder,
875+
oldName
876+
),
877+
contents,
878+
(e) => {
879+
state.savingProgress = e
880+
emitter.emit('render')
881+
}
913882
)
914-
)
915-
} else if (openFile.source == 'disk') {
916-
await disk.renameFile(
917-
disk.getFullPath(
918-
state.diskNavigationRoot,
919-
openFile.parentFolder,
920-
oldName
921-
),
922-
disk.getFullPath(
923-
state.diskNavigationRoot,
924-
openFile.parentFolder,
925-
openFile.fileName
883+
} else if (openFile.source == 'disk') {
884+
await disk.saveFileContent(
885+
disk.getFullPath(
886+
state.diskNavigationRoot,
887+
openFile.parentFolder,
888+
oldName
889+
),
890+
contents
926891
)
927-
)
892+
}
893+
} catch (e) {
894+
log('error', e)
895+
}
896+
// RENAME FILE
897+
try {
898+
if (openFile.source == 'board') {
899+
await serial.renameFile(
900+
serial.getFullPath(
901+
state.boardNavigationRoot,
902+
openFile.parentFolder,
903+
oldName
904+
),
905+
serial.getFullPath(
906+
state.boardNavigationRoot,
907+
openFile.parentFolder,
908+
openFile.fileName
909+
)
910+
)
911+
} else if (openFile.source == 'disk') {
912+
await disk.renameFile(
913+
disk.getFullPath(
914+
state.diskNavigationRoot,
915+
openFile.parentFolder,
916+
oldName
917+
),
918+
disk.getFullPath(
919+
state.diskNavigationRoot,
920+
openFile.parentFolder,
921+
openFile.fileName
922+
)
923+
)
924+
}
925+
} catch(e) {
926+
log('error', e)
927+
}
928+
} else if (!fullPathExists) {
929+
// SAVE FILE CONTENTS
930+
const contents = openFile.editor.editor.state.doc.toString()
931+
try {
932+
if (openFile.source == 'board') {
933+
await serial.get_prompt()
934+
await serial.saveFileContent(
935+
serial.getFullPath(
936+
state.boardNavigationRoot,
937+
openFile.parentFolder,
938+
openFile.fileName
939+
),
940+
contents,
941+
(e) => {
942+
state.savingProgress = e
943+
emitter.emit('render')
944+
}
945+
)
946+
} else if (openFile.source == 'disk') {
947+
await disk.saveFileContent(
948+
disk.getFullPath(
949+
state.diskNavigationRoot,
950+
openFile.parentFolder,
951+
openFile.fileName
952+
),
953+
contents
954+
)
955+
}
956+
} catch (e) {
957+
log('error', e)
928958
}
929-
} catch(e) {
930-
log('error', e)
931959
}
932960

933961
state.renamingTab = null

0 commit comments

Comments
 (0)