wangEditor+vue富文本编译器自定义扩展新菜单(插入域)

 可在config.js编写新菜单功能

 // class MySelectMenu implements ISelectMenu {   // TS 语法
  class MySelectMenu {                       // JS 语法
    constructor() {
      this.title = '插入域',
        this.tag = 'select'
      this.selectPanelWidth = 200
    }
    // 下拉框的选项
    // getOptions(editor: IDomEditor) {   // TS 语法
    getOptions(editor) {            // JS 语法
      const options = [
        {
          value: '插入域', text: '插入域',
          selected: true
        },
      ]
      const { insertField = [] } = editor.getMenuConfig('insertField')
      console.log('insertField', insertField)
      insertField.forEach((item) => {
        const { text, value } = item
        options.push({
          text,
          value
        })
      })
      return options
    }
    // 菜单是否需要激活(如选中加粗文本,“加粗”菜单会激活),用不到则返回 false
    // isActive(editor: IDomEditor): boolean {    // TS 语法
    isActive(editor) {                      // JS 语法
      return false
    }
    // 获取菜单执行时的 value ,用不到则返回空 字符串或 false
    // getValue(editor: IDomEditor): string | boolean {    // TS 语法
    getValue(editor) { 
      return '插入域'
    }
    // 菜单是否需要禁用(如选中 H1 ,“引用”菜单被禁用),用不到则返回 false
    // isDisabled(editor: IDomEditor): boolean {   // TS 语法
    isDisabled(editor) { // JS 语法
      // console.log('this.getOptions.length==1',this.getOptions(editor).length==1);
      // console.log('this.getOptions.length',this.getOptions(editor));
      if(this.getOptions(editor).length==1)return true       //插入域无其他设置内容则禁用               
      return false
    }
    // 点击菜单时触发的函数
    // exec(editor: IDomEditor, value: string | boolean) {   // TS 语法
    exec(editor, value) {                              // JS 语法
      // Select menu ,这个函数不用写,空着即可
      if(value!='插入域'){
        const innerHTML = `<span style="color: rgb(54, 88, 226);"><strong>#${value}#</strong></span>`
        editor.dangerouslyInsertHtml(innerHTML)
      }
    }
  }



   
  function getinsertField () {
    const insertField = []
    return insertField
  }



   
  const insertField1Conf = {
    key: 'insertField', // 定义 menu key :要保证唯一、不重复(重要)
    factory () {
      return new MySelectMenu() // 把 `YourMenuClass` 替换为你菜单的 class
    },
    // 默认的菜单菜单配置,将存储在 editorConfig.MENU_CONF[key] 中
    // 创建编辑器时,可通过 editorConfig.MENU_CONF[key] = {...} 来修改
    config: {  
      insertField: getinsertField()
    }
  }
   
  export default insertField1Conf

在wangeditor.vue文件中将编写的新菜单注册到wangeditor 

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

<script>
  import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
  import insertField1Conf from "./config";
  import { Boot } from "@wangeditor/editor";
  Boot.registerMenu(insertField1Conf);//注册插入域菜单
  export default {
    components: { Editor, Toolbar },
    props: {
      insertField: {
        type: Array,
        default:[]
      },
    },
    data() {
      return {
        editor: null,
        html: "",
        toolbarConfig: {
          insertKeys: {
            index: 0, // 插入的位置,基于当前的 toolbarKeys
            keys: ["|", "insertField", "|"],
          },
        },
        editorConfig: {
          placeholder: '"请输入内容..."',
          MENU_CONF: {
            insertField: {
              insertField:[...this.insertField] //设置插入域内容
            },
          },
        },
        mode: "default", // or 'simple'
      };
    },
    watch: {
      insertField: {
        deep: true,
      },
    },
    methods: {
      onCreated(editor) {
        this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错
      },
    },
    mounted() {},
    beforeDestroy() {
      const editor = this.editor;
      if (editor == null) return;
      editor.destroy(); // 组件销毁时,及时销毁编辑器
    },
  };
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>

 导入到运用的vue文件里

import wangEditor from "./components/wangEditor.vue";

  <wangEditor :insertField="insertField"></wangEditor>

  insertField: [
    {value: "深圳1",text: "深圳1",},
    {value: "深圳2",text: "深圳2",},
    {value: "深圳3",text: "深圳3",},
  ],

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用\[1\]和\[2\]提供了关于在Vue中使用wangeditor的代码示例。根据这些示例,你可以按照以下步骤在Vue2中使用wangeditor: 1. 首先,使用npm安装wangeditor库。在终端中运行以下命令: ``` npm install wangeditor ``` 2. 在Vue组件中引入wangeditor,并创建一个编辑器实例。你可以参考\[1\]和\[2\]中的代码示例来完成这一步骤。 3. 在模板中添加一个div元素作为编辑器的容器。你可以使用`ref`属性来引用这个div元素。 4. 在mounted钩子函数中,初始化编辑器实例,并配置编辑器的相关选项。你可以根据需要自定义编辑器的菜单、图片上传路径等选项。 5. 在methods中定义一个保存数据的方法,通过`this.editor.txt.html()`获取编辑器生成的HTML内容。 6. 在模板中使用编辑器组件,并传入相应的props。你可以参考\[3\]中的代码示例来了解如何在模板中使用编辑器组件。 需要注意的是,以上步骤仅提供了使用wangeditor的基本流程,具体的实现方式可能会根据你的项目需求而有所不同。你可以根据自己的实际情况进行相应的调整和修改。 #### 引用[.reference_title] - *1* [Laravel+WangEditor+Vue图片上传](https://blog.csdn.net/DigitalCamer/article/details/119611947)[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^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [wangeditor+vue使用](https://blog.csdn.net/weixin_44094836/article/details/111190693)[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^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值