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

Create link only from port 'Out'

Open silvacpp opened this issue 8 years ago • 5 comments

Sorry for bad english, i want to disallow links to be created by 'In' port, bidirectional links didn't fit in my diagram, i want a way to create a one way directional links .

silvacpp avatar Mar 07 '18 01:03 silvacpp

you can probably do this with a custom port model. See this line for details on how to implement it yourself:

https://github.com/projectstorm/react-diagrams/blob/ed50438744728066c31daafc087c176aa18cc25e/src/models/PortModel.ts#L94

you would basically return true/false to enable or disable linking. You can also control whether a link can be created at all on this line:

https://github.com/projectstorm/react-diagrams/blob/ed50438744728066c31daafc087c176aa18cc25e/src/models/PortModel.ts#L75

^ here you would return null to prevent a link from being created.

dylanvorster avatar Mar 10 '18 13:03 dylanvorster

This doesn't seem to work for me. When I return null from my PortModel's createLinkModel I get this error:

Uncaught TypeError: Cannot read property 'setSourcePort' of null

image

I believe this call to link.setSourcePort on line 502 should be moved inside the following IF block:

https://github.com/projectstorm/react-diagrams/blob/a95010652dc16bd47375d7e4715cd02953ea75c2/src/widgets/DiagramWidget.tsx#L501-L504

purplesquirrels avatar Mar 15 '19 02:03 purplesquirrels

This doesn't seem to work for me. When I return null from my PortModel's createLinkModel I get this error:

Uncaught TypeError: Cannot read property 'setSourcePort' of null

image

I believe this call to link.setSourcePort on line 502 should be moved inside the following IF block:

https://github.com/projectstorm/react-diagrams/blob/a95010652dc16bd47375d7e4715cd02953ea75c2/src/widgets/DiagramWidget.tsx#L501-L504

same issue

sjw441500 avatar Nov 05 '19 19:11 sjw441500

Same issue!

yossicahen avatar Feb 23 '20 15:02 yossicahen

I've faced the same issue. To solve, you need to create a CustomPortModel (you will need also a <CustomPortWidget /> and a CustomPortFactory as well) and implement the following function:

canLinkToPort = (targetPort: DefaultPortModel) => {
  const links = Object.values(this.getLinks() || {});
  return (
    this.getParent().getID() !== targetPort.getParent().getID() &&
    links.length === 1 &&
    targetPort.getName() === "in"
  );
};

Ps.: you can change the last instruction to match your use case or just assume that all the in ports are called "in".

erickvieira avatar Sep 22 '20 22:09 erickvieira