【vue】vue-quill-editor富文本編輯器實(shí)現(xiàn)圖片上傳

封裝vue-quill-editor實(shí)現(xiàn)富文本點(diǎn)擊圖標(biāo)、選擇文件、上傳圖片到服務(wù)端、上傳成功返回圖片地址、顯示圖片
實(shí)現(xiàn)點(diǎn)擊圖標(biāo)/按鈕,打開選擇文件需要 自定義工具欄 基礎(chǔ)寫法可參考:富文本基本使用
文章參考:引路文章之改造vue-quill-editor:實(shí)現(xiàn)圖片上傳到服務(wù)器再插入富文本

代碼實(shí)現(xiàn)如下:

<template>
    <div class="editor">
        <quillEditor v-model="content"  ref="rishTextEditor"  :options="editorOption"  @change="onChange" >
          <div id="toolbar" slot="toolbar">
            <!-- Add subscript and superscript buttons -->
            <span class="ql-formats" title="加粗"><button type="button" class="ql-bold"></button></span>
            <span class="ql-formats" title="斜體"><button type="button" class="ql-italic"></button></span>
            <!--顏色、字體大小等 下拉框要自己補(bǔ)充option value -->
            <select class="ql-align" title="對齊">
                    <option selected> </option>
                    <option value="center"></option>
                    <option value="right"></option>
                    <option value="justify"></option>
            </select>
           <!-- 上傳圖片圖標(biāo) -->
           <span class="ql-formats">
                <button type="button" @click="imgClick" style="outline:none" title="圖片上傳">
                <svg viewBox="0 0 18 18">
                   <rect class="ql-stroke" height="10" width="12" x="3" y="4"></rect> 
                  <circle class="ql-fill" cx="6" cy="7" r="1"></circle> 
                  <polyline class="ql-even ql-fill" points="5 12 5 11 7 9 8 10 11 7 13 9 13 12 5 12"></polyline> 
                </svg>
                </button>
            </span>
     </div> 
</template>
<script>
  import 'quill/dist/quill.core.css'
  import 'quill/dist/quill.snow.css'
  import 'quill/dist/quill.bubble.css'

  import {quillEditor} from 'vue-quill-editor'

  export default {
    name: "editor",
    components: { quillEditor },
    props: {
      value: {
        type: String
      },
      /*上傳圖片的地址*/
      uploadUrl: {
        type: String,
        default: '/'
      },
      /*上傳圖片的file控件name*/
      fileName: {
        type: String,
        default: 'file'
      },
      maxUploadSize:{
        type:Number,
        default: 1024 * 1024 * 500
      }
    },
    data() {
      return {
        content: '',
        editorOption: {
          modules: {
            toolbar: '#toolbar'
          }
        },
      }
    },
    methods: {
      onChange() {
        this.$emit('input', this.content)
      },
      /*選擇上傳圖片切換*/
      onFileChange(e) {
        var fileInput = e.target;
        if (fileInput.files.length === 0) {
          return
        }
      /*確定圖片要插入的位置*/
        this.editor.focus();
        if (fileInput.files[0].size > this.maxUploadSize) {
          this.$alert('圖片不能大于500KB', '圖片尺寸過大', {
            confirmButtonText: '確定',
            type: 'warning',
          })
        }
        var data = new FormData;
        data.append(this.fileName, fileInput.files[0]);
        /*請求接口*/
        this.$http.post(this.uploadUrl, data)
          .then(res => {
            if (res.data) {
              /*使用insertEmbed獲取到圖片url顯示在富文本框內(nèi),支持圖片和視頻類型*/
              this.editor.insertEmbed(this.editor.getSelection().index, 'image', res.data)
            }
          })
      },
      /*點(diǎn)擊上傳圖片按鈕*/
      imgClick() {
        if (!this.uploadUrl) {
          console.log('no editor uploadUrl');
          return;
        }
        /*內(nèi)存創(chuàng)建input file 選擇文件*/
        var input = document.createElement('input');
        input.type = 'file';
        input.name = this.fileName;
        input.accept = 'image/jpeg,image/png,image/jpg,image/gif';
        input.onchange = this.onFileChange;
        input.click()
      }
    },
    computed: {
      editor() {
        return this.$refs.rishTextEditor.quill
      }
    },
    mounted() {
      this.content = this.value
    },
    watch: {
      'value'(newVal, oldVal) {
        if (this.editor) {
          if (newVal !== this.content) {
            this.content = newVal
          }
        }
      },
    }
  }
</script>
insertEmbed(index: Number, type: String, value: any, source: String = 'api'): Delta

該方法是用于在富文本框中顯示圖片/視頻,參數(shù)包含index【當(dāng)前文件插入的位置】,type【當(dāng)前文件的類型】,source【來源】
api docs:insertEmbed

小棨留言:文章描述或者語法理解不到位的地方,請大家多多指教!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容