-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
|
||
/** | ||
* Upload a file | ||
* @todo use Promise.withResolvers | ||
*/ | ||
export function upload(type: string, multiple = false): Promise<File> { | ||
return new Promise<File>((resolve, reject) => { | ||
const input = document.createElement('input'); | ||
input.type = 'file'; | ||
if (type) input.accept = type; | ||
|
||
if (multiple) input.multiple = true; | ||
|
||
input.addEventListener('change', e => { | ||
const file = input.files[0]; | ||
Check failure on line 16 in src/dom.ts GitHub Actions / ubuntu-latest
|
||
file ? resolve(file) : reject(new ReferenceError('No files uploaded')); | ||
}); | ||
|
||
input.click(); | ||
}); | ||
} | ||
|
||
/** | ||
* Downloads some data | ||
*/ | ||
export function download(data: BlobPart, name: string): void { | ||
const link = document.createElement('a'); | ||
link.href = URL.createObjectURL(new Blob([data])); | ||
link.download = name; | ||
link.click(); | ||
} |