2010-07-28 9 views

risposta

4
CKEDITOR.plugins.registered['save']= 
    { 
    init : function(editor) 
    { 
     var command = editor.addCommand('save', 
      { 
       modes : { wysiwyg:1, source:1 }, 
       exec : function(editor) { 
       //YOUR CODE 
       } 
      } 
     ); 
     editor.ui.addButton('Save',{label : 'YOUR LABEL',command : 'save'}); 
    } 
    } 
2

Se si desidera ignorare il Salva comando per un solo esempio, si può provare il seguente codice:

var editor = $('#myTextarea').ckeditorGet(); // Retrieving CKeditor instance with jQuery 
editor.getCommand('save').exec = function(editor) { 
    // Do whatever you need to 
    ... 
    return true; 
}; 

Questo dovrebbe funzionare per qualsiasi comando CKEditor.

+0

Se si sovrascrive la funzione exec in questo modo verrà chiamato senza alcun parametro. Ma dato che normalmente hai la variabile 'editor' nello scope esterno puoi ancora lavorarci. Basta ricordare di omettere il parametro 'editor' nella definizione della funzione perché altrimenti sovrascriverebbe la variabile' editor' dell'ambito. – flu

10

Il current top answer ha incasinato il gruppo della barra degli strumenti per me (mettere il pulsante Salva alla fine), e il other answer non ha funzionato in ckeditor v4.

Ecco come farlo in ckeditor 4:

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 editor = ev.editor; 
     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

ottimo esempio, grazie: D – Zombyii

+0

Questo ha funzionato per me. – devman81

0
function configureEditor(id) { 
    var editor = CKEDITOR.replace(id); 
    editor.on("instanceReady", function() { 
     // overwrite the default save function 
     editor.addCommand("save", { 
      modes: { wysiwyg: 1, source: 1 }, 
      exec: function() { 
       // get the editor content 
       var theData = editor.getData(); 
       alert("insert your code here"); 
      } 
     }); 
     editor.ui.addButton('Save', { label: 'My Save', command: 'save', enabled: 'true' }); 
     var saveButton = $('#cke_' + id).find('.cke_button__save'); 
     saveButton.removeClass('cke_button_disabled'); 
    }); 
} 
0

In CKEditor 4, il plugin di salvataggio è destinata ad essere annullabile . Se non sei sicuro, puoi sempre dare un'occhiata allo source. È possibile annullare l'evento e applicare la propria logica in un gestore, come in questo esempio:

//assuming editor is a CKEDITOR.editor instance 
editor.on('save', function (event) { 
    event.cancel(); 
    //your custom command logic 
    //(you can access the editor instance through event.editor) 
}); 

vorrei consigliare contro la creazione di un nuovo comando e sostituendo il default con esso, in quanto è una soluzione inutile.

Problemi correlati