@zodash/open-file
Version:
Open file dialog to select file in browser
55 lines (54 loc) • 1.79 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.openFile = void 0;
/**
*
* @param options Open file dialog to select file in browser
* @returns
*/
function openFile(options) {
return new Promise((resolve) => {
const input = document.createElement('input');
//
input.style.position = 'fixed';
input.style.bottom = '0';
input.style.left = '0';
input.style.visibility = 'hidden';
//
input.setAttribute('type', 'file');
if (options === null || options === void 0 ? void 0 : options.accept) {
input.setAttribute('accept', options.accept);
}
else if (options === null || options === void 0 ? void 0 : options.mimeTypes) {
const accept = options.mimeTypes.join(',');
input.setAttribute('accept', accept);
}
if (options === null || options === void 0 ? void 0 : options.multiple) {
input.setAttribute('multiple', '');
}
if (options === null || options === void 0 ? void 0 : options.isDirectory) {
input.setAttribute('directory', 'directory');
input.setAttribute('webkitdirectory', 'webkitdirectory');
}
//
function onChange() {
clean();
resolve(input.files);
}
function mount() {
document.body.appendChild(input);
input.addEventListener('change', onChange);
}
function clean() {
input.removeEventListener('change', onChange);
document.body.removeChild(input);
}
function open() {
mount();
input.click();
}
open();
});
}
exports.openFile = openFile;
exports.default = openFile;
;