代码之家  ›  专栏  ›  技术社区  ›  Irina

如何禁用某些元素的CKEditor上下文菜单?

  •  0
  • Irina  · 技术社区  · 7 年前

    我有一些 <div> 具有特殊类的元素,我希望它们不允许通过上下文菜单进行编辑。 有没有办法做到这一点? 我试过:

    CKEDITOR.dtd.$nonEditable = "div(myclass)";
    

    allowedContent: {
                        $1: {
                            elements: CKEDITOR.dtd,
                            attributes: true,
                            styles: true,
                            classes: true
                        }
                    },
                    disallowedContent: "div(myclass)",
    

    我还试图排除 <部门> 从…起 menu_groups: ''; ,但我不能排除 <部门> 这是特定的课程,只有全部。

    enter image description here

    附言:我需要保持沉默 右键菜单 插件,只需删除某些元素。

    0 回复  |  直到 7 年前
        1
  •  1
  •   Yasar    5 年前

    我使用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
       }
    });
    

    });

    推荐文章