如何在CkEditor中右键单击添加其他菜单项?

时间:2013-05-12 11:29:59

标签: php javascript ckeditor

在CKeditor中,当我们右键单击图像时,会出现四个菜单项。

cut
copy
paste
image properties

我想再添加两个菜单项

test1
test2 -> subtest2
         subtest3 

test1将是一个菜单,test2将有两个子菜单。

另外,如何为此新菜单项添加操作?例如,单击test1应在右侧绘制边框。点击subtest2将添加图像阴影等。

除此之外。当我们右键单击div和table时,我想做类似的事情。

我找到了上下文菜单插件,但我需要知道如何使用它。

4 个答案:

答案 0 :(得分:7)

我也遇到了这个问题。感谢CKEditor的错误文档,我终于在整个下午尝试了不同的方式。 这段代码在我的网站上运行良好 - Drupal 6& CKEditor 4。

// Assume I already Have 3 commands
// insertTick, insertTickxxx, and insertTickxxxandxxx

if (editor.addMenuItems) {
  // 1st, add a Menu Group
  // tip: name it the same as your plugin. (I'm not sure about this)
  editor.addMenuGroup('tick', 3);

  // 2nd, use addMenuItems to add items
  editor.addMenuItems({
      // 2.1 add the group again, and give it getItems, return all the child items
      tick :
      {
        label : 'Insert Tick',
        group : 'tick',
        order : 21,
        getItems : function() {
          return {
            tick_insertTick : CKEDITOR.TRISTATE_OFF,
            tick_insertQuestionMark : CKEDITOR.TRISTATE_OFF,
            tick_insertTickandQuestion : CKEDITOR.TRISTATE_OFF
          };
        }
      },

      // 2.2 Now add the child items to the group.
      tick_insertTick :
      {
        label : 'Insert a tick',
        group : 'tick',
        command : 'insertTick',
        order : 22
      },

      tick_insertQuestionMark :
      {
        label : 'Insert Question Mark',
        group : 'tick',
        command : 'insertQuestionMark',
        order : 23
      },

      tick_insertTickandQuestion :
      {
        label : 'insert Tick and Question',
        group : 'tick',
        command : 'insertTickandQuestion',
        order : 24
      }
  });
}

// 3rd, add Listener, and return the Menu Group
if (editor.contextMenu) {
  editor.contextMenu.addListener(function(element, selection) {
    return {
      tick : CKEDITOR.TRISTATE_OFF
    };
  });
}

这将显示为

插入勾选 - >插入一个勾号

--------------插入问号

--------------插入勾号和问号

答案 1 :(得分:1)

请参阅此链接http://blog.ale-re.net/2010/06/ckeditor-context-menu.html,其中详细说明了您想要的内容

答案 2 :(得分:1)

  1. 注册新的ckEditor命令;
  2. 添加新的菜单组;
  3. 添加新菜单项;
  4. 将点击事件侦听器添加到上下文菜单。

        // Register a command execute on context menu item click
        editor.addCommand('test1', {
            exec : editor => {
                console.log(editor);
                alert('test1');
            }
        });
    
        // Check for context menu and add new item/s
        if ( editor.contextMenu ) {
            // Add group and item/s
            editor.addMenuGroup( 'testGroup' );
            editor.addMenuItem( 'testItem', {
                label: 'Test 1',
                icon: this.path + 'icons/test.png',
                command: 'test1',
                group: 'testGroup'
            });
    
            // Add event listener
            editor.contextMenu.addListener(element => {
                console.log(element);
                return { testItem: CKEDITOR.TRISTATE_OFF };
            });
        }
    

有关更多信息,请检查此链接: https://ckeditor.com/docs/ckeditor4/latest/guide/plugin_sdk_sample_2.html

答案 3 :(得分:0)

我也尝试相同。最后我得到了一个解决方案。所以在这里我分享它。它可以帮助别人。

      <script type="text/javascript">
   // Menu code
 var config = {
  toolbar: [],
removePlugins : 'pastetext,clipboard'   
 };

var editor = CKEDITOR.appendTo('ckeditor', config);



editor.on( 'instanceReady', function(e) { 
 var e = e.editor;

  // Commands
e.addCommand("cv_claimant_Birthdate", {
    exec: function(e) {
        e.insertText("\{\!claimant.Birthdate\}");
    }
});
e.addCommand("cv_claimant_Name", {
    exec: function(e) {
        e.insertText("\{\!claimant.Name\}");
    }
});
e.addCommand("cv_claim_Name", {
    exec: function(e) {
        e.insertText("\{\!claim.Name\}");
    }
});
e.addCommand("cv_claim_CreatedDate", {
    exec: function(e) {
        e.insertText("\{\!claim.CreatedDate\}");
    }
});

// Listener
e.contextMenu.addListener(function(element, selection) {
    return {
        cv: CKEDITOR.TRISTATE_ON
    };
});

// Menu Groups; different numbers needed for the group separator to show
e.addMenuGroup("cv", 500);
e.addMenuGroup("cv_people", 100);
e.addMenuGroup("cv_claim", 200);

// Menus (nested)
e.addMenuItems({
    // Top level
    cv: {
        label: "Insert Merge Field",
        group: "cv",
        getItems: function() {
            return {
                cv_claimant: CKEDITOR.TRISTATE_OFF,
                cv_claim: CKEDITOR.TRISTATE_OFF,
            };
        }
    },
    // One sub-menu
    cv_claimant: {
        label: "Claimant Person (claimant)",
        group: "cv_people",
        getItems: function() {
            return {
                cv_claimant_Birthdate: CKEDITOR.TRISTATE_OFF,
                cv_claimant_Name: CKEDITOR.TRISTATE_OFF,
            };
        }
    },
    // These run commands
    cv_claimant_Birthdate: {
        label: "Birthdate (Birthdate: date)",
        group: "cv_people",
        command: "cv_claimant_Birthdate"
    },
    cv_claimant_Name: {
        label: "Full Name (Name: string)",
        group: "cv_people",
        command: "cv_claimant_Name"
    },
    // Another sub-menu
    cv_claim: {
        label: "Claim (claim)",
        group: "cv_claim",
        getItems: function() {
            return {
                cv_claim_CreatedDate: CKEDITOR.TRISTATE_OFF,
                cv_claim_Name: CKEDITOR.TRISTATE_OFF,
            };
        }
    },
    // These run commands
    cv_claim_Name: {
        label: "Claim Number (Name: string)",
        group: "cv_claim",
        command: "cv_claim_Name"
    },
    cv_claim_CreatedDate: {
        label: "Created Date (CreatedDate: datetime)",
        group: "cv_claim",
        command: "cv_claim_CreatedDate"
    },
});
 });   






</script>