1 | import isString from './is-string';
|
2 | import toString from './to-string';
|
3 | import { default as measureTextWidth } from './measure-text-width';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export default (function (text, maxWidth, font, str) {
|
15 | if (str === void 0) { str = '...'; }
|
16 | var STEP = 16;
|
17 | var PLACEHOLDER_WIDTH = measureTextWidth(str, font);
|
18 | var leftText = !isString(text) ? toString(text) : text;
|
19 | var leftWidth = maxWidth;
|
20 | var r = [];
|
21 | var currentText;
|
22 | var currentWidth;
|
23 | if (measureTextWidth(text, font) <= maxWidth) {
|
24 | return text;
|
25 | }
|
26 |
|
27 |
|
28 | while (true) {
|
29 |
|
30 | currentText = leftText.substr(0, STEP);
|
31 |
|
32 | currentWidth = measureTextWidth(currentText, font);
|
33 |
|
34 | if (currentWidth + PLACEHOLDER_WIDTH > leftWidth) {
|
35 | if (currentWidth > leftWidth) {
|
36 | break;
|
37 | }
|
38 | }
|
39 | r.push(currentText);
|
40 |
|
41 | leftWidth -= currentWidth;
|
42 | leftText = leftText.substr(STEP);
|
43 |
|
44 | if (!leftText) {
|
45 | return r.join('');
|
46 | }
|
47 | }
|
48 |
|
49 |
|
50 | while (true) {
|
51 |
|
52 | currentText = leftText.substr(0, 1);
|
53 |
|
54 | currentWidth = measureTextWidth(currentText, font);
|
55 |
|
56 | if (currentWidth + PLACEHOLDER_WIDTH > leftWidth) {
|
57 | break;
|
58 | }
|
59 | r.push(currentText);
|
60 |
|
61 | leftWidth -= currentWidth;
|
62 | leftText = leftText.substr(1);
|
63 | if (!leftText) {
|
64 | return r.join('');
|
65 | }
|
66 | }
|
67 | return "" + r.join('') + str;
|
68 | });
|
69 |
|
\ | No newline at end of file |