update 优化 富文本Editor组件检验图片格式

This commit is contained in:
疯狂的狮子Li 2023-10-09 10:34:58 +08:00
parent 81dcd8b512
commit e35df6d1d2
2 changed files with 26 additions and 11 deletions

View File

@ -9,14 +9,13 @@
name="file"
:show-file-list="false"
:headers="headers"
style="display: none"
ref="uploadRef"
v-if="type == 'url'"
>
</el-upload>
<div class="editor">
<quill-editor
ref="myQuillEditor"
ref="quillEditorRef"
v-model:content="content"
contentType="html"
@textChange="(e) => $emit('update:modelValue', content)"
@ -68,7 +67,7 @@ const { proxy } = getCurrentInstance();
//
const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/system/oss/upload");
const headers = ref({ Authorization: "Bearer " + getToken() });
const myQuillEditor = ref();
const quillEditorRef = ref();
const options = ref({
theme: "snow",
@ -101,7 +100,7 @@ const options = ref({
},
}
},
placeholder: '请输入内容',
placeholder: "请输入内容",
readOnly: props.readOnly,
});
@ -114,7 +113,7 @@ const styles = computed(() => {
style.height = `${props.height}px`;
}
return style;
})
});
const content = ref("");
watch(() => props.modelValue, (v) => {
@ -125,10 +124,10 @@ watch(() => props.modelValue, (v) => {
//
function handleUploadSuccess(res, file) {
//
let quill = toRaw(myQuillEditor.value).getQuill();
//
if (res.code == 200) {
//
let quill = toRaw(myQuillEditor.value).getQuill();
//
let length = quill.selection.savedRange.index;
// res
@ -144,6 +143,13 @@ function handleUploadSuccess(res, file) {
//
function handleBeforeUpload(file) {
const type = ["image/jpeg", "image/jpg", "image/png", "image/svg"];
const isJPG = type.includes(file.type);
//
if (!isJPG) {
proxy.$modal.msgError(`图片格式错误!`);
return false;
}
//
if (props.fileSize) {
const isLt = file.size / 1024 / 1024 < props.fileSize;
@ -164,6 +170,9 @@ function handleUploadError(err) {
</script>
<style>
.editor-img-uploader {
display: none;
}
.editor, .ql-toolbar {
white-space: pre-wrap !important;
line-height: normal !important;

View File

@ -47,7 +47,7 @@ export default {
type: Boolean,
default: false,
},
// (MB)
/* 上传文件大小限制(MB) */
fileSize: {
type: Number,
default: 5,
@ -129,7 +129,6 @@ export default {
if (this.type == 'url') {
let toolbar = this.Quill.getModule("toolbar");
toolbar.addHandler("image", (value) => {
this.uploadType = "image";
if (value) {
this.$refs.upload.$children[0].$refs.input.click();
} else {
@ -158,6 +157,13 @@ export default {
},
//
handleBeforeUpload(file) {
const type = ["image/jpeg", "image/jpg", "image/png", "image/svg"];
const isJPG = type.includes(file.type);
//
if (!isJPG) {
this.$message.error(`图片格式错误!`);
return false;
}
//
if (this.fileSize) {
const isLt = file.size / 1024 / 1024 < this.fileSize;
@ -169,10 +175,10 @@ export default {
return true;
},
handleUploadSuccess(res, file) {
//
let quill = this.Quill;
//
if (res.code == 200) {
//
let quill = this.Quill;
//
let length = quill.getSelection().index;
// res.url