utilium 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dom.d.ts +9 -0
- package/dist/dom.js +29 -0
- package/package.json +1 -1
- package/readme.md +1 -1
- package/src/dom.ts +32 -0
package/dist/dom.d.ts
ADDED
package/dist/dom.js
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
2
|
+
/**
|
3
|
+
* Upload a file
|
4
|
+
* @todo use Promise.withResolvers
|
5
|
+
*/
|
6
|
+
export function upload(type, multiple = false) {
|
7
|
+
return new Promise((resolve, reject) => {
|
8
|
+
const input = document.createElement('input');
|
9
|
+
input.type = 'file';
|
10
|
+
if (type)
|
11
|
+
input.accept = type;
|
12
|
+
if (multiple)
|
13
|
+
input.multiple = true;
|
14
|
+
input.addEventListener('change', e => {
|
15
|
+
const file = input.files?.[0];
|
16
|
+
file ? resolve(file) : reject(new ReferenceError('No files uploaded'));
|
17
|
+
});
|
18
|
+
input.click();
|
19
|
+
});
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
* Downloads some data
|
23
|
+
*/
|
24
|
+
export function download(data, name) {
|
25
|
+
const link = document.createElement('a');
|
26
|
+
link.href = URL.createObjectURL(new Blob([data]));
|
27
|
+
link.download = name;
|
28
|
+
link.click();
|
29
|
+
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
package/src/dom.ts
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-expressions */
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Upload a file
|
5
|
+
* @todo use Promise.withResolvers
|
6
|
+
*/
|
7
|
+
export function upload(type: string, multiple = false): Promise<File> {
|
8
|
+
return new Promise<File>((resolve, reject) => {
|
9
|
+
const input = document.createElement('input');
|
10
|
+
input.type = 'file';
|
11
|
+
if (type) input.accept = type;
|
12
|
+
|
13
|
+
if (multiple) input.multiple = true;
|
14
|
+
|
15
|
+
input.addEventListener('change', e => {
|
16
|
+
const file = input.files?.[0];
|
17
|
+
file ? resolve(file) : reject(new ReferenceError('No files uploaded'));
|
18
|
+
});
|
19
|
+
|
20
|
+
input.click();
|
21
|
+
});
|
22
|
+
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Downloads some data
|
26
|
+
*/
|
27
|
+
export function download(data: BlobPart, name: string): void {
|
28
|
+
const link = document.createElement('a');
|
29
|
+
link.href = URL.createObjectURL(new Blob([data]));
|
30
|
+
link.download = name;
|
31
|
+
link.click();
|
32
|
+
}
|