1
1
import setAttributes from '../utils/setAttributes' ;
2
2
import normalizeColor from '../utils/normalizeColor' ;
3
3
4
+ /**
5
+ * Wrap each line of given text in a `<tspan>` element and append these
6
+ * lines to the given SVGTextElement
7
+ *
8
+ * @param {SVGTextElement } textElement A text element to hold the split text
9
+ * @param {String } textContent String to render with line breaks
10
+ */
11
+ function insertLineBreaks ( textElement , textContent ) {
12
+ const lines = ( textContent || '' ) . split ( '\n' ) ;
13
+ // can't use dy attribute here since we want empty lines to take up space as well,
14
+ // so we will update y manually based on font size
15
+ const x = textElement . getAttribute ( 'x' ) ;
16
+ let y = Number ( textElement . getAttribute ( 'y' ) ) ;
17
+ const size = Number ( textElement . getAttribute ( 'font-size' ) ) ;
18
+ for ( const line of lines ) {
19
+ const tspan = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'tspan' ) ;
20
+ tspan . setAttribute ( 'y' , y . toString ( ) ) ;
21
+ tspan . setAttribute ( 'x' , x ) ;
22
+ tspan . innerHTML = line ;
23
+ textElement . appendChild ( tspan ) ;
24
+
25
+ y += size ;
26
+ }
27
+ }
28
+
4
29
/**
5
30
* Create SVGTextElement from an annotation definition.
6
31
* This is used for anntations of type `textbox`.
@@ -20,7 +45,8 @@ export default function renderText(a) {
20
45
transform : `rotate(${ a . rotation } )` ,
21
46
style : 'white-space: pre'
22
47
} ) ;
23
- text . innerHTML = a . content ;
48
+
49
+ insertLineBreaks ( text , a . content ) ;
24
50
25
51
let g = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'g' ) ;
26
52
g . appendChild ( text ) ;
0 commit comments