Skip to content

Commit

Permalink
tweaks and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ad1992 committed May 15, 2024
1 parent 835da69 commit e7e9f65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/parser/flowchart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number>();
const edges = mermaidParser
.getEdges()
Expand All @@ -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);
});
Expand Down
19 changes: 9 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e7e9f65

Please sign in to comment.