美文网首页
【vue】vue-quill-editor富文本编辑器实现图片上

【vue】vue-quill-editor富文本编辑器实现图片上

作者: 小棨 | 来源:发表于2019-06-15 16:44 被阅读0次

封装vue-quill-editor实现富文本点击图标、选择文件、上传图片到服务端、上传成功返回图片地址、显示图片
实现点击图标/按钮,打开选择文件需要 自定义工具栏 基础写法可参考:富文本基本使用
文章参考:引路文章之改造vue-quill-editor:实现图片上传到服务器再插入富文本

代码实现如下:

<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>
            <!--颜色、字体大小等 下拉框要自己补充option value -->
            <select class="ql-align" title="对齐">
                    <option selected> </option>
                    <option value="center"></option>
                    <option value="right"></option>
                    <option value="justify"></option>
            </select>
           <!-- 上传图片图标 -->
           <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显示在富文本框内,支持图片和视频类型*/
              this.editor.insertEmbed(this.editor.getSelection().index, 'image', res.data)
            }
          })
      },
      /*点击上传图片按钮*/
      imgClick() {
        if (!this.uploadUrl) {
          console.log('no editor uploadUrl');
          return;
        }
        /*内存创建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

该方法是用于在富文本框中显示图片/视频,参数包含index【当前文件插入的位置】,type【当前文件的类型】,source【来源】
api docs:insertEmbed

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

相关文章

网友评论

      本文标题:【vue】vue-quill-editor富文本编辑器实现图片上

      本文链接:https://www.haomeiwen.com/subject/fdmbfctx.html