-
Notifications
You must be signed in to change notification settings - Fork 284
/
TextTruncationUtils.ts
134 lines (127 loc) · 3.38 KB
/
TextTruncationUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import measureText from 'measure-text';
import _ from 'lodash';
export function getTextDiagonal(textHeight: number, textWidth: number) {
return Math.sqrt(Math.pow(textWidth, 2) + Math.pow(textHeight, 2));
}
export function getTextHeight(
text: string,
fontFamily: string,
fontSize: string
) {
return measureText({ text, fontFamily, fontSize, lineHeight: 1 }).height
.value;
}
export function getTextWidth(
text: string,
fontFamily: string,
fontSize: string
) {
return measureText({ text, fontFamily, fontSize, lineHeight: 1 }).width
.value;
}
function splitTextByWidth(
text: string,
maxWidth: number,
fontFamily: string,
fontSize: string
) {
const ret: string[] = [];
let index = 0;
let chunk = '';
while (index < text.length) {
chunk += text[index];
if (getTextWidth(chunk, fontFamily, fontSize) >= maxWidth) {
ret.push(chunk);
chunk = '';
}
index += 1;
}
if (chunk.length) {
ret.push(chunk);
}
return ret;
}
export function wrapText(
text: string,
maxWidth: number,
fontFamily: string,
fontSize: string
): string[] {
if (getTextWidth(text, fontFamily, fontSize) <= maxWidth) {
return [text];
} else {
// label too big, need to wrap to fit
let words = text.split(/\s+/g); // first split words, for nicer breaks if possible
// next split chunks of max width
words = _.flatten(
words.map(word =>
splitTextByWidth(word, maxWidth, fontFamily, fontSize)
)
);
let currentLine = '';
const ret: string[] = [];
for (const word of words) {
if (getTextWidth(word, fontFamily, fontSize) >= maxWidth) {
if (currentLine.length) {
ret.push(currentLine);
}
ret.push(word);
currentLine = '';
} else if (
getTextWidth(
(currentLine.length ? currentLine + ' ' : currentLine) +
word,
fontFamily,
fontSize
) >= maxWidth
) {
if (currentLine.length) {
ret.push(currentLine);
}
currentLine = word;
} else {
if (currentLine.length) {
currentLine += ' ';
}
currentLine += word;
}
}
if (currentLine.length) {
ret.push(currentLine);
}
return ret;
}
}
export function truncateWithEllipsis(
text: string,
maxWidth: number,
fontFamily: string,
fontSize: string
): string {
const wrapped = splitTextByWidth(text, maxWidth, fontFamily, fontSize);
if (wrapped.length > 1) {
return wrapped[0] + '...';
} else {
return wrapped[0];
}
}
export function truncateWithEllipsisReport(
text: string,
maxWidth: number,
fontFamily: string,
fontSize: string
) {
const wrapped = splitTextByWidth(text, maxWidth, fontFamily, fontSize);
let isTruncated = false;
if (wrapped.length > 1) {
text = wrapped[0] + '...';
isTruncated = true;
} else {
text = wrapped[0];
isTruncated = false;
}
return {
text,
isTruncated,
};
}