aboutsummaryrefslogtreecommitdiff
path: root/src_js/file.ts
blob: 0466339d3b9535bf9a88a233f8eaec090c090021 (plain) (blame)
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
export function load(ext: string): Promise<File | null> {
    const el = document.createElement('input');
    (el as any).style = 'display: none';
    el.type = 'file';
    el.accept = ext;
    document.body.appendChild(el);

    return new Promise<File | null>(resolve => {
        const listener = (evt: Event) => {
            const f = (evt.target as HTMLInputElement).files?.[0] ?? null;
            document.body.removeChild(el);
            resolve(f);
        };
        el.addEventListener('change', listener);
        // TODO blur not fired on close???
        el.addEventListener('blur', listener);
        el.click();
    });
}


export function save(f: File) {
    const a = document.createElement('a');
    a.download = f.name;
    a.rel = 'noopener';
    a.href = URL.createObjectURL(f);
    setTimeout(() => { URL.revokeObjectURL(a.href); }, 20000);
    setTimeout(() => { a.click(); }, 0);
}