You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TinyMCE 6.7.0的插入/编辑链接对话框中添加自定义按钮?

在TinyMCE插入/编辑链接对话框添加自定义按钮的有效方案

之前用于实现该需求的旧方案已失效,调用时会触发this.oldOpen.apply相关报错。当前editor.windowManager仅支持以下方法:

alert()
close()
confirm()
open()
openUrl()

目前可通过拦截对话框打开事件、修改原有链接对话框配置的方式添加自定义按钮,具体实现如下:

方法一:拦截对话框打开事件注入自定义按钮

tinymce.init({
  selector: 'textarea',
  setup: function(editor) {
    const originalOpen = editor.windowManager.open;
    editor.windowManager.open = function(args) {
      // 识别插入/编辑链接对话框
      if (args.title === 'Insert link' || args.title === 'Edit link') {
        // 在原有按钮组末尾添加自定义按钮
        args.buttons.push({
          text: '自定义按钮',
          onclick: function() {
            // 编写自定义逻辑示例
            console.log('自定义按钮触发');
            // 执行操作后可选择关闭对话框
            this.parent().close();
          }
        });
      }
      return originalOpen.apply(this, arguments);
    };
  },
  plugins: 'link',
  toolbar: 'link'
});

方法二:直接修改链接插件的对话框配置

tinymce.init({
  selector: 'textarea',
  setup: function(editor) {
    editor.on('init', function() {
      // 获取链接插件的对话框配置
      const linkDialogConfig = editor.plugins.link.getDialogConfig();
      // 添加自定义按钮到按钮数组
      linkDialogConfig.buttons.push({
        text: '预设链接插入',
        primary: false,
        onclick: function() {
          // 自定义操作示例:插入固定链接内容
          editor.insertContent('<a href="https://example.com">预设跳转链接</a>');
          editor.windowManager.close();
        }
      });
      // 更新插件的对话框配置
      editor.plugins.link.setDialogConfig(linkDialogConfig);
    });
  },
  plugins: 'link',
  toolbar: 'link'
});

以上两种方案均基于当前editor.windowManager支持的方法实现,不会触发旧方案中的报错,可根据实际需求选择使用。

内容的提问来源于stack exchange,提问作者heero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 18:12:20