CKeditor保存事件

发布于 2024-10-10 21:39:49 字数 1205 浏览 4 评论 0原文

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

ckeditor/plugins/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”事件。

有什么建议吗?

另一件事:如果编辑器的内容自上次保存以来没有更改(它不是脏的),如何禁用该按钮?

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:

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

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见 2024-10-17 21:39:49

我有一个解决方法。

在外面我可以监听设置事件:

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

这样至少会触发一个事件...
但当我从外部手动设置内容时我应该小心......

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...

下壹個目標 2024-10-17 21:39:49

试试这个,你需要完成 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';

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';
伪心 2024-10-17 21:39:49

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

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文