vue-quill-editor二次封装,实现自定义文件上传方式

 更新时间:2024年03月07日 09:24:06   作者:jsmeng626  
这篇文章主要介绍了vue-quill-editor二次封装,实现自定义文件上传方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

实现步骤

  • 先将vue-quill-editor组件引入进来,自定义配置,在自定义配置中添加upload操作,但是由于vue-quill-editor组件不支持文件上传,该操作是无效的,逻辑需要自己去实现
  • 给添加的upload按钮自定义css样式,文件上传的图标,大小等
  • 模拟upload点击事件,在editor的配置中,有一个handler对象,来处理自定义的upload点击
  • 点击之后我们去让他触发element-ui中的文件上传组件的上传操作,所以我们将el-upload组件放在页面上,然后用css隐藏起来,在点击富文本中upload图标时,来模拟点击el-upload,弹出上传文件操作框
  • 然后自定义el-upload的上传方法,在上传后的回调中去调用自己的接口,拿到一个文件路径字符串
  • 再将这个字符串通过quill对象插入到富文本中,也就是vue-quill-editor组件依赖的父对象,掺入a链接是通过继承父类中的create方法来实现
  • 其他不明白的可以看代码中的注释

完整代码

1、quill-editor-upload自定义组件

<template>
  <div>
    <quill-editor ref="quillEditor" :options="editorOption" :content="content" @change="onEditorChange($event)"
      @blur="$emit('blur')"></quill-editor>
    <!-- 隐藏的upload -->
    <el-upload class="uploadFile" drag action="#" :http-request="uploadFile" :limit="1" :on-exceed="handleExceed"
      :on-remove="handleRemove" :file-list="fileList">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        将文件拖到此处,或<em>点击上传</em>
      </div>
    </el-upload>
  </div>
</template>
<script>
import Quill from 'quill';
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { editorUploadPath } from "@/api/excel/excel.js";
 
// 工具栏配置
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', 'upload'],
  ['clean']                                         // remove formatting button
]
 
// 自定义插入a链接
var Link = Quill.import('formats/link');
class FileBlot extends Link {  // 继承Link Blot
  static create(value) {
    let node = undefined
    if (value && !value.href) {  // 适应原本的Link Blot
      node = super.create(value);
    }
    else {  // 自定义Link Blot
      node = super.create(value.href);
      node.setAttribute('download', value.innerText);  // 左键点击即下载
      node.innerText = value.innerText;
      node.download = value.innerText;
    }
    return node;
  }
}
FileBlot.blotName = 'link';
FileBlot.tagName = 'A';
Quill.register(FileBlot);
 
export default {
  name: "quill-editor-upload",
  components: { quillEditor },
  // 自定义v-model,prop为props中的属性,代表v-model传入的值;event表示反向输出给v-model的事件名
  model: {
    prop: 'content',
    event: 'changeContent'
  },
  // 自定义v-model, v-model传入的值
  props: {
    content: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      fileList: [], // 文件列表
      // editor配置
      editorOption: {
        placeholder: '请输入正文......',
        modules: {
          toolbar: {
            container: toolbarOptions,  // 工具栏
            handlers: {
              // 模拟upload按钮点击事件
              'upload': (value => {
                if (value) {
                  document.querySelector('.uploadFile input').click()
                }
              })
            }
          }
        }
      }
    }
  },
  methods: {
    // 自定义v-model,将editor选择器change事件提供的html传出去
    onEditorChange(e) {
      this.$emit('changeContent', e.html)
    },
    // 自定义文件上传
    uploadFile(res) {
      // console.log('上传成功', res);
      let myFormData = new FormData();
      myFormData.append("file", res.file);
      editorUploadPath(myFormData).then((response) => {
        if (response.code === 0) {
          let fileNameLength = res.file.name.length
          // 插入链接
          let quill = this.$refs.quillEditor.quill
          let length = quill.getSelection().index;
          quill.insertEmbed(length, 'link', { href: response.msg, innerText: res.file.name }, "api")
          quill.setSelection(length + fileNameLength)
          this.fileList = []
        }
      });
    },
    // 文件超出限制提示
    handleExceed() {
      this.$message.warning(`当前限制选择 1 个文件,请删除后更新!`);
    },
    // 移除文件
    handleRemove() {
      this.fileList = [];
    },
  }
}
</script>
<style scoped>
/* 文件上传图标样式 */
/deep/ .quill-editor .ql-snow.ql-toolbar .ql-formats .ql-upload {
  background: url("../assets/file.png");
  background-size: 16px 16px;
  background-position: center center;
  background-repeat: no-repeat;
}
 
/* 隐藏upload上传,用模拟事件触发 */
.uploadFile {
  width: 0;
  height: 0;
  display: none;
}
</style>

2、使用该组件

<el-form-item label="内容" prop="content">
    <quillEditorUpload v-model="form.content"></quillEditorUpload>
</el-form-item>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue实现列表无缝滚动效果

    vue实现列表无缝滚动效果

    这篇文章主要为大家详细介绍了vue实现列表无缝滚动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Vue组件中prop属性使用说明实例代码详解

    Vue组件中prop属性使用说明实例代码详解

    这篇文章主要介绍了Vue组件中prop属性使用说明,非常不错具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-05-05
  • Vue.js 父子组件通信的十种方式

    Vue.js 父子组件通信的十种方式

    最近一直在做 Vue项目代码层面上的优化,写文章是很easy的事情,今天小编给大家分享Vue.js 父子组件通信的十种方式,感兴趣的的朋友跟随小编一起看看吧
    2018-10-10
  • vue-cli npm如何解决vue项目中缺失core-js的问题

    vue-cli npm如何解决vue项目中缺失core-js的问题

    这篇文章主要介绍了vue-cli npm如何解决vue项目中缺失core-js的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • Vue 菜单栏点击切换单个class(高亮)的方法

    Vue 菜单栏点击切换单个class(高亮)的方法

    今天小编就为大家分享一篇Vue 菜单栏点击切换单个class(高亮)的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Pinia入门学习之实现简单的用户状态管理

    Pinia入门学习之实现简单的用户状态管理

    Vue3虽然相对于Vue2很多东西都变了,但是核心的东西还是没有变,比如说状态管理、路由等,再Vue3中尤大神推荐我们使用pinia来实现状态管理,他也说pinia就是Vuex的新版本,这篇文章主要给大家介绍了关于Pinia入门学习之实现简单的用户状态管理的相关资料,需要的朋友可以参考下
    2022-11-11
  • Vue全局loading及错误提示的思路与实现

    Vue全局loading及错误提示的思路与实现

    这篇文章主要给大家介绍了关于Vue全局loading及错误提示的思路与实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Vue具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • vue中v-html妙用及空白行消除方式

    vue中v-html妙用及空白行消除方式

    这篇文章主要介绍了vue中v-html妙用及空白行消除方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Vue props用法详解(小结)

    Vue props用法详解(小结)

    这篇文章主要介绍了Vue props用法详解(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • vue-quill-editor富文本编辑器超详细入门使用步骤

    vue-quill-editor富文本编辑器超详细入门使用步骤

    vue中很多项目都需要用到富文本编辑器,在使用了ueditor和tinymce后,发现并不理想,所以果断使用vue-quill-editor来实现,下面这篇文章主要给大家介绍了关于vue-quill-editor富文本编辑器入门使用步骤的相关资料,需要的朋友可以参考下
    2022-08-08

最新评论