在“插入/编辑链接”对话框上插入自定义按钮?

时间:2019-03-12 06:02:08

标签: tinymce tinymce-5

我想在tinymce v5的“插入/编辑链接”对话框/弹出窗口中添加自定义按钮。

我只有在调用函数的设置选项中才有这段代码。

function tinyMceEditLink(editor) {
    console.log("tinyMceEditLink");

    editor.on("click", function(e) {
        console.log("this click");
    });
}

1 个答案:

答案 0 :(得分:1)

我将第一个承认这一点有些古怪,但是您可以尝试:

function tinyMceEditLink(editor) {
    editor.windowManager.oldOpen = editor.windowManager.open;  // save for later
    editor.windowManager.open = function (t, r) {    // replace with our own function
        var modal = this.oldOpen.apply(this, [t, r]);  // call original

        if (t.title === "Insert/Edit Link") {
            $('.tox-dialog__footer-end').append(
                '<button title="Custom button" type="button" data-alloy-tabstop="true" tabindex="-1" class="tox-button" id="custom_button">Custom button</button>'
            );

            $('#custom_button').on('click', function () {
                //Replace this with your custom function
                console.log('Running custom function')
            });
        }

        return modal; // Template plugin is dependent on this return value
    };
}

这将为您提供以下结果:

enter image description here

设置:

tinymce.init({
      selector: "#mytextarea",  // change this value according to your HTML
      plugins: "link",
      menubar: "insert",
      toolbar: "link",
      setup: function(editor) {
        // Register our custom button callback function
        editor.on('init',function(e) {
            tinyMceEditLink(editor);
        });

        // Register some other event callbacks...
        editor.on('click', function (e) {
            console.log('Editor was clicked');
        });

        editor.on('keyup', function (e) {
            console.log('Someone typed something');
        });

      }
    });

提示:

  1. 如果要将按钮放在页脚的左侧,可以将$('.tox-dialog__footer-end')...替换为$('.tox-dialog__footer-start')...
  2. 这目前适用于默认外观,对.tox-dialog__footer类的更改可能会破坏这种情况。
  3. 任何对库进行的更改标题为“插入/编辑链接”的更新都将破坏此错误。
  4. 上面的示例要求jQuery可以正常工作。
  5. 这是最低限度的示例。使用配置指南来自定义工具栏,设置事件等。
相关问题