薄穆
//重写编辑器的图片预览方法
var toolbar = quill.getModule('toolbar');
toolbar.addHandler('image',function(){
var 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', function () {
if (fileInput.files != null && fileInput.files[0] != null) {
var formData = new FormData();
formData.append('file', fileInput.files[0]);
$.ajax({
url: '/upload',
type: 'POST',
cache: false,
data: formData,
processData: false,
contentType: false
}).done(function(res) {
var range=quill.getSelection(true);
quill.insertEmbed(range.index, 'image', "/public/upload/"+res.url);
quill.setSelection(range.index+1);
}).fail(function(res) {});
}
});
this.container.appendChild(fileInput);
}
fileInput.click();
});