Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone know if there's an equivalent of TinyMCE 3.5.8 Editor.onEvent(...) is in TinyMCE 4.0b1

The plugin I'm trying to port(https://github.com/NYTimes/ice) to TinyMCE 4 needs to have access to a keypress event BEFORE it is handled by the MCE editor and works with onEvent(...) in 3.5.8 but needs to be migrated to something more like on('event') in tinymce 4, however there isn't an obvious alternative.

In tiny MCE 3.5.8 I have

            ed.onEvent.add(function(ed, e) {
                return changeEditor.handleEvent(e);
            });

But I need something more like

                ed.on('event', function(e) {
                    return changeEditor.handleEvent(e);
                });

However the ed.on('event',...) doesn't seem to exist in tinymce 4.

It needs to be able to catch the delete key before any other event handler for the keydown, keyup, and keypress.

like image 798
Curt Mullin Avatar asked Jun 28 '13 17:06

Curt Mullin


1 Answers

Ok, After 2 workdays trying to get this to work I figured out what the problem was with this particular issue.

For starters there is no equivalent to onEvent(...) in tinymce 4. However the plugin doesn't need access to every event anyway.

If you are going to port any tinymce plugin that uses the onEvent() method then you will need to identify the events the plugin is trying to handle and explicitly set the event handler for each of the events that need to be handled:

                ed.on('mousedown', function(e) {
                    return changeEditor.handleEvent(e);
                });

                ed.on('keyup', function(e) {
                    return changeEditor.handleEvent(e);
                });

                ed.on('keydown', function(e) {
                    return changeEditor.handleEvent(e);
                });

                ed.on('keypress', function(e) {
                    return changeEditor.handleEvent(e);
                });

In my case I needed to not only delegate the mousedown,mouseup, keyup,keydown, and keypress events to the plugin I also had to prevent them from being fired prematurely by the editor/textarea:

ed.on('keydown', function(e) {
     // prevent the delete key and backspace keys from firing twice
     if(e.keyCode == 46 || e.keyCode==8)
        e.preventDefault();
});

So keep this in mind if you run into a similar issue.

Oh and I added a fork of this ICE plugin on my github: https://github.com/catsgotmytongue/ice/

like image 112
Curt Mullin Avatar answered Oct 07 '22 17:10

Curt Mullin