Vue使用富文本編輯器Vue-Quill-Editor(含圖片自定義上傳服務(wù)、清除復(fù)制粘貼樣式等)
使用教程(注意細看總結(jié)部分,寫了幾點,希望有所幫助):
1、安裝插件:npm install vue-quill-editor
2、安裝插件依賴:npm install quill
3、main.js文件中引入:
import Vue from 'vue'
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(VueQuillEditor)
new Vue({
VueQuillEditor,
render: h => h(App),
}).$mount('#app')
4、vue頁面中使用(代碼完整,復(fù)制就能使用):
<template>
<div id="quillEditorId">
<el-upload
class="avatarUploader"
action="https://jsonplaceholder.typicode.com/posts/"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
<quill-editor
id="myQuillEditorId"
ref="myQuillEditor"
v-model="ruleForm.editeContent"
:options="editorOption"
@change="handelEditorChange($event)"
>
</quill-editor>
</div>
</template>
<script>
const toolbarOptions = [
['bold', 'italic', 'underline', 'strike'], //加粗,斜體,下劃線,刪除線
['blockquote', 'code-block'], //引用、代碼塊兒
[{ header: 1 }, { header: 2 }], //標題,鍵值對的形式;1、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: [] }], //字體
[{ align: [] }], //對齊方式
['clean'], //清除字體樣式
['image'], //上傳圖片、上傳視頻(video)、超鏈接(link)
]
export default {
data() {
return {
imageUrl: '',
editeContent: '',
editorOption: {
modules: {
clipboard: {
// 粘貼版,處理粘貼時候的自帶樣式
matchers: [[Node.ELEMENT_NODE, this.HandleCustomMatcher]],
},
toolbar: {
container: toolbarOptions, // 工具欄
handlers: {
image: function(value) {
if (value) {
// 獲取隱藏的上傳圖片的class,不一定是.el-icon-plus。觸發(fā)上傳圖片事件
document.querySelector('.el-icon-plus').click()
} else {
this.quill.format('image', false)
}
},
},
},
},
placeholder: '',
},
}
},
computed: {},
async mounted() {},
methods: {
handleAvatarSuccess(res, file) {
// 圖片上傳成功后的回調(diào)
console.log(res, file)
},
beforeAvatarUpload(data) {
// 思路:上傳圖片至服務(wù)后,拿到返回的圖片地址。直接創(chuàng)建image標簽插入光標所在的位置
// 圖片上傳服務(wù)(本地服務(wù)或者阿里云服務(wù))
// 獲取富文本組件實例
let quill = this.$refs.myQuillEditor.quill
// 上傳服務(wù)成功后,按根據(jù)光標位置把圖片插入編輯器中
if (data.url) {
// 獲取光標所在位置,data.url表示上傳服務(wù)后返回的圖片地址
let length = quill.getSelection().index
// 插入圖片,data.url為服務(wù)返回的圖片鏈接地址
quill.insertEmbed(length, 'image', data.url)
// 調(diào)整光標到最后
quill.setSelection(length + 1)
} else {
this.$message.closeAll()
this.$message.error('圖片插入失敗')
}
},
handelEditorChange(el) {
console.log(el, 'el')
},
HandleCustomMatcher(node, Delta) {
// 文字、圖片等,從別處復(fù)制而來,清除自帶樣式,轉(zhuǎn)為純文本
let ops = []
Delta.ops.forEach(op => {
if (op.insert && typeof op.insert === 'string') {
ops.push({
insert: op.insert,
})
}
})
Delta.ops = ops
return Delta
},
},
}
</script>
<style scoped lang="scss">
#quillEditorId {
.avatarUploader {
display: none; // 隱藏上傳圖片組件
}
}
</style>
總結(jié):
1、變量toolbarOptions表示自定義的工具欄,可以參照官網(wǎng)(官網(wǎng)寫的比較簡單)或者細看本文代碼(有詳細注釋);
2、如果不單獨處理圖片,圖片會被直接轉(zhuǎn)義成base64,跟隨DOM一塊兒上傳服務(wù);
3、本文對圖片做了自定義處理,選擇本地圖片時,會單獨上傳到服務(wù),返回地址后,直接插入到富文本編輯中的當(dāng)前節(jié)點??创a中editorOption的handlers的image函數(shù),以及插入富文本編輯器當(dāng)前光標函數(shù)beforeAvatarUpload,代碼中有詳細注釋;
4、粘貼板,變量clipboard。如果需要清理復(fù)制的自帶樣式,使用粘貼板進行清理,函數(shù)HandleCustomMatcher;
5、對于復(fù)制粘貼的情況,多說一句。過程中,編輯器已經(jīng)將原有的DOM轉(zhuǎn)為編輯器允許存在的DOM元素,所以這塊兒不用再處理(處理起來,也會有點復(fù)雜)。
到此這篇關(guān)于Vue使用富文本編輯器Vue-Quill-Editor(含圖片自定義上傳服務(wù)、清除復(fù)制粘貼樣式等)的文章就介紹到這了,更多相關(guān)vue富文本編輯器Vue-Quill-Editor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解
這篇文章主要為大家介紹了vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Vue清除定時器setInterval優(yōu)化方案分享
這篇文章主要介紹了Vue清除定時器setInterval優(yōu)化方案分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
使用echarts柱狀圖實現(xiàn)select下拉刷新數(shù)據(jù)
這篇文章主要介紹了使用echarts柱狀圖實現(xiàn)select下拉刷新數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

