@@ -407,23 +407,27 @@ export default class Draw {
407407 foreignObject : SVGForeignObjectElement = name ?. parentNode as SVGForeignObjectElement
408408
409409 const [ width , height ] : number [ ] = ( ( ) => {
410- if ( name ?. offsetWidth !== 0 ) {
410+ if ( ! this . browserIsFirefox ( ) ) {
411411 // Default case
412412 // Text is rendered based on needed width and height
413+ // works well at least for chrome and safari
413414 name . style . setProperty ( 'width' , 'auto' )
414415 name . style . setProperty ( 'height' , 'auto' )
415416 return [ name . clientWidth , name . clientHeight ]
416- } else if ( node ?. name ?. length > 0 ) {
417+ } else {
417418 // More recent versions of firefox seem to render too late to actually fetch the width and height of the dom element.
418419 // In these cases, try to approximate height and width before rendering.
419420 name . style . setProperty ( 'width' , '100%' )
420421 name . style . setProperty ( 'height' , '100%' )
421- return [ node . name . length * node . font . size / 1.9 , node . font . size * 1.2 ]
422- } else {
423- // Default values if empty
424- return [ 20 , 20 ]
422+ // split by line break
423+ const linesByLineBreaks = name . textContent . split ( / \r ? \n | \r | \n / g)
424+ // take longest line as width, when no lines are present use 1 as length
425+ const width = Math . max ( ...linesByLineBreaks . map ( ( line : string ) => line . length ) , 1 )
426+ // take number of lines as height factor
427+ const height = linesByLineBreaks . length
428+ return [ width * node . font . size / 1.2 , height * node . font . size * 1.2 ]
425429 }
426- } ) ( )
430+ } ) ( ) . map ( ( value : number ) => Math . max ( value , 25 ) )
427431
428432 foreignObject . setAttribute ( 'x' , ( - width / 2 ) . toString ( ) )
429433 foreignObject . setAttribute ( 'y' , ( - height / 2 ) . toString ( ) )
@@ -469,4 +473,11 @@ export default class Draw {
469473 return event . target [ 'classList' ] [ 0 ] === 'link-text'
470474 }
471475
476+ /**
477+ * Checks if the browser is firefox
478+ * @returns {boolean }
479+ */
480+ private browserIsFirefox ( ) : boolean {
481+ return navigator . userAgent . toLowerCase ( ) . indexOf ( 'firefox' ) > - 1
482+ }
472483}
0 commit comments