wangEditer 富文本编辑器 ---基于vue2.0

npm install @wangeditor/editor --save

npm install @wangeditor/editor-for-vue --save

import E from 'wangeditor'

1. 下载并引入

2. 编辑组件

3. init配置项

4. 配置自定义需求(图片上传方式, 更新数据方式 ...)

5. 获取富文本数据或HTML

<template>
  <div class="app-container">
    <h3>富文本标签:</h3>
    <el-button @click="getEditorInfo">获取文本内容</el-button>
    <div ref="editor" class="text" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import E from 'wangeditor'
export default {
  name: 'Editor',
  data() {
    return {
      editorContent: ''
    }
  },
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.editor = new E(this.$refs.editor)
      // 自定义菜单配置
      this.editor.config.zIndex = 5
      // 文件上传
      this.editor.config.customUploadImg = (files, insert) => {
        this.imageToBase64(files, false, insert)
      }
      this.editor.create()
      // 初始化数据
      // editoror.txt.html(this.editorContent)
    },
    getEditorInfo() {
      this.editor.txt.html()
    },
    //获取图片base64
    imageToBase64(file, isCompression, callback) {
      // 判断图片类型
      // let isJpg
      if (file.type == 'image/jpeg' || file.type == 'image/png' || file.type == 'image/jpg') return this.$message.error("请上传合规格式图片!")
      // 创建一个HTML5的FileReader对象
      let reader = new FileReader()
      // 创建一个img对象
      let img = new Image()
      if (file) {
        reader.readAsDataURL(file[0])
      }
      reader.onload = (e) => {
        // let base64Str = reader.result.split[','][1]
        img.src = e.target.result
        // base64地址图片加载完毕后执行
        img.onload = function () {
          // 缩放图片需要canvas(也可以在DOM中直接定义canvas标签,这样就能把压缩完的图片不转base64也能直接显示出来)
          let canvas = document.createElement('canvas')
          let context = canvas.getContext('2d')
          // 图片原始尺寸
          let originWidth = this.width
          let originHeight = this.height
          // 最大尺寸限制,可通过设置宽高来实现图片压缩程度
          let maxWidth = 300,
            maxHeight = 300
          // 目标尺寸
          let targetWidth = originWidth
          let targetHeight = originHeight
          // 图片尺寸超过最大尺寸限制
          if (originWidth > maxWidth || originHeight > maxHeight) {
            if (originWidth / originHeight > maxWidth / maxHeight) {
              // 更改宽度,按照宽度限定尺寸
              targetWidth = maxWidth
              targetHeight = Math.round(maxWidth * (originHeight / originWidth))
            } else {
              targetHeight = maxHeight
              targetWidth = Math.round(maxHeight * (originWidth / originHeight))
            }
          }
          // 对图片进行缩放
          canvas.width = targetWidth
          canvas.height = targetHeight
          // 清除画布
          context.clearRect(0, 0, targetWidth, targetHeight)
          /** 图片压缩
           * 第一个参数是创建的img对象
           * 第二三个参数是左上角坐标
           * 后两个参数是画布区域宽高
           */
          context.drawImage(img, 0, 0, targetWidth, targetHeight)
          /** 压缩后的base64文件
           * 第一个参数可以为image/jpeg或image/webp类型的图片
           * 第二个参数设置图片质量取值0-1,超出则以默认值0.92替代
           */
          let newUrl = canvas.toDataURL('image/jpeg', 0.02)
          console.log(newUrl);
          if (isCompression) { // 返回压缩后的base64
            callback(newUrl)
          } else { // 返回不压缩的base64
            callback(e.target.result)
          }
        }
      }
    },
  }
}
</script>

<style scoped>
.text {
  text-align: left;
  width: 100%;
  height: 400px;
}
::v-deep .w-e-text-container {
  height: 420px !important;
}
</style>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值