4

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

cut
copy
paste
image properties

我想再添加两个菜单项,

test1
test2 -> subtest2
         subtest3 

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

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

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

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

4

3 回答 3

9

我也遇到了这个问题。多亏了 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
    };
  });
}

这将显示为

插入记号 -> 插入记号

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

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

于 2013-06-28T01:16:13.367 回答
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

于 2019-04-22T14:12:43.893 回答
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>
于 2016-12-22T10:13:52.307 回答