1- function _typeof ( obj ) { return obj && typeof Symbol !== "undefined" && obj . constructor === Symbol ? "symbol" : typeof obj ; }
2-
31( function ( global , factory ) {
42 if ( typeof define === "function" && define . amd ) {
53 define ( [ 'module' , 'exports' , 'react' ] , factory ) ;
@@ -33,7 +31,7 @@ function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.const
3331 }
3432 }
3533
36- var _createClass = ( function ( ) {
34+ var _createClass = function ( ) {
3735 function defineProperties ( target , props ) {
3836 for ( var i = 0 ; i < props . length ; i ++ ) {
3937 var descriptor = props [ i ] ;
@@ -49,14 +47,14 @@ function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.const
4947 if ( staticProps ) defineProperties ( Constructor , staticProps ) ;
5048 return Constructor ;
5149 } ;
52- } ) ( ) ;
50+ } ( ) ;
5351
5452 function _possibleConstructorReturn ( self , call ) {
5553 if ( ! self ) {
5654 throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ;
5755 }
5856
59- return call && ( ( typeof call === 'undefined' ? 'undefined' : _typeof ( call ) ) === "object" || typeof call === "function" ) ? call : self ;
57+ return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ;
6058 }
6159
6260 function _inherits ( subClass , superClass ) {
@@ -75,7 +73,7 @@ function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.const
7573 if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ;
7674 }
7775
78- var TextTruncate = ( function ( _Component ) {
76+ var TextTruncate = function ( _Component ) {
7977 _inherits ( TextTruncate , _Component ) ;
8078
8179 function TextTruncate ( ) {
@@ -104,23 +102,15 @@ function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.const
104102 }
105103
106104 _createClass ( TextTruncate , [ {
107- key : 'componentWillMount ' ,
108- value : function componentWillMount ( ) {
105+ key : 'componentDidMount ' ,
106+ value : function componentDidMount ( ) {
109107 var canvas = document . createElement ( 'canvas' ) ;
110108 var docFragment = document . createDocumentFragment ( ) ;
109+ var style = window . getComputedStyle ( this . refs . scope ) ;
110+ var font = [ style [ 'font-weight' ] , style [ 'font-style' ] , style [ 'font-size' ] , style [ 'font-family' ] ] . join ( ' ' ) ;
111111 docFragment . appendChild ( canvas ) ;
112112 this . canvas = canvas . getContext ( '2d' ) ;
113- }
114- } , {
115- key : 'componentDidMount' ,
116- value : function componentDidMount ( ) {
117- var style = window . getComputedStyle ( this . refs . scope ) ;
118- var font = [ ] ;
119- font . push ( style [ 'font-weight' ] ) ;
120- font . push ( style [ 'font-style' ] ) ;
121- font . push ( style [ 'font-size' ] ) ;
122- font . push ( style [ 'font-family' ] ) ;
123- this . canvas . font = font . join ( ' ' ) ;
113+ this . canvas . font = font ;
124114 this . forceUpdate ( ) ;
125115
126116 if ( this . props . raf ) {
@@ -147,39 +137,56 @@ function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.const
147137 key : 'getRenderText' ,
148138 value : function getRenderText ( ) {
149139 var textWidth = this . measureWidth ( this . props . text ) ;
150- var ellipsisWidth = this . measureWidth ( this . props . truncateText ) ;
151- var scopeWidth = this . refs . scope . offsetWidth ;
152-
140+ var scopeWidth = this . refs . scope . getBoundingClientRect ( ) . width ;
153141 if ( scopeWidth >= textWidth ) {
154142 return this . props . text ;
155143 } else {
156- var n = 0 ;
157- var max = this . props . text . length ;
144+ var currentPos = 1 ;
145+ var maxTextLength = this . props . text . length ;
158146 var text = '' ;
159147 var splitPos = 0 ;
160148 var startPos = 0 ;
161149 var line = this . props . line ;
150+ var width = 0 ;
151+ var lastIsEng = false ;
162152 while ( line -- ) {
163153 var ext = line ? '' : this . props . truncateText ;
164- while ( n <= max ) {
165- n ++ ;
166- text = this . props . text . substr ( startPos , n ) ;
167- if ( this . measureWidth ( text + ext ) > scopeWidth ) {
168- splitPos = text . lastIndexOf ( ' ' ) ;
154+ while ( currentPos <= maxTextLength ) {
155+ text = this . props . text . substr ( startPos , currentPos ) ;
156+ width = this . measureWidth ( text + ext ) ;
157+ if ( width < scopeWidth ) {
158+ splitPos = this . props . text . indexOf ( ' ' , currentPos + 1 ) ;
169159 if ( splitPos === - 1 ) {
170- splitPos = n - 1 ;
160+ currentPos += 1 ;
161+ lastIsEng = false ;
162+ } else {
163+ lastIsEng = true ;
164+ currentPos = splitPos ;
171165 }
172- startPos += splitPos ;
166+ } else {
167+ do {
168+ currentPos -- ;
169+ text = this . props . text . substr ( startPos , currentPos ) ;
170+ if ( text [ text . length - 1 ] === ' ' ) {
171+ text = this . props . text . substr ( startPos , currentPos - 1 ) ;
172+ }
173+ if ( lastIsEng ) {
174+ currentPos = text . lastIndexOf ( ' ' ) ;
175+ text = this . props . text . substr ( startPos , currentPos ) ;
176+ }
177+ width = this . measureWidth ( text + ext ) ;
178+ } while ( width >= scopeWidth ) ;
179+ startPos += currentPos ;
173180 break ;
174181 }
175182 }
176- if ( n >= max ) {
177- startPos = max ;
183+
184+ if ( currentPos >= maxTextLength ) {
185+ startPos = maxTextLength ;
178186 break ;
179187 }
180- n = 0 ;
181188 }
182- return startPos === max ? this . props . text : this . props . text . substr ( 0 , startPos - 1 ) + this . props . truncateText ;
189+ return startPos === maxTextLength ? this . props . text : this . props . text . substr ( 0 , startPos ) + this . props . truncateText ;
183190 }
184191 }
185192 } , {
@@ -189,28 +196,27 @@ function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.const
189196 if ( this . refs . scope ) {
190197 text = this . getRenderText ( ) ;
191198 }
192- var attrs = {
193- ref : 'scope'
194- } ;
199+
200+ var attrs = { } ;
195201 if ( this . props . showTitle ) {
196202 attrs . title = this . props . text ;
197203 }
198204
199205 return _react2 . default . createElement (
200206 'div' ,
201- null ,
207+ { ref : 'scope' , style : { overflow : 'hidden' } } ,
202208 _react2 . default . createElement (
203209 'div' ,
204210 attrs ,
205211 text
206212 ) ,
207- this . props . textTruncateChild
213+ this . props . text === text ? null : this . props . textTruncateChild
208214 ) ;
209215 }
210216 } ] ) ;
211217
212218 return TextTruncate ;
213- } ) ( _react . Component ) ;
219+ } ( _react . Component ) ;
214220
215221 TextTruncate . propTypes = {
216222 text : _react2 . default . PropTypes . string ,
0 commit comments