wangEditor封装富文本编辑器组件

<template>
  <div style="border: 1px solid #ccc;">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editorRef"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="height: 300px; overflow-y: hidden;"
      v-model="valueHtml"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
    />
  </div>
</template>

<script>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { getCurrentInstance, defineComponent, onBeforeUnmount, ref, shallowRef, watch } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { _signature } from '@/utils/utils.js'

export default defineComponent({
  components: { Editor, Toolbar },
  props: {
    editContent: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const { proxy } = getCurrentInstance()
    // 编辑器实例,必须用 shallowRef
    const editorRef = shallowRef()
    const valueHtml = ref('')
    // 内容 HTML
    watch(() => props.editContent, (newV) => {
      valueHtml.value = newV
    }, { immediate: true })
    const toolbarConfig = {}
    // 上传文件
    const uploadFile = (file, insertFn, type) => {
      // 上传图片相关声明
      const { VUE_APP_APPKEY, VUE_APP_BASE_URL } = process.env
      const uploadData = {
        file,
        app_key: VUE_APP_APPKEY,
        timestamp: new Date().getTime(),
        token: sessionStorage.getItem('token') ? sessionStorage.getItem('token') : '',
        type,
        sign: _signature({
          app_key: VUE_APP_APPKEY,
          timestamp: new Date().getTime(),
          token: sessionStorage.getItem('token') ? sessionStorage.getItem('token') : '',
          type
        })
      }
      const formData = new FormData()
      formData.append('file', file)
      formData.append('app_key', uploadData.app_key)
      formData.append('timestamp', uploadData.timestamp)
      formData.append('token', uploadData.token)
      formData.append('type', uploadData.type)
      formData.append('sign', uploadData.sign)
      const uploadUrl = VUE_APP_BASE_URL + '/services/vp/motorcade-backofficews/uploadFile'
      const xhr = new XMLHttpRequest()
      xhr.open('POST', uploadUrl, true)
      xhr.send(formData)
      xhr.addEventListener('load', () => {
        const { code, message, result } = JSON.parse(xhr.response)
        if (code !== 200) return proxy.$message.warning(message)
        if (type === 'image') {
          insertFn(result, file.name, result)
        }
        if (type === 'video') {
          insertFn(result)
        }
      })
    }
    const editorConfig = {
      placeholder: '请输入内容...',
      MENU_CONF: {
        uploadImage: {
          customUpload: (file, insertFn) => {
            uploadFile(file, insertFn, 'image')
          }
        },
        uploadVideo: {
          customUpload: (file, insertFn) => {
            uploadFile(file, insertFn, 'video')
          }
        }
      }
    }
    // 组件销毁时,也及时销毁编辑器
    onBeforeUnmount(() => {
      const editor = editorRef.value
      if (editor == null) return
      editor.destroy()
    })
    const handleCreated = (editor) => {
      editorRef.value = editor // 记录 editor 实例,重要!
    }
    return {
      editorRef,
      valueHtml,
      mode: 'default', // 或 'simple'
      toolbarConfig,
      editorConfig,
      handleCreated
    }
  }
})
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wangEditor是一款基于JavaScript和CSS开发的Web文本编辑器,它具有轻量级、简洁、易用的特点。相比于普通的文本编辑器文本编辑器可以输入超越文本的数据内容,包括上传图片、输入表情、字体大小字号调整、颜色设置、对齐方式等功能操作。\[1\] 在使用wangEditor文本编辑器时,首先需要引入相关代码。在editor.vue文件中,可以使用以下代码引入文本编辑器: ```html <template> <div> <div ref="editor" style="text-align:left"></div> </div> </template> <script> import E from 'wangeditor' export default { name: 'MyEditor', data() { return { editorContent: '', editor: null } }, props: { value: { type: String, required: true } }, model: { prop: 'value' }, methods: { getContent: function () { alert(this.editorContent) }, _initEditor(that) { var editor = new E(this.$refs.editor) editor.config.zIndex = 100 editor.create() that.editor = editor } }, mounted() { this._initEditor(this) setTimeout(() => { this.editor.txt.html(this.value) }, 1000) } } </script> <style scoped> </style> ``` 以上代码是一个使用wangEditor的基本示例,通过在组件中引入wangEditor并进行相关配置,可以实现文本编辑功能。\[2\] 如果在项目中多个地方都需要使用文本编辑器,可以将文本编辑器封装成一个组件,以减少重复代码。具体的使用方法可以参考相关文档和示例。\[3\] #### 引用[.reference_title] - *1* [文本编辑器wangEditor的使用(即学即用)](https://blog.csdn.net/kid00712138/article/details/122495640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [wangEditor文本编辑器(第一章:基础使用)](https://blog.csdn.net/DW14687/article/details/118440176)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值