All files / lib files.js

0% Statements 0/17
0% Branches 0/15
0% Functions 0/3
0% Lines 0/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                 
/**
 *
 * @param {object} [param0 ]
 * @param {string | string[]} [param0.accept] - accepted file types, e.g. "image/*,.pdf"
 * @param {boolean} [param0.multiple] - allow multiple file selection
 * @returns {Promise<File[]>} - selected files
 */
export async function promptForFiles({ accept = '', multiple = false } = {}) {
	const input = document.createElement('input');
	input.type = 'file';
	if (accept) input.accept = typeof accept === 'string' ? accept : accept.join(',');
	if (multiple) input.multiple = true;
 
	return new Promise((resolve) => {
		input.addEventListener('change', (event) => {
			if (!(event.currentTarget instanceof HTMLInputElement)) return;
			if (!event.currentTarget.files) return;
			const files = Array.from(event.currentTarget.files);
			if (files.length === 0) return;
			resolve(files);
		});
		input.click();
	});
}