vue3中基于WangEditor5的二次封装

文章展示了如何在Vue项目中集成WangEditor5,包括安装、引入CSS、组件配置、编辑器模式、菜单栏定制以及各种事件处理(如onCreated、onBlur、onChange等)。同时,提到了编辑器高度、只读模式、上传图片配置等具体选项的设置方法。
摘要由CSDN通过智能技术生成

 WangEditor5官网:

安装 | wangEditor

详细配置看官网

<!-- WangEditor -->
<template>
  <div>
    <div class="editor-content-border">
      <Toolbar
        class="toolbar-border-b"
        :editor="editorRef"
        :default-config="toolbarConfig"
        :mode="mode"
      />
      <Editor
        v-model="valueHtml"
        :style="{ 'height': editorHeight, 'overflow-y': 'hidden'}"
        :default-config="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
        @onBlur="handleBlur"
        @onChange="handleChange"
        @onDestroyed="handleDestroyed"
        @onFocus="handleFocus"
      />
    </div>
  </div>
</template>

<script lang='ts'>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, defineComponent, reactive, shallowRef, toRefs } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { IDomEditor } from '@wangeditor/editor'

export default defineComponent({
  name: 'CdWangEditorFive', // 富文本编辑器 5
  components: { Editor, Toolbar },
  props: {
    mode: { // 'default' 'simple'
      type: String,
      default: 'default'
    },
    toolbarConfig: { // 菜单栏
      type: Object,
      default: () => {
        // 删除菜单
        // excludeKeys: [
        //   'headerSelect',
        //   'uploadImage', // 子菜单
        //   'group-video' // 排除菜单组,写菜单组 key 的值即可
        // ],
        // 自定义 modal 的定位和其他样式
        // modalAppendToBody: true,
      }
    },
    editorConfig: { // 编辑器配置
      type: Object,
      default: () => {
        // placeholder: '请输入内容...',
        // readOnly: false, // 只读
        // autoFocus: true, // 配置编辑器默认是否 focus 默认true
        // scroll: true, // 编辑器是否支持滚动
        // hoverbarKeys: {
        //   'image': {
        //     menuKeys: [
        //       'imageWidth30',
        //       'imageWidth50',
        //       'imageWidth100',
        //       'deleteImage',
        //     ]
        //   }
        // },
        // MENU_CONF: {
        //   uploadImage: {
        //     server: '/aaaa',
        //     fieldName: 'custom-field-name'
        //     // 继续写其他配置...

        //     // 【注意】不需要修改的不用写,wangEditor 会去 merge 当前其他配置
        //   },
        // },
      }
    },
    // blur事件
    handleBlur: {
      type: Function,
      default: () => {}
    },
    // change事件
    handleChange: {
      type: Function,
      default: () => {}
    },
    // focus事件
    handleFocus: {
      type: Function,
      default: () => {}
    },
    handleDestroyed: {
      type: Function,
      default: () => {}
    },
    // 自定义样式以及handleCreated事件用
    customStyle: {
      type: Function,
      default: () => {
        // editor.on('modalOrPanelShow', modalOrPanel => {
        //   console.log(modalOrPanel)
        //   if (modalOrPanel.type !== 'modal') return
        //   const { $elem } = modalOrPanel // modal element

        //   // 设置 modal 样式(定位、z-index)
        //   // 显示蒙层
        // })
        // editor.on('modalOrPanelHide', () => {
        //   // 隐藏蒙层
        // })
      }
    },
    editorHeight: {
      type: String,
      default: '500px'
    },
  },
  setup(props) {
    // editorRef 必须用 shallowRef
    const editorRef = shallowRef()
    const state = reactive({
      valueHtml: '', // 绑定的值
      // 拿值和传值
      gainTxt: (flag, txt): string | void => {
        if (flag === 'get') {
          setTimeout(() => {
            state.valueHtml = txt
          }, 100)
        } else if (flag === 'set') {
          return state.valueHtml === '<p><br></p>' ? : '' : state.valueHtml
        }
      },
      handleCreated: (editor: IDomEditor) => {
        editorRef.value = editor // 记录 editor 实例,重要!
        // 判断 modalAppendToBody 为true 在修改样式
        props.customStyle(editor, props?.toolbarConfig?.modalAppendToBody)
      },
    })
    // 组件销毁时,也及时销毁编辑器
    onBeforeUnmount(() => {
      const editor = editorRef.value
      if (editor == null) return
      editor.destroy()
    })
    return {
      editorRef,
      ...toRefs(state)
    }
  }
})
</script>

<style scoped lang="less">
@bottom-1:1px solid #ccc;

.editor-content-border {
  border: @bottom-1;
}
.toolbar-border-b {
  border-bottom: @bottom-1;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值