6

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");
    });
}
heero
  • 278
  • 4
  • 15

1 Answers1

7

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.
Lance
  • 895
  • 1
  • 9
  • 18
  • I got an error: Uncaught TypeError: Cannot read property 'open' of undefined on this line: editor.windowManager.oldOpen = editor.windowManager.open; – heero Mar 25 '19 at 07:50
  • I added my setup above. How does it compare to yours? – Lance Mar 28 '19 at 11:34
  • Its alredy working now. I have other issues like editor event on keypress, keyup or keydown not working somehow. – heero Mar 29 '19 at 09:51
  • I added to the setup code above to demonstrate how you would register other event callbacks like 'keyup' and 'click'. I just tested and it seems to work well. – Lance Mar 30 '19 at 09:52
  • hi @lance, I got this error on keyup "Uncaught RangeError: Maximum call stack size exceeded at Object.editor.windowManager.open [as oldOpen]" function callback with tinyMceEditLink(editor); in it. – heero Apr 11 '19 at 08:29