​vue3使用wangeditor并且添加自己的自定义菜单工具

在wangeditor中可以自定义自己的菜单按钮,点击后弹出自己的界面来扩展功能

在vue工具类文件夹下创建ts文件,在官方文档中是有价绍的,我这里使用了jq的一写写法,所有需要引用jq,不需要的酌情添加,我需要实现的功能就是给wangeditor富文本编辑菜单添加一个按钮,点击后的出现一个300px宽的页面,页面需要一个图片上传的按钮,点击后打开文件选择页面选择图片,选择上传后在页面展示缩小版的图片,并且在边上需要一个按钮,点击后复制指定大小的img标签代码,直接上代码了

下面是我需要导入引用的部分

import type { IDomEditor, IModalMenu, SlateNode } from "@wangeditor/editor";
import $ from "jquery";
import { ref } from "vue";
import { 图片上传接口 } from "@/api/reqapi";
import type { FieldValues } from "plus-pro-components";
import useClipboard from "vue-clipboard3";

如何就是wangeditor的配置和需要展示的内容配置了


const { toClipboard } = useClipboard();

export class BigFileUploadMenu implements IModalMenu {
  title: string;
  tag: string;
  showModal: boolean;
  modalWidth: number;
  iconSvg: string;

  constructor() {
    this.title = "图片上传";
    this.iconSvg =
      '<svg viewBox="0 0 1024 1024" version="1.1" ><path d="M320 627.2l29.866667-29.866667 29.866666 29.866667 64 64 149.333334-149.333333 29.866666-29.866667 29.866667 29.866667 196.266667 196.266666V298.666667H213.333333v435.2l106.666667-106.666667z m29.866667 29.866667L238.933333 768h136.533334l-4.266667-4.266667 42.666667-42.666666-64-64zM823.466667 768l-196.266667-196.266667-196.266667 196.266667h392.533334zM896 256v554.666667H170.666667V256h725.333333z" fill="#444444" p-id="2819"></path></svg>';
    this.tag = "button";
    this.showModal = true;
    this.modalWidth = 270;
  }

  isActive(_editor: IDomEditor): boolean {
    return false;
  }
  getValue(_editor: IDomEditor): string | boolean {
    return "";
  }
  isDisabled(_editor: IDomEditor): boolean {
    return false;
  }
  exec(_editor: IDomEditor, value: string | boolean) {}
  // 弹出框 modal 的定位:1. 返回某一个 SlateNode; 2. 返回 null (根据当前选区自动定位)
  getModalPositionNode(_editor: IDomEditor): SlateNode | null {
    return null;
  }

  // 定义 modal 内部的 DOM Element
  getModalContentElem(editor: IDomEditor): DOMElement {
    const $li = $("<ul style='width: 100%;'></ul>");
    const $content = $("<div style='height: 250px;overflow-y: auto;'></div>");
    const $button = $(
      "<div style='width: 100%; height:50px;'><button style='height: 30px;  display: flex;justify-content: center;align-items: center; '>上传图片</button></div>"
    );
    $content.append($button);

    $button.on("click", () => {
      const input = document.createElement("input");
      input.type = "file";
      input.accept = "image/*";
      input.addEventListener("change", event => {
        let imgstring: string = "";
        const file = (event.target as HTMLInputElement).files![0];
        const reader = new FileReader();
        let img_url = "";
        reader.onload = async () => {
          let values = ref<FieldValues>({
            content: ""
          });
          const base64String = reader.result as string;
          imgstring =
            '<img src="' +
            base64String +
            '" alt="图片.jpg" data-href="" style=""/>';
          values.value.content = imgstring;
          const rep_data: any = await 上传图片接口(values);
          let imgcont = rep_data.content;
          img_url =
            '<img src="' +
            imgcont +
            '" alt="图片.jpg" data-href="" style="height: 90px; width:auto;"/>';
          let imgbutt =
            "<button id='test' style='height: 30px; margin-top: 10px;'>复制图片代码</button>";
          let imgbt = "<li >" + img_url + imgbutt + "</li>";
          let imght =
            "<img src='" + imgcont + "' style='height: 373px; width:274; '/>";
          const $img_urls = $(imgbt);
          $content.append($li.append($img_urls));
          $("#test").on("click", async () => {
            await toClipboard(imght);
            console.log("复制成功!");
          });
        };
        reader.readAsDataURL(file);
      });
      input.click();
    });

    return $content[0];
  }
}

