11class Word {
2- constructor ( val , idx ) {
2+ constructor ( val , idx , tag = null ) {
33 this . val = val ;
44 this . idx = idx ;
55 this . id = `(${ this . val } , ${ this . idx } )` ;
66
7- this . tag = null ;
87 this . h = 0 ; //num slots
98
109 this . ww = 0 ;
11- this . wh = 0 ;
10+ this . wh = texts . wordText . maxHeight + Config . textPaddingY * 2 ;
1211 this . wx = 0 ;
1312 this . wy = 0 ;
1413
@@ -19,8 +18,11 @@ class Word {
1918 this . parentsR = [ ] ; //who connects to me and is attached to my right side
2019 this . parentsC = [ ] ; //who connects to me and is attached to the center (ie, for multilinks)
2120
22- this . tw = 0 ; //width of text part of word, used also to determine minimum size of word rect
23- this . th = 0 ;
21+ let wh = getTextWidthAndHeight ( val , texts . wordText . style ) ;
22+ this . tw = wh . w ; //width of text part of word, used also to determine minimum size of word rect
23+ this . th = wh . h ;
24+
25+ this . setTag ( tag ) ;
2426
2527 this . percPos = 0.0 ; //this is used to indicate where along the row the word is positioned, used when resizing the browser's width, or when popping open a right panel.
2628
@@ -39,16 +41,13 @@ class Word {
3941 this . text = null ; //the svg text
4042 this . tagtext = null ; //the svg text for a tag
4143
42- this . maxtextw = null ; //either the word text width, or the tag text with, whichever is wider
43-
4444 this . leftHandle = null ; //the left draggable handle to resize word
4545 this . rightHandle = null ; //the right draggable handle to resize word
4646
4747 this . tempX = 0.0 ;
4848 this . tempW = 0.0 ;
4949 this . tempY = 0.0 ;
5050 }
51-
5251
5352 //take temp values and update actual svg values
5453 update ( ) {
@@ -57,11 +56,11 @@ class Word {
5756
5857 this . bbox = this . underneathRect . bbox ( ) ;
5958
60- this . text . x ( this . tempX + ( this . tempW / 2 ) - ( this . text . length ( ) / 2 ) ) ;
59+ this . text . x ( this . tempX + this . tempW / 2 ) ;
6160 this . rightX = this . tempX + this . tempW ;
6261
6362 if ( this . tag != null ) {
64- this . tagtext . x ( this . tempX + ( this . tempW / 2 ) - ( this . tagtext . length ( ) / 2 ) ) ;
63+ this . tagtext . x ( this . tempX + this . tempW / 2 ) ;
6564
6665 this . leftHandle . x ( this . tempX ) ;
6766 this . rightHandle . x ( this . rightX - Config . handleW ) ;
@@ -72,19 +71,25 @@ class Word {
7271 this . percPos = ( this . leftX - Config . edgePadding ) / ( Config . svgWidth - Config . edgePadding * 2 ) ;
7372 }
7473
75- draw ( ) {
74+ setTag ( tag ) {
75+ let tagw = tag === null ? 0 : getTextWidth ( tag , texts . tagText . style ) ;
76+ this . tw = Math . max ( tagw , this . tw ) ;
77+ this . tag = tag ;
78+ }
79+
80+ draw ( ) {
81+
82+
7683 let g = this . svg = draw . group ( ) . addClass ( 'word' ) ;
7784
7885 this . underneathRect = g . rect ( this . ww , this . wh )
7986 . x ( this . wx )
8087 . y ( this . wy )
8188 . addClass ( 'word--underneath' ) ;
8289
83- var textwh = getTextWidthAndHeight ( this . val , texts . wordText . style ) ;
84-
8590 this . text = g . text ( this . val )
8691 . y ( this . wy + Config . textPaddingY * 2 - texts . wordText . descent )
87- . x ( this . wx + ( this . ww / 2 ) - ( textwh . w / 2 ) )
92+ . x ( this . wx + this . ww / 2 )
8893 . font ( texts . wordText . style ) ;
8994
9095 this . bbox = this . underneathRect . bbox ( ) ;
@@ -93,8 +98,7 @@ class Word {
9398 this . percPos = ( this . leftX - Config . edgePadding ) / ( Config . svgWidth - Config . edgePadding * 2 ) ;
9499
95100 if ( this . tag != null ) {
96- var textwh = getTextWidthAndHeight ( this . tag , texts . tagText . style ) ;
97- var tagXPos = this . twx + ( this . ww / 2 ) - ( textwh . w / 2 ) ;
101+ var tagXPos = this . wx + this . ww / 2 ;
98102
99103 //add in tag text, if the word has an associated tag
100104 this . tagtext = g . text ( this . tag )
@@ -130,7 +134,7 @@ class Word {
130134 }
131135
132136 get minWidth ( ) { //min width is the maximum of: the word text, the tag text, or the size of the two handles + a little bit
133- return Math . max ( Config . minWordWidth , this . maxtextw ) ;
137+ return Math . max ( Config . minWordWidth , this . tw ) ;
134138 }
135139
136140 //maxWidth() must return a value less than row width - Config.edgePaddings, else will try to reposition long words forever!!!
0 commit comments