CKEditor, AJAX Save

后端 未结 10 1415
走了就别回头了
走了就别回头了 2021-01-03 01:22

Can you provide an example on how to setup CKEditor to save via AJAX using the Save button in the CKEditor toolbar?

I\'m interested in creating a CKEditor AJAX save

10条回答
  •  囚心锁ツ
    2021-01-03 01:32

    Try copying straight from _source/plugins/save/plugin.js and changing as needed. Create your new plugin in /path/to/ckeditor/plugins (i.e. Not in /path/to/ckeditor/_source/plugins). For example, in /path/to/ckeditor/plugins create a new directory "AjaxSave", then in that directory create a file "plugin.js". Then in that file do something like this (adapted from the normal "save" plugin in the source folder):

    (function()
    {
      var saveCmd =
      {
        modes : { wysiwyg:1, source:1 },
        exec : function( editor )
        {
          var $form = editor.element.$.form;
          if ( $form )
          {
              try
              {
                editor.updateElement();
    //Here is where you put your ajax submit function. For example... if you are using
    // jQuery and the ajaxform plugin, do something like this:
                $($form).ajaxSubmit({
                   success: function(response){
                     //do something with the response
                   }
                });
              } catch ( e ) {
                //alert(e);
              }
          }
        }
      }
      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,
                icon: "/img/save.png"
             });
         }
       });
    })();
    

    Then in the config, where you define your toolbar, change 'AjaxSave' for 'Save'.

    EDIT: you must also add config.extraPlugins = "ajaxsave"; to the config.

提交回复
热议问题