wangeditor二次封装

RichEditor.vue

<template>
  <div class="root" style="border: 1px solid #ccc margin-top: 10px">
    <!-- 工具栏 -->
    <Toolbar :defaultConfig="toolbarConfig" :editor="editor" style="border-bottom: 1px solid #ccc" />
    <!-- 编辑器 -->
    <div class="editor-area">
      <div class="area">编辑区</div>
      <Editor :defaultConfig="editorConfig" :value="value" @onCreated="onCreated" class="editor" v-on="$listeners" />
      <div class="area">预览区</div>
      <!-- <div class="editor preview" v-html="$richTextAddOssToken(value)"></div> -->
      <div class="editor preview" v-html="value"></div>
      <div class="area">[注:小程序端不同手机屏幕像素不同,显示略有差异]</div>
    </div>
  </div>
</template>

<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue"
import { imageUpload, imageDelete } from '@/api/basic/system'

export default {
  name: "RichEditor",
  components: { Editor, Toolbar },
  props: {
    value: ''
  },
  data () {
    return {
      editor: null,
      toolbarConfig: {
        // toolbarKeys: [ /* 显示哪些菜单,如何排序、分组 */ ],
        // excludeKeys: [ /* 隐藏哪些菜单 */ ],
        excludeKeys: ['fontFamily', 'group-video', 'fullScreen', 'insertLink', 'blockquote']
      },
      editorConfig: {
        placeholder: "请输入内容...",
        // autoFocus: false,

        // 所有的菜单配置,都要在 MENU_CONF 属性下
        MENU_CONF: {
          uploadImage: {
            // 单个文件的最大体积限制,默认为 2M
            maxFileSize: 10 * 1024 * 1024, // 10M
            // 最多可上传几个文件,默认为 100
            maxNumberOfFiles: 100,
            // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
            allowedFileTypes: ['image/png', 'image/jpeg', 'image/jpg', 'image/bmp'],
            // 超时时间,默认为 10 秒
            timeout: 30000 // 30 秒
          }
        }
      },
      originalImgs: [], // 初始图片
      insertedImgs: [] // 数据库新增图片
    }
  },
  methods: {
    onCreated (editor) {
      this.editor = Object.seal(editor) // 【注意】一定要用 Object.seal() 否则会报错
    },
    setOriginalImgs (html) { // 父组件mounted时调用
      this.originalImgs = html.match(/evo-oss[0-9a-z/.-]*\?/g).map(item => item.replace('evo-oss/', '').replace('?', ''))
    },
    updateImgs () { // 父组件保存时调用
      let finalImgs = this.editor.getElemsByType('image').map(item => item.src.match(/evo-oss[0-9a-z/.-]*\?/g)[0].replace('evo-oss/', '').replace('?', ''))
      let allServerImgs = this.originalImgs.concat(this.insertedImgs)
      let wastedImgs = allServerImgs.filter(i => finalImgs.indexOf(i) === -1)
      this.originalImgs = [].concat(finalImgs)
      this.insertedImgs = []
      wastedImgs.length && imageDelete(wastedImgs)
      // 清空编辑历史,防止撤销被删除的图片
      this.editor.history.redos = []
      this.editor.history.undos = []
    },
    deleteWastedImgs () { // 父组件beforeDestroy时调用
      this.insertedImgs.length && imageDelete(this.insertedImgs)
    }
  },
  created () {
    this.editorConfig.MENU_CONF.uploadImage.customUpload = async (image, insertFn) => {
      // image 即选中的文件
      // 自己实现上传,并得到图片 url alt href
      const form = new FormData()
      form.append('image', image)
      let res = await imageUpload(form)
      if (res.success) {
        console.log('上传了', res.data)
        this.insertedImgs.push(res.data)
        insertFn(`/evo-apigw/evo-oss/${res.data}?token=${localStorage.getItem('accessToken')}`, '加载失败')
      }
    }
  },
  beforeDestroy () {
    console.log('RichEditor beforeDestroy')
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时,及时销毁 editor ,重要!!!
  }
}
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
<style lang="less" scoped>
.root {
  position: relative;
}
.area {
  display: inline-block;
  vertical-align: top;
  max-width: 135px;
  text-align: justify;
  color: rgb(140, 140, 140);
}
.editor {
  display: inline-block;
  // width: 1300px;
  width: 330px;
  height: 700px;
  overflow-y: scroll;
  border: 1px solid rgb(204, 204, 204);
  border-radius: 5px;
}
.preview {
  width: 330px;
  vertical-align: top;
  padding: 16px;
}
.editor-area {
  margin: 0 auto;
  // width: fit-content;
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WangEditor是一款基于javascript和jQuery开发的富文本编辑器,它支持常见的文本编辑操作、图片上传和格式化等功能。如果您想对WangEditor进行二次封装和使用,可以按照以下步骤进行: 1. 下载WangEditor 您可以从官网上下载WangEditor的源码文件,也可以使用npm进行安装。 2. 引入WangEditorWangEditor的JS和CSS文件引入到您的HTML文件中: ```html <link rel="stylesheet" type="text/css" href="path/to/wangEditor.css"> <script type="text/javascript" src="path/to/jquery.js"></script> <script type="text/javascript" src="path/to/wangEditor.js"></script> ``` 3. 创建编辑器实例 在HTML文件中创建一个div元素,作为编辑器的容器,然后通过JavaScript代码创建编辑器实例: ```html <div id="editor"></div> <script type="text/javascript"> var editor = new wangEditor('editor'); editor.create(); </script> ``` 4. 配置编辑器 您可以通过传递配置对象来配置编辑器,例如: ```html <div id="editor"></div> <script type="text/javascript"> var editor = new wangEditor('editor'); // 配置编辑器 editor.config.uploadImgUrl = '/upload'; editor.config.uploadImgFileName = 'image'; editor.config.menus = [ 'bold', 'italic', 'underline', 'strikeThrough', 'eraser', 'foreColor', 'backColor', 'justifyLeft', 'justifyCenter', 'justifyRight', 'table', 'code', 'undo', 'redo' ]; editor.create(); </script> ``` 5. 使用编辑器 编辑器创建完成后,您可以使用它提供的方法来操作文本内容,例如: ```javascript // 获取编辑器内容 var content = editor.$txt.html(); // 设置编辑器内容 editor.$txt.html('<p>这是新的内容</p>'); ``` 以上就是二次封装和使用WangEditor的基本步骤,您可以根据自己的需要进行配置和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值