@@ -589,7 +589,6 @@ async function store(state, emitter) {
589589 return
590590 }
591591
592-
593592 for ( let i in state . selectedFiles ) {
594593 const file = state . selectedFiles [ i ]
595594 if ( file . type == 'folder' ) {
@@ -637,8 +636,145 @@ async function store(state, emitter) {
637636 emitter . emit ( 'render' )
638637 } )
639638
640- emitter . on ( 'rename-file' , ( ) => { /* TODO */ } )
641- emitter . on ( 'finish-renaming' , ( ) => { /* TODO */ } )
639+ emitter . on ( 'rename-file' , ( source , item ) => {
640+ log ( 'rename-file' , source , item )
641+ state . renamingFile = source
642+ emitter . emit ( 'render' )
643+ } )
644+ emitter . on ( 'finish-renaming' , async ( value ) => {
645+ log ( 'finish-renaming' , value )
646+
647+ // You can only rename one file, the selected one
648+ const file = state . selectedFiles [ 0 ]
649+
650+ if ( ! value || file . fileName == value ) {
651+ state . renamingFile = null
652+ emitter . emit ( 'render' )
653+ return
654+ }
655+
656+ state . isSaving = true
657+ emitter . emit ( 'render' )
658+
659+ // Check if new name overwrites something
660+ if ( state . renamingFile == 'board' && state . isConnected ) {
661+ // Check if it will overwrite something
662+ const willOverwrite = await checkOverwrite ( {
663+ fileNames : [ value ] ,
664+ parentPath : disk . getFullPath (
665+ state . boardNavigationRoot , state . boardNavigationPath , ''
666+ ) ,
667+ source : 'board'
668+ } )
669+ if ( willOverwrite . length > 0 ) {
670+ let message = `You are about to overwrite the following file/folder on your board:\n\n`
671+ message += `${ value } \n\n`
672+ message += `Are you sure you want to proceed?`
673+ const confirmAction = confirm ( message , 'Cancel' , 'Yes' )
674+ if ( ! confirmAction ) {
675+ state . isSaving = false
676+ state . renamingFile = null
677+ emitter . emit ( 'render' )
678+ return
679+ }
680+
681+ if ( file . type == 'folder' ) {
682+ await removeBoardFolder (
683+ serial . getFullPath (
684+ state . boardNavigationRoot ,
685+ state . boardNavigationPath ,
686+ value
687+ )
688+ )
689+ } else if ( file . type == 'file' ) {
690+ await serial . removeFile (
691+ serial . getFullPath (
692+ state . boardNavigationRoot ,
693+ state . boardNavigationPath ,
694+ value
695+ )
696+ )
697+ }
698+ }
699+ } else if ( state . renamingFile == 'disk' ) {
700+ // Check if it will overwrite something
701+ const willOverwrite = await checkOverwrite ( {
702+ fileNames : [ value ] ,
703+ parentPath : disk . getFullPath (
704+ state . diskNavigationRoot , state . diskNavigationPath , ''
705+ ) ,
706+ source : 'disk'
707+ } )
708+ if ( willOverwrite . length > 0 ) {
709+ let message = `You are about to overwrite the following file/folder on your disk:\n\n`
710+ message += `${ value } \n\n`
711+ message += `Are you sure you want to proceed?`
712+ const confirmAction = confirm ( message , 'Cancel' , 'Yes' )
713+ if ( ! confirmAction ) {
714+ state . isSaving = false
715+ state . renamingFile = null
716+ emitter . emit ( 'render' )
717+ return
718+ }
719+
720+ if ( file . type == 'folder' ) {
721+ await disk . removeFolder (
722+ disk . getFullPath (
723+ state . diskNavigationRoot ,
724+ state . diskNavigationPath ,
725+ value
726+ )
727+ )
728+ } else if ( file . type == 'file' ) {
729+ await disk . removeFile (
730+ disk . getFullPath (
731+ state . diskNavigationRoot ,
732+ state . diskNavigationPath ,
733+ value
734+ )
735+ )
736+ }
737+
738+ }
739+ }
740+
741+ try {
742+ if ( state . renamingFile == 'board' ) {
743+ await serial . renameFile (
744+ serial . getFullPath (
745+ state . boardNavigationRoot ,
746+ state . boardNavigationPath ,
747+ file . fileName
748+ ) ,
749+ serial . getFullPath (
750+ state . boardNavigationRoot ,
751+ state . boardNavigationPath ,
752+ value
753+ )
754+ )
755+ } else {
756+ await disk . renameFile (
757+ disk . getFullPath (
758+ state . diskNavigationRoot ,
759+ state . diskNavigationPath ,
760+ file . fileName
761+ ) ,
762+ disk . getFullPath (
763+ state . diskNavigationRoot ,
764+ state . diskNavigationPath ,
765+ value
766+ )
767+ )
768+ }
769+ } catch ( e ) {
770+ alert ( `The file ${ file . fileName } could not be renamed to ${ value } ` )
771+ }
772+
773+ state . isSaving = false
774+ state . renamingFile = null
775+ emitter . emit ( 'refresh-files' )
776+ emitter . emit ( 'render' )
777+ } )
642778
643779 emitter . on ( 'toggle-file-selection' , ( file , source , event ) => {
644780 log ( 'toggle-file-selection' , file , source , event )
@@ -1018,7 +1154,6 @@ async function checkBoardFile({ root, parentFolder, fileName }) {
10181154
10191155async function checkOverwrite ( { fileNames = [ ] , parentPath, source } ) {
10201156 let files = [ ]
1021- let overwrite = [ ]
10221157 if ( source === 'board' ) {
10231158 files = await getBoardFiles ( parentPath )
10241159 } else {
0 commit comments