From e7e9f65e95c1f93709ca092fd1e8856ce66d2e92 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Wed, 15 May 2024 21:12:34 +0530 Subject: [PATCH] tweaks and improvements --- src/parser/flowchart.ts | 8 +++++--- src/utils.ts | 19 +++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/parser/flowchart.ts b/src/parser/flowchart.ts index 747f8e46..cae51d7c 100644 --- a/src/parser/flowchart.ts +++ b/src/parser/flowchart.ts @@ -234,6 +234,7 @@ export const parseMermaidFlowChartDiagram = ( vertices[id] = parseVertex(vertices[id], containerEl); }); + // Track the count of edges based on the edge id const edgeCountMap = new Map(); const edges = mermaidParser .getEdges() @@ -242,9 +243,10 @@ export const parseMermaidFlowChartDiagram = ( return containerEl.querySelector(`[id*="L-${edge.start}-${edge.end}"]`); }) .map((data: any) => { - const edgeIdentifier = `${data.start}-${data.end}`; - const count = edgeCountMap.get(edgeIdentifier) || 0; - edgeCountMap.set(edgeIdentifier, count + 1); + const edgeId = `${data.start}-${data.end}`; + + const count = edgeCountMap.get(edgeId) || 0; + edgeCountMap.set(edgeId, count + 1); return parseEdge(data, count, containerEl); }); diff --git a/src/utils.ts b/src/utils.ts index eeabff97..9c3e11f7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -123,17 +123,16 @@ export const computeEdgePositions = ( ) { const lastPoint = array[array.length - 1]; - /** - * Euclidean distance formula. - * If the distance between the last point and the current point (second last point) is greater than X include the last point. - * This is to ensure we have a distance for render the edge. - */ - const distance = Math.sqrt( - Math.pow(lastPoint.x - point.x, 2) + - Math.pow(lastPoint.y - point.y, 2) + // Get the distance between the last point and second last point using Euclidean distance formula + const distance = Math.hypot( + lastPoint.x - point.x, + lastPoint.y - point.y ); - - return distance > 50; + // Include the second last point if the distance between the + // last point and second last point is > 20. + // This is to ensure we have a distance for render the edge. + // 20 seems to be a good enough distance to render the edge + return distance > 20; } // Always include the start point, or if the current point is not the same as the previous point