Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribe to callbacks on a plugin for redactor.js

I want to create a plugin for the redactor.js WYSIWYG editor, but I cannot find the way to subscribe to redactor callbacks from my plugin. I need to use keydownCallback and autosaveCallback.

RedactorPlugins.myPlugin = {
 init: function()
 {
    //Code
 }
}
like image 630
RogerRtx Avatar asked Jul 30 '13 03:07

RogerRtx


2 Answers

I actually went with defining the plugin & using the callback to call it; this allows for multiple functions/plugins to be called from the callback and also feels like the intended approach.

Plugin:

RedactorPlugins.advanced = {
    your_method: function() {
        // Add code here…
    }
}

your_method() is now available on the redactor object & simply callable through the below:

Redactor call:

$('#redactor').redactor({
    keydownCallback: function() {
        this.your_method();

        // Additional keyDownCallbacks:
        this.another_method();
        this.yet_another_method();
    }
});

Original answer:

I know it’s late but came across this my self this evening.

This worked for me:

RedactorPlugins.advanced = {
    init: function() {
        this.opts.keydownCallback = function(e) {
            // Add code here…
        }
    }
}

It simply calls the options and adds the keyDownCallback. The only trouble is it can only be added by one plugin (as the last on called always overwrites the previous).

like image 128
roj Avatar answered Nov 20 '22 10:11

roj


Great idea with this.opts in roj's original answer. It should be possible to preserve the old callback function like this:

RedactorPlugins.advanced = {
    init: function() {
        var oldKeydownCallback = this.opts.keydownCallback;

        this.opts.keydownCallback = function(e) {
            // Add code here…

            if (oldKeydownCallback)
                oldKeydownCallback();
        }
    }
}

Explanation: Reference to the old callback will be preserved in oldKeydownCallback which is then included in the closure and available within your own callback, so you can just call it once your own callback function has finished executing its own code.

like image 2
Miloš Rašić Avatar answered Nov 20 '22 10:11

Miloš Rašić