Element UI 整合富文本编辑器 vue-quill-editor

后台编辑文章经常用到编辑器,这里选用github上star相对比较多的vue-quill-editor,直接上代码:

main.js

// 引入
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)

template:

...

<el-form-item label="详情" prop="content">
    <quill-editor 
        v-model="form.remark" 
        ref="myQuillEditor" 
        :options="editorOption" 
        @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
        @change="onEditorChange($event)">
    </quill-editor>
</el-form-item>
// 用于替换编辑器base64上传
<div style="display:none;">
    <el-upload class="edit-uploader" :action="uploadPicUrl"
        :show-file-list="false"
        :headers="header"
        :on-success="editorUploadSuccess"
        :on-error="editorUploadError"
        :before-upload="beforeEditorUpload" >
            <i class="el-icon-plus avatar-uploader-icon" ref="aUpload"></i>
    </el-upload>
</div>


...   

<script>

export default {
    data() {
        return {
            editorOption: {
                modules: {
                    toolbar: {container:[
                    ['bold', 'italic', 'underline', 'strike'],
                    ['blockquote', 'code-block'],
                    [{ '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] }],
                    [{ 'font': [] }],
                    [{ 'color': [] }, { 'background': [] }],
                    [{ 'align': [] }],
                    ['clean'],
                    ['link', 'image']
                    ],
                    handlers: {
                        'image': function(value) {
                            if (value) {
                                document.querySelector('.edit-uploader input').click()
                            } else {
                                this.quill.format('image', false);
                            }
                            // this.$refs.aUpload.click() //自定义图片上传回调
                        }
                    }
                    },
                    syntax: {
                        highlight: text => hljs.highlightAuto(text).value
                    }
                },

            }
        }
    },
    methods: {
        onEditorReady(editor) { // 准备编辑器
        },
        onEditorBlur(){}, // 失去焦点事件
        onEditorFocus(){}, // 获得焦点事件
        onEditorChange(){}, // 内容改变事件
        beforeEditorUpload() {
            // 显示loading动画
            this.quillUpdateImg = true
        },

        editorUploadSuccess(res, file) {
            // 获取富文本组件实例
            let quill = this.$refs.myQuillEditor.quill
            // 如果上传成功
            if (res.code === 0) {
                // 获取光标所在位置
                let length = quill.getSelection().index;
                // 插入图片  res.info为服务器返回的图片地址
                quill.insertEmbed(length, 'image', res.data.filepath)
                // 调整光标到最后
                quill.setSelection(length + 1)
            } else {
                this.$message.error('图片插入失败')
            }
            // loading动画消失
            this.quillUpdateImg = false
        },

        // 富文本图片上传失败
        editorUploadError() {
            // loading动画消失
            this.quillUpdateImg = false
            this.$message.error('图片插入失败')
        }
    }
}
</script>

这里需要注意的是,编辑器默认使用base64上传,我们使用elementui的上传组件替换掉原来的图片上传。

本作品采用《CC 协议》,转载必须注明作者和本文链接
分享开发知识,欢迎交流。公众号:开源到
讨论数量: 3

老哥 你这个 失去焦点的时候 怎么拿这个 富文本的内容? 这个富文本编辑器的 空格 你是怎么处理的?这个富文本编辑器 都没有源码编辑 的,太多坑了

2年前 评论
已下线 (楼主) 2年前
chuanwen (作者) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!