vue3 wangeditor 自定义SelectMenu以及附件上传(文档末尾有相关文档)

引入编辑器

yarn add @wangeditor/editor
 或者 npm install @wangeditor/editor --save
yarn add @wangeditor/editor-for-vue@next
或者 npm install @wangeditor/editor-for-vue@next --save

引入附件上传

yarn add @wangeditor/plugin-upload-attachment

引入wangeditor编辑器

import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { Boot} from '@wangeditor/editor'
//附件上传
import attachmentModule from '@wangeditor/plugin-upload-attachment'

自定义功能

调用编辑器实例之前注册自定义编辑器组件

 class MySlesctMenu {
      constructor() {
            this.title = '测试菜单',
            this.tag = 'select'
      }
      // 下拉框的选项
      getOptions(){
        const options = [
          { value: '0', text: '快捷回复' },
          // { value: 'beijing', text: '北京', styleForRenderMenuList: { 'font-size': '32px', 'font-weight': 'bold' } },
          { value: 'beijing', text: '北京'},
          { value: 'shanghai', text: '上海'},
          { value: 'shenzhen', text: '深圳' }
        ]
        return options
      }
      // 获取菜单执行时的 value ,用不到则返回空 字符串或 false
      getValue() { return '0' }
      // 菜单是否需要激活(如选中加粗文本,“加粗”菜单会激活),用不到则返回 false
      isActive() { return false }
      // 菜单是否需要禁用(如选中 H1 ,“引用”菜单被禁用),用不到则返回 false
      isDisabled() { return false }
      // 点击菜单时触发的函数
      exec(editor, value) {
        if (this.isDisabled(editor)) return
        editor.insertText(value) // value 即 this.value(editor) 的返回值
      }
    }
    const menuConf = {
      key: 'my-menu-1', // menu key ,唯一。注册之后,需通过 toolbarKeys 配置到工具栏
      factory() {
        return new MySlesctMenu()
      },
    }
    //注册下拉
    Boot.registerMenu(menuConf)

现在下拉就注册完成了

然后编辑器实例,必须用 shallowRef

    const editorRef = shallowRef()

然后是编辑器的一些配置

    // 工具栏配置 
    insertKeys 添加自定义组件  上面已经注册过了
     excludeKeys 屏蔽组件
    const toolbarConfig = {
      insertKeys:{
        index: 99,
        keys: ['my-menu-1','uploadAttachment']
      },
      excludeKeys:['blockquote','header1','header2','header3','bold','underline','italic','through','clearStyle','bulletedList','justifyLeft','justifyRight','justifyCenter','insertLink','insertTable','codeBlock','todo']
    }
    // 编辑器配置
    const editorConfig = {
      placeholder: '请输入内容...',
      hoverbarKeys: {
        attachment: {
          menuKeys: ['downloadAttachment'], // “下载附件”菜单
        },
      },
      MENU_CONF: {
        uploadImage:{
          server: '/api'+uploadPath+'uploads',
          // form-data fieldName ,默认值 'wangeditor-uploaded-file'
          fieldName: 'file',
          maxFileSize: 10 * 1024 * 1024, // 1M
          maxNumberOfFiles: 10,
          // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
          allowedFileTypes: ['image/*'],
          meta: {
            name: 'editor',
          },
          // 自定义增加 http  header
          headers: {
            Authorization: Token,
          },
          // 跨域是否传递 cookie ,默认为 false
          withCredentials: true,
          timeout: 5 * 1000, // 5 秒
          // 小于该值就插入 base64 格式(而不上传),默认为 0
          base64LimitKB: 5, // 5kb
          customInsert(res, insertFn) {
            // if(res.code != 200) return ElementPlus.ElMessage.error(res.msg)
            // 从 res 中找到 url alt href ,然后插图图片
            console.log(11111111111)
            insertFn(res.data, res.data, res.data)
          },
        },
    // “上传附件”菜单的配置
    uploadAttachment: {
      server: '/api'+uploadPath+'uploads', // 服务端地址
          timeout: 5 * 1000, // 5s
          fieldName: 'file',
          meta: {
            name: 'editor',
          },
          // 自定义增加 http  header
          headers: {
            Authorization: Token,
          },
      // 跨域是否传递 cookie ,默认为 false
      withCredentials: true,
      maxFileSize: 10 * 1024 * 1024, // 10M
      onBeforeUpload(file) {
        console.log('onBeforeUpload', file)
        return file // 上传 file 文件
        // return false // 会阻止上传
      },
      onProgress(progress) {
        console.log('onProgress', progress)
      },
      onSuccess(file, res) {
        console.log('onSuccess', file, res)
      },
      onFailed(file, res) {
        // alert(res.message)
        console.log('onFailed', file, res)
      },
      onError(file, err, res) {
        // alert(err.message)
        console.error('onError', file, err, res)
      },
      // // 上传成功后,用户自定义插入文件
      customInsert(res, file, insertFn) {
        console.log('customInsert', res)
        const url  = res.data || {}
        if (!url) throw new Error(`url is empty`)
        console.log(insertFn)
        //创建html节点
        // 插入附件到编辑器
        insertFn(`[Click to download attachment-${file.name}]`, url)
      },
      // 插入到编辑器后的回调
      onInsertedAttachment(elem) {
        console.log('inserted attachment', elem)
      },
    },
      }
    }

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

    const handleCreated = (editor) => {
      editorRef.value = editor // 记录 editor 实例,重要!
    }
     return {
      editorRef,
      valueHtml,
      mode: 'simple', // 或 'simple'  'default'
      toolbarConfig,
      editorConfig,
      handleCreated,

    };

