如何使用加载gif添加ajax保存按钮到CKeditor 4.2.1.[工作样本插件]

kas*_*ans 5 ajax jquery save ckeditor

我发布这个是因为这可能对那些不知道如何在正常和内联编辑模式下向ckeditor显示保存图标的人有所帮助.我正在寻找一个简单的保存插件,但找不到与ckeditor 4.2.1一起使用的插件.我决定自己做.在我的回答中,您将找到该插件的代码以及谷歌驱动器下载链接.此下载包含保存图标以及加载gif图标.

该插件将向工具栏添加一个保存按钮.单击此按钮将向服务器发出异步发布请求.在请求期间,保存按钮将显示动画ajax加载程序.

kas*_*ans 11

下载工作插件:https://docs.google.com/file/d/0B0dQ8h7Cze23cUJGb2dJM1h2LWM/edit? pli = 1

这个插件使用jquery但你可以使用纯javascript轻松地重写它!在包含ckeditor之前,请确保将jquery包含在您的页面中

首先在config.js文件中注册插件(只需在config.js文件的末尾添加这些行)

config.extraPlugins = 'savebtn';//savebtn is the plugin's name
config.saveSubmitURL = 'http://server/link/to/post/';//link to serverside script to handle the post
Run Code Online (Sandbox Code Playgroud)

现在我们准备将插件添加到ckeditor.下载插件(请参阅上面的google drive下载链接)并解压缩ckeditors插件文件夹中的zip文件.(下载包含下面的脚本和使用过的图标)

而已.这个插件现在应该可以工作了!

作为参考,我在本答案的底部包含了源代码(plugin.js).如果您不知道发生了什么,我建议您阅读评论.此答案的代码中的注释与实际插件文件中的注释略有不同.最新的评论可以在这里找到.业务逻辑完全相同.

plugin.js

CKEDITOR.plugins.add( 'savebtn', {
    icons: 'savebtn',
    init: function( editor ) {
        //add a new command to the editor. 
        //We give the command a name 'savecontent', 
        //so we can  reference it later. 
        editor.addCommand( 'savecontent', {
            //this is the business logic of our 'savecontent' command. 
            //this function gets executed when clicking the button.
            //you can change/replace the logic of this function  
            //according to your own needs.
            exec : function(editor){

                //get the text from ckeditor you want to save
                var data = editor.getData();

                //get the current url (optional)
                var page = document.URL;

                //path to the ajaxloader gif
                loading_icon=CKEDITOR.basePath+'plugins/savebtn/icons/loader.gif';

                //css style for setting the standard save icon. 
                //We need this when the request is completed.
                normal_icon=$('.cke_button__savebtn_icon').css('background-image');

                //replace the standard save icon with the ajaxloader icon. 
                //We do this with css.
                $('.cke_button__savebtn_icon').css("background-image", "url("+loading_icon+")");

                //Now we are ready to post to the server...
                $.ajax({
                    url: editor.config.saveSubmitURL,//the url to post at... configured in config.js
                    type: 'POST', 
                    data: {text: data, id: editor.name, page: page},//editor.name contains the id of the current editable html tag
                })
                .done(function(response) {
                    console.log("success");
                    alert('id: '+editor.name+' \nurl: '+page+' \ntext: '+data);

                })
                .fail(function() {
                    console.log("error");
                })
                .always(function() {
                    console.log("complete");
                    //set the button icon back to the original
                    $('.cke_button__savebtn_icon').css("background-image", normal_icon);
                });


            } 
    });


//add the save button to the toolbar. Mind that we declare command: 'savecontent'. 
//This way ckeditor knows what to do when clicking the button.

        editor.ui.addButton( 'savebtn', {
            label: 'Save',
            command: 'savecontent'
           // toolbar: 'insert'
        });


    }
});
Run Code Online (Sandbox Code Playgroud)