@@ -24,6 +24,8 @@ const conf = {
24
24
noteMargin : 10 ,
25
25
// Space between messages
26
26
messageMargin : 35 ,
27
+ // Multiline message alignment
28
+ messageAlign : 'center' ,
27
29
// mirror actors under diagram
28
30
mirrorActors : false ,
29
31
// Depending on css styling this might need adjustment
@@ -230,26 +232,38 @@ const drawMessage = function(elem, startx, stopx, verticalPos, msg, sequenceInde
230
232
const g = elem . append ( 'g' ) ;
231
233
const txtCenter = startx + ( stopx - startx ) / 2 ;
232
234
233
- let textElem ;
235
+ let textElems = [ ] ;
234
236
let counterBreaklines = 0 ;
235
237
let breaklineOffset = 17 ;
236
238
const breaklines = msg . message . split ( / < b r \s * \/ ? > / gi) ;
237
239
for ( const breakline of breaklines ) {
238
- textElem = g
239
- . append ( 'text' ) // text label for the x axis
240
- . attr ( 'x' , txtCenter )
241
- . attr ( 'y' , verticalPos - 7 + counterBreaklines * breaklineOffset )
242
- . style ( 'text-anchor' , 'middle' )
243
- . attr ( 'class' , 'messageText' )
244
- . text ( breakline . trim ( ) ) ;
240
+ textElems . push (
241
+ g
242
+ . append ( 'text' ) // text label for the x axis
243
+ . attr ( 'x' , txtCenter )
244
+ . attr ( 'y' , verticalPos - 7 + counterBreaklines * breaklineOffset )
245
+ . style ( 'text-anchor' , 'middle' )
246
+ . attr ( 'class' , 'messageText' )
247
+ . text ( breakline . trim ( ) )
248
+ ) ;
245
249
counterBreaklines ++ ;
246
250
}
247
251
const offsetLineCounter = counterBreaklines - 1 ;
248
252
const totalOffset = offsetLineCounter * breaklineOffset ;
249
253
250
- bounds . bumpVerticalPos ( totalOffset ) ;
254
+ let textWidths = textElems . map ( function ( textElem ) {
255
+ return ( textElem . _groups || textElem ) [ 0 ] [ 0 ] . getBBox ( ) . width ;
256
+ } ) ;
257
+ let textWidth = Math . max ( ...textWidths ) ;
258
+ for ( const textElem of textElems ) {
259
+ if ( conf . messageAlign === 'left' ) {
260
+ textElem . attr ( 'x' , txtCenter - textWidth / 2 ) . style ( 'text-anchor' , 'start' ) ;
261
+ } else if ( conf . messageAlign === 'right' ) {
262
+ textElem . attr ( 'x' , txtCenter + textWidth / 2 ) . style ( 'text-anchor' , 'end' ) ;
263
+ }
264
+ }
251
265
252
- let textWidth = ( textElem . _groups || textElem ) [ 0 ] [ 0 ] . getBBox ( ) . width ;
266
+ bounds . bumpVerticalPos ( totalOffset ) ;
253
267
254
268
let line ;
255
269
if ( startx === stopx ) {
0 commit comments