zoukankan      html  css  js  c++  java
  • 改造vue-quill-editor:实现图片上传到服务器再插入富文本 quillUpdateImg: false,

    需求概述

    vue-quill-editor是我们再使用vue框架的时候常用的一个富文本编辑器,在进行富文本编辑的时候,我们往往要插入一些图片,vue-quill-editor默认的处理方式是直接将图片转成base64编码,这样的结果是整个富文本的html片段十分冗余,通常来讲,每个服务器端接收的post的数据大小都是有限制的,这样的话有可能导致提交失败,或者是用户体验很差,数据要传递很久才全部传送到服务器。
    因此,在富文本编辑的过程中,对于图片的处理,我们更合理的做法是将图片上传到服务器,再将图片链接插入到富文本中,以达到最优的体验。
    废话不多说,接下来直接看如何改造

        <div>
            <!-- 图片上传组件辅助-->
            <el-upload
                    class="avatar-uploader"
                    :action="serverUrl"
                    name="img"
                    :headers="header"
                    :show-file-list="false"
                    :on-success="uploadSuccess"
                    :on-error="uploadError"
                    :before-upload="beforeUpload">
            </el-upload>
            <!--富文本编辑器组件-->
           <el-row v-loading="quillUpdateImg">
            <quill-editor
                    v-model="detailContent"
                    ref="myQuillEditor"
                    :options="editorOption"
                    @change="onEditorChange($event)"
                    @ready="onEditorReady($event)"
            >
            </quill-editor>
           </el-row>
        </div>
    // 工具栏配置
    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'],
      ['clean']                                         // remove formatting button
    ]export default {
    data() {
                return {
              quillUpdateImg: false, serverUrl:
    '', // 这里写你要上传的图片服务器地址 header: {token: sessionStorage.token}, // 有的图片服务器要求请求头需要有token之类的参数,写在这里 detailContent: '', // 富文本内容 editorOption: { placeholder: '', theme: 'snow', // or 'bubble' modules: { toolbar: { container: toolbarOptions, // 工具栏 handlers: { 'image': function (value) { if (value) { document.querySelector('#quill-upload input').click() } else { this.quill.format('image', false); } } } } } } } } }

    上传组件的方法

     // 富文本图片上传前
                    // 富文本图片上传前
                beforeUpload() {
                    // 显示loading动画
                    this.quillUpdateImg = true
                },
                
                uploadSuccess(res, file) {
                    // res为图片服务器返回的数据
                    // 获取富文本组件实例
                    let quill = this.$refs.myQuillEditor.quill
                    // 如果上传成功
                    if (res.code === '200' && res.info !== null) {
                        // 获取光标所在位置
                        let length = quill.getSelection().index;
                        // 插入图片  res.info为服务器返回的图片地址
                        quill.insertEmbed(length, 'image', res.info)
                        // 调整光标到最后
                        quill.setSelection(length + 1)
                    } else {
                        this.$message.error('图片插入失败')
                    }
                    // loading动画消失
                    this.quillUpdateImg = false
                },
           
                // 富文本图片上传失败
                uploadError() {
                    // loading动画消失
                    this.quillUpdateImg = false
                    this.$message.error('图片插入失败')
                }
  • 相关阅读:
    gridview 列排序问题
    asp.net中session丢失原因分析
    网页flash挡住了TQ的解决办法
    php正确率比较高的安装教程
    PHP5+APACHE2.2配置
    HTML5: HTML5 网站设计作品欣赏
    ASP.NET开源CMS汇总
    ASP.NET:页面传值的几种方式
    Access:数据库操作常见错误
    observer pattern
  • 原文地址:https://www.cnblogs.com/wangjae/p/14442534.html
Copyright © 2011-2022 走看看