Skip to content

Commit

Permalink
release 0.18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinyChang committed Feb 21, 2022
1 parent 2a30bbe commit 363c6fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog

- 0.18.0 Fix text width not calculate correctly
- 0.17.0 Support React 17
- 0.16.0 Change wrapper tag from div to span
- 0.15.0 Update dependencies to fix security issues
Expand Down
18 changes: 12 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
return obj;
}

var PRECISION = 0.0001;

var isEqual = function isEqual(n1, n2) {
return Math.abs(n1 - n2) < PRECISION;
};

var TextTruncate = function (_Component) {
_inherits(TextTruncate, _Component);

Expand Down Expand Up @@ -242,7 +248,7 @@
}, {
key: "measureWidth",
value: function measureWidth(text) {
return Math.ceil(this.canvas.measureText(text).width);
return this.canvas.measureText(text).width;
}
}, {
key: "getRenderText",
Expand All @@ -265,10 +271,11 @@

if (scopeWidth === 0) {
return null;
} // return if all of text can be displayed
}

var fullTextWidth = this.measureWidth(text); // return if all of text can be displayed

if (scopeWidth >= this.measureWidth(text)) {
if (scopeWidth > fullTextWidth || isEqual(scopeWidth, fullTextWidth)) {
this.onToggled(false);
return (0, _react.createElement)(textElement, props, text);
}
Expand Down Expand Up @@ -351,7 +358,7 @@
}

width = this.measureWidth(truncatedText + ext);
} while (width >= scopeWidth && truncatedText.length > 0);
} while ((width > scopeWidth || isEqual(width, scopeWidth)) && truncatedText.length > 0);

startPos += currentPos;
break;
Expand Down Expand Up @@ -406,7 +413,6 @@
fontSize = style.fontSize,
fontFamily = style.fontFamily;
var renderText = this.scope && line ? this.getRenderText() : (0, _react.createElement)(textElement, props, text);
;
var rootProps = {
ref: function ref(el) {
_this2.scope = el;
Expand Down Expand Up @@ -436,7 +442,7 @@
onTruncated: _propTypes2["default"].func,
onToggled: _propTypes2["default"].func,
text: _propTypes2["default"].string,
textElement: _propTypes2["default"].node,
textElement: _propTypes2["default"].elementType,
textTruncateChild: _propTypes2["default"].node,
truncateText: _propTypes2["default"].string,
maxCalculateTimes: _propTypes2["default"].number
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-text-truncate",
"version": "0.17.0",
"version": "0.18.0",
"description": "Truncate text for React.js",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 363c6fd

Please sign in to comment.