Blazor-Diagrams / Blazor.Diagrams

A fully customizable and extensible all-purpose diagrams library for Blazor
https://blazor-diagrams.zhaytam.com
MIT License
921 stars 177 forks source link

Event to notify when two nodes are linked and provide the changed ports. #182

Closed jake1164 closed 1 year ago

jake1164 commented 2 years ago

Based on #121 I understand the best way to get notified when two nodes have been linked is to use the SourcePortChanged and TargetPortChanged.

However, It would be a lot cleaner to just subscribe to an event in BaseLinkModel.cs:

        /// <summary>
        /// An event that fires when two ports are linked.
        /// </summary>
        public event Action<BaseLinkModel, PortModel, PortModel>? NodesLinked;

Then in the SetTargetPort & SetSourcePort just fire the event with the source and target ports.


        public void SetSourcePort(PortModel port)
        {
            if (SourcePort == port)
                return;

            var old = SourcePort;
            SourcePort?.RemoveLink(this);
            SourcePort = port;
            SourcePort.AddLink(this);
            SourceNode = SourcePort.Parent;
            SourcePortChanged?.Invoke(this, old, SourcePort);

            if (TargetPort != null)
                NodesLinked?.Invoke(this, SourcePort, TargetPort);
        }

        public void SetTargetPort(PortModel? port)
        {
            if (TargetPort == port)
                return;

            var old = TargetPort;
            TargetPort?.RemoveLink(this);
            TargetPort = port;
            TargetPort?.AddLink(this);
            TargetNode = TargetPort?.Parent;
            TargetPortChanged?.Invoke(this, old, TargetPort);

            if (SourcePort != null && TargetPort != null)
                NodesLinked?.Invoke(this, SourcePort, TargetPort);
        }
jake1164 commented 2 years ago

I can gladly provide a pull request and update samples. I am also flexible on the Event name 'NodesLinked'.

Rickybarb5 commented 2 years ago

I can gladly provide a pull request and update samples. I am also flexible on the Event name 'NodesLinked'.

We used to have this with the LinkAttached event, but it was removed on 2.0.0 (not sure why, maybe performance reasons).

jake1164 commented 2 years ago

Looks like the alternative in 2.0.0 as per #75 is TargetPortChanged, which does not provide the same functionality and pushes a lot of logic into the client.

zHaytam commented 1 year ago

Part of #214