2020-10-20 20:53:21 +08:00
|
|
|
<template>
|
2020-10-21 12:00:30 +08:00
|
|
|
<div style="position: relative" class="rich-text-editor" :class="{'rich-text-editor-error':tooLong}">
|
2020-10-20 20:53:21 +08:00
|
|
|
<div :id="id" class="editor-core" ref="editor" style="height:350px;"></div>
|
2020-10-21 12:00:30 +08:00
|
|
|
<div class="text-too-long" v-if="tooLong">{{$t('validate.tooLong')}}</div>
|
2020-10-20 20:53:21 +08:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import 'quill/dist/quill.snow.css'
|
|
|
|
|
import Quill from 'quill'
|
|
|
|
|
export default {
|
|
|
|
|
name: "richTextEditor",
|
|
|
|
|
props:{
|
2020-12-15 18:47:30 +08:00
|
|
|
editData:String,
|
2020-10-20 20:53:21 +08:00
|
|
|
},
|
|
|
|
|
data(){
|
|
|
|
|
return {
|
|
|
|
|
id:'editor-'+this.guid(),
|
|
|
|
|
quill:null,
|
|
|
|
|
options:{
|
|
|
|
|
theme: 'snow',
|
|
|
|
|
modules:{
|
|
|
|
|
toolbar:[
|
|
|
|
|
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
|
|
|
|
|
['bold', 'italic', 'underline', 'strike'],
|
2020-10-21 09:58:31 +08:00
|
|
|
['link','code-block'],
|
2020-10-20 20:53:21 +08:00
|
|
|
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
|
|
|
|
|
[{ 'color': [] }, { 'background': [] }],
|
|
|
|
|
[{ 'align': [] }],
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-10-21 12:00:30 +08:00
|
|
|
maxLength:0,//记录最大长度
|
|
|
|
|
tooLong:false,
|
2020-10-20 20:53:21 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
created() {
|
|
|
|
|
},
|
|
|
|
|
methods:{
|
|
|
|
|
initEditor:function(){
|
|
|
|
|
let $self=this;
|
|
|
|
|
if(!this.quill){
|
|
|
|
|
this.quill = new Quill(this.$refs.editor, this.options);
|
|
|
|
|
this.quill.on('selection-change',function(range, oldRange, source){
|
|
|
|
|
let tooltip=$self.$el.querySelector('.ql-tooltip')
|
|
|
|
|
if(tooltip){
|
|
|
|
|
let left=tooltip.style.left;
|
|
|
|
|
if(left&&/\-\d+\.?\d+?px/.test(left)){
|
|
|
|
|
tooltip.style.left = '10px';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-10-21 12:00:30 +08:00
|
|
|
this.quill.on('text-change',function(delta, oldDelta, source){
|
|
|
|
|
let length=$self.getHtml().length;
|
|
|
|
|
if(length>60000){
|
|
|
|
|
$self.quill.deleteText($self.maxLength-1,$self.quill.getText().length-1,'api')
|
|
|
|
|
$self.tooLong=true;
|
|
|
|
|
}else{
|
|
|
|
|
$self.maxLength=$self.quill.getText().length;
|
|
|
|
|
$self.tooLong=false;
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-10-20 20:53:21 +08:00
|
|
|
this.$nextTick(()=>{
|
|
|
|
|
this.$emit('after-init')
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
getHtml:function(){
|
2020-10-21 12:00:30 +08:00
|
|
|
let html=`<div class="editor-core ql-container ql-snow"><div class="ql-editor">${this.quill.root.innerHTML}</div></div>`;
|
|
|
|
|
return html;
|
|
|
|
|
},
|
|
|
|
|
getContent:function(){
|
|
|
|
|
if(this.tooLong){
|
|
|
|
|
return false;
|
|
|
|
|
}else{
|
|
|
|
|
return this.getHtml();
|
|
|
|
|
}
|
2020-10-20 20:53:21 +08:00
|
|
|
},
|
|
|
|
|
guid() {
|
|
|
|
|
function S4() {
|
2020-12-23 16:41:04 +08:00
|
|
|
return (((1 + window.crypto.getRandomValues()) * 0x10000) | 0).toString(16).substring(1);
|
2020-10-20 20:53:21 +08:00
|
|
|
}
|
|
|
|
|
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
watch:{
|
|
|
|
|
editData:{
|
|
|
|
|
immediate:true,
|
|
|
|
|
handler(n,o){
|
|
|
|
|
this.$nextTick(()=>{
|
|
|
|
|
this.initEditor();
|
|
|
|
|
this.quill.clipboard.dangerouslyPasteHTML(null,this.editData,'api')
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted() {
|
|
|
|
|
this.initEditor();
|
2020-11-20 11:19:25 +08:00
|
|
|
},
|
|
|
|
|
beforeDestroy(){
|
|
|
|
|
this.quill.off('selection-change');
|
|
|
|
|
this.quill.off('text-change');
|
2020-10-20 20:53:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style>
|
2020-10-21 14:48:25 +08:00
|
|
|
.rich-text-editor .ql-editor{
|
|
|
|
|
overflow: auto !important;
|
2020-10-20 20:53:21 +08:00
|
|
|
}
|
2020-10-21 12:00:30 +08:00
|
|
|
.text-too-long{
|
|
|
|
|
color: #F56C6C;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
line-height: 1;
|
|
|
|
|
padding-top: 4px;
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 100%;
|
|
|
|
|
left: 0;
|
|
|
|
|
}
|
|
|
|
|
.rich-text-editor-error {
|
|
|
|
|
border: 1px solid #F56C6C;
|
|
|
|
|
}
|
|
|
|
|
.rich-text-editor-error .ql-container.ql-snow {
|
|
|
|
|
border: unset !important
|
|
|
|
|
}
|
2020-10-20 20:53:21 +08:00
|
|
|
|
2020-10-21 12:00:30 +08:00
|
|
|
.rich-text-editor-error .ql-toolbar.ql-snow{
|
|
|
|
|
border-top: unset !important;
|
|
|
|
|
border-left: unset !important;
|
|
|
|
|
border-right: unset !important;
|
2020-10-20 20:53:21 +08:00
|
|
|
}
|
2020-10-21 12:00:30 +08:00
|
|
|
|
2020-10-20 20:53:21 +08:00
|
|
|
</style>
|