61298

CKEditor - Add Context Menu Item to Images

Question:

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

Answer1:

Use <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.dom.node-method-getAscendant" rel="nofollow">getAscendant()</a> to chcek the element is an img:

editor.contextMenu.addListener( function( element, selection ) { if ( element.getAscendant( 'img', true ) ) { return {

Recommend