富文本粘贴图片上传到服务器

dev_2022-05-11
jialong.yu 3 years ago
parent 3abbbaf09b
commit d6f052d4e2
  1. 57
      src/components/quill/index.vue

@ -139,39 +139,66 @@ export default {
this.Quill = null;
},
methods: {
init() {
init () {
const editor = this.$refs.editor;
//
this.Quill = new Quill(editor, this.options);
const ins = this.Quill
//
this.Quill.pasteHTML(this.currentValue);
if (this.toTop) {
ins.pasteHTML(this.currentValue);
if(this.toTop){
this.$nextTick(() => {
window.scrollTo(0, 0);
});
window.scrollTo(0,0)
})
}
//
this.Quill.on("text-change", (delta, oldDelta, source) => {
ins.on('text-change', (delta, oldDelta, source) => {
const html = this.$refs.editor.children[0].innerHTML;
const text = this.Quill.getText();
const text = ins.getText();
const quill = this.Quill;
//
this.currentValue = html;
// v-model
this.$emit("input", html);
this.$emit('input', html);
//
this.$emit("on-change", { html, text, quill });
this.$emit('on-change', { html, text, quill });
});
// quill
this.Quill.on("text-change", (delta, oldDelta, source) => {
this.$emit("on-text-change", delta, oldDelta, source);
ins.on('text-change', (delta, oldDelta, source) => {
this.$emit('on-text-change', delta, oldDelta, source);
});
this.Quill.on("selection-change", (range, oldRange, source) => {
this.$emit("on-selection-change", range, oldRange, source);
ins.on('selection-change', (range, oldRange, source) => {
this.$emit('on-selection-change', range, oldRange, source);
});
this.Quill.on("editor-change", (eventName, ...args) => {
this.$emit("on-editor-change", eventName, ...args);
ins.on('editor-change', (eventName, ...args) => {
this.$emit('on-editor-change', eventName, ...args);
});
//
ins.root.addEventListener('paste', evt => {
if (evt.clipboardData && evt.clipboardData.files && evt.clipboardData.files.length) {
evt.preventDefault();
//
[].forEach.call(evt.clipboardData.files, file => {
if (!file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) {
return
}
const param = new FormData()
param.append('file', file)
// base64
this.$post(this.api.fileupload, param, {
headers: { "Content-Type": "multipart/form-data" }
}).then(res => {
var range = ins.getSelection()
if (range) {
//
ins.insertEmbed(range.index, 'image', res.data.filesResult.fileUrl)
//
ins.setSelection(range.index + 1)
}
}).catch(res => {})
});
}
}, false)
},
beforeUpload(file) {
this.loading = true;

Loading…
Cancel
Save