wangeditor 富文本编辑器

一、安装依赖

npm install wangeditor -S

二、封装成一个组件(Wangeditor.vue)

<template lang="html">
  <div class="editor">
    <div ref="toolbar" class="toolbar"></div>
    <div ref="editor" class="text"></div>
  </div>
</template>

<script>
import mixin from '@/mixins/mixin'
import E from 'wangeditor'
import { imgUploadUrl } from '@/api/url.js'
import SparkMD5 from 'spark-md5'
import { upload } from '@/api/api.js'

export default {
  name: 'Wangeditor',
  mixins: [mixin],
  props: {
    value: {
      type: String,
      default: ''
    },
    isClear: {
      type: Boolean,
      default: false
    },
    // 禁止修改内容
    isDisabled: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    isClear(val) {
      // 清除文本域内容
      if (val) {
        this.editor.txt.clear()
        this.info_ = null
      }
    },
    // value为编辑框输入的内容,这里我监听了一下值,当父组件调用的时候,如果给value赋值了,子组件将会显示父组件赋给的值
    value(value) {
      if (value !== this.editor.txt.html()) {
        this.editor.txt.html(this.value)
      }
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  data() {
    return {
      info_: null,
      editor: null
    }
  },
  mounted() {
    this.setEditor()
    this.editor.txt.html(this.value)
    // 禁止修改内容
    if (this.isDisabled) {
      this.editor.$textElem.attr('contenteditable', !this.isDisabled) // false 表示禁止修改
    }
  },
  methods: {
    setEditor() {
      this.editor = new E(this.$refs.toolbar, this.$refs.editor)
      this.editor.config.uploadImgShowBase64 = true // base 64 存储图片
      this.editor.config.uploadImgServer = imgUploadUrl // 配置服务器端地址
      this.editor.config.uploadImgHeaders = {} // 自定义 header
      this.editor.config.uploadFileName = 'file' // 后端接受上传文件的参数名
      this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
      this.editor.config.uploadImgMaxLength = 1 // 限制一次最多上传 3 张图片
      this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间

      // 配置菜单
      this.editor.config.menus = [
        'head', // 标题
        'bold', // 粗体
        'fontSize', // 字号
        'fontName', // 字体
        'italic', // 斜体
        'underline', // 下划线
        'strikeThrough', // 删除线
        'foreColor', // 文字颜色
        'backColor', // 背景颜色
        'link', // 插入链接
        'list', // 列表
        'justify', // 对齐方式
        'quote', // 引用
        'emoticon', // 表情
        'image', // 插入图片
        'table', // 表格
        'video', // 插入视频
        'code', // 插入代码
        'undo', // 撤销
        'redo', // 重复
        'fullscreen' // 全屏
      ]
      this.editor.config.uploadImgHooks = {
        before: (xhr, editor, files) => {
          // console.log('上传之前')
        },
        success: (xhr, editor, result) => {
          // console.log('上传成功');
        },
        fail: (xhr, editor, result) => {
          // console.log('上传失败原因:', result)
        },
        error: (xhr, editor) => {
          // console.log('上传出错');
        }
      }
      // 上传图片
      this.editor.config.customUploadImg = (files, insert) => {
        const formData = new FormData()
        const name = files[0].name.replace(/.+\./, '')
        formData.append('file', files[0])
        formData.append('suffix', name)

        // 计算 MD5 值
        const fileReader = new FileReader()
        const dataFile = files[0]
        // const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice
        const spark = new SparkMD5.ArrayBuffer()
        fileReader.readAsArrayBuffer(dataFile)
        // 异步执行函数
        fileReader.onload = e => {
          spark.append(e.target.result)
          const md5 = spark.end()
          formData.append('md5', md5)

          // 上传
          upload(imgUploadUrl, formData).then(res => {
            if (res.resultCode === 1) {
              const imgUrl = this.getImgUrl(res.data.id) // 后台返回图片id
              insert(imgUrl) // 生成 img 标签并插入文章中
            }
          })
        }
      }
      this.editor.config.onchange = html => {
        this.info_ = html // 绑定当前输入的值
        this.$emit('change', this.info_) // 将内容同步到父组件中
      }
      // 创建富文本编辑器
      this.editor.create()
    }
  }
}
</script>

<style lang="scss" scoped>
.editor {
  width: 100%;

  .toolbar {
    border: 1px solid #cccccc;
  }

  .text {
    height: 250px;
    border: 1px solid #cccccc;
    border-top: none;

    /* 滚动条 */
    ::v-deep .w-e-text::-webkit-scrollbar {
      width: 10px;
      height: 10px;
      background-color: transparent;
    }
    /* 滚动槽 */
    ::v-deep .w-e-text::-webkit-scrollbar-track {
      background-color: transparent;
    }
    /* 滚动滑块 */
    ::v-deep .w-e-text::-webkit-scrollbar-thumb {
      border-radius: 5px;
      background-color: #c4c6cc;
    }
  }
}
</style>

三、在父组件中使用

<el-form-item
    label="中心内容"
    prop="centerSummary"
    >
		<Wangeditor v-model="form.centerSummary"></Wangeditor>
</el-form-item>

四、相关链接

  • wangEditor5:https://www.wangeditor.com
  • wangEditor4:https://www.wangeditor.com/v4
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值