富文本编辑组件支持只读

This commit is contained in:
Josway 2021-02-22 17:49:17 +08:00
parent 0b5c7f4c96
commit 339b932a3d

View File

@ -1,195 +1,200 @@
<template> <template>
<div class="editor" ref="editor" :style="styles"></div> <div class="editor" ref="editor" :style="styles"></div>
</template> </template>
<script> <script>
import Quill from "quill"; import Quill from "quill";
import "quill/dist/quill.core.css"; import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css"; import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css"; import "quill/dist/quill.bubble.css";
export default { export default {
name: "Editor", name: "Editor",
props: { props: {
/* 编辑器的内容 */ /* 编辑器的内容 */
value: { value: {
type: String, type: String,
default: "", default: "",
}, },
/* 高度 */ /* 高度 */
height: { height: {
type: Number, type: Number,
default: null, default: null,
}, },
/* 最小高度 */ /* 最小高度 */
minHeight: { minHeight: {
type: Number, type: Number,
default: null, default: null,
}, },
}, /* 只读 */
data() { readOnly: {
return { type: Boolean,
Quill: null, default: false,
currentValue: "", }
options: { },
theme: "snow", data() {
bounds: document.body, return {
debug: "warn", Quill: null,
modules: { currentValue: "",
// options: {
toolbar: [ theme: "snow",
["bold", "italic", "underline", "strike"], // 线 线 bounds: document.body,
["blockquote", "code-block"], // debug: "warn",
[{ list: "ordered" }, { list: "bullet" }], // modules: {
[{ indent: "-1" }, { indent: "+1" }], // //
[{ size: ["small", false, "large", "huge"] }], // toolbar: [
[{ header: [1, 2, 3, 4, 5, 6, false] }], // ["bold", "italic", "underline", "strike"], // 线 线
[{ color: [] }, { background: [] }], // ["blockquote", "code-block"], //
[{ align: [] }], // [{ list: "ordered" }, { list: "bullet" }], //
["clean"], // [{ indent: "-1" }, { indent: "+1" }], //
["link", "image", "video"] // [{ size: ["small", false, "large", "huge"] }], //
], [{ header: [1, 2, 3, 4, 5, 6, false] }], //
}, [{ color: [] }, { background: [] }], //
placeholder: "请输入内容", [{ align: [] }], //
readOnly: false, ["clean"], //
}, ["link", "image", "video"] //
}; ],
}, },
computed: { placeholder: "请输入内容",
styles() { readOnly: this.readOnly,
let style = {}; },
if (this.minHeight) { };
style.minHeight = `${this.minHeight}px`; },
} computed: {
if (this.height) { styles() {
style.height = `${this.height}px`; let style = {};
} if (this.minHeight) {
return style; style.minHeight = `${this.minHeight}px`;
}, }
}, if (this.height) {
watch: { style.height = `${this.height}px`;
value: { }
handler(val) { return style;
if (val !== this.currentValue) { },
this.currentValue = val === null ? "" : val; },
if (this.Quill) { watch: {
this.Quill.pasteHTML(this.currentValue); value: {
} handler(val) {
} if (val !== this.currentValue) {
}, this.currentValue = val === null ? "" : val;
immediate: true, if (this.Quill) {
}, this.Quill.pasteHTML(this.currentValue);
}, }
mounted() { }
this.init(); },
}, immediate: true,
beforeDestroy() { },
this.Quill = null; },
}, mounted() {
methods: { this.init();
init() { },
const editor = this.$refs.editor; beforeDestroy() {
this.Quill = new Quill(editor, this.options); this.Quill = null;
this.Quill.pasteHTML(this.currentValue); },
this.Quill.on("text-change", (delta, oldDelta, source) => { methods: {
const html = this.$refs.editor.children[0].innerHTML; init() {
const text = this.Quill.getText(); const editor = this.$refs.editor;
const quill = this.Quill; this.Quill = new Quill(editor, this.options);
this.currentValue = html; this.Quill.pasteHTML(this.currentValue);
this.$emit("input", html); this.Quill.on("text-change", (delta, oldDelta, source) => {
this.$emit("on-change", { html, text, quill }); const html = this.$refs.editor.children[0].innerHTML;
}); const text = this.Quill.getText();
this.Quill.on("text-change", (delta, oldDelta, source) => { const quill = this.Quill;
this.$emit("on-text-change", delta, oldDelta, source); this.currentValue = html;
}); this.$emit("input", html);
this.Quill.on("selection-change", (range, oldRange, source) => { this.$emit("on-change", { html, text, quill });
this.$emit("on-selection-change", range, oldRange, source); });
}); this.Quill.on("text-change", (delta, oldDelta, source) => {
this.Quill.on("editor-change", (eventName, ...args) => { this.$emit("on-text-change", delta, oldDelta, source);
this.$emit("on-editor-change", eventName, ...args); });
}); this.Quill.on("selection-change", (range, oldRange, source) => {
}, this.$emit("on-selection-change", range, oldRange, source);
}, });
}; this.Quill.on("editor-change", (eventName, ...args) => {
</script> this.$emit("on-editor-change", eventName, ...args);
});
<style> },
.editor, .ql-toolbar { },
white-space: pre-wrap!important; };
line-height: normal !important; </script>
}
.quill-img { <style>
display: none; .editor, .ql-toolbar {
} white-space: pre-wrap!important;
.ql-snow .ql-tooltip[data-mode="link"]::before { line-height: normal !important;
content: "请输入链接地址:"; }
} .quill-img {
.ql-snow .ql-tooltip.ql-editing a.ql-action::after { display: none;
border-right: 0px; }
content: "保存"; .ql-snow .ql-tooltip[data-mode="link"]::before {
padding-right: 0px; content: "请输入链接地址:";
} }
.ql-snow .ql-tooltip.ql-editing a.ql-action::after {
.ql-snow .ql-tooltip[data-mode="video"]::before { border-right: 0px;
content: "请输入视频地址:"; content: "保存";
} padding-right: 0px;
}
.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before { .ql-snow .ql-tooltip[data-mode="video"]::before {
content: "14px"; content: "请输入视频地址:";
} }
.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 { .ql-snow .ql-picker.ql-size .ql-picker-label::before,
content: "10px"; .ql-snow .ql-picker.ql-size .ql-picker-item::before {
} content: "14px";
.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 { .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
content: "18px"; .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="huge"]::before, }
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before { .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
content: "32px"; .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
} content: "18px";
}
.ql-snow .ql-picker.ql-header .ql-picker-label::before, .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item::before { .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
content: "文本"; content: "32px";
} }
.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 { .ql-snow .ql-picker.ql-header .ql-picker-label::before,
content: "标题1"; .ql-snow .ql-picker.ql-header .ql-picker-item::before {
} content: "文本";
.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 { .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
content: "标题2"; .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="3"]::before, }
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before { .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
content: "标题3"; .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="4"]::before, }
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before { .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
content: "标题4"; .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="5"]::before, }
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before { .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
content: "标题5"; .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="6"]::before, }
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before { .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
content: "标题6"; .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
} content: "标题5";
}
.ql-snow .ql-picker.ql-font .ql-picker-label::before, .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before { .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
content: "标准字体"; content: "标题6";
} }
.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 { .ql-snow .ql-picker.ql-font .ql-picker-label::before,
content: "衬线字体"; .ql-snow .ql-picker.ql-font .ql-picker-item::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 { .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
content: "等宽字体"; .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
} content: "衬线字体";
</style> }
.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>