CKeditor保存事件

时间:2011-01-06 20:39:03

标签: events save ckeditor

我按照本主题中的步骤进行操作:CKEditor, AJAX Save  如果有人按下AjaxSave按钮,我试图触发自定义的'saved.ckeditor'事件。但我没有成功。

的CKEditor /插件/ ajaxsave / plugin.js:

(function(){
    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'
            });
        }
   });  
})();

如果我在函数中获取或设置了编辑器数据,则会自动触发get和set事件。但我甚至无法手动触发'getData.ckeditor'事件。

任何提示?

另一件事:如果编辑器的内容自上次保存后没有改变(如果它不脏),我怎么能禁用该按钮?

3 个答案:

答案 0 :(得分:0)

我有一个解决方法。

外面我可以听设置事件:

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
    });
};

...按下按钮时,请使用当前值设置数据:

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

这样至少会触发一个事件...... 但是当我从外面手动设置内容时,我要小心......

答案 1 :(得分:0)

试试这个,你需要完成exec()函数

(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'
            });
        }
    });
})();

并且不要忘记在config.js中启用插件

config.extraPlugins = 'ajaxSave';

答案 2 :(得分:0)

您可以编辑常规保存按钮的功能以执行您想要的操作:

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>