vue3使用富文本编辑器wang-editor5

3 篇文章 1 订阅

官网:https://www.wangeditor.com/

1.封装成公共组件:

<template>
  <div style="border: 1px solid #ccc; z-index: 100">
    <!-- 工具栏 -->
    <Toolbar 
      :editor="editorRef" 
      :defaultConfig="toolbarConfig"
      :mode="mode" 
      style="border-bottom: 1px solid #ccc"
    />
    <!-- 编辑器 -->
    <Editor
      v-model="newValueHtml"
      :style="style"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
    />
  </div>
</template>

<script lang="ts" setup>
import '@wangeditor/editor/dist/css/style.css'
import { onBeforeUnmount, ref, shallowRef, onMounted, watch } from 'vue'
import constant from '@/utils/constant'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { IDomEditor, IEditorConfig } from '@wangeditor/editor'

const props = defineProps({
  valueHtml: {
    type: String,
    required: true
  },
  mode: {
    type: String,
    default: 'default' // 可选值:[default | simple]
  },
  placeholder: {
    type: String,
    default: ''
  },
  style: {
    type: String,
    default: 'min-height: 300px;'
  },
  disabled: {
    type: Boolean,
    default: false
  }
})

// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef()
const newValueHtml = ref('')
const globalUrl = (window as any).globalUrl

watch(() => props.valueHtml, () => {
  newValueHtml.value = JSON.parse(JSON.stringify(props.valueHtml))
})

type InsertFnType = (url: string, alt: string, href: string) => void

// 工具栏配置
const toolbarConfig = ref({})
// 编辑器配置
const editorConfig: Partial<IEditorConfig> = {
  placeholder: props.placeholder,
  // readOnly: props.disabled,
  MENU_CONF: {
    uploadImage: {
      server: constant.uploadUrl,
      fieldName: 'file',
      // 自定义插入图片
      customInsert(res: any, insertFn: InsertFnType) {
        // res 即服务端的返回结果
        // 从 res 中找到 url alt href ,然后插图图片
        let url = `${globalUrl.url}${res.data.realPath}`
        insertFn(url, res.data.filename, '')
      }
    }
  }
}

// 组件销毁时,也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value
  if (editor == null) {
    return
  }
  editor.destroy()
})

const handleCreated = (editor: any) => {
  editorRef.value = editor
}

// 获取富文本内容
const getValueHtml = () => {
  return editorRef.value.getHtml()
}

defineExpose({
  getValueHtml
})
</script>

2.在组件内调用:

<WangEditor ref="wangEditorRef" :valueHtml="dataForm.productContent" placeholder="请输入产品内容"></WangEditor>

<script lang="ts" setup>
import WangEditor from '@/components/wang-editor/index.vue'
const wangEditorRef = ref() // 富文本编辑器ref
dataForm.productContent = wangEditorRef.value.getValueHtml()
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清岚_lxn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值