update 优化 富文本Editor组件检验图片格式
This commit is contained in:
parent
81dcd8b512
commit
e35df6d1d2
@ -9,14 +9,13 @@
|
|||||||
name="file"
|
name="file"
|
||||||
:show-file-list="false"
|
:show-file-list="false"
|
||||||
:headers="headers"
|
:headers="headers"
|
||||||
style="display: none"
|
|
||||||
ref="uploadRef"
|
ref="uploadRef"
|
||||||
v-if="type == 'url'"
|
v-if="type == 'url'"
|
||||||
>
|
>
|
||||||
</el-upload>
|
</el-upload>
|
||||||
<div class="editor">
|
<div class="editor">
|
||||||
<quill-editor
|
<quill-editor
|
||||||
ref="myQuillEditor"
|
ref="quillEditorRef"
|
||||||
v-model:content="content"
|
v-model:content="content"
|
||||||
contentType="html"
|
contentType="html"
|
||||||
@textChange="(e) => $emit('update:modelValue', content)"
|
@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 uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + "/system/oss/upload");
|
||||||
const headers = ref({ Authorization: "Bearer " + getToken() });
|
const headers = ref({ Authorization: "Bearer " + getToken() });
|
||||||
const myQuillEditor = ref();
|
const quillEditorRef = ref();
|
||||||
|
|
||||||
const options = ref({
|
const options = ref({
|
||||||
theme: "snow",
|
theme: "snow",
|
||||||
@ -101,7 +100,7 @@ const options = ref({
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
placeholder: '请输入内容',
|
placeholder: "请输入内容",
|
||||||
readOnly: props.readOnly,
|
readOnly: props.readOnly,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -114,7 +113,7 @@ const styles = computed(() => {
|
|||||||
style.height = `${props.height}px`;
|
style.height = `${props.height}px`;
|
||||||
}
|
}
|
||||||
return style;
|
return style;
|
||||||
})
|
});
|
||||||
|
|
||||||
const content = ref("");
|
const content = ref("");
|
||||||
watch(() => props.modelValue, (v) => {
|
watch(() => props.modelValue, (v) => {
|
||||||
@ -125,10 +124,10 @@ watch(() => props.modelValue, (v) => {
|
|||||||
|
|
||||||
// 图片上传成功返回图片地址
|
// 图片上传成功返回图片地址
|
||||||
function handleUploadSuccess(res, file) {
|
function handleUploadSuccess(res, file) {
|
||||||
// 获取富文本实例
|
|
||||||
let quill = toRaw(myQuillEditor.value).getQuill();
|
|
||||||
// 如果上传成功
|
// 如果上传成功
|
||||||
if (res.code == 200) {
|
if (res.code == 200) {
|
||||||
|
// 获取富文本实例
|
||||||
|
let quill = toRaw(myQuillEditor.value).getQuill();
|
||||||
// 获取光标位置
|
// 获取光标位置
|
||||||
let length = quill.selection.savedRange.index;
|
let length = quill.selection.savedRange.index;
|
||||||
// 插入图片,res为服务器返回的图片链接地址
|
// 插入图片,res为服务器返回的图片链接地址
|
||||||
@ -144,6 +143,13 @@ function handleUploadSuccess(res, file) {
|
|||||||
|
|
||||||
// 图片上传前拦截
|
// 图片上传前拦截
|
||||||
function handleBeforeUpload(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) {
|
if (props.fileSize) {
|
||||||
const isLt = file.size / 1024 / 1024 < props.fileSize;
|
const isLt = file.size / 1024 / 1024 < props.fileSize;
|
||||||
@ -164,6 +170,9 @@ function handleUploadError(err) {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
.editor-img-uploader {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
.editor, .ql-toolbar {
|
.editor, .ql-toolbar {
|
||||||
white-space: pre-wrap !important;
|
white-space: pre-wrap !important;
|
||||||
line-height: normal !important;
|
line-height: normal !important;
|
||||||
|
@ -47,7 +47,7 @@ export default {
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
},
|
},
|
||||||
// 上传文件大小限制(MB)
|
/* 上传文件大小限制(MB) */
|
||||||
fileSize: {
|
fileSize: {
|
||||||
type: Number,
|
type: Number,
|
||||||
default: 5,
|
default: 5,
|
||||||
@ -129,7 +129,6 @@ export default {
|
|||||||
if (this.type == 'url') {
|
if (this.type == 'url') {
|
||||||
let toolbar = this.Quill.getModule("toolbar");
|
let toolbar = this.Quill.getModule("toolbar");
|
||||||
toolbar.addHandler("image", (value) => {
|
toolbar.addHandler("image", (value) => {
|
||||||
this.uploadType = "image";
|
|
||||||
if (value) {
|
if (value) {
|
||||||
this.$refs.upload.$children[0].$refs.input.click();
|
this.$refs.upload.$children[0].$refs.input.click();
|
||||||
} else {
|
} else {
|
||||||
@ -158,6 +157,13 @@ export default {
|
|||||||
},
|
},
|
||||||
// 上传前校检格式和大小
|
// 上传前校检格式和大小
|
||||||
handleBeforeUpload(file) {
|
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) {
|
if (this.fileSize) {
|
||||||
const isLt = file.size / 1024 / 1024 < this.fileSize;
|
const isLt = file.size / 1024 / 1024 < this.fileSize;
|
||||||
@ -169,10 +175,10 @@ export default {
|
|||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
handleUploadSuccess(res, file) {
|
handleUploadSuccess(res, file) {
|
||||||
// 获取富文本组件实例
|
|
||||||
let quill = this.Quill;
|
|
||||||
// 如果上传成功
|
// 如果上传成功
|
||||||
if (res.code == 200) {
|
if (res.code == 200) {
|
||||||
|
// 获取富文本组件实例
|
||||||
|
let quill = this.Quill;
|
||||||
// 获取光标所在位置
|
// 获取光标所在位置
|
||||||
let length = quill.getSelection().index;
|
let length = quill.getSelection().index;
|
||||||
// 插入图片 res.url为服务器返回的图片地址
|
// 插入图片 res.url为服务器返回的图片地址
|
||||||
|
Loading…
x
Reference in New Issue
Block a user