I have a diagram that can be build up by the user by dragging over elements from the palette. I wanted to prevent user from building invalid diagram - all elements should be linked (but drop should be allowed for first node as there must be initial point). I found on forum a code below, but it does not do what I really wanted here (note: notification code is my custom function):
diagram.addDiagramListener('ExternalObjectsDropped', function (_e: DiagramEvent) {
const newnode = diagram.selection.first();
if (
newnode &&
(newnode as Node).linksConnected.count === 0 &&
diagram.model.nodeDataArray.length > 1
) {
diagram.commandHandler.deleteSelection();
if (showNotificationBox) {
showNotificationBox(
DIAGRAM_NOTIFICATION.ERROR,
i18next.t('policy.classification.custom.modal.dropNotAllowed')
);
}
}
});
diagram.commandHandler.deleteSelection();
removes the current existing node from the diagram and keeps a newly dragged node, but I wanted to remove the dragged node and keep the current diagram structure instead.
When I changed this function to diagram.currentTool.doCancel();
is does nothing, new node is dropped.
Is this possible to do using this event?
Before I tried using mouseDrop diagram function like this:
myDiagram.mouseDrop = function(e) {
myDiagram.currentTool.doCancel();
}
It worked correctly for dropping new nodes from the palette but prevented the user from moving the existing node around the diagram, each time I moved node to a different place it was moved back with notification.
So what I wanted to achieve is allow moving nodes around, allow to drop node on a node (I have a function that automatically links them), prevent from dropping on the diagram if not linked.