vue-quill-editor如何自定义图片上传并且给图片添加

2020-04-18  本文已影响0人  辉夜真是太可爱啦
<template>
    <el-upload
      class="upload-demo"
      :headers="myHeaders"
      style="display: none;"
      :action=""
      :show-file-list="false"
      :before-upload="beforeUpload"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :file-list="fileList">
      <button id="article-uploader" @click.prevent="">+ 上传已有验厂报告</button>
    </el-upload>
    <quill-editor v-model="queryList.content"
                  ref="myQuillEditor"
                  id="myQuillEditor"
                  :options="editorOption">
    </quill-editor>
</template>
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
];

data(){
  return{
    editorOption: {
        modules: {
            toolbar:{
                container: toolbarOptions,
                handlers: {
                    'image':function (value) {
                        if (value) {
                            document.querySelector('#article-uploader').click();     //这里就是点击上传图片按钮会触发的事件,然后,你这里直接触发我们饿了么上传图片的按钮点击
                        }else {
                            this.quill.format('image', false);
                        }
                    }
                }
            }
        },
    },
  }
}
  import Quill from 'vue-quill-editor'

  let BlockEmbed = Quill.Quill.imports['blots/embed'];
  class ImageBlot extends BlockEmbed {
      static create(value) {
          let node = super.create();
          node.setAttribute('src', value.src);
          node.setAttribute('alt', value.alt);
          return node;
      }

      static value(node) {
          return {
              src: node.getAttribute('src'),
              alt: node.getAttribute('alt')
          }
      }
  }
  ImageBlot.blotName = 'image';
  ImageBlot.tagName = 'img';
  Quill.Quill.register(ImageBlot);
  
  methods:{
    uploadSuccess(res){
      let quill=this.$refs.myQuillEditor.quill;
      quill.insertEmbed(length, 'image',{src:this.imgUrl + res.result.url,alt:res.result.id});
      let length = quill.getLength();
      quill.setSelection(length +1);
    },
  }
上一篇 下一篇

猜你喜欢

热点阅读