Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image dialog — extend onOk, instead of total overwrite

I have found out that I can hook into onOk with this:

editor.on('dialogShow', function (ev)
{
    var name = ev.data.getName();
    var definition = ev.data.definition;

    if (name == 'image')
    {
        definition.onOk = function(e)
        {
            console.log( e );
        };
    }
});

Awesome, unless now the default behavior is dropped, resulting in no image being added to the CK content.

Checking up on CK's source, I do not want to break 74 lines worth of functionality provided by default.

My goal is to simply run the image through a callback after it has been appended.

Is copy/paste, modify the only way to retain extend the functionality, or is there another way?

like image 731
tomsseisums Avatar asked Dec 20 '22 04:12

tomsseisums


2 Answers

Small improvement of maximkou's solution:

var oldImplementation = definition.onOk;
definition.onOk = function( e ) {
    oldImplementation.apply( this, [].slice.call( arguments ) );
    console.log( e );
};

This solution is ok and AFAIK it's the cleanest one.

Update: I've found a better solution - there's dialog#ok event about which I've just learnt :). So you don't need to change dialog's definition - you can bind your event listener like this:

editor.on('dialogShow', function ( evt ) {
    if ( evt.data.getName() == 'image' ) {
        var listener = evt.data.on( 'ok', function() {
            console.log( 'ok!' );
        } );

        // We need to remove that listener, to avoid duplicating it on
        // next dialogShow.
        evt.data.on( 'hide', function() {
            listener.removeListener();
        } );
    }
} );
like image 154
Reinmar Avatar answered Dec 30 '22 11:12

Reinmar


var oldImplementation = definition.onOk;
definition.onOk = function(e)
{
    oldImplementation(e);
    console.log( e );
};
like image 45
maximkou Avatar answered Dec 30 '22 09:12

maximkou