@@ -36,6 +36,7 @@ import {
36
36
import { AsyncReturnType } from '../utils/TypeHelper' ;
37
37
import { safeTakeEvery as takeEvery , safeTakeLatest as takeLatest } from './SafeEffects' ;
38
38
import { WORKSPACE_BASE_PATHS } from 'src/pages/fileSystem/createInBrowserFileSystem' ;
39
+ import { EditorTabState } from '../workspace/WorkspaceTypes' ;
39
40
40
41
const DISCOVERY_DOCS = [ 'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest' ] ;
41
42
const SCOPES =
@@ -195,7 +196,7 @@ export function* persistenceSaga(): SagaIterator {
195
196
// TODO find a file to open instead of deleting all active tabs?
196
197
// TODO without modifying WorkspaceReducer in one function this would cause errors - called by onChange of Playground.tsx?
197
198
// TODO change behaviour of WorkspaceReducer to not create program.js every time folder mode changes with 0 tabs existing?
198
- yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ; // refreshes folder view TODO super jank?
199
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
199
200
200
201
yield call ( showSuccessMessage , `Loaded folder ${ name } .` , 1000 ) ;
201
202
@@ -553,25 +554,32 @@ export function* persistenceSaga(): SagaIterator {
553
554
PERSISTENCE_SAVE_FILE ,
554
555
function * ( { payload : { id, name } } : ReturnType < typeof actions . persistenceSaveFile > ) {
555
556
let toastKey : string | undefined ;
557
+
558
+ const [ currFolderObject ] = yield select ( // TODO resolve type here?
559
+ ( state : OverallState ) => [
560
+ state . playground . persistenceFile
561
+ ]
562
+ ) ;
563
+
564
+ yield call ( ensureInitialisedAndAuthorised ) ;
565
+
566
+ const [ activeEditorTabIndex , editorTabs , chapter , variant , external ] = yield select (
567
+ ( state : OverallState ) => [
568
+ state . workspaces . playground . activeEditorTabIndex ,
569
+ state . workspaces . playground . editorTabs ,
570
+ state . workspaces . playground . context . chapter ,
571
+ state . workspaces . playground . context . variant ,
572
+ state . workspaces . playground . externalLibrary
573
+ ]
574
+ ) ;
575
+
556
576
try {
557
577
toastKey = yield call ( showMessage , {
558
578
message : `Saving as ${ name } ...` ,
559
579
timeout : 0 ,
560
580
intent : Intent . PRIMARY
561
581
} ) ;
562
582
563
- yield call ( ensureInitialisedAndAuthorised ) ;
564
-
565
- const [ activeEditorTabIndex , editorTabs , chapter , variant , external ] = yield select (
566
- ( state : OverallState ) => [
567
- state . workspaces . playground . activeEditorTabIndex ,
568
- state . workspaces . playground . editorTabs ,
569
- state . workspaces . playground . context . chapter ,
570
- state . workspaces . playground . context . variant ,
571
- state . workspaces . playground . externalLibrary
572
- ]
573
- ) ;
574
-
575
583
if ( activeEditorTabIndex === null ) {
576
584
throw new Error ( 'No active editor tab found.' ) ;
577
585
}
@@ -582,6 +590,20 @@ export function* persistenceSaga(): SagaIterator {
582
590
variant,
583
591
external
584
592
} ;
593
+ if ( ( currFolderObject as PersistenceFile ) . isFolder ) {
594
+ yield call ( console . log , "folder opened! updating pers specially" ) ;
595
+ const persistenceFileArray : PersistenceFile [ ] = yield select ( ( state : OverallState ) => state . fileSystem . persistenceFileArray ) ;
596
+ const currPersistenceFile = persistenceFileArray . find ( e => e . path === ( editorTabs [ activeEditorTabIndex ] as EditorTabState ) . filePath ) ;
597
+ if ( ! currPersistenceFile ) {
598
+ throw new Error ( 'Persistence file not found' ) ;
599
+ }
600
+ yield call ( updateFile , currPersistenceFile . id , currPersistenceFile . name , MIME_SOURCE , code , config ) ;
601
+ currPersistenceFile . lastSaved = new Date ( ) ;
602
+ yield put ( actions . addPersistenceFile ( currPersistenceFile ) ) ;
603
+ yield call ( showSuccessMessage , `${ name } successfully saved to Google Drive.` , 1000 ) ;
604
+ return ;
605
+ }
606
+
585
607
yield call ( updateFile , id , name , MIME_SOURCE , code , config ) ;
586
608
yield put ( actions . playgroundUpdatePersistenceFile ( { id, name, lastSaved : new Date ( ) } ) ) ;
587
609
yield call ( showSuccessMessage , `${ name } successfully saved to Google Drive.` , 1000 ) ;
@@ -599,12 +621,12 @@ export function* persistenceSaga(): SagaIterator {
599
621
yield takeEvery (
600
622
PERSISTENCE_CREATE_FILE ,
601
623
function * ( { payload } : ReturnType < typeof actions . persistenceCreateFile > ) {
624
+ yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
625
+
602
626
const newFilePath = payload ;
603
627
yield call ( console . log , "create file " , newFilePath ) ;
604
628
605
- // const persistenceFileArray: PersistenceFile[] = yield select((state: OverallState) => state.fileSystem.persistenceFileArray);
606
-
607
- // look for parent folder persistenceFile
629
+ // look for parent folder persistenceFile TODO modify action so name is supplied?
608
630
const regexResult = / ^ ( .* [ \\ \/ ] ) ? ( \. * .* ?) ( \. [ ^ . ] + ?| ) $ / . exec ( newFilePath ) ;
609
631
const parentFolderPath = regexResult ? regexResult [ 1 ] . slice ( 0 , - 1 ) : undefined ;
610
632
if ( ! parentFolderPath ) {
@@ -638,6 +660,8 @@ export function* persistenceSaga(): SagaIterator {
638
660
} ;
639
661
const newFilePersistenceFile : PersistenceFile = yield call ( createFile , newFileName , parentFolderId , MIME_SOURCE , '' , config ) ;
640
662
yield put ( actions . addPersistenceFile ( { ...newFilePersistenceFile , lastSaved : new Date ( ) , path : newFilePath } ) ) ;
663
+ yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
664
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
641
665
yield call (
642
666
showSuccessMessage ,
643
667
`${ newFileName } successfully saved to Google Drive.` ,
@@ -649,13 +673,15 @@ export function* persistenceSaga(): SagaIterator {
649
673
yield takeEvery (
650
674
PERSISTENCE_CREATE_FOLDER ,
651
675
function * ( { payload } : ReturnType < typeof actions . persistenceCreateFolder > ) {
676
+ yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
677
+
652
678
const newFolderPath = payload ;
653
679
yield call ( console . log , "create folder " , newFolderPath ) ;
654
680
655
681
656
682
// const persistenceFileArray: PersistenceFile[] = yield select((state: OverallState) => state.fileSystem.persistenceFileArray);
657
683
658
- // look for parent folder persistenceFile
684
+ // look for parent folder persistenceFile TODO modify action so name is supplied?
659
685
const regexResult = / ^ ( .* [ \\ \/ ] ) ? ( \. * .* ?) ( \. [ ^ . ] + ?| ) $ / . exec ( newFolderPath ) ;
660
686
const parentFolderPath = regexResult ? regexResult [ 1 ] . slice ( 0 , - 1 ) : undefined ;
661
687
if ( ! parentFolderPath ) {
@@ -677,6 +703,8 @@ export function* persistenceSaga(): SagaIterator {
677
703
678
704
const newFolderId : string = yield call ( createFolderAndReturnId , parentFolderId , newFolderName ) ;
679
705
yield put ( actions . addPersistenceFile ( { lastSaved : new Date ( ) , path : newFolderPath , id : newFolderId , name : newFolderName , parentId : parentFolderId } ) ) ;
706
+ yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
707
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
680
708
yield call (
681
709
showSuccessMessage ,
682
710
`Folder ${ newFolderName } successfully saved to Google Drive.` ,
@@ -688,6 +716,8 @@ export function* persistenceSaga(): SagaIterator {
688
716
yield takeEvery (
689
717
PERSISTENCE_DELETE_FILE ,
690
718
function * ( { payload } : ReturnType < typeof actions . persistenceDeleteFile > ) {
719
+ yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
720
+
691
721
const filePath = payload ;
692
722
yield call ( console . log , "delete file " , filePath ) ;
693
723
@@ -698,8 +728,10 @@ export function* persistenceSaga(): SagaIterator {
698
728
yield call ( console . log , "cannot find pers file for " , filePath ) ;
699
729
return ;
700
730
}
701
- yield call ( deleteFileOrFolder , persistenceFile . id ) ; // assume this succeeds all the time
731
+ yield call ( deleteFileOrFolder , persistenceFile . id ) ; // assume this succeeds all the time? TODO
702
732
yield put ( actions . deletePersistenceFile ( persistenceFile ) ) ;
733
+ yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
734
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
703
735
yield call (
704
736
showSuccessMessage ,
705
737
`${ persistenceFile . name } successfully deleted from Google Drive.` ,
@@ -711,6 +743,8 @@ export function* persistenceSaga(): SagaIterator {
711
743
yield takeEvery (
712
744
PERSISTENCE_DELETE_FOLDER ,
713
745
function * ( { payload } : ReturnType < typeof actions . persistenceDeleteFolder > ) {
746
+ yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
747
+
714
748
const folderPath = payload ;
715
749
yield call ( console . log , "delete folder " , folderPath ) ;
716
750
@@ -721,8 +755,10 @@ export function* persistenceSaga(): SagaIterator {
721
755
yield call ( console . log , "cannot find pers file for " , folderPath ) ;
722
756
return ;
723
757
}
724
- yield call ( deleteFileOrFolder , persistenceFile . id ) ; // assume this succeeds all the time
758
+ yield call ( deleteFileOrFolder , persistenceFile . id ) ; // assume this succeeds all the time? TODO
725
759
yield put ( actions . deletePersistenceFile ( persistenceFile ) ) ;
760
+ yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
761
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
726
762
yield call (
727
763
showSuccessMessage ,
728
764
`Folder ${ persistenceFile . name } successfully deleted from Google Drive.` ,
@@ -734,6 +770,8 @@ export function* persistenceSaga(): SagaIterator {
734
770
yield takeEvery (
735
771
PERSISTENCE_RENAME_FILE ,
736
772
function * ( { payload : { oldFilePath, newFilePath} } : ReturnType < typeof actions . persistenceRenameFile > ) {
773
+ yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
774
+
737
775
yield call ( console . log , "rename file " , oldFilePath , " to " , newFilePath ) ;
738
776
739
777
// look for file
@@ -744,7 +782,7 @@ export function* persistenceSaga(): SagaIterator {
744
782
return ;
745
783
}
746
784
747
- // new name
785
+ // new name TODO: modify action so name is supplied?
748
786
const regexResult = / ^ ( .* [ \\ \/ ] ) ? ( \. * .* ?) ( \. [ ^ . ] + ?| ) $ / . exec ( newFilePath ) ;
749
787
if ( ! regexResult ) {
750
788
yield call ( console . log , "regex fail " , newFilePath ) ;
@@ -757,6 +795,8 @@ export function* persistenceSaga(): SagaIterator {
757
795
758
796
// handle pers file
759
797
yield put ( actions . updatePersistenceFilePathAndNameByPath ( oldFilePath , newFilePath , newFileName ) ) ;
798
+ yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
799
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
760
800
yield call (
761
801
showSuccessMessage ,
762
802
`${ newFileName } successfully renamed in Google Drive.` ,
@@ -768,7 +808,46 @@ export function* persistenceSaga(): SagaIterator {
768
808
yield takeEvery (
769
809
PERSISTENCE_RENAME_FOLDER ,
770
810
function * ( { payload : { oldFolderPath, newFolderPath} } : ReturnType < typeof actions . persistenceRenameFolder > ) {
811
+ yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
812
+
771
813
yield call ( console . log , "rename folder " , oldFolderPath , " to " , newFolderPath ) ;
814
+
815
+ // look for folder
816
+ const persistenceFileArray : PersistenceFile [ ] = yield select ( ( state : OverallState ) => state . fileSystem . persistenceFileArray ) ;
817
+ const persistenceFile = persistenceFileArray . find ( e => e . path === oldFolderPath ) ;
818
+ if ( ! persistenceFile ) {
819
+ yield call ( console . log , "cannot find pers file for " , oldFolderPath ) ;
820
+ return ;
821
+ }
822
+
823
+ // new name TODO: modify action so name is supplied?
824
+ const regexResult = / ^ ( .* [ \\ \/ ] ) ? ( \. * .* ?) ( \. [ ^ . ] + ?| ) $ / . exec ( newFolderPath ) ;
825
+ if ( ! regexResult ) {
826
+ yield call ( console . log , "regex fail " , newFolderPath ) ;
827
+ return ;
828
+ }
829
+ const newFolderName = regexResult [ 2 ] + regexResult [ 3 ] ;
830
+
831
+ // old name TODO: modify action so name is supplied?
832
+ const regexResult2 = / ^ ( .* [ \\ \/ ] ) ? ( \. * .* ?) ( \. [ ^ . ] + ?| ) $ / . exec ( oldFolderPath ) ;
833
+ if ( ! regexResult2 ) {
834
+ yield call ( console . log , "regex fail " , oldFolderPath ) ;
835
+ return ;
836
+ }
837
+ const oldFolderName = regexResult2 [ 2 ] + regexResult2 [ 3 ] ;
838
+
839
+ // call gapi
840
+ yield call ( renameFileOrFolder , persistenceFile . id , newFolderName ) ;
841
+
842
+ // handle pers file
843
+ yield put ( actions . updatePersistenceFolderPathAndNameByPath ( oldFolderPath , newFolderPath , oldFolderName , newFolderName ) ) ;
844
+ yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
845
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
846
+ yield call (
847
+ showSuccessMessage ,
848
+ `Folder ${ newFolderName } successfully renamed in Google Drive.` ,
849
+ 1000
850
+ ) ;
772
851
}
773
852
) ;
774
853
}
0 commit comments