2

我正在尝试扩展 Quill 中的图像功能。

我收到一个错误

Uncaught ReferenceError: Emitter is not defined at FileReader.reader.onload

所以我尝试像这样导入发射器:

var Emitter = Quill.import('core/emitter');

但后来我得到另一个错误:

quill Cannot import emitter. Are you sure it was registered?

阅读文档我不知道如何解决这个问题。

这是我正在使用的代码:

function imageHandler() {
        var Delta = Quill.import('delta');
        var Emitter = Quill.import('core/emitter');
        let fileInput = this.container.querySelector('input.ql-image[type=file]');
        if (fileInput == null) {
        fileInput = document.createElement('input');
        fileInput.setAttribute('type', 'file');
        fileInput.setAttribute('accept', 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon');
        fileInput.classList.add('ql-image');
        fileInput.addEventListener('change', () => {
            if (fileInput.files != null && fileInput.files[0] != null) {
                var file_size = fileInput.files[0].size;
                console.log('Image size: '+file_size);
                let reader = new FileReader();
                reader.onload = (e) => {
                    let range = this.quill.getSelection(true);
                    this.quill.updateContents(new Delta()
                    .retain(range.index)
                    .delete(range.length)
                    .insert({ image: e.target.result })
                    , Emitter.sources.USER);
                    this.quill.setSelection(range.index + 1, Emitter.sources.SILENT);
                    fileInput.value = "";
                }
                reader.readAsDataURL(fileInput.files[0]);
            }
        });
        this.container.appendChild(fileInput);
        }
        fileInput.click();
    }
4

0 回答 0