-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfile.ts
62 lines (52 loc) · 1.68 KB
/
file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {stringToBase64} from 'uint8array-extras';
const filePickerOptions: FilePickerOptions = {
types: [
{
accept: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'application/json': '.json',
},
},
],
};
const isModern = typeof showOpenFilePicker === 'function';
async function loadFileOld(): Promise<string> {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
const eventPromise = new Promise<Event>(resolve => {
input.addEventListener('change', resolve, {once: true});
});
input.click();
const event = await eventPromise;
const file = (event.target as HTMLInputElement).files![0];
if (!file) {
throw new Error('No file selected');
}
return file.text();
}
async function saveFileOld(text: string, suggestedName: string): Promise<void> {
// Use data URL because Safari doesn't support saving blob URLs
// Use base64 or else linebreaks are lost
const url = `data:application/json;base64,${stringToBase64(text)}`;
const link = document.createElement('a');
link.download = suggestedName;
link.href = url;
link.click();
}
async function loadFileModern(): Promise<string> {
const [fileHandle] = await showOpenFilePicker(filePickerOptions);
const file = await fileHandle.getFile();
return file.text();
}
async function saveFileModern(text: string, suggestedName: string) {
const fileHandle = await showSaveFilePicker({
...filePickerOptions,
suggestedName,
});
const writable = await fileHandle.createWritable();
await writable.write(text);
await writable.close();
}
export const loadFile = isModern ? loadFileModern : loadFileOld;
export const saveFile = isModern ? saveFileModern : saveFileOld;