From 31da01bdd1e4fe8fb8eec41f401d73bcf6cc18b7 Mon Sep 17 00:00:00 2001 From: ahao <liuhaoai545@gmail.com> Date: 星期六, 06 一月 2024 17:27:01 +0800 Subject: [PATCH] fix https://gitee.com/dromara/RuoYi-Vue-Plus/issues/I8TMAI --- src/components/Editor/index.vue | 331 +++++++++++++++++++++++++++--------------------------- 1 files changed, 164 insertions(+), 167 deletions(-) diff --git a/src/components/Editor/index.vue b/src/components/Editor/index.vue index a0728eb..fc30c85 100644 --- a/src/components/Editor/index.vue +++ b/src/components/Editor/index.vue @@ -1,6 +1,7 @@ <template> <div> <el-upload + v-if="type === 'url'" :action="upload.url" :before-upload="handleBeforeUpload" :on-success="handleUploadSuccess" @@ -9,239 +10,235 @@ name="file" :show-file-list="false" :headers="upload.headers" - style="display: none" - v-if="type === 'url'" > + <i ref="uploadRef"></i> </el-upload> - <div class="editor"> - <quill-editor - ref="myQuillEditor" - v-model:content="content" - contentType="html" - @textChange="(e: any) => $emit('update:modelValue', content)" - :options="options" - :style="styles" - /> - </div> + </div> + <div class="editor"> + <quill-editor + ref="quillEditorRef" + v-model:content="content" + content-type="html" + :options="options" + :style="styles" + @text-change="(e: any) => $emit('update:modelValue', content)" + /> </div> </template> <script setup lang="ts"> -import { QuillEditor, Quill } from '@vueup/vue-quill'; import '@vueup/vue-quill/dist/vue-quill.snow.css'; -import { getToken } from "@/utils/auth"; -import { ComponentInternalInstance } from "vue"; + +import { QuillEditor, Quill } from '@vueup/vue-quill'; +import { propTypes } from '@/utils/propTypes'; +import { globalHeaders } from '@/utils/request'; + +defineEmits(['update:modelValue']); const props = defineProps({ - /* 缂栬緫鍣ㄧ殑鍐呭 */ - modelValue: { - type: String, - }, - /* 楂樺害 */ - height: { - type: Number, - default: null, - }, - /* 鏈�灏忛珮搴� */ - minHeight: { - type: Number, - default: null, - }, - /* 鍙 */ - readOnly: { - type: Boolean, - default: false, - }, - /* 涓婁紶鏂囦欢澶у皬闄愬埗(MB) */ - fileSize: { - type: Number, - default: 5, - }, - /* 绫诲瀷锛坆ase64鏍煎紡銆乽rl鏍煎紡锛� */ - type: { - type: String, - default: "url", - } + /* 缂栬緫鍣ㄧ殑鍐呭 */ + modelValue: propTypes.string, + /* 楂樺害 */ + height: propTypes.number.def(400), + /* 鏈�灏忛珮搴� */ + minHeight: propTypes.number.def(400), + /* 鍙 */ + readOnly: propTypes.bool.def(false), + /* 涓婁紶鏂囦欢澶у皬闄愬埗(MB) */ + fileSize: propTypes.number.def(5), + /* 绫诲瀷锛坆ase64鏍煎紡銆乽rl鏍煎紡锛� */ + type: propTypes.string.def('url') }); const { proxy } = getCurrentInstance() as ComponentInternalInstance; const upload = reactive<UploadOption>({ - headers: { Authorization: "Bearer " + getToken() }, - url: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload' -}) -const myQuillEditor = ref(); + headers: globalHeaders(), + url: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload' +}); +const quillEditorRef = ref(); +const uploadRef = ref<HTMLDivElement>(); -const options = ref({ - theme: "snow", - bounds: document.body, - debug: "warn", - modules: { - // 宸ュ叿鏍忛厤缃� - toolbar: { - container: [ - ["bold", "italic", "underline", "strike"], // 鍔犵矖 鏂滀綋 涓嬪垝绾� 鍒犻櫎绾� - ["blockquote", "code-block"], // 寮曠敤 浠g爜鍧� - [{ list: "ordered" }, { list: "bullet"} ], // 鏈夊簭銆佹棤搴忓垪琛� - [{ indent: "-1" }, { indent: "+1" }], // 缂╄繘 - [{ size: ["small", false, "large", "huge"] }], // 瀛椾綋澶у皬 - [{ header: [1, 2, 3, 4, 5, 6, false] }], // 鏍囬 - [{ color: [] }, { background: [] }], // 瀛椾綋棰滆壊銆佸瓧浣撹儗鏅鑹� - [{ align: [] }], // 瀵归綈鏂瑰紡 - ["clean"], // 娓呴櫎鏂囨湰鏍煎紡 - ["link", "image", "video"] // 閾炬帴銆佸浘鐗囥�佽棰� - ], - handlers: { - image: function (value: any) { - if (value) { - // 璋冪敤element鍥剧墖涓婁紶 - (document.querySelector(".editor-img-uploader>.el-upload") as HTMLDivElement)?.click(); - } else { - Quill.format("image", true); - } - }, - }, +const options = ref<any>({ + theme: 'snow', + bounds: document.body, + debug: 'warn', + modules: { + // 宸ュ叿鏍忛厤缃� + toolbar: { + container: [ + ['bold', 'italic', 'underline', 'strike'], // 鍔犵矖 鏂滀綋 涓嬪垝绾� 鍒犻櫎绾� + ['blockquote', 'code-block'], // 寮曠敤 浠g爜鍧� + [{ list: 'ordered' }, { list: 'bullet' }], // 鏈夊簭銆佹棤搴忓垪琛� + [{ indent: '-1' }, { indent: '+1' }], // 缂╄繘 + [{ size: ['small', false, 'large', 'huge'] }], // 瀛椾綋澶у皬 + [{ header: [1, 2, 3, 4, 5, 6, false] }], // 鏍囬 + [{ color: [] }, { background: [] }], // 瀛椾綋棰滆壊銆佸瓧浣撹儗鏅鑹� + [{ align: [] }], // 瀵归綈鏂瑰紡 + ['clean'], // 娓呴櫎鏂囨湰鏍煎紡 + ['link', 'image', 'video'] // 閾炬帴銆佸浘鐗囥�佽棰� + ], + handlers: { + image: (value: boolean) => { + if (value) { + // 璋冪敤element鍥剧墖涓婁紶 + uploadRef.value.click(); + } else { + Quill.format('image', true); + } } - }, - placeholder: '璇疯緭鍏ュ唴瀹�', - readOnly: props.readOnly, + } + } + }, + placeholder: '璇疯緭鍏ュ唴瀹�', + readOnly: props.readOnly }); const styles = computed(() => { - let style: any = {}; - if (props.minHeight) { - style.minHeight = `${props.minHeight}px`; - } - if (props.height) { - style.height = `${props.height}px`; - } - return style; -}) + let style: any = {}; + if (props.minHeight) { + style.minHeight = `${props.minHeight}px`; + } + if (props.height) { + style.height = `${props.height}px`; + } + return style; +}); -const content = ref(""); -watch(() => props.modelValue, (v) => { +const content = ref(''); +watch( + () => props.modelValue, + (v: string) => { if (v !== content.value) { - content.value = v === undefined ? "<p></p>" : v; + content.value = v === undefined ? '<p></p>' : v; } -}, { immediate: true }); + }, + { immediate: true } +); // 鍥剧墖涓婁紶鎴愬姛杩斿洖鍥剧墖鍦板潃 const handleUploadSuccess = (res: any) => { + // 濡傛灉涓婁紶鎴愬姛 + if (res.code === 200) { // 鑾峰彇瀵屾枃鏈疄渚� - let quill = toRaw(myQuillEditor.value).getQuill(); - // 濡傛灉涓婁紶鎴愬姛 - if (res.code === 200) { - // 鑾峰彇鍏夋爣浣嶇疆 - let length = quill.selection.savedRange.index; - // 鎻掑叆鍥剧墖锛宺es涓烘湇鍔″櫒杩斿洖鐨勫浘鐗囬摼鎺ュ湴鍧� - quill.insertEmbed(length, "image", res.data.url); - // 璋冩暣鍏夋爣鍒版渶鍚� - quill.setSelection(length + 1); - proxy?.$modal.closeLoading(); - } else { - proxy?.$modal.loading(res.msg); - proxy?.$modal.closeLoading(); - } -} + let quill = toRaw(quillEditorRef.value).getQuill(); + // 鑾峰彇鍏夋爣浣嶇疆 + let length = quill.selection.savedRange.index; + // 鎻掑叆鍥剧墖锛宺es涓烘湇鍔″櫒杩斿洖鐨勫浘鐗囬摼鎺ュ湴鍧� + quill.insertEmbed(length, 'image', res.data.url); + // 璋冩暣鍏夋爣鍒版渶鍚� + quill.setSelection(length + 1); + proxy?.$modal.closeLoading(); + } else { + proxy?.$modal.msgError('鍥剧墖鎻掑叆澶辫触'); + proxy?.$modal.closeLoading(); + } +}; // 鍥剧墖涓婁紶鍓嶆嫤鎴� const handleBeforeUpload = (file: any) => { - // 鏍℃鏂囦欢澶у皬 - if (props.fileSize) { - const isLt = file.size / 1024 / 1024 < props.fileSize; - if (!isLt) { - proxy?.$modal.msgError(`涓婁紶鏂囦欢澶у皬涓嶈兘瓒呰繃 ${props.fileSize} MB!`); - return false; - } + 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; + if (!isLt) { + proxy?.$modal.msgError(`涓婁紶鏂囦欢澶у皬涓嶈兘瓒呰繃 ${props.fileSize} MB!`); + return false; } - proxy?.$modal.loading('姝e湪涓婁紶鏂囦欢锛岃绋嶅��...'); - return true; -} + } + proxy?.$modal.loading('姝e湪涓婁紶鏂囦欢锛岃绋嶅��...'); + return true; +}; // 鍥剧墖澶辫触鎷︽埅 const handleUploadError = (err: any) => { - console.error(err); - proxy?.$modal.msgError('涓婁紶鏂囦欢澶辫触'); -} + proxy?.$modal.msgError('涓婁紶鏂囦欢澶辫触'); +}; </script> <style> -.editor, .ql-toolbar { +.editor-img-uploader { + display: none; +} +.editor, +.ql-toolbar { white-space: pre-wrap !important; line-height: normal !important; } .quill-img { display: none; } -.ql-snow .ql-tooltip[data-mode="link"]::before { - content: "璇疯緭鍏ラ摼鎺ュ湴鍧�:"; +.ql-snow .ql-tooltip[data-mode='link']::before { + content: '璇疯緭鍏ラ摼鎺ュ湴鍧�:'; } .ql-snow .ql-tooltip.ql-editing a.ql-action::after { border-right: 0; - content: "淇濆瓨"; + content: '淇濆瓨'; padding-right: 0; } - -.ql-snow .ql-tooltip[data-mode="video"]::before { - content: "璇疯緭鍏ヨ棰戝湴鍧�:"; +.ql-snow .ql-tooltip[data-mode='video']::before { + content: '璇疯緭鍏ヨ棰戝湴鍧�:'; } - .ql-snow .ql-picker.ql-size .ql-picker-label::before, .ql-snow .ql-picker.ql-size .ql-picker-item::before { - content: "14px"; + content: '14px'; } -.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before, -.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before { - content: "10px"; +.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='small']::before, +.ql-snow .ql-picker.ql-size .ql-picker-item[data-value='small']::before { + content: '10px'; } -.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before, -.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before { - content: "18px"; +.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='large']::before, +.ql-snow .ql-picker.ql-size .ql-picker-item[data-value='large']::before { + content: '18px'; } -.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before, -.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before { - content: "32px"; +.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='huge']::before, +.ql-snow .ql-picker.ql-size .ql-picker-item[data-value='huge']::before { + content: '32px'; } - .ql-snow .ql-picker.ql-header .ql-picker-label::before, .ql-snow .ql-picker.ql-header .ql-picker-item::before { - content: "鏂囨湰"; + content: '鏂囨湰'; } -.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before, -.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before { - content: "鏍囬1"; +.ql-snow .ql-picker.ql-header .ql-picker-label[data-value='1']::before, +.ql-snow .ql-picker.ql-header .ql-picker-item[data-value='1']::before { + content: '鏍囬1'; } -.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before, -.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before { - content: "鏍囬2"; +.ql-snow .ql-picker.ql-header .ql-picker-label[data-value='2']::before, +.ql-snow .ql-picker.ql-header .ql-picker-item[data-value='2']::before { + content: '鏍囬2'; } -.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before, -.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before { - content: "鏍囬3"; +.ql-snow .ql-picker.ql-header .ql-picker-label[data-value='3']::before, +.ql-snow .ql-picker.ql-header .ql-picker-item[data-value='3']::before { + content: '鏍囬3'; } -.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before, -.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before { - content: "鏍囬4"; +.ql-snow .ql-picker.ql-header .ql-picker-label[data-value='4']::before, +.ql-snow .ql-picker.ql-header .ql-picker-item[data-value='4']::before { + content: '鏍囬4'; } -.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before, -.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before { - content: "鏍囬5"; +.ql-snow .ql-picker.ql-header .ql-picker-label[data-value='5']::before, +.ql-snow .ql-picker.ql-header .ql-picker-item[data-value='5']::before { + content: '鏍囬5'; } -.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before, -.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before { - content: "鏍囬6"; +.ql-snow .ql-picker.ql-header .ql-picker-label[data-value='6']::before, +.ql-snow .ql-picker.ql-header .ql-picker-item[data-value='6']::before { + content: '鏍囬6'; } - .ql-snow .ql-picker.ql-font .ql-picker-label::before, .ql-snow .ql-picker.ql-font .ql-picker-item::before { - content: "鏍囧噯瀛椾綋"; + content: '鏍囧噯瀛椾綋'; } -.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before, -.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before { - content: "琛嚎瀛椾綋"; +.ql-snow .ql-picker.ql-font .ql-picker-label[data-value='serif']::before, +.ql-snow .ql-picker.ql-font .ql-picker-item[data-value='serif']::before { + content: '琛嚎瀛椾綋'; } -.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before, -.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before { - content: "绛夊瀛椾綋"; +.ql-snow .ql-picker.ql-font .ql-picker-label[data-value='monospace']::before, +.ql-snow .ql-picker.ql-font .ql-picker-item[data-value='monospace']::before { + content: '绛夊瀛椾綋'; } </style> -- Gitblit v1.9.3