富文本编辑器上传图片bug

This commit is contained in:
zhanglipeng 2021-01-27 15:34:05 +08:00
parent 2236b55dd1
commit fb5c2e5962
3 changed files with 52 additions and 11 deletions

View File

@ -2,6 +2,7 @@
<div> <div>
<!-- 图片上传组件辅助 --> <!-- 图片上传组件辅助 -->
<el-upload <el-upload
:key="randomString(3)"
class="avatar-uploader quill-img" class="avatar-uploader quill-img"
:action="uploadImgUrl" :action="uploadImgUrl"
name="file" name="file"
@ -15,9 +16,10 @@
<!-- 富文本组件 --> <!-- 富文本组件 -->
<quill-editor <quill-editor
:id="randomString(3)"
class="ql-editor" class="ql-editor"
v-model="content" v-model="content"
ref="quillEditor" :ref="toref"
:options="editorOption" :options="editorOption"
@blur="onEditorBlur($event)" @blur="onEditorBlur($event)"
@focus="onEditorFocus($event)" @focus="onEditorFocus($event)"
@ -59,6 +61,14 @@ export default {
type: Number, type: Number,
default: 4000, //kb default: 4000, //kb
}, },
quillIndex: {
type: Number,
default: 0,
},
toref: {
type: String,
default: "quillEditor",
},
}, },
components: { quillEditor }, components: { quillEditor },
data() { data() {
@ -66,17 +76,18 @@ export default {
content: this.value, content: this.value,
uploadImgUrl: "", uploadImgUrl: "",
editorOption: { editorOption: {
placeholder: "",
theme: "snow", // or 'bubble' theme: "snow", // or 'bubble'
placeholder: "请输入内容", placeholder: "请输入内容",
modules: { modules: {
toolbar: { toolbar: {
container: toolbarOptions, container: toolbarOptions,
handlers: { handlers: {
image: function (value) { image: (value) =>{
if (value) { if (value) {
// input // input
document.querySelector(".quill-img input").click(); // document.querySelector(".quill-img input").click();
const index = this.quillIndex;
document.querySelectorAll(".quill-img input")[index].click();
} else { } else {
this.quill.format("image", false); this.quill.format("image", false);
} }
@ -107,7 +118,17 @@ export default {
// //
this.$emit("input", this.content); this.$emit("input", this.content);
}, },
//
randomString(len) {
//oOLl,9gq,Vv,Uu,I1
var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
var tempLen = chars.length,
tempStr = "";
for (var i = 0; i < len; ++i) {
tempStr += chars.charAt(Math.floor(Math.random() * tempLen));
}
return tempStr;
},
// //
quillImgBefore(file) { quillImgBefore(file) {
let fileType = file.type; let fileType = file.type;
@ -120,15 +141,35 @@ export default {
}, },
quillImgSuccess(res, file) { quillImgSuccess(res, file) {
// // res
// //
// let quill = this.$refs.quillEditor.quill;
// //
// if (res.code == 200) {
// //
// let length = quill.getSelection().index;
// // res.url
// quill.insertEmbed(length, "image", res.url);
// //
// quill.setSelection(length + 1);
// } else {
// this.$message.error("");
// }
// res // res
// //
let quill = this.$refs.quillEditor.quill; let toeval = "this.$refs." + this.toref + ".quill";
let quill = eval(toeval);
// //
if (res.code == 200) { if (res.code == 200) {
// //
let length = quill.getSelection().index; let length = quill.selection.savedRange.index;
// res.url // res.url
quill.insertEmbed(length, "image", res.url); quill.insertEmbed(
length,
"image",
risun.util.nginxServerUrl() + res.fileName
);
// //
quill.setSelection(length + 1); quill.setSelection(length + 1);
} else { } else {

View File

@ -80,7 +80,7 @@ export default {
}, },
data() { data() {
return { return {
title: "幼儿学习与发展档案(教师)", title: "幼儿学习与发展档案(家长)",
classname: "", classname: "",
childid: "", childid: "",
xnxq:"", xnxq:"",
@ -109,7 +109,7 @@ export default {
// //
xnxqOptions: [], xnxqOptions: [],
// //
title: "", // title: "",
// //
open: false, open: false,
// //

View File

@ -383,7 +383,7 @@ export default {
const id = row.id || this.ids; const id = row.id || this.ids;
getActivity(id).then(response => { getActivity(id).then(response => {
this.form = response.data; this.form = response.data;
console.log(this.form); // console.log(this.form);
this.typeOptions = response.types; this.typeOptions = response.types;
this.form.type = response.typeIds; this.form.type = response.typeIds;