From dc8f9c645689cc7cfbf817bba231bbd6ed3d6cfa Mon Sep 17 00:00:00 2001
From: zHaytam <haytam.zanid@gmail.com>
Date: Mon, 22 Mar 2021 15:22:14 +0100
Subject: [PATCH] fix: markers wrong position

---
 Blazor.Diagrams.sln                           |  2 +-
 .../Routers/Routers.Orthogonal.cs             |  9 +-----
 .../Components/LinkWidget.razor.cs            | 31 ++++++++++++++++++-
 3 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/Blazor.Diagrams.sln b/Blazor.Diagrams.sln
index f1876bf9..520964b5 100644
--- a/Blazor.Diagrams.sln
+++ b/Blazor.Diagrams.sln
@@ -27,7 +27,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{CEEAE4C2-CE68-4FC3-9E0F-D4781B91F7F4}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazor.Diagrams.Core.Tests", "tests\Blazor.Diagrams.Core.Tests\Blazor.Diagrams.Core.Tests.csproj", "{36B4DCCD-45AB-4338-9224-DDAF386A23A3}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Diagrams.Core.Tests", "tests\Blazor.Diagrams.Core.Tests\Blazor.Diagrams.Core.Tests.csproj", "{36B4DCCD-45AB-4338-9224-DDAF386A23A3}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/src/Blazor.Diagrams.Core/Routers/Routers.Orthogonal.cs b/src/Blazor.Diagrams.Core/Routers/Routers.Orthogonal.cs
index a0d89102..3ccedbc4 100644
--- a/src/Blazor.Diagrams.Core/Routers/Routers.Orthogonal.cs
+++ b/src/Blazor.Diagrams.Core/Routers/Routers.Orthogonal.cs
@@ -83,17 +83,10 @@ public static Point[] Orthogonal(Diagram _, BaseLinkModel link)
             var origin = ExtrudeCp(originA, shapeMargin, sideA);
             var destination = ExtrudeCp(originB, shapeMargin, sideB);
 
-            var start = originA;
-            var end = originB;
-
             var path = ShortestPath(graph, origin, destination);
             if (path.Length > 0)
             {
-                var result = new List<Point>();
-                result.Add(start);
-                result.AddRange(SimplifyPath(path));
-                result.Add(end);
-                return result.ToArray();
+                return SimplifyPath(path);
             }
             else
             {
diff --git a/src/Blazor.Diagrams/Components/LinkWidget.razor.cs b/src/Blazor.Diagrams/Components/LinkWidget.razor.cs
index 5af60cf1..7eae007d 100644
--- a/src/Blazor.Diagrams/Components/LinkWidget.razor.cs
+++ b/src/Blazor.Diagrams/Components/LinkWidget.razor.cs
@@ -65,7 +65,36 @@ private LinkVertexModel CreateVertex(double clientX, double clientY, int index)
                 if (!Link.SourcePort.Initialized || Link.TargetPort?.Initialized == false)
                     return (null, null);
 
-                return (Link.SourcePort.MiddlePosition, Link.TargetPort?.MiddlePosition ?? Link.OnGoingPosition);
+                var source = GetPortPositionBasedOnAlignment(Link.SourcePort, Link.SourceMarker);
+                var target = GetPortPositionBasedOnAlignment(Link.TargetPort, Link.TargetMarker);
+                return (source, target ?? Link.OnGoingPosition);
+            }
+        }
+
+        private Point GetPortPositionBasedOnAlignment(PortModel port, LinkMarker marker)
+        {
+            if (marker == null)
+                return port.MiddlePosition;
+
+            var pt = port.Position;
+            switch (port.Alignment)
+            {
+                case PortAlignment.Top:
+                    return new Point(pt.X + port.Size.Width / 2, pt.Y);
+                case PortAlignment.TopRight:
+                    return new Point(pt.X + port.Size.Width, pt.Y);
+                case PortAlignment.Right:
+                    return new Point(pt.X + port.Size.Width, pt.Y + port.Size.Height / 2);
+                case PortAlignment.BottomRight:
+                    return new Point(pt.X + port.Size.Width, pt.Y + port.Size.Height);
+                case PortAlignment.Bottom:
+                    return new Point(pt.X + port.Size.Width / 2, pt.Y + port.Size.Height);
+                case PortAlignment.BottomLeft:
+                    return new Point(pt.X, pt.Y + port.Size.Height);
+                case PortAlignment.Left:
+                    return new Point(pt.X, pt.Y + port.Size.Height / 2);
+                default:
+                    return pt;
             }
         }