How to limit out-ports to only have a single link, allow moving of links, and how to delete a link
See original GitHub issueI am using v6.2.0 of this amazing library. I want a system with nodes that have a single in-port and multiple out-ports. I want to restrict this such that the user can only link an out-port to an in-port (i.e. the source port of a link must be an out-port and the target port must be an in-port). The user should not be able to create “loose links” (i.e. links that have one end not connected to any port). In addition, each out-port can only have a single link attached to it. I have managed to achieve this by doing the following:
...
const engine = createEngine();
const state = engine.getStateMachine().getCurrentState();
if (state instanceof DefaultDiagramState) {
state.dragNewLink.config.allowLooseLinks = false;
}
...
const model = new DiagramModel();
...
model.registerListener({
linksUpdated: (e: any) => {
if (e.isCreated) {
const link = e.link;
const sourcePort = link.getSourcePort() as DefaultPortModel;
if (Object.keys(sourcePort.getLinks()).length > 1) {
link.remove();
} else if (sourcePort.getOptions().in) {
link.remove();
}
}
}
});
engine.setModel(model);
...
Here is an example of what I can create so far:
Questions
-
Is this the best way of achieving this?
-
I also want the ability for the user to drag the endpoint of a link from one out-port to another out-port (or from one in-port to another in-port). How can this be done?
-
I am using the DefaultLinkModel and see that when you click on a link it adds a new control point to it. However, the nice Bezier curves then turn into straight lines. Is there a way to keep these lines as curves when additional control points are added? With a new point added, I can press the delete key on the keyboard to delete that point if I want to. However, I cannot work out how to just select a link so that I can delete it using the same mechanism. Could you please explain how to delete links?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:17 (5 by maintainers)
Top GitHub Comments
@ShrutiChandak19 I have managed to restrict the number of links to 1 by providing my own port model that extends this libraries
PortModel
. In the constructor of this, I call thesuper
class and pass it some options, one of which ismaximumLinks: 1
.Here is some sample code that might illustrate this better:
Hope that helps.
That’s how I did it
Thanks