Skip to content

Commit 2fc5745

Browse files
committed
Support Multi-line Actor Descriptions
- Add support for <br/> delimiter in actor descriptions. - Add actorFontFamily and actorFontSize options to sequence diagram. - Change default actor description font from times to sans. Fix mermaid-js#384 mermaid-js#702 mermaid-js#755
1 parent a499296 commit 2fc5745

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

dist/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@
238238

239239
<div class="mermaid">
240240
sequenceDiagram
241+
participant Alice
242+
participant Bob
243+
participant John as John<br/>Second Line
241244
Alice ->> Bob: Hello Bob, how are you?
242245
Bob-->>John: How about you John?
243246
Bob--x Alice: I am good thanks!

src/diagrams/sequence/sequenceRenderer.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const conf = {
1717
width: 150,
1818
// Height of actor boxes
1919
height: 65,
20+
actorFontSize: 14,
21+
actorFontFamily: '"Open-Sans", "sans-serif"',
2022
// Margin around loop boxes
2123
boxMargin: 10,
2224
boxTextMargin: 5,

src/diagrams/sequence/svgDraw.js

+24-16
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const drawActor = function (elem, left, verticalPos, description, conf) {
8989
drawRect(g, rect)
9090

9191
_drawTextCandidateFunc(conf)(description, g,
92-
rect.x, rect.y, rect.width, rect.height, { 'class': 'actor' })
92+
rect.x, rect.y, rect.width, rect.height, { 'class': 'actor' }, conf)
9393
}
9494

9595
export const anchorElement = function (elem) {
@@ -252,22 +252,30 @@ const _drawTextCandidateFunc = (function () {
252252
_setTextAttrs(text, textAttrs)
253253
}
254254

255-
function byTspan (content, g, x, y, width, height, textAttrs) {
256-
const text = g.append('text')
257-
.attr('x', x + width / 2).attr('y', y)
258-
.style('text-anchor', 'middle')
259-
text.append('tspan')
260-
.attr('x', x + width / 2).attr('dy', '0')
261-
.text(content)
262-
263-
text.attr('y', y + height / 2.0)
264-
.attr('dominant-baseline', 'central')
265-
.attr('alignment-baseline', 'central')
266-
267-
_setTextAttrs(text, textAttrs)
255+
function byTspan (content, g, x, y, width, height, textAttrs, conf) {
256+
const { actorFontSize, actorFontFamily } = conf
257+
258+
const lines = content.split(/<br\/?>/ig)
259+
for (let i = 0; i < lines.length; i++) {
260+
const dy = (i * actorFontSize) - (actorFontSize * (lines.length - 1) / 2)
261+
const text = g.append('text')
262+
.attr('x', x + width / 2).attr('y', y)
263+
.style('text-anchor', 'middle')
264+
.style('font-size', actorFontSize)
265+
.style('font-family', actorFontFamily)
266+
text.append('tspan')
267+
.attr('x', x + width / 2).attr('dy', dy)
268+
.text(lines[i])
269+
270+
text.attr('y', y + height / 2.0)
271+
.attr('dominant-baseline', 'central')
272+
.attr('alignment-baseline', 'central')
273+
274+
_setTextAttrs(text, textAttrs)
275+
}
268276
}
269277

270-
function byFo (content, g, x, y, width, height, textAttrs) {
278+
function byFo (content, g, x, y, width, height, textAttrs, conf) {
271279
const s = g.append('switch')
272280
const f = s.append('foreignObject')
273281
.attr('x', x).attr('y', y)
@@ -280,7 +288,7 @@ const _drawTextCandidateFunc = (function () {
280288
.style('text-align', 'center').style('vertical-align', 'middle')
281289
.text(content)
282290

283-
byTspan(content, s, x, y, width, height, textAttrs)
291+
byTspan(content, s, x, y, width, height, textAttrs, conf)
284292
_setTextAttrs(text, textAttrs)
285293
}
286294

0 commit comments

Comments
 (0)