Skip to content

Commit a2386df

Browse files
committed
Modify folder open for GDrive to load empty folders into BrowserFS
1 parent 9b83a73 commit a2386df

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

src/commons/fileSystem/FileSystemUtils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ export const rmdirRecursively = (fileSystem: FSModule, directoryPath: string): P
225225
export const writeFileRecursively = (
226226
fileSystem: FSModule,
227227
filePath: string,
228-
fileContents: string
228+
fileContents: string,
229+
onlyCreateFolder?: boolean
229230
): Promise<void> => {
230231
return new Promise((resolve, reject) => {
231232
// Create directories along the path if they do not exist.
@@ -255,15 +256,16 @@ export const writeFileRecursively = (
255256
promise.catch(err => reject(err));
256257
}
257258

258-
// Write to the file.
259-
fileSystem.writeFile(filePath, fileContents, err => {
260-
if (err) {
261-
reject(err);
262-
return;
263-
}
264-
265-
resolve();
266-
});
259+
if (!onlyCreateFolder) {
260+
// Write to the file.
261+
fileSystem.writeFile(filePath, fileContents, err => {
262+
if (err) {
263+
reject(err);
264+
return;
265+
}
266+
});
267+
}
268+
resolve();
267269
});
268270
};
269271

src/commons/sagas/PersistenceSaga.tsx

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ export function* persistenceSaga(): SagaIterator {
136136
}
137137

138138
yield call(store.dispatch, actions.disableFileSystemContextMenus());
139-
140-
// Note: for mimeType, text/plain -> file, application/vnd.google-apps.folder -> folder
141-
142139
if (mimeType === MIME_FOLDER) {
143140
// handle folders
144141
toastKey = yield call(showMessage, {
@@ -148,27 +145,20 @@ export function* persistenceSaga(): SagaIterator {
148145
});
149146

150147
const fileList = yield call(getFilesOfFolder, id, name); // this needed the extra scope mimetypes to have every file
151-
// TODO: add type for each resp?
152148
yield call(console.log, 'fileList', fileList);
153149

154150
const fileSystem: FSModule | null = yield select(
155151
(state: OverallState) => state.fileSystem.inBrowserFileSystem
156152
);
157153
// If the file system is not initialised, do nothing.
158154
if (fileSystem === null) {
159-
yield call(console.log, 'no filesystem!');
160-
return;
155+
throw new Error("No filesystem!");
161156
}
162157

163-
// Begin
164-
165-
// rm everything TODO replace everything hardcoded with playground?
166158
yield call(rmFilesInDirRecursively, fileSystem, '/playground');
167-
168-
// clear all persistence files
169159
yield call(store.dispatch, actions.deleteAllPersistenceFiles());
170160

171-
// add tlrf
161+
// add top level root folder
172162
yield put(
173163
actions.addPersistenceFile({
174164
id,
@@ -179,6 +169,8 @@ export function* persistenceSaga(): SagaIterator {
179169
})
180170
);
181171

172+
yield call(store.dispatch, actions.updateRefreshFileViewKey());
173+
182174
for (const currFile of fileList) {
183175
if (currFile.isFolder === true) {
184176
yield call(console.log, 'not file ', currFile);
@@ -191,6 +183,14 @@ export function* persistenceSaga(): SagaIterator {
191183
isFolder: true
192184
})
193185
);
186+
yield call(
187+
writeFileRecursively,
188+
fileSystem,
189+
'/playground' + currFile.path + "/dummy", // workaround to make empty folders
190+
"",
191+
true
192+
);
193+
yield call(store.dispatch, actions.updateRefreshFileViewKey());
194194
continue;
195195
}
196196
yield put(
@@ -206,50 +206,39 @@ export function* persistenceSaga(): SagaIterator {
206206
fileId: currFile.id,
207207
alt: 'media'
208208
});
209-
console.log(currFile.path);
210-
console.log(contents.body === '');
211209
yield call(
212210
writeFileRecursively,
213211
fileSystem,
214212
'/playground' + currFile.path,
215213
contents.body
216214
);
217215
yield call(showSuccessMessage, `Loaded file ${currFile.path}.`, 1000);
216+
yield call(store.dispatch, actions.updateRefreshFileViewKey());
218217
}
219218

220219
// set source to chapter 4 TODO is there a better way of handling this
221220
yield put(
222221
actions.chapterSelect(parseInt('4', 10) as Chapter, Variant.DEFAULT, 'playground')
223222
);
224-
// open folder mode TODO enable button
225-
//yield call(store.dispatch, actions.setFolderMode("playground", true));
226-
yield call(store.dispatch, actions.enableFileSystemContextMenus());
227-
228-
// DDDDDDDDDDDDDDDebug
229-
const test = yield select((state: OverallState) => state.fileSystem.persistenceFileArray);
230-
yield call(console.log, test);
231223

232-
// refresh needed
224+
yield call(store.dispatch, actions.enableFileSystemContextMenus());
233225
yield call(
234226
store.dispatch,
235227
actions.removeEditorTabsForDirectory('playground', WORKSPACE_BASE_PATHS['playground'])
236-
); // TODO hardcoded
237-
// TODO find a file to open instead of deleting all active tabs?
238-
// TODO without modifying WorkspaceReducer in one function this would cause errors - called by onChange of Playground.tsx?
239-
// TODO change behaviour of WorkspaceReducer to not create program.js every time folder mode changes with 0 tabs existing?
240-
yield call(store.dispatch, actions.updateRefreshFileViewKey());
241-
242-
yield call(showSuccessMessage, `Loaded folder ${name}.`, 1000);
228+
);
243229

244-
// TODO does not update playground on loading folder
245-
yield call(console.log, 'ahfdaskjhfkjsadf', parentId);
246230
yield put(
247231
actions.playgroundUpdatePersistenceFolder({ id, name, parentId, lastSaved: new Date() })
248232
);
249233

234+
// delay to increase likelihood addPersistenceFile for last loaded file has completed
235+
// and for the toasts to not overlap
236+
yield call(() => new Promise( resolve => setTimeout(resolve, 1000)));
237+
yield call(showSuccessMessage, `Loaded folder ${name}.`, 1000);
250238
return;
251239
}
252240

241+
// Below is for handling opening of single files
253242
toastKey = yield call(showMessage, {
254243
message: 'Opening file...',
255244
timeout: 0,
@@ -286,15 +275,15 @@ export function* persistenceSaga(): SagaIterator {
286275
)
287276
);
288277
}
289-
290278
yield call(showSuccessMessage, `Loaded ${name}.`, 1000);
291279
} catch (ex) {
292280
console.error(ex);
293-
yield call(showWarningMessage, `Error while opening file.`, 1000);
281+
yield call(showWarningMessage, `Error while opening file/folder.`, 1000);
294282
} finally {
295283
if (toastKey) {
296284
dismiss(toastKey);
297285
}
286+
yield call(store.dispatch, actions.updateRefreshFileViewKey());
298287
}
299288
});
300289

0 commit comments

Comments
 (0)