@@ -22,7 +22,6 @@ import {
22
22
} from '../../features/persistence/PersistenceTypes' ;
23
23
import { store } from '../../pages/createStore' ;
24
24
import { OverallState } from '../application/ApplicationTypes' ;
25
- import { ExternalLibraryName } from '../application/types/ExternalTypes' ;
26
25
import { LOGIN_GOOGLE , LOGOUT_GOOGLE } from '../application/types/SessionTypes' ;
27
26
import {
28
27
retrieveFilesInWorkspaceAsRecord ,
@@ -107,6 +106,13 @@ export function* persistenceSaga(): SagaIterator {
107
106
let toastKey : string | undefined ;
108
107
try {
109
108
yield call ( ensureInitialisedAndAuthorised ) ;
109
+ const fileSystem : FSModule | null = yield select (
110
+ ( state : OverallState ) => state . fileSystem . inBrowserFileSystem
111
+ ) ;
112
+ // If the file system is not initialised, do nothing.
113
+ if ( fileSystem === null ) {
114
+ throw new Error ( "No filesystem!" ) ;
115
+ }
110
116
const { id, name, mimeType, picked, parentId } = yield call (
111
117
pickFile ,
112
118
'Pick a file/folder to open' ,
@@ -125,7 +131,7 @@ export function* persistenceSaga(): SagaIterator {
125
131
contents : (
126
132
< p >
127
133
Opening < strong > { name } </ strong > will overwrite the current contents of your workspace.
128
- Are you sure?
134
+ All local files/folders will be deleted. Are you sure?
129
135
</ p >
130
136
) ,
131
137
positiveLabel : 'Open' ,
@@ -147,14 +153,6 @@ export function* persistenceSaga(): SagaIterator {
147
153
const fileList = yield call ( getFilesOfFolder , id , name ) ; // this needed the extra scope mimetypes to have every file
148
154
yield call ( console . log , 'fileList' , fileList ) ;
149
155
150
- const fileSystem : FSModule | null = yield select (
151
- ( state : OverallState ) => state . fileSystem . inBrowserFileSystem
152
- ) ;
153
- // If the file system is not initialised, do nothing.
154
- if ( fileSystem === null ) {
155
- throw new Error ( "No filesystem!" ) ;
156
- }
157
-
158
156
yield call ( rmFilesInDirRecursively , fileSystem , '/playground' ) ;
159
157
yield call ( store . dispatch , actions . deleteAllPersistenceFiles ( ) ) ;
160
158
@@ -221,14 +219,13 @@ export function* persistenceSaga(): SagaIterator {
221
219
actions . chapterSelect ( parseInt ( '4' , 10 ) as Chapter , Variant . DEFAULT , 'playground' )
222
220
) ;
223
221
224
- yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
225
222
yield call (
226
223
store . dispatch ,
227
224
actions . removeEditorTabsForDirectory ( 'playground' , WORKSPACE_BASE_PATHS [ 'playground' ] )
228
225
) ;
229
226
230
227
yield put (
231
- actions . playgroundUpdatePersistenceFolder ( { id, name, parentId, lastSaved : new Date ( ) } )
228
+ actions . playgroundUpdatePersistenceFile ( { id, name, parentId, lastSaved : new Date ( ) , isFolder : true } )
232
229
) ;
233
230
234
231
// delay to increase likelihood addPersistenceFile for last loaded file has completed
@@ -245,36 +242,32 @@ export function* persistenceSaga(): SagaIterator {
245
242
intent : Intent . PRIMARY
246
243
} ) ;
247
244
248
- const { result : meta } = yield call ( [ gapi . client . drive . files , 'get' ] , {
249
- // get fileid here using gapi.client.drive.files
250
- fileId : id ,
251
- fields : 'appProperties'
252
- } ) ;
253
245
const contents = yield call ( [ gapi . client . drive . files , 'get' ] , { fileId : id , alt : 'media' } ) ;
254
- const activeEditorTabIndex : number | null = yield select (
255
- ( state : OverallState ) => state . workspaces . playground . activeEditorTabIndex
246
+
247
+ yield call ( rmFilesInDirRecursively , fileSystem , '/playground' ) ;
248
+ yield call ( store . dispatch , actions . deleteAllPersistenceFiles ( ) ) ;
249
+
250
+ // add file to BrowserFS
251
+ yield call (
252
+ writeFileRecursively ,
253
+ fileSystem ,
254
+ '/playground/' + name ,
255
+ contents . body
256
256
) ;
257
- if ( activeEditorTabIndex === null ) {
258
- throw new Error ( 'No active editor tab found.' ) ;
259
- }
260
- yield put ( actions . updateEditorValue ( 'playground' , activeEditorTabIndex , contents . body ) ) ; // CONTENTS OF SELECTED FILE LOADED HERE
261
- yield put ( actions . playgroundUpdatePersistenceFile ( { id, name, lastSaved : new Date ( ) } ) ) ;
262
- if ( meta && meta . appProperties ) {
263
- yield put (
264
- actions . chapterSelect (
265
- parseInt ( meta . appProperties . chapter || '4' , 10 ) as Chapter ,
266
- meta . appProperties . variant || Variant . DEFAULT ,
267
- 'playground'
268
- )
269
- ) ;
270
- yield put (
271
- actions . externalLibrarySelect (
272
- Object . values ( ExternalLibraryName ) . find ( v => v === meta . appProperties . external ) ||
273
- ExternalLibraryName . NONE ,
274
- 'playground'
275
- )
276
- ) ;
277
- }
257
+ // update playground PersistenceFile
258
+ const newPersistenceFile = { id, name, lastSaved : new Date ( ) , path : '/playground/' + name } ;
259
+ yield put ( actions . playgroundUpdatePersistenceFile ( newPersistenceFile ) ) ;
260
+ // add file to persistenceFileArray
261
+ yield put ( actions . addPersistenceFile ( newPersistenceFile ) ) ;
262
+
263
+ yield call (
264
+ store . dispatch ,
265
+ actions . removeEditorTabsForDirectory ( 'playground' , WORKSPACE_BASE_PATHS [ 'playground' ] )
266
+ ) ;
267
+
268
+ // delay to increase likelihood addPersistenceFile for last loaded file has completed
269
+ // and for the toasts to not overlap
270
+ yield call ( ( ) => new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ) ;
278
271
yield call ( showSuccessMessage , `Loaded ${ name } .` , 1000 ) ;
279
272
} catch ( ex ) {
280
273
console . error ( ex ) ;
@@ -283,6 +276,7 @@ export function* persistenceSaga(): SagaIterator {
283
276
if ( toastKey ) {
284
277
dismiss ( toastKey ) ;
285
278
}
279
+ yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
286
280
yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
287
281
}
288
282
} ) ;
@@ -310,7 +304,8 @@ export function* persistenceSaga(): SagaIterator {
310
304
) ;
311
305
312
306
if ( activeEditorTabIndex === null ) {
313
- throw new Error ( 'No active editor tab found.' ) ;
307
+ yield call ( showWarningMessage , `Please open an editor tab.` , 1000 ) ;
308
+ return ;
314
309
}
315
310
const code = editorTabs [ activeEditorTabIndex ] . value ;
316
311
@@ -420,11 +415,12 @@ export function* persistenceSaga(): SagaIterator {
420
415
) ;
421
416
if ( areAllFilesSavedGoogleDrive ( updatedPersistenceFileArray ) ) {
422
417
yield put (
423
- actions . playgroundUpdatePersistenceFolder ( {
418
+ actions . playgroundUpdatePersistenceFile ( {
424
419
id : currPersistenceFile . id ,
425
420
name : currPersistenceFile . name ,
426
421
parentId : currPersistenceFile . parentId ,
427
- lastSaved : new Date ( )
422
+ lastSaved : new Date ( ) ,
423
+ isFolder : true
428
424
} )
429
425
) ;
430
426
}
@@ -438,8 +434,10 @@ export function* persistenceSaga(): SagaIterator {
438
434
) ;
439
435
return ;
440
436
}
441
- yield put ( actions . playgroundUpdatePersistenceFile ( pickedFile ) ) ;
442
- yield put ( actions . persistenceSaveFile ( pickedFile ) ) ;
437
+ // Single file mode case
438
+ const singleFileModePersFile : PersistenceFile = { ...pickedFile , lastSaved : new Date ( ) , path : '/playground/' + pickedFile . name } ;
439
+ yield put ( actions . playgroundUpdatePersistenceFile ( singleFileModePersFile ) ) ;
440
+ yield put ( actions . persistenceSaveFile ( singleFileModePersFile ) ) ;
443
441
} else {
444
442
const response : AsyncReturnType < typeof showSimplePromptDialog > = yield call (
445
443
showSimplePromptDialog ,
@@ -465,8 +463,6 @@ export function* persistenceSaga(): SagaIterator {
465
463
return ;
466
464
}
467
465
468
- // yield call(store.dispatch, actions.disableFileSystemContextMenus());
469
-
470
466
const config : IPlaygroundConfig = {
471
467
chapter,
472
468
variant,
@@ -542,7 +538,21 @@ export function* persistenceSaga(): SagaIterator {
542
538
return ;
543
539
}
544
540
545
- yield put ( actions . playgroundUpdatePersistenceFile ( { ...newFile , lastSaved : new Date ( ) } ) ) ;
541
+
542
+ // Single file case
543
+ const newPersFile : PersistenceFile = { ...newFile , lastSaved : new Date ( ) , path : "/playground/" + newFile . name } ;
544
+ if ( ! currPersistenceFile ) { // no file loaded prior
545
+ // update playground pers file
546
+ yield put ( actions . playgroundUpdatePersistenceFile ( newPersFile ) ) ;
547
+ // add new pers file to persFileArray
548
+ } else { // file loaded prior
549
+ // update playground pers file
550
+ yield put ( actions . playgroundUpdatePersistenceFile ( newPersFile ) ) ;
551
+ // remove old pers file from persFileArray
552
+ // add new pers file to persFileArray
553
+ }
554
+
555
+ //
546
556
yield call (
547
557
showSuccessMessage ,
548
558
`${ response . value } successfully saved to Google Drive.` ,
@@ -760,11 +770,12 @@ export function* persistenceSaga(): SagaIterator {
760
770
}
761
771
762
772
yield put (
763
- actions . playgroundUpdatePersistenceFolder ( {
773
+ actions . playgroundUpdatePersistenceFile ( {
764
774
id : topLevelFolderId ,
765
775
name : topLevelFolderName ,
766
776
parentId : saveToDir . id ,
767
- lastSaved : new Date ( )
777
+ lastSaved : new Date ( ) ,
778
+ isFolder : true
768
779
} )
769
780
) ;
770
781
@@ -860,11 +871,12 @@ export function* persistenceSaga(): SagaIterator {
860
871
}
861
872
862
873
yield put (
863
- actions . playgroundUpdatePersistenceFolder ( {
874
+ actions . playgroundUpdatePersistenceFile ( {
864
875
id : currFolderObject . id ,
865
876
name : currFolderObject . name ,
866
877
parentId : currFolderObject . parentId ,
867
- lastSaved : new Date ( )
878
+ lastSaved : new Date ( ) ,
879
+ isFolder : true
868
880
} )
869
881
) ;
870
882
yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
@@ -891,7 +903,7 @@ export function* persistenceSaga(): SagaIterator {
891
903
yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
892
904
let toastKey : string | undefined ;
893
905
894
- const [ currFolderObject ] = yield select (
906
+ const [ playgroundPersistenceFile ] = yield select (
895
907
( state : OverallState ) => [ state . playground . persistenceFile ]
896
908
) ;
897
909
@@ -908,8 +920,9 @@ export function* persistenceSaga(): SagaIterator {
908
920
) ;
909
921
910
922
try {
911
- if ( activeEditorTabIndex === null ) {
912
- throw new Error ( 'No active editor tab found.' ) ;
923
+ if ( activeEditorTabIndex === null && ! playgroundPersistenceFile . isFolder ) {
924
+ yield call ( showWarningMessage , `Please have ${ name } open as the active editor tab.` , 1000 ) ;
925
+ return ;
913
926
}
914
927
const code = editorTabs [ activeEditorTabIndex ] . value ;
915
928
@@ -918,7 +931,7 @@ export function* persistenceSaga(): SagaIterator {
918
931
variant,
919
932
external
920
933
} ;
921
- if ( ( currFolderObject as PersistenceFile ) . isFolder ) {
934
+ if ( ( playgroundPersistenceFile as PersistenceFile ) . isFolder ) {
922
935
yield call ( console . log , 'folder opened! updating pers specially' ) ;
923
936
const persistenceFileArray : PersistenceFile [ ] = yield select (
924
937
( state : OverallState ) => state . fileSystem . persistenceFileArray
@@ -957,27 +970,36 @@ export function* persistenceSaga(): SagaIterator {
957
970
) ;
958
971
if ( areAllFilesSavedGoogleDrive ( updatedPersistenceFileArray ) ) {
959
972
yield put (
960
- actions . playgroundUpdatePersistenceFolder ( {
961
- id : currFolderObject . id ,
962
- name : currFolderObject . name ,
963
- parentId : currFolderObject . parentId ,
964
- lastSaved : new Date ( )
973
+ actions . playgroundUpdatePersistenceFile ( {
974
+ id : playgroundPersistenceFile . id ,
975
+ name : playgroundPersistenceFile . name ,
976
+ parentId : playgroundPersistenceFile . parentId ,
977
+ lastSaved : new Date ( ) ,
978
+ isFolder : true
965
979
} )
966
980
) ;
967
981
}
968
982
969
983
return ;
970
984
}
971
985
986
+ if ( ( editorTabs [ activeEditorTabIndex ] as EditorTabState ) . filePath !== playgroundPersistenceFile . path ) {
987
+ yield call ( showWarningMessage , `Please have ${ name } open as the active editor tab.` , 1000 ) ;
988
+ return ;
989
+ }
990
+
972
991
toastKey = yield call ( showMessage , {
973
992
message : `Saving as ${ name } ...` ,
974
993
timeout : 0 ,
975
994
intent : Intent . PRIMARY
976
995
} ) ;
977
996
978
997
yield call ( updateFile , id , name , MIME_SOURCE , code , config ) ;
979
- yield put ( actions . playgroundUpdatePersistenceFile ( { id, name, lastSaved : new Date ( ) } ) ) ;
998
+ const updatedPlaygroundPersFile = { ...playgroundPersistenceFile , lastSaved : new Date ( ) } ;
999
+ yield put ( actions . addPersistenceFile ( updatedPlaygroundPersFile ) ) ;
1000
+ yield put ( actions . playgroundUpdatePersistenceFile ( updatedPlaygroundPersFile ) ) ;
980
1001
yield call ( showSuccessMessage , `${ name } successfully saved to Google Drive.` , 1000 ) ;
1002
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
981
1003
} catch ( ex ) {
982
1004
console . error ( ex ) ;
983
1005
yield call ( showWarningMessage , `Error while saving file.` , 1000 ) ;
@@ -1312,7 +1334,7 @@ export function* persistenceSaga(): SagaIterator {
1312
1334
if ( currFolderObject . name === oldFolderName ) {
1313
1335
// update playground PersistenceFile
1314
1336
yield put (
1315
- actions . playgroundUpdatePersistenceFolder ( { ...currFolderObject , name : newFolderName } )
1337
+ actions . playgroundUpdatePersistenceFile ( { ...currFolderObject , name : newFolderName , isFolder : true } )
1316
1338
) ;
1317
1339
}
1318
1340
} catch ( ex ) {
0 commit comments