mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-14 22:09:29 +02:00
Merge pull request #6683 from mermaid-js/6576-state-diagram-label-position
6576: State diagram edge label position
This commit is contained in:
@@ -637,6 +637,11 @@ export const insertEdge = function (elem, edge, clusterDb, diagramType, startNod
|
||||
log.info('arrowTypeEnd', edge.arrowTypeEnd);
|
||||
|
||||
addEdgeMarkers(svgPath, edge, url, id, diagramType, strokeColor);
|
||||
const midIndex = Math.floor(points.length / 2);
|
||||
const point = points[midIndex];
|
||||
if (!utils.isLabelCoordinateInPath(point, svgPath.attr('d'))) {
|
||||
pointsHasChanged = true;
|
||||
}
|
||||
|
||||
let paths = {};
|
||||
if (pointsHasChanged) {
|
||||
|
@@ -884,6 +884,7 @@ export default {
|
||||
runFunc,
|
||||
entityDecode,
|
||||
insertTitle,
|
||||
isLabelCoordinateInPath,
|
||||
parseFontSize,
|
||||
InitIDGenerator,
|
||||
};
|
||||
@@ -960,3 +961,23 @@ export function handleUndefinedAttr(
|
||||
) {
|
||||
return attrValue ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the x or y coordinate of the edge label
|
||||
* appears in the given SVG path data string.
|
||||
*
|
||||
* @param point - The Point object with x and y properties to check.
|
||||
* @param dAttr - SVG path data string (the 'd' attribute of an SVG path element).
|
||||
* @returns - True if the rounded x or y coordinate of the edge label is found
|
||||
* in the sanitized path data string; otherwise, false.
|
||||
*/
|
||||
export function isLabelCoordinateInPath(point: Point, dAttr: string) {
|
||||
const roundedX = Math.round(point.x);
|
||||
const roundedY = Math.round(point.y);
|
||||
|
||||
const sanitizedD = dAttr.replace(/(\d+\.\d+)/g, (match) =>
|
||||
Math.round(parseFloat(match)).toString()
|
||||
);
|
||||
|
||||
return sanitizedD.includes(roundedX.toString()) || sanitizedD.includes(roundedY.toString());
|
||||
}
|
||||
|
Reference in New Issue
Block a user