@@ -258,6 +258,13 @@ export function* persistenceSaga(): SagaIterator {
258
258
259
259
yield takeLatest ( PERSISTENCE_SAVE_FILE_AS , function * ( ) : any {
260
260
let toastKey : string | undefined ;
261
+ const persistenceFileArray : PersistenceFile [ ] = yield select ( ( state : OverallState ) => state . fileSystem . persistenceFileArray ) ;
262
+ const [ currPersistenceFile ] = yield select (
263
+ ( state : OverallState ) => [
264
+ state . playground . persistenceFile
265
+ ]
266
+ ) ;
267
+ yield call ( console . log , "currpersfile " , currPersistenceFile ) ;
261
268
try {
262
269
yield call ( ensureInitialisedAndAuthorised ) ;
263
270
@@ -313,6 +320,58 @@ export function* persistenceSaga(): SagaIterator {
313
320
if ( ! reallyOverwrite ) {
314
321
return ;
315
322
}
323
+
324
+ yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
325
+ // Case: Picked a file to overwrite
326
+ if ( currPersistenceFile && currPersistenceFile . isFolder ) {
327
+ yield call ( console . log , "folder opened, handling save_as differently! overwriting file" ) ;
328
+ // First case: Chosen location is within TLRF - so need to call methods to update PersistenceFileArray
329
+ // Other case: Chosen location is outside TLRF - don't care
330
+
331
+ yield call ( console . log , "curr pers file " , currPersistenceFile , " pickedDir " , pickedDir , " pickedFile " , pickedFile ) ;
332
+ const localFileTarget = persistenceFileArray . find ( e => e . id === pickedFile . id ) ;
333
+ if ( localFileTarget ) {
334
+ toastKey = yield call ( showMessage , {
335
+ message : `Saving as ${ localFileTarget . name } ...` ,
336
+ timeout : 0 ,
337
+ intent : Intent . PRIMARY
338
+ } ) ;
339
+ // identical to just saving a file locally
340
+ const fileSystem : FSModule | null = yield select (
341
+ ( state : OverallState ) => state . fileSystem . inBrowserFileSystem
342
+ ) ;
343
+ if ( fileSystem === null ) {
344
+ yield call ( console . log , "no filesystem!" ) ;
345
+ throw new Error ( "No filesystem" ) ;
346
+ }
347
+
348
+ // Save to GDrive
349
+ const [ chapter , variant , external ] = yield select (
350
+ ( state : OverallState ) => [
351
+ state . workspaces . playground . context . chapter ,
352
+ state . workspaces . playground . context . variant ,
353
+ state . workspaces . playground . externalLibrary
354
+ ]
355
+ ) ;
356
+ const config : IPlaygroundConfig = {
357
+ chapter,
358
+ variant,
359
+ external
360
+ } ;
361
+
362
+ yield call ( updateFile , localFileTarget . id , localFileTarget . name , MIME_SOURCE , code , config ) ;
363
+
364
+ yield put ( actions . addPersistenceFile ( { ...localFileTarget , lastSaved : new Date ( ) , lastEdit : undefined } ) ) ;
365
+ yield call ( writeFileRecursively , fileSystem , localFileTarget . path ! , code ) ;
366
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
367
+ }
368
+ yield call (
369
+ showSuccessMessage ,
370
+ `${ pickedFile . name } successfully saved to Google Drive.` ,
371
+ 1000
372
+ ) ;
373
+ return ;
374
+ }
316
375
yield put ( actions . playgroundUpdatePersistenceFile ( pickedFile ) ) ;
317
376
yield put ( actions . persistenceSaveFile ( pickedFile ) ) ;
318
377
} else {
@@ -340,6 +399,8 @@ export function* persistenceSaga(): SagaIterator {
340
399
return ;
341
400
}
342
401
402
+ yield call ( store . dispatch , actions . disableFileSystemContextMenus ( ) ) ;
403
+
343
404
const config : IPlaygroundConfig = {
344
405
chapter,
345
406
variant,
@@ -352,7 +413,7 @@ export function* persistenceSaga(): SagaIterator {
352
413
intent : Intent . PRIMARY
353
414
} ) ;
354
415
355
- const newFile = yield call (
416
+ const newFile : PersistenceFile = yield call (
356
417
createFile ,
357
418
response . value ,
358
419
saveToDir . id ,
@@ -361,6 +422,45 @@ export function* persistenceSaga(): SagaIterator {
361
422
config
362
423
) ;
363
424
425
+ //Case: Chose to save as a new file
426
+ if ( currPersistenceFile && currPersistenceFile . isFolder ) {
427
+ yield call ( console . log , "folder opened, handling save_as differently! saving as new file" ) ;
428
+ // First case: Chosen location is within TLRF - so need to call methods to update PersistenceFileArray
429
+ // Other case: Chosen location is outside TLRF - don't care
430
+
431
+ yield call ( console . log , "curr persFileArr " , persistenceFileArray , " pickedDir " , pickedDir , " pickedFile " , pickedFile , " saveToDir " , saveToDir ) ;
432
+ let needToUpdateLocal = false ;
433
+ let localFolderTarget : PersistenceFile ;
434
+ for ( let i = 0 ; i < persistenceFileArray . length ; i ++ ) {
435
+ if ( persistenceFileArray [ i ] . isFolder && persistenceFileArray [ i ] . id === saveToDir . id ) {
436
+ needToUpdateLocal = true ;
437
+ localFolderTarget = persistenceFileArray [ i ] ;
438
+ break ;
439
+ }
440
+ }
441
+
442
+ if ( needToUpdateLocal ) {
443
+ const fileSystem : FSModule | null = yield select (
444
+ ( state : OverallState ) => state . fileSystem . inBrowserFileSystem
445
+ ) ;
446
+ if ( fileSystem === null ) {
447
+ yield call ( console . log , "no filesystem!" ) ;
448
+ throw new Error ( "No filesystem" ) ;
449
+ }
450
+ const newPath = localFolderTarget ! . path + "/" + response . value ;
451
+ yield put ( actions . addPersistenceFile ( { ...newFile , lastSaved : new Date ( ) , path : newPath } ) ) ;
452
+ yield call ( writeFileRecursively , fileSystem , newPath , code ) ;
453
+ yield call ( store . dispatch , actions . updateRefreshFileViewKey ( ) ) ;
454
+ }
455
+
456
+ yield call (
457
+ showSuccessMessage ,
458
+ `${ response . value } successfully saved to Google Drive.` ,
459
+ 1000
460
+ ) ;
461
+ return ;
462
+ }
463
+
364
464
yield put ( actions . playgroundUpdatePersistenceFile ( { ...newFile , lastSaved : new Date ( ) } ) ) ;
365
465
yield call (
366
466
showSuccessMessage ,
@@ -375,6 +475,7 @@ export function* persistenceSaga(): SagaIterator {
375
475
if ( toastKey ) {
376
476
dismiss ( toastKey ) ;
377
477
}
478
+ yield call ( store . dispatch , actions . enableFileSystemContextMenus ( ) ) ;
378
479
}
379
480
} ) ;
380
481
0 commit comments