@@ -46,7 +46,7 @@ export default class Ellipsis extends Component {
46
46
}
47
47
}
48
48
49
- componentWillReceiveProps ( nextProps ) {
49
+ componenthillReceiveProps ( nextProps ) {
50
50
if ( this . props . lines !== nextProps . lines ) {
51
51
this . computeLine ( ) ;
52
52
}
@@ -55,17 +55,15 @@ export default class Ellipsis extends Component {
55
55
computeLine = ( ) => {
56
56
const { lines } = this . props ;
57
57
if ( lines && ! isSupportLineClamp ) {
58
- const fontSize = parseInt ( window . getComputedStyle ( this . node ) . fontSize , 10 ) || 14 ;
59
58
const text = this . shadowChildren . innerText ;
60
- const targetWidth = ( this . node . offsetWidth || this . node . parentNode . offsetWidth ) * lines ;
59
+ const targetHeight = this . root . offsetHeight ;
61
60
const shadowNode = this . shadow . firstChild ;
62
61
63
62
// bisection
64
- const tw = ( targetWidth - ( lines * ( fontSize / 2 ) ) - fontSize ) ;
65
63
const len = text . length ;
66
64
const mid = Math . floor ( len / 2 ) ;
67
65
68
- const count = this . bisection ( tw , mid , 0 , len , text , shadowNode ) ;
66
+ const count = this . bisection ( targetHeight , mid , 0 , len , text , shadowNode ) ;
69
67
70
68
this . setState ( {
71
69
text,
@@ -74,40 +72,45 @@ export default class Ellipsis extends Component {
74
72
}
75
73
}
76
74
77
- bisection = ( tw , m , b , e , text , shadowNode ) => {
75
+ bisection = ( th , m , b , e , text , shadowNode ) => {
76
+ const suffix = '...' ;
78
77
let mid = m ;
79
78
let end = e ;
80
79
let begin = b ;
81
- shadowNode . innerHTML = text . substring ( 0 , mid ) ;
82
- let sw = shadowNode . offsetWidth ;
80
+ shadowNode . innerHTML = text . substring ( 0 , mid ) + suffix ;
81
+ let sh = shadowNode . offsetHeight ;
83
82
84
- if ( sw < tw ) {
85
- shadowNode . innerHTML = text . substring ( 0 , mid + 1 ) ;
86
- sw = shadowNode . offsetWidth ;
87
- if ( sw >= tw ) {
83
+ if ( sh < th ) {
84
+ shadowNode . innerHTML = text . substring ( 0 , mid + 1 ) + suffix ;
85
+ sh = shadowNode . offsetHeight ;
86
+ if ( sh >= th ) {
88
87
return mid ;
89
88
} else {
90
89
begin = mid ;
91
90
mid = Math . floor ( ( end - begin ) / 2 ) + begin ;
92
- return this . bisection ( tw , mid , begin , end , text , shadowNode ) ;
91
+ return this . bisection ( th , mid , begin , end , text , shadowNode ) ;
93
92
}
94
93
} else {
95
94
if ( mid - 1 < 0 ) {
96
95
return mid ;
97
96
}
98
- shadowNode . innerHTML = text . substring ( 0 , mid - 1 ) ;
99
- sw = shadowNode . offsetWidth ;
100
- if ( sw <= tw ) {
97
+ shadowNode . innerHTML = text . substring ( 0 , mid - 1 ) + suffix ;
98
+ sh = shadowNode . offsetHeight ;
99
+ if ( sh <= th ) {
101
100
return mid ;
102
101
} else {
103
102
end = mid ;
104
103
mid = Math . floor ( ( end - begin ) / 2 ) + begin ;
105
- return this . bisection ( tw , mid , begin , end , text , shadowNode ) ;
104
+ return this . bisection ( th , mid , begin , end , text , shadowNode ) ;
106
105
}
107
106
}
108
107
}
109
108
110
- handleRef = ( n ) => {
109
+ handleRoot = ( n ) => {
110
+ this . root = n ;
111
+ }
112
+
113
+ handleNode = ( n ) => {
111
114
this . node = n ;
112
115
}
113
116
@@ -130,7 +133,6 @@ export default class Ellipsis extends Component {
130
133
...restProps
131
134
} = this . props ;
132
135
133
-
134
136
const cls = classNames ( styles . ellipsis , className , {
135
137
[ styles . lines ] : ( lines && ! isSupportLineClamp ) ,
136
138
[ styles . lineClamp ] : ( lines && isSupportLineClamp ) ,
@@ -160,7 +162,7 @@ export default class Ellipsis extends Component {
160
162
}
161
163
162
164
const childNode = (
163
- < span >
165
+ < span ref = { this . handleNode } >
164
166
{
165
167
( targetCount > 0 ) && text . substring ( 0 , targetCount )
166
168
}
@@ -173,7 +175,7 @@ export default class Ellipsis extends Component {
173
175
return (
174
176
< div
175
177
{ ...restProps }
176
- ref = { this . handleRef }
178
+ ref = { this . handleRoot }
177
179
className = { cls }
178
180
>
179
181
{
0 commit comments