react-diagrams icon indicating copy to clipboard operation
react-diagrams copied to clipboard

How delete links without targetPort linked

Open edwinjoel1984 opened this issue 7 years ago • 3 comments

Hello.

I am trying to create a series of connected nodes, but I have not been able to control when a link is created without a target.

links

Any help would be very useful.

edwinjoel1984 avatar Aug 30 '19 19:08 edwinjoel1984

@edwinjoel1984 Were you able to figure out an approach for this?

timrbula avatar Sep 16 '19 19:09 timrbula

This how I make it works. In diagramModel, I add an event listener: diagramModel.addListener({ linksUpdated: this.removeNotValidLink, });

When you created new link, or update targetPort of a link, linksUpdated() event should be called, and then trigger function removeNotValidLink(). On removeNotValidLink(), I map though all links and remove which have targetPort === null. For better user experience and performance, I will trigger removeNotValidLink() after 10s since the last event listener linksUpdated() call, and delete all previous setTimeout()

removeNotValidLink = () => {
    if (checkValidLinkTimeout) {
      clearTimeout(checkValidLinkTimeout);
    }
    checkValidLinkTimeout = setTimeout(() => {
      const model = app.getDiagramEngine().getDiagramModel();
      const listLink = Object.values(model.getLinks());
      listLink.forEach(link => {
        if (!link.targetPort) {
          model.removeLink(link);
          // console.log('invalid');
          this.forceUpdate();
        }
      });
    }, 10000);
  };

I hope that work for you!

TranVanKung avatar Nov 20 '19 09:11 TranVanKung

Hi there, folks in the future

link.targetPort is a protected property.

this.props.app.getModal() is :

public getModel(){
   return this.diagramEngine.getModel();
}

And in your container do the following.

Here I am using @emotion to create

You can put onMouseUp in your container

also

<S.Body onMouseUp={() => {
      const model = this.props.app.getModel();
      const listLink = Object.values(model.getLinks());
      listLink.forEach(link => {
	      if(!link.getTargetPort()){
		      model.removeLink(link);
	      }
       });
}}>

That's how I did it, thanks!

vivek-verse avatar May 17 '22 06:05 vivek-verse