VUE的富文本編輯器

https://www.cnblogs.com/dmcl/p/7152711.html  使用 UEDITORjavascript

今天介紹一下  css

vue-quill-editor富文本編輯器html

1、npm 安裝 vue-quill-editor 
2、在main.js中引入vue

import VueQuillEditor from 'vue-quill-editor' // require styles 引入樣式 import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' Vue.use(VueQuillEditor)

3、在模塊中引用java

<template> <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"> </quill-editor> </template> <script> import { quillEditor } from 'vue-quill-editor' export default{ data(){ return { content:null, editorOption:{} } }, methods:{ onEditorBlur(){//失去焦點事件 }, onEditorFocus(){//得到焦點事件 }, onEditorChange(){//內容改變事件 } } } </script> 
那麼你若是不須要那麼多的工具欄功能要怎麼辦呢;應該是經過options來修改可是他的默認值是什麼的 
源碼:
import { quillEditor } from 'vue-quill-editor';
import * as Quill from 'quill'  //引入編輯器
var fonts = ['SimSun', 'SimHei','Microsoft-YaHei','KaiTi','FangSong','Arial'];
var Font = Quill.import('formats/font');
Font.whitelist = fonts; //將字體加入到白名單
Quill.register(Font, true);
const toolbarOptions = [
['bold', 'italic', 'underline', 'strike'],
['blockquote'],

[{ 'header': 1 }, { 'header': 2 }],
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
[{ 'script': 'sub'}, { 'script': 'super' }],
[{ 'indent': '-1'}, { 'indent': '+1' }],
[{ 'direction': 'rtl' }],

[{ 'size': ['small', false, 'large', 'huge'] }],
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],

[{ 'color': [] }, { 'background': [] }],
[{ 'font': fonts }], //把上面定義的字體數組放進來

[{ 'align': [] }],

['clean'],
['image']
]

export default {
data () {
return {
  
editorOption: {
placeholder: '在此處輸入內容',
theme: 'snow', // or 'bubble'
modules:{
toolbar:{
container: toolbarOptions,
handlers: {
image: function(value) { +圖片上傳
if (value) {
document.querySelector('.avatar-uploader input').click()
} else {
this.quill.format('image', false);
}
},
}
}
},
},

}
}
components: {
quillEditor
},
computed: {

editor() {
return this.$refs.myTextEditor.quillEditor;
}
},
methods: {
uploadSuccess (res,file) {
console.log(res)
let self = this;
// res爲圖片服務器返回的數據
// 獲取富文本組件實例
let quill = this.$refs.myTextEditor.quill;
const endFileNmaeList = {
image: ['png', 'jpg', 'gif', 'jpeg'],
};
// 若是上傳成功
if (res.code === 1 && res.data !== null) {
// 獲取光標所在位置
let length = quill.getSelection().index;
// res.url爲服務器返回的圖片地址
var quillList = quill.getContents();
if(quillList.ops.length <= 0) {
quillList = [{
insert: {image: this.url + res.data}
}, {
insert: '\n'
}];
} else {
quillList.push({
insert: {image: this.url + res.data}
});
}
//quill.insertEmbed(length, 'image', this.url + res.data)
quill.setContents(quillList);
// 調整光標到最後
quill.setSelection(length + 1);
} else {
this.$message.error( '添加失敗');
}
// loading動畫消失
this.quillUpdateImg = false;
},
}}
相關文章
相關標籤/搜索