Skip to content

Commit 6e5e677

Browse files
committed
refactor with text-anchor middle
1 parent 4d45cac commit 6e5e677

File tree

7 files changed

+67
-89
lines changed

7 files changed

+67
-89
lines changed

css/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ svg text {
9797
#drawing text, .link--labels text {
9898
pointer-events: none;
9999
}
100+
#drawing text {
101+
text-anchor: middle;
102+
}
100103

101104
.node { cursor: pointer; }
102105
.link, .link--dashed {

index.html

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
Config.handleH = texts.wordText.maxHeight + (Config.textPaddingY * 2);
7373
Config.wordHeight = texts.wordText.maxHeight + (Config.textPaddingY * 2);
74-
Config.minWordWidth = Math.max( getTextWidthAndHeight("i", texts.wordText.style).w + (Config.handleW * 2), getTextWidthAndHeight("i", texts.tagText.style).w + (Config.handleW * 2) );
74+
Config.minWordWidth = getTextWidth("i", texts.wordText.style) + (Config.handleW * 2);
7575

7676
var rowOffsetX = 0;
7777
var rowOffsetWord = null;
@@ -124,7 +124,7 @@
124124

125125
[].concat(parser.data.entities, parser.data.triggers).forEach(el => {
126126
if (words[el.tokenIndex]) {
127-
words[el.tokenIndex].tag = el.type;
127+
words[el.tokenIndex].setTag(el.type);
128128
words[el.tokenIndex].eventId = el.id;
129129
}
130130
});
@@ -286,33 +286,10 @@
286286
let row = rows[rows.length - 1];
287287
row.words.push(newWord);
288288

289-
290-
var wh = getTextWidthAndHeight(newWord.val, texts.wordText.style);
291-
292-
//calculate the width of the Word
293-
newWord.tw = wh.w;
294-
newWord.maxtextw = wh.w;
295-
296-
if (newWord.tag != null) {
297-
var twh = getTextWidthAndHeight(newWord.tag, texts.tagText.style);
298-
299-
if (twh.w > word.tw) {
300-
newWord.tw = twh.w; //think tw is ONLY used for checking minWidth, so this should be ok
301-
newWord.maxtextw = twh.w;
302-
}
303-
}
304289
//calculate x position and width of the Word
305290
newWord.wx = 80;
306291
newWord.ww = newWord.tw + (Config.textPaddingX * 2);
307-
308-
309-
if (newWord.tag != null) {
310-
newWord.tww = newWord.ww; //maxtextw + (Config.textPaddingX * 2);
311-
newWord.twx = newWord.wx;
312-
}
313-
newWord.h = 0; //the number of link levels is 0 for the word itself
314-
newWord.wh = texts.wordText.maxHeight + Config.textPaddingY*2;
315-
newWord.wy = row.ry + rows[0].rh - newWord.wh;
292+
newWord.wy = row.wordY;
316293
newWord.row = row;
317294

318295

js/interact.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function moveWordToNewPosition(w, nx, ny) {
490490
// called this again after bbox because possible race issues?
491491
w.underneathRect.x(nx);
492492
w.underneathRect.y(ny);
493-
w.text.x(nx + (w.bbox.w/2) - (w.text.length()/2) );
493+
w.text.x(nx + w.bbox.w/2);
494494
w.text.y(ny + Config.textPaddingY*2); // - texts.wordText.descent);
495495

496496

@@ -499,7 +499,7 @@ function moveWordToNewPosition(w, nx, ny) {
499499
//Looks like the entire row size jumps one pixel when adding a new row... think this may cause the 1 px differentce
500500

501501
if (w.tag != null) {
502-
w.tagtext.x(nx + (w.bbox.w/2) - (w.tagtext.length()/2) );
502+
w.tagtext.x(nx + w.bbox.w/2);
503503
w.tagtext.y(ny + Config.textPaddingY/2);// - texts.tagText.descent);
504504

505505
var handley = ny + ( w.wh / 2 ) - ( Config.handleH / 2 );
@@ -682,12 +682,11 @@ function setUpWordDraggable(word) {
682682

683683
function setWordToY(word, wy) {
684684
word.underneathRect.y(wy);
685-
word.bbox = word.underneathRect.bbox();
686-
word.text.y(word.bbox.y + Config.textPaddingY*2);
685+
word.text.y(wy + Config.textPaddingY*2);
687686

688-
if (word.tag != null) {
689-
word.tagtext.y(word.bbox.y + Config.textPaddingY/2);
690-
word.leftHandle.y(wy);
691-
word.rightHandle.y(wy);
692-
}
687+
if (word.tag != null) {
688+
word.tagtext.y(wy + Config.textPaddingY/2);
689+
word.leftHandle.y(wy);
690+
word.rightHandle.y(wy);
691+
}
693692
}

js/render.js

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -286,22 +286,7 @@ function setUpRowsAndWords(words) {
286286
for (var i = 0; i < words.length; i++) {
287287

288288
var word = words[i];
289-
290-
var wh = getTextWidthAndHeight(word.val, texts.wordText.style);
291-
292-
//calculate the width of the Word
293-
word.tw = wh.w;
294-
word.maxtextw = wh.w;
295289

296-
if (word.tag != null) {
297-
var twh = getTextWidthAndHeight(word.tag, texts.tagText.style);
298-
299-
if (twh.w > word.tw) {
300-
word.tw = twh.w; //think tw is ONLY used for checking minWidth, so this should be ok
301-
word.maxtextw = twh.w;
302-
}
303-
}
304-
305290
//what row will this Word belong to?
306291

307292
if (i == 0) { //if first word, then obviously in first row
@@ -332,12 +317,6 @@ function setUpRowsAndWords(words) {
332317
word.wx = x;
333318
word.ww = word.tw + (Config.textPaddingX * 2);
334319

335-
336-
if (word.tag != null) {
337-
word.tww = word.ww; //maxtextw + (Config.textPaddingX * 2);
338-
word.twx = word.wx;
339-
}
340-
341320
x += word.ww + Config.wordPadding;
342321
}
343322

@@ -358,9 +337,7 @@ function setUpRowsAndWords(words) {
358337
for (var ii = 0; ii < row.words.length; ii++) {
359338
var word = row.words[ii];
360339

361-
word.h = 0; //the number of link levels is 0 for the word itself
362-
word.wh = texts.wordText.maxHeight + Config.textPaddingY*2;
363-
word.wy = row.ry + row.rh - word.wh;
340+
word.wy = row.wordY;
364341

365342
if (word.tag != null) {
366343
var tag_textwh = getTextWidthAndHeight(word.tag, texts.tagText.style);
@@ -1045,16 +1022,27 @@ function calculateLinkLabels(idx, rowNum, x, y, link, isHidden) {
10451022

10461023
var twh = link.textWH;
10471024

1048-
var rect = {x: (x - 2 - twh.w/2), y: (y - link.textStyle.maxHeight/2), w: (twh.w + 4), h: link.textStyle.maxHeight, style:style, rowNum: rowNum};
1049-
1050-
1051-
if (Config.redraw) {
1052-
text = {text: link.textStr, x: (x - twh.w/2), y: (y - link.textStyle.maxHeight/2), style: (link.textStyle.style), visibility:true};
1053-
}
1054-
else {
1055-
text = {text: link.textStr, x: (x - twh.w/2), y: (y - link.textStyle.maxHeight/2 - link.textStyle.descent), style: (link.textStyle.style), visibility:true};
1056-
}
1057-
link.labels[idx] = {rect:rect, text:text};
1025+
var rect = {
1026+
x: (x - 2 - twh.w/2),
1027+
y: (y - link.textStyle.maxHeight/2),
1028+
w: (twh.w + 4),
1029+
h: link.textStyle.maxHeight,
1030+
style:style,
1031+
rowNum: rowNum
1032+
};
1033+
1034+
y -= Config.redraw ?
1035+
(link.textStyle.maxHeight/2) :
1036+
(link.textStyle.maxHeight/2 + link.textStyle.descent);
1037+
text = {
1038+
x,
1039+
y,
1040+
text: link.textStr,
1041+
style: link.textStyle.style,
1042+
visibility:true
1043+
};
1044+
1045+
link.labels[idx] = {rect, text};
10581046

10591047
if (isHidden) {
10601048
link.labels[idx].text.visibility = false;

js/row.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ class Row {
118118
return returnVal;
119119
}
120120

121-
121+
get wordY() {
122+
return this.ry + this.rh - (texts.wordText.maxHeight + Config.textPaddingY*2);
123+
}
122124
get minWidth() {
123125
return this.words.reduce((acc, val) => acc + val.minWidth, 0);
124126
}

js/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function createTestWords(numWords, minLength, maxLength) {
77
var w = new Word(getLetterString(i, minLength, maxLength), i);
88

99
if (Math.random() < 0.5) {
10-
w.tag = getRandomString(3,10);
10+
w.setTag(getRandomString(3,10));
1111
}
1212

1313

@@ -222,7 +222,12 @@ function getRandomStyle() {
222222

223223
}
224224

225-
225+
function getTextWidth(word, fs) {
226+
let t = draw.text(word).font(fs)
227+
let l = t.length();
228+
t.remove();
229+
return l;
230+
}
226231
function getTextWidthAndHeight(word, fs) {
227232
var text2 = draw.text(word).font(fs);
228233
textbbox = text2.bbox();

js/word.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class 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

Comments
 (0)