vue富文本vue-quill-editor(带上传图片)

导入

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)

html

<div class="sendCot" v-loading="editorLoading">
                                <input type="file" ref="uploadInput" @change="EditorUpload" class="uploadImgEditor" name="" hidden id="">
                                <quill-editor v-model="content"
                                    :options="editorOption"
                                    ref="QuillEditor">
                                </quill-editor>
                            </div>
script
data () {
    return {
      editorLoading: false,
      editorOption: {},
      content: ''
    }
  },
async created () {
    this.editorOption = {
      modules: {
        toolbar: {
          container: [
            ['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
          ], // 工具栏
          handlers: {
            'image': function (value) {
              if (value) {
                document.querySelector('.uploadImgEditor').click()
              } else {
                this.quill.format('image', false)
              }
            }
          }
        }
      }
    }
  },
methods: {
    EditorUpload () {
      var file = this.$refs.uploadInput.files[0]
      var fd = new FormData()
      fd.append('file', file)
      this.uploadLogo(fd, file)
    },
    uploadLogo (dt, file) {
      var that = this
      let config = {
        headers: { 'Content-Type': 'multipart/form-data', 'authToken': global.token }
      }
      this.editorLoading = true
      axios
        .post(global.baseUrl + `attach/saveImage`, dt, config)
        .then(response => {
          if (response.data.success) {
            let quill = this.$refs.QuillEditor.quill
            let length = quill.getSelection().index
            quill.insertEmbed(length, 'image', response.data.body.attach.netHref)
            quill.setSelection(length + 1)
            that.editorLoading = false
          } else {
            this.$message.error(response.data.msg)
            that.editorLoading = false
          }
        })
        .catch(error => {
          console.log(error)
        })
    }
  }
}

 

posted @ 2019-09-04 10:29  略略略额  阅读(268)  评论(0)    收藏  举报