Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert custom button on Insert/Edit Link dialog?

I want to add a custom button on the Insert/Edit Link dialog/popup in tinymce v5.

I only got this code for the setup option where I put in call to a function.

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

    editor.on("click", function(e) {
        console.log("this click");
    });
}
like image 768
heero Avatar asked Mar 12 '19 06:03

heero


1 Answers

I'll be the first to admit this is a bit hacky, but you could try:

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
    };
}

This will give you the following result:

enter image description here

Setup:

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');
        });

      }
    });

Tips:

  1. You can replace $('.tox-dialog__footer-end')... with $('.tox-dialog__footer-start')... if you want your button on the left hand side of the footer.
  2. This currently works on the default skin, changes to .tox-dialog__footer class could break this.
  3. Any updates to the library that change the title "Insert/Edit Link" will break this.
  4. The above example requires jQuery to work.
  5. This is a bare minimum example. Use the configuration guide to customize the toolbar, setup events etc.
like image 171
Lance Avatar answered Dec 13 '22 18:12

Lance