I have a “…” button with a context menu attached to it. I want the delete menu button to actually delete this node. This is a code that I have and it actually works:
nodeTemplate = new go.Node(go.Panel.Auto, {
width: 100,
height: 40,
})
.add(
new go.Shape("RoundedRectangle", {
fill: "whitesmoke",
strokeWidth: 1,
isPanelMain: true,
})
)
.add(
go.GraphObject.make("Button", {
alignment: go.Spot.RightCenter,
contextMenu: go.GraphObject.make("ContextMenu").add(
go.GraphObject.make("ContextMenuButton", {
click: (e: go.InputEvent, obj: go.GraphObject) => {
const node = e.diagram.findNodeForKey(obj?.part?.data?.key);
if (node) e.diagram.select(node);
e.diagram.commandHandler.deleteSelection();
},
}).add(new go.TextBlock("Delete"))
),
click: (e: go.InputEvent) => {
if (e.targetObject) {
e.diagram.commandHandler.showContextMenu(e.targetObject);
}
},
}).add(new go.TextBlock("..."))
);
But I have two questions:
- I feel like there must be an easier way to get the
Node
inside the click handler without resorting toe.diagram.findNodeForKey()
. Perhaps fromobj
or theevent
. But I couldn’t find anything. - Is there any API that
commandHanlder
can accept a node to delete/copy/paste/cut that is not the current selection?