From 339b932a3dea57068f20c40af384641c399c375e Mon Sep 17 00:00:00 2001 From: Josway <jxh98@foxmail.com> Date: 星期一, 22 二月 2021 17:49:17 +0800 Subject: [PATCH] 富文本编辑组件支持只读 --- ruoyi-ui/src/components/Editor/index.vue | 395 ++++++++++++++++++++++++++++--------------------------- 1 files changed, 200 insertions(+), 195 deletions(-) diff --git a/ruoyi-ui/src/components/Editor/index.vue b/ruoyi-ui/src/components/Editor/index.vue index 9054995..ba2ea73 100644 --- a/ruoyi-ui/src/components/Editor/index.vue +++ b/ruoyi-ui/src/components/Editor/index.vue @@ -1,195 +1,200 @@ -<template> - <div class="editor" ref="editor" :style="styles"></div> -</template> - -<script> -import Quill from "quill"; -import "quill/dist/quill.core.css"; -import "quill/dist/quill.snow.css"; -import "quill/dist/quill.bubble.css"; - -export default { - name: "Editor", - props: { - /* 缂栬緫鍣ㄧ殑鍐呭 */ - value: { - type: String, - default: "", - }, - /* 楂樺害 */ - height: { - type: Number, - default: null, - }, - /* 鏈�灏忛珮搴� */ - minHeight: { - type: Number, - default: null, - }, - }, - data() { - return { - Quill: null, - currentValue: "", - options: { - theme: "snow", - bounds: document.body, - debug: "warn", - modules: { - // 宸ュ叿鏍忛厤缃� - toolbar: [ - ["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"] // 閾炬帴銆佸浘鐗囥�佽棰� - ], - }, - placeholder: "璇疯緭鍏ュ唴瀹�", - readOnly: false, - }, - }; - }, - computed: { - styles() { - let style = {}; - if (this.minHeight) { - style.minHeight = `${this.minHeight}px`; - } - if (this.height) { - style.height = `${this.height}px`; - } - return style; - }, - }, - watch: { - value: { - handler(val) { - if (val !== this.currentValue) { - this.currentValue = val === null ? "" : val; - if (this.Quill) { - this.Quill.pasteHTML(this.currentValue); - } - } - }, - immediate: true, - }, - }, - mounted() { - this.init(); - }, - beforeDestroy() { - this.Quill = null; - }, - methods: { - init() { - const editor = this.$refs.editor; - this.Quill = new Quill(editor, this.options); - this.Quill.pasteHTML(this.currentValue); - this.Quill.on("text-change", (delta, oldDelta, source) => { - const html = this.$refs.editor.children[0].innerHTML; - const text = this.Quill.getText(); - const quill = this.Quill; - this.currentValue = html; - this.$emit("input", html); - this.$emit("on-change", { html, text, quill }); - }); - this.Quill.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); - }); - this.Quill.on("editor-change", (eventName, ...args) => { - this.$emit("on-editor-change", eventName, ...args); - }); - }, - }, -}; -</script> - -<style> -.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.ql-editing a.ql-action::after { - border-right: 0px; - content: "淇濆瓨"; - padding-right: 0px; -} - -.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"; -} -.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="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: "鏂囨湰"; -} -.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="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="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-font .ql-picker-label::before, -.ql-snow .ql-picker.ql-font .ql-picker-item::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: "绛夊瀛椾綋"; -} -</style> \ No newline at end of file +<template> + <div class="editor" ref="editor" :style="styles"></div> +</template> + +<script> +import Quill from "quill"; +import "quill/dist/quill.core.css"; +import "quill/dist/quill.snow.css"; +import "quill/dist/quill.bubble.css"; + +export default { + name: "Editor", + props: { + /* 缂栬緫鍣ㄧ殑鍐呭 */ + value: { + type: String, + default: "", + }, + /* 楂樺害 */ + height: { + type: Number, + default: null, + }, + /* 鏈�灏忛珮搴� */ + minHeight: { + type: Number, + default: null, + }, + /* 鍙 */ + readOnly: { + type: Boolean, + default: false, + } + }, + data() { + return { + Quill: null, + currentValue: "", + options: { + theme: "snow", + bounds: document.body, + debug: "warn", + modules: { + // 宸ュ叿鏍忛厤缃� + toolbar: [ + ["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"] // 閾炬帴銆佸浘鐗囥�佽棰� + ], + }, + placeholder: "璇疯緭鍏ュ唴瀹�", + readOnly: this.readOnly, + }, + }; + }, + computed: { + styles() { + let style = {}; + if (this.minHeight) { + style.minHeight = `${this.minHeight}px`; + } + if (this.height) { + style.height = `${this.height}px`; + } + return style; + }, + }, + watch: { + value: { + handler(val) { + if (val !== this.currentValue) { + this.currentValue = val === null ? "" : val; + if (this.Quill) { + this.Quill.pasteHTML(this.currentValue); + } + } + }, + immediate: true, + }, + }, + mounted() { + this.init(); + }, + beforeDestroy() { + this.Quill = null; + }, + methods: { + init() { + const editor = this.$refs.editor; + this.Quill = new Quill(editor, this.options); + this.Quill.pasteHTML(this.currentValue); + this.Quill.on("text-change", (delta, oldDelta, source) => { + const html = this.$refs.editor.children[0].innerHTML; + const text = this.Quill.getText(); + const quill = this.Quill; + this.currentValue = html; + this.$emit("input", html); + this.$emit("on-change", { html, text, quill }); + }); + this.Quill.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); + }); + this.Quill.on("editor-change", (eventName, ...args) => { + this.$emit("on-editor-change", eventName, ...args); + }); + }, + }, +}; +</script> + +<style> +.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.ql-editing a.ql-action::after { + border-right: 0px; + content: "淇濆瓨"; + padding-right: 0px; +} + +.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"; +} +.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="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: "鏂囨湰"; +} +.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="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="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-font .ql-picker-label::before, +.ql-snow .ql-picker.ql-font .ql-picker-item::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: "绛夊瀛椾綋"; +} +</style> -- Gitblit v1.9.3