上面就是uplimage.ts的全部代码,下面就是在自己组件的富文本编辑器中使用我们的工具,当然,导入还是不可缺少的,组件导入如下,第一句是导入自己菜单需要的,后面的是wangeditor需要的

import { BigFileUploadMenu } from "@/utils/uplimage";
import { Boot, type IDomEditor } from "@wangeditor/editor";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

导入后我们需要另一个,就是将我们的注册进菜单,下面就是注册进菜单

/** 记录 editor 实例,重要!*/

const handleCreated = editor => {
  editorRef.value = editor;
  const BigFileUploadMenuButton = {
    key: "BigFileUploadMenu", // 定义 menu key :要保证唯一、不重复(重要)

    factory() {
      return new BigFileUploadMenu(); // 把 `YourMenuClass` 替换为你菜单的 class
    }
  };
  Boot.registerMenu(BigFileUploadMenuButton);
};

注册之后就是在页面菜单显示我们需要的按钮了,菜单配置如下

// 工具栏配置
const toolbarConfig = {
  toolbarKeys: [
    // 菜单 key
    "headerSelect",
    "bold", // 加粗
    "italic", // 斜体
    "through", // 删除线
    "underline", // 下划线
    "color", // 文字颜色
    "fontSize", // 字体大小
    "lineHeight", // 行高
    "uploadImage", // 上传图片
    "BigFileUploadMenu", //上传图片不限制大小
    "delIndent", // 缩进
    "indent", // 增进
    "deleteImage", //删除图片
    "divider", // 分割线
    "insertTable", // 插入表格
    "justifyCenter", // 居中对齐
    "justifyJustify", // 两端对齐
    "justifyLeft", // 左对齐
    "justifyRight", // 右对齐
    "undo", // 撤销
    "redo", // 重做
    "clearStyle", // 清除格式
    "fullScreen", // 全屏
    "codeBlock" //代码块
  ]
};

看看效果吧

其中可以看见我们的菜单图标已经显示出来了

点击后看看,我们需要的已经展示出来了,然后就是看功能了

点击上传后,图片缩小版显示,点击按钮确定复制了html代码

本次添加功能结束,欧克,跑路拉

Vue3中使用WangEditor5时,查看工具栏配置通常意味着你需要了解如何定制编辑器的工具栏以显示你希望用户使用的功能。WangEditor5 提供了灵活的配置选项,允许开发者自定义工具栏按钮和菜单。 要在Vue3项目中查看或定制WangEditor5的工具栏配置,你可以参考以下步骤: 1. 引入WangEditor5到你的Vue组件中。 2. 创建编辑器实例,并配置`menu`属性来自定义工具栏。 3. 使用`config`方法来设置编辑器的配置项,其中可以包括工具栏的详细配置。 下面是一个简单的示例代码,展示了如何在Vue3组件中配置WangEditor5工具栏: ```javascript <template> <div> <div ref="editorContainer"></div> </div> </template> <script> import { ref, onMounted } from 'vue'; import E from '@wangeditor/editor'; export default { setup() { const editorContainer = ref(null); let editor; onMounted(() => { // 创建编辑器实例 editor = new E(editorContainer.value); // 初始化编辑器 editor.create(); // 自定义工具栏配置 const toolbarConfig = { // 其他工具栏配置... }; // 应用工具栏配置 editor.config.menu = toolbarConfig; }); return { editorContainer, }; }, }; </script> ``` 在上面的代码中,`toolbarConfig`对象就是你定义的工具栏配置,你可以根据需要添加或删除按钮,调整它们的顺序等。 要查看现有的工具栏配置或者编辑器的其他配置项,你可以通过阅读WangEditor的官方文档或查看源代码中的默认配置来获得。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值