我使用if和条件(三元)运算符方法隐藏和禁用上下文菜单项访问
方法1:右键单击时,此条件不会显示上下文菜单项
方法2:此条件禁用contextMenu项功能
TRISTATE_OFF
||
TRISTATE_DISABLED
||
Conditional_Operator
var editor = CKEDITOR.appendTo('editorSpace', { toolbar: [] });
editor.on( 'instanceReady', function(e) {
editor.resize(200, 200);
editor.addCommand("myCommandEdit", {
exec : function( editor ){
alert("Edit DIV"); // write your own function
}});
editor.addCommand("myCommandRemove", {
exec : function( editor ){alert("Div Removed");}
});
// Listener
editor.contextMenu.addListener(function(element, selection ) {
var ascendant = element.getAscendant( 'div' );
var divCondtion = ascendant.getAttribute('class') != 'myclass';
// Method 1
if(divCondtion){
return {
div_Edit : CKEDITOR.TRISTATE_OFF,
div_Remove : CKEDITOR.TRISTATE_OFF
};
}
// Method 2
return {
div_Edit: divCondtion ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
div_Remove: divCondtion ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
};
});
editor.addMenuItems({
div_Edit : {
label : 'Edit DIV',
command : 'myCommandEdit',
group : 'DivEditGroup',
order : 1
},
div_Remove : {
label : 'Remove DIV',
command : 'myCommandRemove',
group : 'DivEditGroup',
order : 2
}
});
});