My modified solution to achieve the “insert node on existing link” functionality is as follows:
function dropTaskOnTransition(e, oldlink) {
const { diagram } = e;
const { type, state } = oldlink.data;
const newTask = diagram.selection.first();
if (newTask.linksConnected.count > 0) return;
const toTask = oldlink.toNode;
// Reconnect the existing link to the new node
oldlink.toNode = newTask;
// Then add links from the new node to the old node
diagram.model.addLinkData({
type,
state: state === 'loop' ? 'success' : state,
from: newTask.key,
to: toTask.key,
});
}
...
mouseDragEnter: (e, link) => {
link.opacity = 0.3;
link.isShadowed = true;
},
mouseDragLeave: (e, link) => {
link.opacity = 1;
link.isShadowed = false;
},
mouseDrop: dropTaskOnTransition
At the moment, the dragEnter/hover interaction is reducing the opacity. How would I go about implementing a preview of what the new links would look like onDragEnter and only adding to the model data on drop, like currently implemented?
Thanks in advance for any information you can provide!
Create an unmodeled Node and two unmodeled Links that are just like what you expect will exist if the drop happens. Put the temporary Node and Links in the “Tool” Layer so that they are in front of everything and so that those changes aren’t recorded in the UndoManager.
Unrelated example of adding unmodeled Nodes and Links to a Diagram: Unbound Parts
@walter Thanks for these examples! These got me close to my end goal. Calling the following function on mouseDragEnter of the current link successfully adds the unmodeled links to the diagram but when it reaches the attempt to add the unmodeled node, the app crashes with the error below.
So it looks like it was complaining about Node being the constructor. I switched it to Part and it doesn’t crash anymore.
So now it works as expected except the newly added nodes don’t display on the diagram until the mouseDrop event, even though they are added during the mouseDragEnter. Say I mouseDragEnter the link 12 times, then once I drop, 12 nodes then appear instead of appearing on mouseDragEnter.
The Node identifier is correctly imported and it is used in other parts of the app as well, not sure what’s going on.
Regardless, adding the location property fixed the delayed rendering of the node, I think I am able to move forward at the moment, thanks for taking time to help with these blockers!