wangeditor 自定义附件上传菜单

官网:自定义扩展新功能 | wangEditor

import {Boot} from "@wangeditor/editor";
//文件上传方法
import {uploadDoc} from "@/api/file/file";
import {Loading} from 'element-ui';

const menu1Conf = {
  key: 'wordUpload',
  factory() {
    return new MyButtonMenu()
  },
}
Boot.registerMenu(menu1Conf)
var e = null;
//图片前缀
var baseUrl = process.env.VUE_APP_BASE_API;

export  default class MyButtonMenu {
  constructor() {
    this.title = 'word上传'
    this.tag = 'button'
    this.showModal = true
    this.modalWidth = 300
  }

  // 菜单是否需要激活(如选中加粗文本,“加粗”菜单会激活),用不到则返回 false
  isActive(editor) {
    return false
  }

  // 获取菜单执行时的 value ,用不到则返回空 字符串或 false
  getValue(editor) {
    return ''
  }

  // 菜单是否需要禁用(如选中 H1 ,“引用”菜单被禁用),用不到则返回 false
  isDisabled(editor) {
    return false
  }

  // 点击菜单时触发的函数
  exec(editor, value) {
    // this.$modal.loading("文件正在处理中,请稍候...");
    e = editor;
    let input = document.createElement('input'); // 创建一个input标签
    input.setAttribute("id","wordUpload")
    // input.setAttribute("style","display:none")
    input.setAttribute("style","display:none")
    input.type ='file';
    input.click();
    input.onchange = inputUpload;

    /*input.addEventListener('click', (event) => {
       let params = new FormData()// FormData类型其实是在XMLHttpRequest 2级定义的,
       params.append('file',event.target.files[0])

    })*/
    input.remove();
    return input;
  }
/*  getModalPositionNode(editor) {
    return null;
  }

  getModalContentElem(editor) {

  }*/

}
const inputUpload = async (event) => {
  let loading = Loading.service({ fullscreen: true });
  let file = event.target.files[0];
  let formData = new FormData();
  formData.append('file', file);
  // 请求文件上传接口
  uploadDoc(formData).then((res) => {
    if(res.code == 200) {
      // e.setHtml("");
      //替换图片前缀
      let content = res.data.replace(/<style>[\s\S]*?<\/style>/ig, '')
      let srcReg = /src=([\'\"]?([^\'\"]*)[\'\"]?)/ig;
      if (content) {
        content = content.replace(srcReg, "src='" + baseUrl + "$2" + "'");
      }
      let reg = /(<\/?font.*?>)|(<\/?div.*?>)|(<\/?a.*?>)/gi;;
      content = content.replace(reg, '');
      e.setHtml(content);
    }
  }).finally(()=>{
    loading.close();
  })
}

vue2引入

data() {
  return {
    editor: null,
    html: "",
    toolbarConfig: {
      //新增菜单
      insertKeys : {
        index: 30,
        keys: ['wordUpload']
      },
      //过滤菜单
      excludeKeys: ["emotion", "group-video"]
    }
}
}

vue3引入

const toolbarConfig = {
  toolbarKeys: [
    'headerSelect',
    'bold',
    'underline',
    "italic",
    "fontSize",
  ],
insertKeys: {
    index: 5, // 插入的位置,基于当前的 toolbarKeys
    keys: ['wordUpload']
}
}
  • 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、付费专栏及课程。

余额充值