diff --git a/samples/TestComponents/Simple.razor.cs b/samples/TestComponents/Simple.razor.cs index c0f51c46..dcb031a5 100644 --- a/samples/TestComponents/Simple.razor.cs +++ b/samples/TestComponents/Simple.razor.cs @@ -14,7 +14,7 @@ protected override void OnInitialized() var node1 = NewNode(0, 0); var node2 = NewNode(300, 300); - node1.GetPort(PortAlignment.RIGHT).AddLink(node2.GetPort(PortAlignment.LEFT)); + diagramManager.AddLink(node1.GetPort(PortAlignment.RIGHT), node2.GetPort(PortAlignment.LEFT)); diagramManager.AddNode(node1); diagramManager.AddNode(node2); } diff --git a/src/Blazor.Diagrams.Core/DiagramManager.cs b/src/Blazor.Diagrams.Core/DiagramManager.cs index 0a1eb56b..6db4ab7c 100644 --- a/src/Blazor.Diagrams.Core/DiagramManager.cs +++ b/src/Blazor.Diagrams.Core/DiagramManager.cs @@ -22,6 +22,7 @@ public class DiagramManager public event Action NodeAdded; public event Action NodeRemoved; public event Action NodeSelectionChanged; + public event Action LinkAdded; public DiagramManager() { @@ -50,6 +51,15 @@ public void RemoveNode(Node node) } } + public Link AddLink(Port source, Port target) + { + var link = new Link(source, target); + source.AddLink(link); + target.AddLink(link); + LinkAdded?.Invoke(link); + return link; + } + public void SelectNode(Node node) { if (SelectedNode == node) diff --git a/src/Blazor.Diagrams.Core/Models/Port.cs b/src/Blazor.Diagrams.Core/Models/Port.cs index 36b20704..99812ee9 100644 --- a/src/Blazor.Diagrams.Core/Models/Port.cs +++ b/src/Blazor.Diagrams.Core/Models/Port.cs @@ -1,9 +1,12 @@ using System.Collections.Generic; +using System.Collections.ObjectModel; namespace Blazor.Diagrams.Core.Models { public class Port : Model { + private List _links = new List(4); + public Port(PortAlignment alignment = PortAlignment.BOTTOM, Point position = null, Size size = null) { Alignment = alignment; @@ -23,22 +26,14 @@ public Port(string id, PortAlignment alignment = PortAlignment.BOTTOM, Point pos public Point Offset { get; set; } public Point Position { get; set; } public Size Size { get; set; } - public List Links { get; } = new List(); - - public Link AddLink(Port targetPort) - { - var link = new Link(this, targetPort); - AddLink(link); - targetPort.AddLink(link); - return link; - } - - internal void AddLink(Link link) => Links.Add(link); + public ReadOnlyCollection Links => _links.AsReadOnly(); public void RefreshAll() { Refresh(); - Links.ForEach(l => l.Refresh()); + _links.ForEach(l => l.Refresh()); } + + internal void AddLink(Link link) => _links.Add(link); } }