Hi everyone! I tried to build a plugin that add some items in context menu to change the format of selected text. The items are supposed to appear when the selection is not empty and I use addMenuItem and removeMenuItem to implement it. However, this doesn't always work and sometimes I have to click right button twice so that those items can appear. Thank you!
Here is my code:
editor.on("selectionChange",
function(){
editor.removeMenuItem('questionMarker');
editor.removeMenuItem('claimMarker');
editor.removeMenuItem('proofMarker');
var sel = editor.getSelection()
if(!(sel.getSelectedText()=="")){
if (editor.addMenuItem) {
editor.addMenuGroup('Marker');
editor.addMenuItem('questionMarker', {
label: 'Mark as question',
command: 'markQuestion',
group: 'Marker'
});
editor.addMenuItem('claimMarker', {
label: 'Mark as claim',
command: 'markClaim',
group: 'Marker'
});
editor.addMenuItem('proofMarker', {
label: 'Mark as proof',
command: 'markProof',
group: 'Marker'
});
}
}
}
);
});

Hi,
Hi,
it is not neccessary to add/remove the menuItems dynamically. You can enable/deactivate it.
Heres an example how we use it in our plugin (its inside the init-function of the plugin), lets say we want to edit a tag called 'testtag':
if (editor.contextMenu) { editor.addMenuGroup('myGroup'); editor.addMenuItem('testtagItem', { label: 'Edit Testtag', icon: '<path to icon>', command: '<name of dialog to open on click>', group: 'myGroup' }); editor.contextMenu.addListener( function( element ) { if ( element.getAscendant( 'testtag', true ) ) { return { testtagItem: CKEDITOR.TRISTATE_OFF }; } }); }In this case, the menu Item is only visible if we open the context menue on a testtag element.
I hope this will help you.
Bye