vue-quill-editor二次封裝,實(shí)現(xiàn)自定義文件上傳方式
更新時(shí)間:2024年03月07日 09:24:06 作者:jsmeng626
這篇文章主要介紹了vue-quill-editor二次封裝,實(shí)現(xiàn)自定義文件上傳方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
實(shí)現(xiàn)步驟
- 先將vue-quill-editor組件引入進(jìn)來,自定義配置,在自定義配置中添加upload操作,但是由于vue-quill-editor組件不支持文件上傳,該操作是無效的,邏輯需要自己去實(shí)現(xiàn)
- 給添加的upload按鈕自定義css樣式,文件上傳的圖標(biāo),大小等
- 模擬upload點(diǎn)擊事件,在editor的配置中,有一個(gè)handler對(duì)象,來處理自定義的upload點(diǎn)擊
- 點(diǎn)擊之后我們?nèi)プ屗|發(fā)element-ui中的文件上傳組件的上傳操作,所以我們將el-upload組件放在頁(yè)面上,然后用css隱藏起來,在點(diǎn)擊富文本中upload圖標(biāo)時(shí),來模擬點(diǎn)擊el-upload,彈出上傳文件操作框
- 然后自定義el-upload的上傳方法,在上傳后的回調(diào)中去調(diào)用自己的接口,拿到一個(gè)文件路徑字符串
- 再將這個(gè)字符串通過quill對(duì)象插入到富文本中,也就是vue-quill-editor組件依賴的父對(duì)象,摻入a鏈接是通過繼承父類中的create方法來實(shí)現(xiàn)
- 其他不明白的可以看代碼中的注釋
完整代碼
1、quill-editor-upload自定義組件
<template> <div> <quill-editor ref="quillEditor" :options="editorOption" :content="content" @change="onEditorChange($event)" @blur="$emit('blur')"></quill-editor> <!-- 隱藏的upload --> <el-upload class="uploadFile" drag action="#" :http-request="uploadFile" :limit="1" :on-exceed="handleExceed" :on-remove="handleRemove" :file-list="fileList"> <i class="el-icon-upload"></i> <div class="el-upload__text"> 將文件拖到此處,或<em>點(diǎn)擊上傳</em> </div> </el-upload> </div> </template>
<script> import Quill from 'quill'; import { quillEditor } from "vue-quill-editor"; import "quill/dist/quill.core.css"; import "quill/dist/quill.snow.css"; import "quill/dist/quill.bubble.css"; import { editorUploadPath } from "@/api/excel/excel.js"; // 工具欄配置 const toolbarOptions = [ ['bold', 'italic', 'underline', 'strike'], // toggled buttons ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], // custom button values [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent [{ 'direction': 'rtl' }], // text direction [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme [{ 'font': [] }], [{ 'align': [] }], ['link', 'image', 'video', 'upload'], ['clean'] // remove formatting button ] // 自定義插入a鏈接 var Link = Quill.import('formats/link'); class FileBlot extends Link { // 繼承Link Blot static create(value) { let node = undefined if (value && !value.href) { // 適應(yīng)原本的Link Blot node = super.create(value); } else { // 自定義Link Blot node = super.create(value.href); node.setAttribute('download', value.innerText); // 左鍵點(diǎn)擊即下載 node.innerText = value.innerText; node.download = value.innerText; } return node; } } FileBlot.blotName = 'link'; FileBlot.tagName = 'A'; Quill.register(FileBlot); export default { name: "quill-editor-upload", components: { quillEditor }, // 自定義v-model,prop為props中的屬性,代表v-model傳入的值;event表示反向輸出給v-model的事件名 model: { prop: 'content', event: 'changeContent' }, // 自定義v-model, v-model傳入的值 props: { content: { type: String, default: '' } }, data() { return { fileList: [], // 文件列表 // editor配置 editorOption: { placeholder: '請(qǐng)輸入正文......', modules: { toolbar: { container: toolbarOptions, // 工具欄 handlers: { // 模擬upload按鈕點(diǎn)擊事件 'upload': (value => { if (value) { document.querySelector('.uploadFile input').click() } }) } } } } } }, methods: { // 自定義v-model,將editor選擇器change事件提供的html傳出去 onEditorChange(e) { this.$emit('changeContent', e.html) }, // 自定義文件上傳 uploadFile(res) { // console.log('上傳成功', res); let myFormData = new FormData(); myFormData.append("file", res.file); editorUploadPath(myFormData).then((response) => { if (response.code === 0) { let fileNameLength = res.file.name.length // 插入鏈接 let quill = this.$refs.quillEditor.quill let length = quill.getSelection().index; quill.insertEmbed(length, 'link', { href: response.msg, innerText: res.file.name }, "api") quill.setSelection(length + fileNameLength) this.fileList = [] } }); }, // 文件超出限制提示 handleExceed() { this.$message.warning(`當(dāng)前限制選擇 1 個(gè)文件,請(qǐng)刪除后更新!`); }, // 移除文件 handleRemove() { this.fileList = []; }, } } </script>
<style scoped> /* 文件上傳圖標(biāo)樣式 */ /deep/ .quill-editor .ql-snow.ql-toolbar .ql-formats .ql-upload { background: url("../assets/file.png"); background-size: 16px 16px; background-position: center center; background-repeat: no-repeat; } /* 隱藏upload上傳,用模擬事件觸發(fā) */ .uploadFile { width: 0; height: 0; display: none; } </style>
2、使用該組件
<el-form-item label="內(nèi)容" prop="content"> <quillEditorUpload v-model="form.content"></quillEditorUpload> </el-form-item>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)列表無縫滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)列表無縫滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue-cli?npm如何解決vue項(xiàng)目中缺失core-js的問題
這篇文章主要介紹了vue-cli?npm如何解決vue項(xiàng)目中缺失core-js的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue 菜單欄點(diǎn)擊切換單個(gè)class(高亮)的方法
今天小編就為大家分享一篇Vue 菜單欄點(diǎn)擊切換單個(gè)class(高亮)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08Pinia入門學(xué)習(xí)之實(shí)現(xiàn)簡(jiǎn)單的用戶狀態(tài)管理
Vue3雖然相對(duì)于Vue2很多東西都變了,但是核心的東西還是沒有變,比如說狀態(tài)管理、路由等,再Vue3中尤大神推薦我們使用pinia來實(shí)現(xiàn)狀態(tài)管理,他也說pinia就是Vuex的新版本,這篇文章主要給大家介紹了關(guān)于Pinia入門學(xué)習(xí)之實(shí)現(xiàn)簡(jiǎn)單的用戶狀態(tài)管理的相關(guān)資料,需要的朋友可以參考下2022-11-11Vue全局loading及錯(cuò)誤提示的思路與實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Vue全局loading及錯(cuò)誤提示的思路與實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue-quill-editor富文本編輯器超詳細(xì)入門使用步驟
vue中很多項(xiàng)目都需要用到富文本編輯器,在使用了ueditor和tinymce后,發(fā)現(xiàn)并不理想,所以果斷使用vue-quill-editor來實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue-quill-editor富文本編輯器入門使用步驟的相關(guān)資料,需要的朋友可以參考下2022-08-08