@@ -16,7 +16,7 @@ import {Bubble} from './bubble.js';
1616 * A bubble that displays non-editable text. Used by the warning icon.
1717 */
1818export class TextBubble extends Bubble {
19- private paragraph : SVGTextElement ;
19+ private paragraph : SVGGElement ;
2020
2121 constructor (
2222 private text : string ,
@@ -49,43 +49,52 @@ export class TextBubble extends Bubble {
4949 */
5050 private stringToSvg ( text : string , container : SVGGElement ) {
5151 const paragraph = this . createParagraph ( container ) ;
52- const spans = this . createSpans ( paragraph , text ) ;
52+ const fragments = this . createTextFragments ( paragraph , text ) ;
5353 if ( this . workspace . RTL )
54- this . rightAlignSpans ( paragraph . getBBox ( ) . width , spans ) ;
54+ this . rightAlignTextFragments ( paragraph . getBBox ( ) . width , fragments ) ;
5555 return paragraph ;
5656 }
5757
58- /** Creates the paragraph container for this bubble's view's spans . */
59- private createParagraph ( container : SVGGElement ) : SVGTextElement {
58+ /** Creates the paragraph container for this bubble's view's text fragments . */
59+ private createParagraph ( container : SVGGElement ) : SVGGElement {
6060 return dom . createSvgElement (
61- Svg . TEXT ,
61+ Svg . G ,
6262 {
6363 'class' : 'blocklyText blocklyBubbleText blocklyNoPointerEvents' ,
64- 'y' : Bubble . BORDER_WIDTH ,
64+ 'transform' : `translate(0,${ Bubble . BORDER_WIDTH } )` ,
65+ 'style' : `direction: ${ this . workspace . RTL ? 'rtl' : 'ltr' } ` ,
6566 } ,
6667 container ,
6768 ) ;
6869 }
6970
70- /** Creates the spans visualizing the text of this bubble. */
71- private createSpans ( parent : SVGTextElement , text : string ) : SVGTSpanElement [ ] {
71+ /** Creates the text fragments visualizing the text of this bubble. */
72+ private createTextFragments (
73+ parent : SVGGElement ,
74+ text : string ,
75+ ) : SVGTextElement [ ] {
76+ let lineNum = 1 ;
7277 return text . split ( '\n' ) . map ( ( line ) => {
73- const tspan = dom . createSvgElement (
74- Svg . TSPAN ,
75- { 'dy ' : '1em' , 'x' : Bubble . BORDER_WIDTH } ,
78+ const fragment = dom . createSvgElement (
79+ Svg . TEXT ,
80+ { 'y ' : ` ${ lineNum } em` , 'x' : Bubble . BORDER_WIDTH } ,
7681 parent ,
7782 ) ;
7883 const textNode = document . createTextNode ( line ) ;
79- tspan . appendChild ( textNode ) ;
80- return tspan ;
84+ fragment . appendChild ( textNode ) ;
85+ lineNum += 1 ;
86+ return fragment ;
8187 } ) ;
8288 }
8389
84- /** Right aligns the given spans. */
85- private rightAlignSpans ( maxWidth : number , spans : SVGTSpanElement [ ] ) {
86- for ( const span of spans ) {
87- span . setAttribute ( 'text-anchor' , 'end' ) ;
88- span . setAttribute ( 'x' , `${ maxWidth + Bubble . BORDER_WIDTH } ` ) ;
90+ /** Right aligns the given text fragments. */
91+ private rightAlignTextFragments (
92+ maxWidth : number ,
93+ fragments : SVGTextElement [ ] ,
94+ ) {
95+ for ( const text of fragments ) {
96+ text . setAttribute ( 'text-anchor' , 'start' ) ;
97+ text . setAttribute ( 'x' , `${ maxWidth + Bubble . BORDER_WIDTH } ` ) ;
8998 }
9099 }
91100
0 commit comments