Skip to content

Commit e46cc7b

Browse files
committed
Bump to 0.5.0
Add new prop: raf (request animation frame)
1 parent 9044f0f commit e46cc7b

File tree

4 files changed

+84
-38
lines changed

4 files changed

+84
-38
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ var TextTruncate = require('react-text-truncate'); // CommonJS or UMD
2020
line={1}
2121
truncateText="…"
2222
text="Lorem Ipsum is simply dummy text of the printing and typesetting industry."
23-
showTitle={true} />
23+
showTitle={true}
24+
raf={true}
25+
/>
2426
```
2527

2628
[Demo](http://shinychang.github.io/React-Text-Truncate/)
2729

2830

2931
## Changelog
32+
* 0.5.0 Add request animation frame
3033
* 0.4.0 New property `textTruncateChild` for show more or others.
3134
* 0.3.7 Support CommonJS and UMD module loader
3235
* 0.3.5 Fix window resize issue

lib/index.js

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
22

3-
define(['module', 'exports', 'react'], function (module, exports, _react) {
3+
(function (global, factory) {
4+
if (typeof define === "function" && define.amd) {
5+
define(['module', 'exports', 'react'], factory);
6+
} else if (typeof exports !== "undefined") {
7+
factory(module, exports, require('react'));
8+
} else {
9+
var mod = {
10+
exports: {}
11+
};
12+
factory(mod, mod.exports, global.react);
13+
global.TextTruncate = mod.exports;
14+
}
15+
})(this, function (module, exports, _react) {
416
'use strict';
517

618
Object.defineProperty(exports, "__esModule", {
@@ -67,12 +79,28 @@ define(['module', 'exports', 'react'], function (module, exports, _react) {
6779
_inherits(TextTruncate, _Component);
6880

6981
function TextTruncate() {
82+
var _Object$getPrototypeO;
83+
84+
var _temp, _this, _ret;
85+
7086
_classCallCheck(this, TextTruncate);
7187

72-
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TextTruncate).call(this));
88+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
89+
args[_key] = arguments[_key];
90+
}
91+
92+
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(TextTruncate)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.animationStep = function (timeStamp) {
93+
if (timeStamp - _this.lastTime < 150) {
94+
_this.loopId = window.requestAnimationFrame(_this.animationStep);
95+
return;
96+
}
7397

74-
_this.onResize = _this.onResize.bind(_this);
75-
return _this;
98+
_this.lastTime = timeStamp;
99+
_this.onResize();
100+
_this.loopId = window.requestAnimationFrame(_this.animationStep);
101+
}, _this.onResize = function () {
102+
_this.forceUpdate();
103+
}, _temp), _possibleConstructorReturn(_this, _ret);
76104
}
77105

78106
_createClass(TextTruncate, [{
@@ -94,17 +122,21 @@ define(['module', 'exports', 'react'], function (module, exports, _react) {
94122
font.push(style['font-family']);
95123
this.canvas.font = font.join(' ');
96124
this.forceUpdate();
97-
window.addEventListener('resize', this.onResize);
125+
126+
if (this.props.raf) {
127+
this.loopId = window.requestAnimationFrame(this.animationStep);
128+
} else {
129+
window.addEventListener('resize', this.onResize);
130+
}
98131
}
99132
}, {
100133
key: 'componentWillUnmount',
101134
value: function componentWillUnmount() {
102-
window.removeEventListener('resize', this.onResize);
103-
}
104-
}, {
105-
key: 'onResize',
106-
value: function onResize() {
107-
this.forceUpdate();
135+
if (this.props.raf) {
136+
window.cancelAnimationFrame(this.loopId);
137+
} else {
138+
window.removeEventListener('resize', this.onResize);
139+
}
108140
}
109141
}, {
110142
key: 'measureWidth',
@@ -185,13 +217,15 @@ define(['module', 'exports', 'react'], function (module, exports, _react) {
185217
truncateText: _react2.default.PropTypes.string,
186218
line: _react2.default.PropTypes.number,
187219
showTitle: _react2.default.PropTypes.bool,
188-
textTruncateChild: _react2.default.PropTypes.node
220+
textTruncateChild: _react2.default.PropTypes.node,
221+
raf: _react2.default.PropTypes.bool
189222
};
190223
TextTruncate.defaultProps = {
191224
text: '',
192225
truncateText: '…',
193226
line: 1,
194-
showTitle: true
227+
showTitle: true,
228+
raf: true
195229
};
196230
exports.default = TextTruncate;
197231
;

package.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-text-truncate",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Truncate text for React.js",
55
"main": "lib/index.js",
66
"scripts": {
@@ -25,29 +25,29 @@
2525
},
2626
"homepage": "https://github.com/ShinyChang/react-text-truncate",
2727
"devDependencies": {
28-
"babel": "^6.1.18",
29-
"babel-core": "^6.2.1",
30-
"babel-loader": "^6.2.0",
28+
"babel": "^6.5.2",
29+
"babel-core": "^6.7.4",
30+
"babel-loader": "^6.2.4",
3131
"babel-plugin-add-module-exports": "^0.1.2",
32-
"babel-plugin-react-transform": "^1.1.1",
32+
"babel-plugin-react-transform": "^2.0.2",
3333
"babel-plugin-transform-es2015-modules-umd": "^6.6.5",
34-
"babel-preset-es2015": "^6.1.18",
35-
"babel-preset-react": "^6.1.18",
36-
"babel-preset-stage-0": "^6.1.18",
37-
"express": "^4.13.3",
38-
"react-transform-catch-errors": "^1.0.0",
39-
"react-transform-hmr": "^1.0.1",
40-
"redbox-react": "^1.1.1",
41-
"webpack": "^1.12.2",
42-
"webpack-dev-middleware": "^1.2.0",
43-
"webpack-hot-middleware": "^2.3.0"
34+
"babel-preset-es2015": "^6.6.0",
35+
"babel-preset-react": "^6.5.0",
36+
"babel-preset-stage-0": "^6.5.0",
37+
"express": "^4.13.4",
38+
"react-transform-catch-errors": "^1.0.2",
39+
"react-transform-hmr": "^1.0.4",
40+
"redbox-react": "^1.2.2",
41+
"webpack": "^1.12.14",
42+
"webpack-dev-middleware": "^1.5.1",
43+
"webpack-hot-middleware": "^2.10.0"
4444
},
4545
"peerDependencies": {
46-
"react": "^0.14.3",
47-
"react-dom": "^0.14.3"
46+
"react": "^0.14.7",
47+
"react-dom": "^0.14.7"
4848
},
4949
"dependencies": {
50-
"react": "^0.14.3",
51-
"react-dom": "^0.14.3"
50+
"react": "^0.14.7",
51+
"react-dom": "^0.14.7"
5252
}
5353
}

src/TextTruncate.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ export default class TextTruncate extends Component {
66
truncateText: React.PropTypes.string,
77
line: React.PropTypes.number,
88
showTitle: React.PropTypes.bool,
9-
textTruncateChild: React.PropTypes.node
9+
textTruncateChild: React.PropTypes.node,
10+
raf: React.PropTypes.bool
1011
};
1112

1213
static defaultProps = {
1314
text: '',
1415
truncateText: '…',
1516
line: 1,
16-
showTitle: true
17+
showTitle: true,
18+
raf: true
1719
};
1820

1921
componentWillMount() {
@@ -22,6 +24,7 @@ export default class TextTruncate extends Component {
2224
docFragment.appendChild(canvas);
2325
this.canvas = canvas.getContext('2d');
2426
}
27+
2528
componentDidMount() {
2629
let style = window.getComputedStyle(this.refs.scope);
2730
let font = [];
@@ -38,29 +41,34 @@ export default class TextTruncate extends Component {
3841
window.addEventListener('resize', this.onResize);
3942
}
4043
}
44+
4145
componentWillUnmount() {
4246
if (this.props.raf) {
4347
window.cancelAnimationFrame(this.loopId);
4448
} else {
4549
window.removeEventListener('resize', this.onResize);
4650
}
4751
}
48-
animationStep = timeStamp => {
52+
53+
animationStep = (timeStamp) => {
4954
if ((timeStamp - this.lastTime) < 150) {
50-
this.loopId = window.requestAnimationFrame(this.animationStep);
51-
return;
55+
this.loopId = window.requestAnimationFrame(this.animationStep);
56+
return;
5257
}
5358

5459
this.lastTime = timeStamp;
5560
this.onResize();
5661
this.loopId = window.requestAnimationFrame(this.animationStep);
5762
};
63+
5864
onResize = () => {
5965
this.forceUpdate();
6066
};
67+
6168
measureWidth(text) {
6269
return this.canvas.measureText(text).width;
6370
}
71+
6472
getRenderText() {
6573
let textWidth = this.measureWidth(this.props.text);
6674
let ellipsisWidth = this.measureWidth(this.props.truncateText);
@@ -100,6 +108,7 @@ export default class TextTruncate extends Component {
100108
: this.props.text.substr(0, startPos - 1) + this.props.truncateText;
101109
}
102110
}
111+
103112
render() {
104113
let text = '';
105114
if (this.refs.scope) {

0 commit comments

Comments
 (0)