Skip to content

Commit 2597204

Browse files
replace import with universal file picker not chrome only
1 parent d50ed62 commit 2597204

File tree

1 file changed

+72
-24
lines changed

1 file changed

+72
-24
lines changed

src/script.js

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -247,32 +247,80 @@ const commands = {
247247
loadVFS();
248248
term.writeln('\r\nVFS loaded from browser storage.');
249249
},
250-
import: async () => {
251-
try {
252-
// Open the file picker
253-
const [fileHandle] = await window.showOpenFilePicker();
254-
const file = await fileHandle.getFile();
255-
if (file.size > 512 * 1024) {
256-
term.writeln('\r\nFile is too large. Maximum size is 512 KB.');
257-
return;
258-
}
259-
const fileContent = await file.text();
260-
//check if valid json
250+
import: () => {
251+
return new Promise((resolve, reject) => {
261252
try {
262-
const importedVFS = JSON.parse(fileContent);
263-
if (typeof importedVFS === 'object' && importedVFS !== null) {
264-
vfs = importedVFS;
265-
cwd_path = '/'; // Reset to root after import
266-
term.writeln('\r\nVFS imported successfully.');
267-
} else {
268-
term.writeln('\r\nInvalid VFS format. Must be a JSON object.');
269-
}
270-
} catch (e) {
271-
term.writeln('\r\nError parsing VFS file. Ensure it is a valid JSON object.');
253+
// Create a hidden file input element
254+
const input = document.createElement('input');
255+
input.type = 'file';
256+
input.accept = '.json';
257+
input.style.display = 'none';
258+
259+
// Handle file selection
260+
input.onchange = async (event) => {
261+
try {
262+
const file = event.target.files[0];
263+
if (!file) {
264+
term.writeln('\r\nNo file selected.');
265+
resolve();
266+
return;
267+
}
268+
269+
if (file.size > 512 * 1024) {
270+
term.writeln('\r\nFile is too large. Maximum size is 512 KB.');
271+
resolve();
272+
return;
273+
}
274+
275+
try {
276+
const fileContent = await file.text();
277+
// Check if valid JSON
278+
try {
279+
const importedVFS = JSON.parse(fileContent);
280+
if (typeof importedVFS === 'object' && importedVFS !== null) {
281+
vfs = importedVFS;
282+
cwd_path = '/'; // Reset to root after import
283+
term.writeln('\r\nVFS imported successfully.');
284+
} else {
285+
term.writeln('\r\nInvalid VFS format. Must be a JSON object.');
286+
}
287+
} catch (e) {
288+
term.writeln('\r\nError parsing VFS file. Ensure it is a valid JSON object.');
289+
}
290+
} catch (error) {
291+
term.writeln('\r\nError reading file: ' + error.message);
292+
}
293+
294+
resolve();
295+
} catch (error) {
296+
term.writeln('\r\nError importing VFS: ' + error.message);
297+
resolve();
298+
} finally {
299+
// Clean up
300+
if (document.body.contains(input)) {
301+
document.body.removeChild(input);
302+
}
303+
}
304+
};
305+
306+
// Handle cancellation
307+
input.oncancel = () => {
308+
term.writeln('\r\nImport cancelled.');
309+
if (document.body.contains(input)) {
310+
document.body.removeChild(input);
311+
}
312+
resolve();
313+
};
314+
315+
// Trigger file picker
316+
document.body.appendChild(input);
317+
input.click();
318+
319+
} catch (error) {
320+
term.writeln('\r\nError importing VFS: ' + error.message);
321+
reject(error);
272322
}
273-
} catch (error) {
274-
term.writeln('\r\nError importing VFS:', error);
275-
}
323+
});
276324
},
277325
export: () => {
278326
const vfsString = JSON.stringify(vfs, null, 2);

0 commit comments

Comments
 (0)