开发者

CKeditor save event

I was following the steps written in this topic: CKEditor, AJAX Save I tried to fire a custom 'saved.ckeditor' event if anybody press the AjaxSave button. But I did not succeeded.

ckeditor/plugins/ajaxsave/plugin.js:

(开发者_如何学Gofunction(){
    var saveCmd =
         {  
            modes : { wysiwyg:1, source:1 },  
            exec : function( editor )  
            {
                editor.fire('saved.ckeditor');
                $(editor).trigger('saved.ckeditor', editor.getData());
                alert(editor.getData());
            }
          }
    var pluginName = 'ajaxsave';
    CKEDITOR.plugins.add( pluginName,
    {
        init : function( editor )
        {
            var command = editor.addCommand( pluginName, saveCmd );
            command.modes = { wysiwyg : !!( editor.element.$.form ) };
            editor.ui.addButton( 'AjaxSave',
            {
                label : editor.lang.save,
                command : pluginName,
                className : 'cke_button_save'
            });
        }
   });  
})();

If I get or set the editor data in the function, the get and set events will automatically be fired. But I could not even fire a 'getData.ckeditor' event manually.

Any tips?

An other thing: how can I disable the button if the editor's content haven't changed since the last save (it is not dirty)?


I have a workaround.

Outside I can listen to the set event:

window.onload = function()
{
    var ckparams={width: 500, height: 400, resize_enabled:false, extraPlugins: 'ajaxsave',toolbar:[['AjaxSave','Source','-','Bold','Italic','Underline','-','Undo','Redo','-','Find','Replace','-','SelectAll','-','SpellChecker','Scayt','-','About']]};
    //CKEDITOR.replace('editor', ckparams);
    var editor = $('textarea.editor').ckeditor(ckparams);
    $(editor).bind('setData.ckeditor', function() {
        //do what I want
    });
};

...and when the button is pressed, set the data with its current value:

var saveCmd =
  {
    modes : { wysiwyg:1, source:1 },
    exec : function( editor )
    {
        editor.setData(editor.getData());
    }
}

This way at least an event is fired... But I shold be careful when I manually set the content from outside...


try this, you need to finish the exec() function

(function() {

    var saveCmd = {
        modes:{wysiwyg:1,source:1 },
        readOnly: 1,

        exec: function( editor ) {
            var data = editor.getData();
            console.info(data);
        }
    };

    var pluginName = 'ajaxSave';

    // Register a plugin named "save".
    CKEDITOR.plugins.add( pluginName, {
        lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en-au,en-ca,en-gb,en,eo,es,et,eu,fa,fi,fo,fr-ca,fr,gl,gu,he,hi,hr,hu,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt-br,pt,ro,ru,sk,sl,sq,sr-latn,sr,sv,th,tr,ug,uk,vi,zh-cn,zh', // %REMOVE_LINE_CORE%
        icons: 'save', // %REMOVE_LINE_CORE%
        init: function( editor ) {

            // Save plugin is for replace mode only.
            if ( editor.elementMode != CKEDITOR.ELEMENT_MODE_REPLACE ) return;

            editor.ui.addButton && editor.ui.addButton( 'Save', {
                label: editor.lang.save.toolbar,
                command: pluginName,
                toolbar: 'document,10'
            });
        }
    });
})();

and don't forget to enable the plugin in config.js

config.extraPlugins = 'ajaxSave';


You can edit the functionality of the regular save button to do what you want:

html:

<textarea id="CKEditor1"></textarea>

javascript:

<script>
    // Need to wait for the ckeditor instance to finish initialization
    // because CKEDITOR.instances.editor.commands is an empty object
    // if you try to use it immediately after CKEDITOR.replace('editor');
    CKEDITOR.on('instanceReady', function (ev) {

        // Create a new command with the desired exec function
        var overridecmd = new CKEDITOR.command(editor, {
            exec: function(editor){
                // Replace this with your desired save button code
                alert(editor.document.getBody().getHtml());
            }
        });

        // Replace the old save's exec function with the new one
        ev.editor.commands.save.exec = overridecmd.exec;
    });

    CKEDITOR.replace('CKEditor1');

</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