Skip to content

Feature/Multiline sequence message alignment #1315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/mermaidAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ margin around notes.
Space between messages.
**Default value 35**.

### messageAlign

Multiline message alignment. Possible values are:

- left
- center **default**
- right

### mirrorActors

mirror actors under diagram.
Expand Down
38 changes: 27 additions & 11 deletions src/diagrams/sequence/sequenceRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const conf = {
noteMargin: 10,
// Space between messages
messageMargin: 35,
// Multiline message alignment
messageAlign: 'center',
// mirror actors under diagram
mirrorActors: false,
// Depending on css styling this might need adjustment
Expand Down Expand Up @@ -230,24 +232,38 @@ const drawMessage = function(elem, startx, stopx, verticalPos, msg, sequenceInde
const g = elem.append('g');
const txtCenter = startx + (stopx - startx) / 2;

let textElem;
let textElems = [];
let counterBreaklines = 0;
let breaklineOffset = 17;
const breaklines = msg.message.split(/<br\s*\/?>/gi);
for (const breakline of breaklines) {
textElem = g
.append('text') // text label for the x axis
.attr('x', txtCenter)
.attr('y', verticalPos - 7 + counterBreaklines * breaklineOffset)
.style('text-anchor', 'middle')
.attr('class', 'messageText')
.text(breakline.trim());
textElems.push(
g
.append('text') // text label for the x axis
.attr('x', txtCenter)
.attr('y', verticalPos - 7 + counterBreaklines * breaklineOffset)
.style('text-anchor', 'middle')
.attr('class', 'messageText')
.text(breakline.trim())
);
counterBreaklines++;
}
const offsetLineCounter = counterBreaklines - 1;
const totalOffset = offsetLineCounter * breaklineOffset;

let textWidth = (textElem._groups || textElem)[0][0].getBBox().width;
let textWidths = textElems.map(function(textElem) {
return (textElem._groups || textElem)[0][0].getBBox().width;
});
let textWidth = Math.max(...textWidths);
for (const textElem of textElems) {
if (conf.messageAlign === 'left') {
textElem.attr('x', txtCenter - textWidth / 2).style('text-anchor', 'start');
} else if (conf.messageAlign === 'right') {
textElem.attr('x', txtCenter + textWidth / 2).style('text-anchor', 'end');
}
}

bounds.bumpVerticalPos(totalOffset);

let line;
if (startx === stopx) {
Expand Down Expand Up @@ -295,9 +311,9 @@ const drawMessage = function(elem, startx, stopx, verticalPos, msg, sequenceInde
} else {
line = g.append('line');
line.attr('x1', startx);
line.attr('y1', verticalPos);
line.attr('y1', verticalPos + totalOffset);
line.attr('x2', stopx);
line.attr('y2', verticalPos);
line.attr('y2', verticalPos + totalOffset);
bounds.insert(
startx,
bounds.getVerticalPos() - 10 + totalOffset,
Expand Down
9 changes: 9 additions & 0 deletions src/mermaidAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ const config = {
*/
messageMargin: 35,

/**
* Multiline message alignment. Possible values are:
* * left
* * center **default**
* * right
*/
messageAlign: 'center',

/**
* mirror actors under diagram.
* **Default value true**.
Expand Down Expand Up @@ -809,6 +817,7 @@ export default mermaidAPI;
* boxTextMargin:5,
* noteMargin:10,
* messageMargin:35,
* messageAlign:'center',
* mirrorActors:true,
* bottomMarginAdj:1,
* useMaxWidth:true,
Expand Down