From 20fd6d35f070909a8d0cef836c0feac892bd14e8 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Sun, 3 Sep 2023 10:46:26 +0530 Subject: [PATCH] refactor: Tidy up direction handling --- .../src/diagrams/sequence/sequenceRenderer.ts | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts b/packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts index 4bc8f7487..a596a3a02 100644 --- a/packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts +++ b/packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts @@ -1405,10 +1405,21 @@ const buildMessageModel = function (msg, actors, diagObj) { } const [fromLeft, fromRight] = activationBounds(msg.from, actors); const [toLeft, toRight] = activationBounds(msg.to, actors); - const arrowDirection = fromLeft <= toLeft ? 'right' : 'left'; - const startx = arrowDirection === 'right' ? fromRight : fromLeft; - let stopx = arrowDirection === 'right' ? toLeft : toRight; - const isToActivation = Math.abs(toLeft - toRight) > 2; + const isArrowToRight = fromLeft <= toLeft; + const startx = isArrowToRight ? fromRight : fromLeft; + let stopx = isArrowToRight ? toLeft : toRight; + + // As the line width is considered, the left and right values will be off by 2. + const isArrowToActivation = Math.abs(toLeft - toRight) > 2; + + /** + * Adjust the value based on the arrow direction + * @param value - The value to adjust + * @returns The adjustment with correct sign to be added to the actual value. + */ + const adjustValue = (value: number) => { + return isArrowToRight ? -value : value; + }; /** * This is an edge case for the first activation. @@ -1417,12 +1428,8 @@ const buildMessageModel = function (msg, actors, diagObj) { * In cases where the message is to an activation that was properly detected, we don't want to move the arrow head * The activation will not be detected on the first message, so we need to move the arrow head */ - if (msg.activate && !isToActivation) { - if (arrowDirection === 'right') { - stopx -= conf.activationWidth / 2 - 1; - } else { - stopx += conf.activationWidth / 2 - 1; - } + if (msg.activate && !isArrowToActivation) { + stopx += adjustValue(conf.activationWidth / 2 - 1); } /** @@ -1430,11 +1437,7 @@ const buildMessageModel = function (msg, actors, diagObj) { * This is not required for open arrows that don't have arrowheads */ if (![diagObj.db.LINETYPE.SOLID_OPEN, diagObj.db.LINETYPE.DOTTED_OPEN].includes(msg.type)) { - if (arrowDirection === 'right') { - stopx -= 3; - } else { - stopx += 3; - } + stopx += adjustValue(3); } const allBounds = [fromLeft, fromRight, toLeft, toRight];