问题
I want to add a context menu item for selected image elements only. The context menu item is currently working but it shows up on every element instead of only image elements. Here is my code so far:
CKEDITOR.on('instanceReady', function(ev) {
editor.addCommand('editImgCmd', {
exec : function( editor ) {
alert('editImgCmd');
}
});
var editImgCmd = {
label : editor.lang.image.menu,
command : 'editImgCmd',
group : 'image'
};
editor.contextMenu.addListener(function(element, selection ) {
return {
editImgCmd : CKEDITOR.TRISTATE_ON
};
});
editor.addMenuItems({
editImgCmd : {
label : 'Edit Image',
command : 'editImgCmd',
group : 'image',
order : 2
}
});
});
回答1:
Use getAscendant() to chcek the element is an img
:
editor.contextMenu.addListener( function( element, selection ) {
if ( element.getAscendant( 'img', true ) ) {
return {
来源:https://stackoverflow.com/questions/37553405/ckeditor-add-context-menu-item-to-images