21 lines
798 B
TypeScript
21 lines
798 B
TypeScript
/**
|
|
* 파일 선택창을 띄우고 고른 파일 하나를 돌려준다. 취소하면 아무 일도 없다.
|
|
* 입력은 고르는 동안 문서에 숨겨 붙임(`.cad-pick-file` · 자동 시험 업로드가 닿게) · 고르거나 취소하면 뗌.
|
|
*/
|
|
export function pickFile(accept: string, onPicked: (file: File) => void): void {
|
|
const input = document.createElement('input');
|
|
input.type = 'file';
|
|
input.accept = accept;
|
|
input.hidden = true;
|
|
input.className = 'cad-pick-file';
|
|
document.querySelectorAll('.cad-pick-file').forEach((old) => old.remove());
|
|
input.addEventListener('change', () => {
|
|
const file = input.files?.[0];
|
|
input.remove();
|
|
if (file) onPicked(file);
|
|
});
|
|
input.addEventListener('cancel', () => input.remove());
|
|
document.body.append(input);
|
|
input.click();
|
|
}
|