MxGraph 中节点、边和 canvas 的自定义(不同)右键单击菜单

Custom(different) right-click menus for node, edge and canvas in MxGraph

我需要在右键单击上下文菜单中提供不同的 actions/options,具体取决于用户是单击节点还是边缘还是空白 canvas 区域。

我一直在研究 drawio/jgraph 源代码,特别是 menus.js 文件,以了解右键单击菜单是如何特定于上下文的。 draw.io 应用程序似乎在 canvas 与顶点和边上有不同的右键单击菜单。但是我还没有遇到区分所点击对象的代码。

如有任何帮助或指点,我们将不胜感激。

你应该扩展 graph.popupMenuHandler.factoryMethod 功能,让我给你一个代码示例

graph 成为您的 graph 对象

graph.popupMenuHandler.factoryMethod = function(menu, cell, evt)
{
  if(cell.edge){
      menu.addItem('First edge option', null, function()
      { 
        alert('This is the first option of edge ');
      })
      menu.addItem('Second edge option', null, function()
      {
        alert('This is the second option of edge ');
      })
  }
  if(cell.vertex){
      menu.addItem('First vertex option', null, function()
      {
        alert('This is the first option of vertex ');
      })
      menu.addItem('Second vertex option', null, function()
      {
        alert('This is the second option of vertex ');
      })
  }
}

希望这对您有所帮助:)