import {FileQuillWatch} from './FileQuillWatch.js'
/**
 * @description 点击文件上传
 */
export function fileHandler() {
    let fileInput = document.querySelector('.quill-file-input');
    if (fileInput === null) {
        fileInput = document.createElement('input');
        fileInput.setAttribute('type', 'file');
        fileInput.setAttribute('accept', '.xlsx,.xls,.pdf,.doc,.docx,.jpg,.png,.zip,.ppt,.wps,.txt');
        fileInput.setAttribute('title', '附件上传');
        fileInput.classList.add('quill-file-input');
        fileInput.style.display = 'none'
        // 监听选择文件
        fileInput.addEventListener('change', function () {
            let self = FileQuillWatch.active
            self.file = fileInput.files[0]
            fileInput.value = ''
            // 如果图片限制大小
            if (self.config.size && self.file.size >= self.config.size * 1024 * 1024 && self.config.loading) {
                self.quillLoading.classList.remove('extend-upload-success')
                self.quillLoading.classList.add('extend-upload-warning-color')
                self.quillLoading.innerHTML = '文件大小超过限制'
                setTimeout(function () {
                    self.quillLoading.classList.remove('extend-upload-warning-color')
                    self.quillLoading.classList.add('extend-upload-success')
                }, 1500)
                return
            }
            self.uploadFile(self.config.change);
        })
        document.body.appendChild(fileInput);
    }
    fileInput.click();
}

Quill.register('modules/fileHandler', fileHandler)