2011-01-06 127 views
1

我按照這個主題編寫的步驟:CKEditor, AJAX Save 我試圖觸發一個自定義的'saved.ckeditor'事件,如果有人按AjaxSave按鈕。但我沒有成功。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'事件。

任何提示?

另一件事:如果編輯器的內容自上次保存後沒有改變(它不髒),我該如何禁用按鈕?

+0

在jquery適配器中,他們這樣做(至少主要):var a = window.jQuery; (a.fn,{ckeditorGet:function(){...}}); var e = a(this); e.trigger('setData.ckeditor',[j]); – BTakacs 2011-01-07 22:23:15

回答

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

這樣至少事件被激發... 但是當我手動設置來自外部的內容時我要小心......

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'; 
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> 
+0

不適用於我 - 按保存按鈕仍然會像以前一樣導致表單發佈。 – HerrimanCoder 2017-03-30 22:21:33