这样就完成了

代码

<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"
          />

然后就可以进行一些相关的操作。因为官方文档对vue3的语法也没多少文档,这也是我凡代码示例出来的
总之:wangeditor 不论是自定义组件还是自带的组件其实都是组件,他们都要经历注册,插入编辑器的过程。然后才能显示

相关文档链接:

自定义扩展新功能
https://www.wangeditor.com/v5/development.html
github代码示例
https://github.com/wangfupeng1988/vue3-wangeditor-demo/blob/main/src/components/ExtendedEditor.vue

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在wangEditor中添加自定义内容,可以使用wangEditor自定义菜单功能。以下是添加自定义菜单的步骤: 1. 首,你需要在JavaScript中定义一个函数,该函数将在单击自定义菜单时运行。例如,你可以定义一个名为myCustomFunction的函数。 ``` function myCustomFunction() { // 在这里添加自定义的代码 } ``` 2. 然后,在wangEditor的配置中添加一个自定义菜单。在菜单的click属性中,指定刚才定义的函数名。例如,你可以添加一个名为“自定义菜单”的菜单,并将其单击事件绑定到myCustomFunction函数。 ``` // 创建一个编辑器实例 var editor = new wangEditor('editor'); // 添加自定义菜单 editor.config.menus = [ 'source', '|', 'bold', 'underline', 'italic', 'strikethrough', 'eraser', 'forecolor', 'bgcolor', '|', 'quote', 'fontfamily', 'fontsize', 'head', 'unorderlist', 'orderlist', 'alignleft', 'aligncenter', 'alignright', 'link', 'unlink', 'table', '|', 'img', 'video', 'insertcode', '|', 'undo', 'redo', '|', 'fullscreen', 'customMenu' ]; // 自定义菜单 editor.config.menus.customMenu = { title: '自定义菜单', type: 'click', click: function () { myCustomFunction(); } }; // 渲染编辑器 editor.create(); ``` 3. 最后,在myCustomFunction函数中添加自定义的代码。例如,你可以在函数中插入一个文本片段。 ``` function myCustomFunction() { // 在光标处插入文本片段 editor.command(null, 'insertHtml', '这是一段自定义内容'); } ``` 这样就可以在wangEditor中添加自定义内容了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值