@@ -237,6 +237,7 @@ const App: React.FC = () => {
237
237
const [ treeExpandedKeys , setTreeExpandedKeys ] = useState < React . Key [ ] > ( [ ] ) ;
238
238
const [ treeSelectedKey , setTreeSelectedKey ] = useState < React . Key > ( '' ) ;
239
239
const [ currentModulePath , setCurrentModulePath ] = useState ( '' ) ;
240
+ const [ currentModule , setCurrentModule ] = useState < commonStorage . Module | null > ( null ) ;
240
241
const [ renameTooltip , setRenameTooltip ] = useState ( 'Rename' ) ;
241
242
const [ copyTooltip , setCopyTooltip ] = useState ( 'Copy' ) ;
242
243
const [ deleteTooltip , setDeleteTooltip ] = useState ( 'Delete' ) ;
@@ -404,38 +405,47 @@ const App: React.FC = () => {
404
405
405
406
// When a module path has become the current module path (either by fetching
406
407
// the most recent module path (soon after the app is loaded) or by the user
407
- // selecting it in the tree, set the current workspace, expend the
408
- // workspace node in the tree .
408
+ // selecting it in the tree, set the current module, update some tooltips, and
409
+ // load the module into the blockly editor .
409
410
useEffect ( ( ) => {
410
411
if ( ignoreEffect ( ) ) {
411
412
return ;
412
413
}
413
- if ( currentModulePath && modules . length > 0 ) {
414
- const workspaceName = commonStorage . getWorkspaceName ( currentModulePath ) ;
415
- const workspacePath = commonStorage . makeModulePath ( workspaceName , workspaceName ) ;
416
414
417
- if ( currentModulePath === workspacePath ) {
415
+ const module = ( modules . length > 0 && currentModulePath )
416
+ ? commonStorage . findModule ( modules , currentModulePath )
417
+ : null ;
418
+ setCurrentModule ( module ) ;
419
+
420
+ if ( module != null ) {
421
+ if ( module . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
418
422
setRenameTooltip ( 'Rename Workspace' ) ;
419
423
setCopyTooltip ( 'Copy Workspace' ) ;
420
424
setDeleteTooltip ( 'Delete Workspace' ) ;
421
- } else {
425
+ } else if ( module . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
422
426
setRenameTooltip ( 'Rename OpMode' ) ;
423
427
setCopyTooltip ( 'Copy OpMode' ) ;
424
428
setDeleteTooltip ( 'Delete OpMode' ) ;
429
+ } else if ( module . moduleType == commonStorage . MODULE_TYPE_MECHANISM ) {
430
+ setRenameTooltip ( 'Rename Mechanism' ) ;
431
+ setCopyTooltip ( 'Copy Mechanism' ) ;
432
+ setDeleteTooltip ( 'Delete Mechanism' ) ;
425
433
}
426
434
427
435
storage . saveEntry ( 'mostRecentModulePath' , currentModulePath ) ;
428
436
if ( blocksEditor . current ) {
429
- blocksEditor . current . loadModuleBlocks ( currentModulePath , workspacePath ) ;
437
+ blocksEditor . current . loadModuleBlocks ( module ) ;
430
438
}
431
439
} else {
440
+ setCurrentModule ( null ) ;
441
+
432
442
setRenameTooltip ( 'Rename' ) ;
433
443
setCopyTooltip ( 'Copy' ) ;
434
444
setDeleteTooltip ( 'Delete' ) ;
435
445
436
446
storage . saveEntry ( 'mostRecentModulePath' , '' ) ;
437
447
if ( blocksEditor . current ) {
438
- blocksEditor . current . loadModuleBlocks ( '' , '' ) ;
448
+ blocksEditor . current . loadModuleBlocks ( null ) ;
439
449
}
440
450
}
441
451
} , [ currentModulePath ] ) ;
@@ -540,9 +550,9 @@ const App: React.FC = () => {
540
550
} ;
541
551
542
552
const handleNewWorkspaceNameOk = ( newWorkspaceName : string ) => {
543
- const newWorkspacePath = commonStorage . makeModulePath ( newWorkspaceName , newWorkspaceName ) ;
553
+ const newWorkspacePath = commonStorage . makeWorkspacePath ( newWorkspaceName ) ;
544
554
if ( newWorkspaceNameModalPurpose === 'NewWorkspace' ) {
545
- const workspaceContent = commonStorage . newModuleContent ( ) ;
555
+ const workspaceContent = commonStorage . newWorkspaceContent ( newWorkspaceName ) ;
546
556
storage . createModule (
547
557
commonStorage . MODULE_TYPE_WORKSPACE , newWorkspacePath , workspaceContent ,
548
558
( success : boolean , errorMessage : string ) => {
@@ -595,10 +605,14 @@ const App: React.FC = () => {
595
605
} ) ;
596
606
} ;
597
607
608
+ // Provide a callback so the NewOpModeNameModal will know what the current
609
+ // workspace name is.
598
610
const getCurrentWorkspaceName = ( ) : string => {
599
- return commonStorage . getWorkspaceName ( currentModulePath ) ;
611
+ return currentModule ? currentModule . workspaceName : '' ;
600
612
} ;
601
613
614
+ // Provide a callback so the NewOpModeNameModal will know what the existing
615
+ // OpMode names are in the current workspace.
602
616
const getOpModeNames = ( workspaceName : string ) : string [ ] => {
603
617
const opModeNames : string [ ] = [ ] ;
604
618
for ( const workspace of modules ) {
@@ -615,7 +629,7 @@ const App: React.FC = () => {
615
629
const handleNewOpModeNameOk = ( workspaceName : string , newOpModeName : string ) => {
616
630
const newOpModePath = commonStorage . makeModulePath ( workspaceName , newOpModeName ) ;
617
631
if ( newOpModeNameModalPurpose === 'NewOpMode' ) {
618
- const opModeContent = commonStorage . newModuleContent ( ) ;
632
+ const opModeContent = commonStorage . newOpModeContent ( workspaceName , newOpModeName ) ;
619
633
storage . createModule (
620
634
commonStorage . MODULE_TYPE_OPMODE , newOpModePath , opModeContent ,
621
635
( success : boolean , errorMessage : string ) => {
@@ -685,61 +699,70 @@ const App: React.FC = () => {
685
699
686
700
const handleRenameClicked = ( ) => {
687
701
checkIfBlocksWereModified ( ( ) => {
688
- const workspaceName = commonStorage . getWorkspaceName ( currentModulePath ) ;
689
- const moduleName = commonStorage . getModuleName ( currentModulePath ) ;
690
- if ( workspaceName === moduleName ) {
702
+ if ( ! currentModule ) {
703
+ return ;
704
+ }
705
+ if ( currentModule . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
691
706
// This is a workspace.
692
707
setNewWorkspaceNameModalPurpose ( 'RenameWorkspace' ) ;
693
- setNewWorkspaceNameModalInitialValue ( workspaceName ) ;
708
+ setNewWorkspaceNameModalInitialValue ( currentModule . workspaceName ) ;
694
709
setNewWorkspaceNameModalIsOpen ( true ) ;
695
- } else {
710
+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
696
711
// This is an OpMode.
697
712
setNewOpModeNameModalPurpose ( 'RenameOpMode' ) ;
698
- setNewOpModeNameModalInitialValue ( moduleName ) ;
713
+ setNewOpModeNameModalInitialValue ( currentModule . moduleName ) ;
699
714
setNewOpModeNameModalIsOpen ( true ) ;
700
715
}
701
716
} ) ;
702
717
} ;
703
718
704
719
const handleCopyClicked = ( ) => {
705
720
checkIfBlocksWereModified ( ( ) => {
706
- const workspaceName = commonStorage . getWorkspaceName ( currentModulePath ) ;
707
- const moduleName = commonStorage . getModuleName ( currentModulePath ) ;
708
- if ( workspaceName === moduleName ) {
721
+ if ( ! currentModule ) {
722
+ return ;
723
+ }
724
+ if ( currentModule . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
709
725
// This is a workspace.
710
726
setNewWorkspaceNameModalPurpose ( 'CopyWorkspace' ) ;
711
- setNewWorkspaceNameModalInitialValue ( workspaceName + '_copy' ) ;
727
+ setNewWorkspaceNameModalInitialValue ( currentModule . workspaceName + '_copy' ) ;
712
728
setNewWorkspaceNameModalIsOpen ( true ) ;
713
- } else {
729
+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
714
730
// This is an OpMode.
715
731
setNewOpModeNameModalPurpose ( 'CopyOpMode' ) ;
716
- setNewOpModeNameModalInitialValue ( moduleName + '_copy' ) ;
732
+ setNewOpModeNameModalInitialValue ( currentModule . moduleName + '_copy' ) ;
717
733
setNewOpModeNameModalIsOpen ( true ) ;
718
734
}
719
735
} ) ;
720
736
} ;
721
737
722
738
const handleDeleteClicked = ( ) => {
723
- const workspaceName = commonStorage . getWorkspaceName ( currentModulePath ) ;
724
- const moduleName = commonStorage . getModuleName ( currentModulePath ) ;
725
-
739
+ if ( ! currentModule ) {
740
+ return ;
741
+ }
726
742
// Show a bubble confirmation box to ask the user if they are sure.
727
743
setPopconfirmTitle ( 'Are you sure?' ) ;
728
- if ( workspaceName === moduleName ) {
744
+ if ( currentModule . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
729
745
setPopconfirmDescription ( 'Press ok to delete this Workspace' ) ;
730
- } else {
746
+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
731
747
setPopconfirmDescription ( 'Press ok to delete this OpMode' ) ;
748
+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_MECHANISM ) {
749
+ // TODO: delete the mechanism.
750
+ return ;
732
751
}
733
752
// Set the function to be executed if the user clicks 'ok'.
734
753
afterPopconfirmOk . current = ( ) => {
735
754
setOpenPopconfirm ( false ) ;
736
755
checkIfBlocksWereModified ( ( ) => {
737
- if ( workspaceName === moduleName ) {
756
+ if ( ! currentModule ) {
757
+ return ;
758
+ }
759
+ if ( currentModule . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
738
760
// This is a workspace.
739
761
// Before deleting it, select another workspace, if there is one.
762
+ const workspaceNameToDelete = currentModule . workspaceName ;
740
763
let foundAnotherWorkspace = false ;
741
764
for ( const workspace of modules ) {
742
- if ( workspace . workspaceName !== workspaceName ) {
765
+ if ( workspace . workspaceName !== workspaceNameToDelete ) {
743
766
setCurrentModulePath ( workspace . modulePath ) ;
744
767
foundAnotherWorkspace = true ;
745
768
break ;
@@ -748,7 +771,7 @@ const App: React.FC = () => {
748
771
if ( ! foundAnotherWorkspace ) {
749
772
setCurrentModulePath ( '' ) ;
750
773
}
751
- storage . deleteWorkspace ( workspaceName ,
774
+ storage . deleteWorkspace ( workspaceNameToDelete ,
752
775
( success : boolean , errorMessage : string ) => {
753
776
if ( success ) {
754
777
setTriggerListModules ( ! triggerListModules ) ;
@@ -757,11 +780,12 @@ const App: React.FC = () => {
757
780
setAlertErrorVisible ( true ) ;
758
781
}
759
782
} ) ;
760
- } else {
783
+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
761
784
// This is an OpMode.
762
- const workspacePath = commonStorage . makeModulePath ( workspaceName , workspaceName ) ;
785
+ const modulePathToDelete = currentModulePath ;
786
+ const workspacePath = commonStorage . makeWorkspacePath ( currentModule . workspaceName ) ;
763
787
setCurrentModulePath ( workspacePath ) ;
764
- storage . deleteOpMode ( currentModulePath ,
788
+ storage . deleteOpMode ( modulePathToDelete ,
765
789
( success : boolean , errorMessage : string ) => {
766
790
if ( success ) {
767
791
setTriggerListModules ( ! triggerListModules ) ;
0 commit comments