Skip to content

Commit e89a65f

Browse files
authored
Merge pull request #102 from arduino/feature/rename-tab2
Renaming tabs
2 parents 1e43d2d + 6a25c8a commit e89a65f

File tree

7 files changed

+725
-70
lines changed

7 files changed

+725
-70
lines changed
240 KB
Loading

ui/arduino2/documents/24-04-09-lab4mpy-modeling.svg

Lines changed: 413 additions & 0 deletions
Loading

ui/arduino2/main.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ button.small .icon {
186186
background: rgba(255, 255, 255, 0.5);
187187
}
188188

189+
.tab .text input {
190+
box-sizing: border-box;
191+
border: none;
192+
border-radius: none;
193+
height: 100%;
194+
width: 100%;
195+
background: rgba(255, 255, 255, 0.5);
196+
font-family: inherit;
197+
font-size: inherit;
198+
outline-color: #F4BA00;
199+
}
200+
189201
#code-editor {
190202
flex: 1 0 0;
191203
align-self: stretch;

ui/arduino2/store.js

Lines changed: 240 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async function store(state, emitter) {
2424
state.creatingFile = null
2525
state.renamingFile = null
2626
state.creatingFolder = null
27+
state.renamingTab = null
2728

2829
state.availablePorts = []
2930

@@ -214,82 +215,69 @@ async function store(state, emitter) {
214215
let openFile = state.openFiles.find(f => f.id === state.editingFile)
215216

216217
let willOverwrite = false
217-
if (openFile.parentFolder === null) { // it's a new file
218+
const oldParentFolder = openFile.parentFolder
219+
const isNewFile = oldParentFolder === null
220+
221+
if (isNewFile) {
218222
// Define parent folder
219223
if (openFile.source == 'board') {
220224
openFile.parentFolder = state.boardNavigationPath
221-
// Check for overwrite
222-
willOverwrite = await serial.fileExists(
223-
serial.getFullPath(
224-
state.boardNavigationRoot,
225-
openFile.parentFolder,
226-
openFile.fileName
227-
)
228-
)
229225
} else if (openFile.source == 'disk') {
230226
openFile.parentFolder = state.diskNavigationPath
231-
// Check for overwrite
232-
willOverwrite = await disk.fileExists(
233-
disk.getFullPath(
234-
state.diskNavigationRoot,
235-
openFile.parentFolder,
236-
openFile.fileName
237-
)
238-
)
239227
}
240228

241-
} else if (openFile.parentFolder !== null) {
242-
// Check if the current full path exists
243-
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
244253
if (openFile.source == 'board') {
245-
fullPathExists = await serial.fileExists(
254+
openFile.parentFolder = state.boardNavigationPath
255+
// Check for overwrite
256+
willOverwrite = await serial.fileExists(
246257
serial.getFullPath(
247258
state.boardNavigationRoot,
248259
openFile.parentFolder,
249260
openFile.fileName
250261
)
251262
)
252263
} else if (openFile.source == 'disk') {
253-
fullPathExists = await disk.fileExists(
264+
openFile.parentFolder = state.diskNavigationPath
265+
// Check for overwrite
266+
willOverwrite = await disk.fileExists(
254267
disk.getFullPath(
255268
state.diskNavigationRoot,
256269
openFile.parentFolder,
257270
openFile.fileName
258271
)
259272
)
260273
}
261-
262-
if (!fullPathExists) {
263-
// Redefine parent folder
264-
if (openFile.source == 'board') {
265-
openFile.parentFolder = state.boardNavigationPath
266-
// Check for overwrite
267-
willOverwrite = await serial.fileExists(
268-
serial.getFullPath(
269-
state.boardNavigationRoot,
270-
openFile.parentFolder,
271-
openFile.fileName
272-
)
273-
)
274-
} else if (openFile.source == 'disk') {
275-
openFile.parentFolder = state.diskNavigationPath
276-
// Check for overwrite
277-
willOverwrite = await disk.fileExists(
278-
disk.getFullPath(
279-
state.diskNavigationRoot,
280-
openFile.parentFolder,
281-
openFile.fileName
282-
)
283-
)
284-
}
285-
}
286274
}
287275

288276
if (willOverwrite) {
289277
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')
290278
if (!confirmation) {
291279
state.isSaving = false
292-
openFile.parentFolder = null
280+
openFile.parentFolder = oldParentFolder
293281
emitter.emit('render')
294282
return
295283
}
@@ -367,7 +355,7 @@ async function store(state, emitter) {
367355
if (state.isConnected) {
368356
state.boardFiles = await getBoardFiles(
369357
serial.getFullPath(
370-
'/',
358+
state.boardNavigationRoot,
371359
state.boardNavigationPath,
372360
''
373361
)
@@ -641,8 +629,8 @@ async function store(state, emitter) {
641629
state.renamingFile = source
642630
emitter.emit('render')
643631
})
644-
emitter.on('finish-renaming', async (value) => {
645-
log('finish-renaming', value)
632+
emitter.on('finish-renaming-file', async (value) => {
633+
log('finish-renaming-file', value)
646634

647635
// You can only rename one file, the selected one
648636
const file = state.selectedFiles[0]
@@ -776,6 +764,207 @@ async function store(state, emitter) {
776764
emitter.emit('render')
777765
})
778766

767+
emitter.on('rename-tab', (id) => {
768+
log('rename-tab', id)
769+
state.renamingTab = id
770+
emitter.emit('render')
771+
})
772+
emitter.on('finish-renaming-tab', async (value) => {
773+
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+
785+
let response = canSave({
786+
view: state.view,
787+
isConnected: state.isConnected,
788+
openFiles: state.openFiles,
789+
editingFile: state.editingFile
790+
})
791+
if (response == false) {
792+
log("can't save")
793+
return
794+
}
795+
796+
state.isSaving = true
797+
emitter.emit('render')
798+
799+
const oldParentFolder = openFile.parentFolder
800+
const oldName = openFile.fileName
801+
openFile.fileName = value
802+
803+
const isNewFile = oldParentFolder === null
804+
let fullPathExists = false
805+
if (!isNewFile) {
806+
// Check if full path exists
807+
if (openFile.source == 'board') {
808+
fullPathExists = await serial.fileExists(
809+
serial.getFullPath(
810+
state.boardNavigationRoot,
811+
openFile.parentFolder,
812+
oldName
813+
)
814+
)
815+
} else if (openFile.source == 'disk') {
816+
fullPathExists = await disk.fileExists(
817+
disk.getFullPath(
818+
state.diskNavigationRoot,
819+
openFile.parentFolder,
820+
oldName
821+
)
822+
)
823+
}
824+
}
825+
if (isNewFile || !fullPathExists) {
826+
// Define parent folder
827+
if (openFile.source == 'board') {
828+
openFile.parentFolder = state.boardNavigationPath
829+
} else if (openFile.source == 'disk') {
830+
openFile.parentFolder = state.diskNavigationPath
831+
}
832+
}
833+
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
842+
)
843+
)
844+
} else if (openFile.source == 'disk') {
845+
willOverwrite = await disk.fileExists(
846+
disk.getFullPath(
847+
state.diskNavigationRoot,
848+
openFile.parentFolder,
849+
openFile.fileName
850+
)
851+
)
852+
}
853+
854+
if (willOverwrite) {
855+
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')
856+
if (!confirmation) {
857+
state.renamingTab = null
858+
state.isSaving = false
859+
openFile.fileName = oldName
860+
emitter.emit('render')
861+
return
862+
}
863+
}
864+
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+
}
882+
)
883+
} else if (openFile.source == 'disk') {
884+
await disk.saveFileContent(
885+
disk.getFullPath(
886+
state.diskNavigationRoot,
887+
openFile.parentFolder,
888+
oldName
889+
),
890+
contents
891+
)
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)
958+
}
959+
}
960+
961+
state.renamingTab = null
962+
state.isSaving = false
963+
state.savingProgress = 0
964+
emitter.emit('refresh-files')
965+
emitter.emit('render')
966+
})
967+
779968
emitter.on('toggle-file-selection', (file, source, event) => {
780969
log('toggle-file-selection', file, source, event)
781970
// Single file selection unless holding keyboard key

0 commit comments

Comments
 (0)