2010-07-28 8 views

Respuesta

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

Si desea anular el comando Guardar por sólo un caso, se puede tratar el siguiente código:

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

Esto debería funcionar para cualquier comando CKEditor.

+0

Si sobrescribe la función exec de esta manera, se ejecutará sin ningún parámetro. Pero dado que normalmente tiene la variable 'editor' en el ámbito externo, aún podría trabajar con ella. Solo recuerde omitir el parámetro 'editor' en su definición de función porque de otro modo sobrescribiría su variable 'editor' de ámbito. – flu

10

El current top answer confundió la agrupación de barras de herramientas para mí (coloque el botón guardar al final), y el other answer no funcionó en ckeditor v4.

Así es como hacerlo en 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

muy buen ejemplo, gracias: D – Zombyii

+0

Esto funcionó para mí. – 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

En CKEditor 4, el plugin ahorrar está destinado a ser cancelable . Si no está seguro, siempre se puede echar un vistazo al source. Puede cancelar el evento y aplicar su propia lógica en un controlador, como en este ejemplo:

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

aconsejaría contra la creación de un nuevo comando y reemplazar el valor por defecto con él, ya que es una solución innecesaria.

Cuestiones relacionadas