当前位置: 首页 > Web前端 > vue.js

vue-quill-editor粘贴图片

时间:2023-03-31 15:00:39 vue.js

//富文本编辑器import{container,quillEditor,Quill}from"vue-quill-editor";//粘贴图片上传import{ImageExtend}from'quill-image-paste-module'Quill.register('modules/ImageExtend',ImageExtend)//配置项editorOption:{placeholder:'',theme:'snow',//主题模块:{toolbar:{container:container,//toolbar选项handlers:handlers//事件覆盖},clipboard:{//粘贴过滤器匹配器:[[Node.ELEMENT_NODE,HandleCustomMatcher]]},ImageExtend:{loading:true,name:'imageFile',action:'/api/cms/share/uploadCopyFile',response:(res)=>{返回res.data.url;}}}}functionHandleCustomMatcher(node,Delta){//文本,从别处复制过来,清空自己的样式,转为纯文本letops=[]Delta.ops.forEach(op=>{if(op.insert&&typeofop.insert==='string'){ops.push({insert:op.insert})}})Delta.ops=opsreturnDelta}这种方式只适用于新增。编辑时,初始值中的图片会被过滤掉,导致图片无法显示。修复HandleCustomMatcher后,可以运行HandleCustomMatcher(node,Delta){//文本,从别处复制过来,清空内置样式,转为纯文本if(node.src&&node.src.indexOf('data:image/png')>-1){Delta.ops=[];returnDelta;}letops=[]Delta.ops.forEach(op=>{if(op.insert&&typeofop.insert==='string'){ops.push({insert:op.insert})}elseif(op.insert&&typeofop.insert.image==='string'){ops.push({insert:op.insert})}})Delta.ops=opsreturnDelta}