react-show-more-text 1.6.0 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -7
- package/lib/ShowMoreText.css +8 -0
- package/lib/ShowMoreText.js +21 -59
- package/lib/Truncate.js +46 -120
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
# React Show More Text 1.6.
|
|
1
|
+
# React Show More Text 1.6.2
|
|
2
2
|
|
|
3
3
|
[![NPM version][npm-image]][npm-url]
|
|
4
4
|
[![Downloads][downloads-image]][npm-url]
|
|
5
5
|
[![Build status][travis-image]][travis-url]
|
|
6
|
-
[![Dependency status][david-dm-image]][david-dm-url]
|
|
7
6
|
|
|
8
7
|
The text surrounded by the component will be truncated. Anything surrounded by the component could be evaluated as text. The component react-show-more-text/ShowMoreText is fork of react-show-more/ShowMore, applied improvements, works with React 16.x.x, React 18.x.x added onClick event.
|
|
9
8
|
|
|
@@ -37,7 +36,7 @@ class Foo extends Component {
|
|
|
37
36
|
more="Show more"
|
|
38
37
|
less="Show less"
|
|
39
38
|
className="content-css"
|
|
40
|
-
anchorClass="
|
|
39
|
+
anchorClass="show-more-less-clickable"
|
|
41
40
|
onClick={this.executeOnClick}
|
|
42
41
|
expanded={false}
|
|
43
42
|
width={280}
|
|
@@ -128,7 +127,5 @@ $ npm run storybook --legacy-peer-deps=true
|
|
|
128
127
|
[npm-url]: https://npmjs.org/package/react-show-more-text
|
|
129
128
|
[downloads-image]: http://img.shields.io/npm/dm/react-show-more-text.svg
|
|
130
129
|
[npm-image]: https://badge.fury.io/js/react-show-more-text.svg
|
|
131
|
-
[travis-url]: https://travis-ci.com/devzonetech/react-show-more-text
|
|
132
|
-
[travis-image]: https://travis-ci.com/devzonetech/react-show-more-text.svg?branch=master
|
|
133
|
-
[david-dm-url]: https://david-dm.org/devzonetech/react-show-more-text
|
|
134
|
-
[david-dm-image]: https://david-dm.org/devzonetech/react-show-more-text.svg
|
|
130
|
+
[travis-url]: https://app.travis-ci.com/github/devzonetech/react-show-more-text
|
|
131
|
+
[travis-image]: https://travis-ci.com/devzonetech/react-show-more-text.svg?branch=master
|
package/lib/ShowMoreText.js
CHANGED
|
@@ -1,85 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _react = _interopRequireWildcard(require("react"));
|
|
11
|
-
|
|
12
9
|
var _propTypes = require("prop-types");
|
|
13
|
-
|
|
14
10
|
var _Truncate = _interopRequireDefault(require("./Truncate"));
|
|
15
|
-
|
|
11
|
+
require("./ShowMoreText.css");
|
|
16
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
|
|
18
13
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
19
|
-
|
|
20
14
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
-
|
|
22
15
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
23
|
-
|
|
24
16
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
25
|
-
|
|
26
17
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
27
|
-
|
|
28
18
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
29
|
-
|
|
30
19
|
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
31
|
-
|
|
32
20
|
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
33
|
-
|
|
34
21
|
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
35
|
-
|
|
36
22
|
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
37
|
-
|
|
38
23
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
39
|
-
|
|
40
24
|
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
41
|
-
|
|
42
25
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
43
|
-
|
|
44
26
|
var ShowMoreText = /*#__PURE__*/function (_Component) {
|
|
45
27
|
_inherits(ShowMoreText, _Component);
|
|
46
|
-
|
|
47
28
|
var _super = _createSuper(ShowMoreText);
|
|
48
|
-
|
|
49
29
|
function ShowMoreText(props) {
|
|
50
30
|
var _this;
|
|
51
|
-
|
|
52
31
|
_classCallCheck(this, ShowMoreText);
|
|
53
|
-
|
|
54
32
|
_this = _super.call(this, props);
|
|
55
|
-
|
|
56
33
|
_defineProperty(_assertThisInitialized(_this), "_isMounted", false);
|
|
57
|
-
|
|
58
34
|
_defineProperty(_assertThisInitialized(_this), "handleTruncate", function (truncated) {
|
|
59
35
|
if (_this._isMounted && truncated !== _this.state.truncated) {
|
|
60
36
|
_this.setState({
|
|
61
37
|
truncated: truncated
|
|
62
38
|
});
|
|
63
|
-
|
|
64
39
|
if (truncated) {
|
|
65
40
|
_this.truncateRef.onResize();
|
|
66
41
|
}
|
|
42
|
+
_this.props.onTruncate && _this.props.onTruncate();
|
|
67
43
|
}
|
|
68
44
|
});
|
|
69
|
-
|
|
70
45
|
_defineProperty(_assertThisInitialized(_this), "toggleLines", function (event) {
|
|
71
46
|
event.preventDefault();
|
|
72
|
-
|
|
73
47
|
var _self = _assertThisInitialized(_this);
|
|
74
|
-
|
|
75
48
|
if (!_self.props.expandByClick) {
|
|
76
49
|
if (_self.props.onClick) {
|
|
77
50
|
_self.props.onClick(_self.state.expanded, event);
|
|
78
51
|
}
|
|
79
|
-
|
|
80
52
|
return;
|
|
81
53
|
}
|
|
82
|
-
|
|
83
54
|
if (_this._isMounted) {
|
|
84
55
|
_this.setState({
|
|
85
56
|
expanded: !_this.state.expanded
|
|
@@ -90,21 +61,17 @@ var ShowMoreText = /*#__PURE__*/function (_Component) {
|
|
|
90
61
|
});
|
|
91
62
|
}
|
|
92
63
|
});
|
|
93
|
-
|
|
94
64
|
_this.state = {
|
|
95
65
|
expanded: false,
|
|
96
66
|
truncated: false
|
|
97
67
|
};
|
|
98
68
|
return _this;
|
|
99
69
|
}
|
|
100
|
-
|
|
101
70
|
_createClass(ShowMoreText, [{
|
|
102
71
|
key: "componentDidMount",
|
|
103
72
|
value: function componentDidMount() {
|
|
104
73
|
this._isMounted = true;
|
|
105
|
-
|
|
106
74
|
var _self = this;
|
|
107
|
-
|
|
108
75
|
if (this._isMounted) {
|
|
109
76
|
this.setState({
|
|
110
77
|
expanded: _self.props.expanded
|
|
@@ -120,27 +87,26 @@ var ShowMoreText = /*#__PURE__*/function (_Component) {
|
|
|
120
87
|
key: "render",
|
|
121
88
|
value: function render() {
|
|
122
89
|
var _this2 = this;
|
|
123
|
-
|
|
124
90
|
var _this$props = this.props,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
91
|
+
children = _this$props.children,
|
|
92
|
+
more = _this$props.more,
|
|
93
|
+
less = _this$props.less,
|
|
94
|
+
lines = _this$props.lines,
|
|
95
|
+
anchorClass = _this$props.anchorClass,
|
|
96
|
+
className = _this$props.className,
|
|
97
|
+
width = _this$props.width,
|
|
98
|
+
keepNewLines = _this$props.keepNewLines,
|
|
99
|
+
truncatedEndingComponent = _this$props.truncatedEndingComponent,
|
|
100
|
+
onTruncate = _this$props.onTruncate;
|
|
134
101
|
var _this$state = this.state,
|
|
135
|
-
|
|
136
|
-
|
|
102
|
+
expanded = _this$state.expanded,
|
|
103
|
+
truncated = _this$state.truncated;
|
|
137
104
|
return /*#__PURE__*/_react.default.createElement("div", {
|
|
138
105
|
className: className
|
|
139
106
|
}, /*#__PURE__*/_react.default.createElement(_Truncate.default, {
|
|
140
107
|
width: width,
|
|
141
108
|
lines: !expanded && lines,
|
|
142
|
-
ellipsis: /*#__PURE__*/_react.default.createElement("span", null, truncatedEndingComponent, /*#__PURE__*/_react.default.createElement("
|
|
143
|
-
href: "",
|
|
109
|
+
ellipsis: /*#__PURE__*/_react.default.createElement("span", null, truncatedEndingComponent, /*#__PURE__*/_react.default.createElement("span", {
|
|
144
110
|
className: anchorClass,
|
|
145
111
|
onClick: this.toggleLines
|
|
146
112
|
}, more)),
|
|
@@ -152,7 +118,6 @@ var ShowMoreText = /*#__PURE__*/function (_Component) {
|
|
|
152
118
|
line = /*#__PURE__*/_react.default.createElement("span", {
|
|
153
119
|
key: i
|
|
154
120
|
}, line);
|
|
155
|
-
|
|
156
121
|
if (i === arr.length - 1) {
|
|
157
122
|
return line;
|
|
158
123
|
} else {
|
|
@@ -160,30 +125,27 @@ var ShowMoreText = /*#__PURE__*/function (_Component) {
|
|
|
160
125
|
key: i + 'br'
|
|
161
126
|
})];
|
|
162
127
|
}
|
|
163
|
-
}) : children), !truncated && expanded && /*#__PURE__*/_react.default.createElement("span", null, ' ', /*#__PURE__*/_react.default.createElement("
|
|
164
|
-
href: "",
|
|
128
|
+
}) : children), !truncated && expanded && /*#__PURE__*/_react.default.createElement("span", null, ' ', /*#__PURE__*/_react.default.createElement("span", {
|
|
165
129
|
className: anchorClass,
|
|
166
130
|
onClick: this.toggleLines
|
|
167
131
|
}, less)));
|
|
168
132
|
}
|
|
169
133
|
}]);
|
|
170
|
-
|
|
171
134
|
return ShowMoreText;
|
|
172
135
|
}(_react.Component);
|
|
173
|
-
|
|
174
136
|
_defineProperty(ShowMoreText, "defaultProps", {
|
|
175
137
|
lines: 3,
|
|
176
138
|
more: 'Show more',
|
|
177
139
|
less: 'Show less',
|
|
178
|
-
anchorClass: '',
|
|
140
|
+
anchorClass: 'show-more-less-clickable',
|
|
179
141
|
onClick: undefined,
|
|
180
142
|
expanded: false,
|
|
181
143
|
width: 0,
|
|
182
144
|
keepNewLines: false,
|
|
183
145
|
truncatedEndingComponent: '... ',
|
|
184
|
-
expandByClick: true
|
|
146
|
+
expandByClick: true,
|
|
147
|
+
onTruncate: undefined
|
|
185
148
|
});
|
|
186
|
-
|
|
187
149
|
_defineProperty(ShowMoreText, "propTypes", {
|
|
188
150
|
children: _propTypes.PropTypes.node,
|
|
189
151
|
lines: _propTypes.PropTypes.number,
|
|
@@ -196,8 +158,8 @@ _defineProperty(ShowMoreText, "propTypes", {
|
|
|
196
158
|
width: _propTypes.PropTypes.number,
|
|
197
159
|
keepNewLines: _propTypes.PropTypes.bool,
|
|
198
160
|
truncatedEndingComponent: _propTypes.PropTypes.node,
|
|
199
|
-
expandByClick: _propTypes.PropTypes.bool
|
|
161
|
+
expandByClick: _propTypes.PropTypes.bool,
|
|
162
|
+
onTruncate: _propTypes.PropTypes.func
|
|
200
163
|
});
|
|
201
|
-
|
|
202
164
|
var _default = ShowMoreText;
|
|
203
165
|
exports.default = _default;
|
package/lib/Truncate.js
CHANGED
|
@@ -1,90 +1,57 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _react = _interopRequireDefault(require("react"));
|
|
11
|
-
|
|
12
9
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
13
|
-
|
|
14
10
|
var _excluded = ["children", "ellipsis", "lines"];
|
|
15
|
-
|
|
16
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
|
|
18
12
|
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
19
|
-
|
|
20
13
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
21
|
-
|
|
22
14
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
23
|
-
|
|
24
15
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
25
|
-
|
|
26
16
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
27
|
-
|
|
28
17
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
29
|
-
|
|
30
18
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
31
|
-
|
|
32
19
|
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
33
|
-
|
|
34
20
|
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
35
|
-
|
|
36
21
|
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
37
|
-
|
|
38
22
|
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
39
|
-
|
|
40
23
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
41
|
-
|
|
42
24
|
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
43
|
-
|
|
44
25
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
45
|
-
|
|
46
26
|
var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
47
27
|
_inherits(Truncate, _React$Component);
|
|
48
|
-
|
|
49
28
|
var _super = _createSuper(Truncate);
|
|
50
|
-
|
|
51
29
|
function Truncate() {
|
|
52
30
|
var _this;
|
|
53
|
-
|
|
54
31
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
55
32
|
args[_key] = arguments[_key];
|
|
56
33
|
}
|
|
57
|
-
|
|
58
34
|
_classCallCheck(this, Truncate);
|
|
59
|
-
|
|
60
35
|
_this = _super.call.apply(_super, [this].concat(args));
|
|
61
|
-
|
|
62
36
|
_defineProperty(_assertThisInitialized(_this), "state", {});
|
|
63
|
-
|
|
64
37
|
_defineProperty(_assertThisInitialized(_this), "extractReplaceLinksKeys", function (content) {
|
|
65
38
|
var _self = _assertThisInitialized(_this),
|
|
66
|
-
|
|
67
|
-
|
|
39
|
+
i = 0;
|
|
68
40
|
_this.replacedLinks = [];
|
|
69
41
|
content.replace(/(<a[\s]+([^>]+)>((?:.(?!\<\/a\>))*.)<\/a>)/g, function () {
|
|
70
42
|
var item = Array.prototype.slice.call(arguments, 1, 4);
|
|
71
43
|
item.key = '[' + '@'.repeat(item[2].length - 1) + '=' + i++ + ']';
|
|
72
|
-
|
|
73
44
|
_self.replacedLinks.push(item);
|
|
74
|
-
|
|
75
45
|
content = content.replace(item[0], item.key);
|
|
76
46
|
});
|
|
77
47
|
return content;
|
|
78
48
|
});
|
|
79
|
-
|
|
80
49
|
_defineProperty(_assertThisInitialized(_this), "restoreReplacedLinks", function (content) {
|
|
81
50
|
_this.replacedLinks.forEach(function (item) {
|
|
82
51
|
content = content.replace(item.key, item[0]);
|
|
83
52
|
});
|
|
84
|
-
|
|
85
53
|
return _this.createMarkup(content);
|
|
86
54
|
});
|
|
87
|
-
|
|
88
55
|
_defineProperty(_assertThisInitialized(_this), "innerText", function (node) {
|
|
89
56
|
var div = document.createElement('div');
|
|
90
57
|
var contentKey = 'innerText' in window.HTMLElement.prototype ? 'innerText' : 'textContent';
|
|
@@ -93,72 +60,61 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
93
60
|
var text = div[contentKey];
|
|
94
61
|
var test = document.createElement('div');
|
|
95
62
|
test.innerHTML = 'foo<br/>bar';
|
|
96
|
-
|
|
97
63
|
if (test[contentKey].replace(/\r\n|\r/g, '\n') !== 'foo\nbar') {
|
|
98
64
|
div.innerHTML = div.innerHTML.replace(/<br.*?[\/]?>/gi, '\n');
|
|
99
65
|
text = div[contentKey];
|
|
100
66
|
}
|
|
101
|
-
|
|
102
67
|
return text;
|
|
103
68
|
});
|
|
104
|
-
|
|
105
69
|
_defineProperty(_assertThisInitialized(_this), "onResize", function () {
|
|
106
70
|
_this.calcTargetWidth();
|
|
107
71
|
});
|
|
108
|
-
|
|
109
72
|
_defineProperty(_assertThisInitialized(_this), "onTruncate", function (didTruncate) {
|
|
110
73
|
var onTruncate = _this.props.onTruncate;
|
|
111
|
-
|
|
112
74
|
if (typeof onTruncate === 'function') {
|
|
113
75
|
_this.timeout = window.requestAnimationFrame(function () {
|
|
114
76
|
onTruncate(didTruncate);
|
|
115
77
|
});
|
|
116
78
|
}
|
|
117
79
|
});
|
|
118
|
-
|
|
119
80
|
_defineProperty(_assertThisInitialized(_this), "calcTargetWidth", function (callback) {
|
|
120
81
|
var _assertThisInitialize = _assertThisInitialized(_this),
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
82
|
+
target = _assertThisInitialize.elements.target,
|
|
83
|
+
calcTargetWidth = _assertThisInitialize.calcTargetWidth,
|
|
84
|
+
canvasContext = _assertThisInitialize.canvasContext,
|
|
85
|
+
width = _assertThisInitialize.props.width;
|
|
126
86
|
|
|
87
|
+
// Calculation is no longer relevant, since node has been removed
|
|
127
88
|
if (!target) {
|
|
128
89
|
return;
|
|
129
90
|
}
|
|
91
|
+
var targetWidth = width ||
|
|
92
|
+
// Floor the result to deal with browser subpixel precision
|
|
93
|
+
Math.floor(target.parentNode.getBoundingClientRect().width);
|
|
130
94
|
|
|
131
|
-
|
|
132
|
-
Math.floor(target.parentNode.getBoundingClientRect().width); // Delay calculation until parent node is inserted to the document
|
|
95
|
+
// Delay calculation until parent node is inserted to the document
|
|
133
96
|
// Mounting order in React is ChildComponent, ParentComponent
|
|
134
|
-
|
|
135
97
|
if (!targetWidth) {
|
|
136
98
|
return window.requestAnimationFrame(function () {
|
|
137
99
|
return calcTargetWidth(callback);
|
|
138
100
|
});
|
|
139
101
|
}
|
|
140
|
-
|
|
141
102
|
var style = window.getComputedStyle(target);
|
|
142
103
|
var font = [style['font-weight'], style['font-style'], style['font-size'], style['font-family']].join(' ');
|
|
143
104
|
canvasContext.font = font;
|
|
144
|
-
|
|
145
105
|
_this.setState({
|
|
146
106
|
targetWidth: targetWidth
|
|
147
107
|
}, callback);
|
|
148
108
|
});
|
|
149
|
-
|
|
150
109
|
_defineProperty(_assertThisInitialized(_this), "measureWidth", function (text) {
|
|
151
110
|
return _this.canvasContext.measureText(text).width;
|
|
152
111
|
});
|
|
153
|
-
|
|
154
112
|
_defineProperty(_assertThisInitialized(_this), "ellipsisWidth", function (node) {
|
|
155
113
|
return node.offsetWidth;
|
|
156
114
|
});
|
|
157
|
-
|
|
158
115
|
_defineProperty(_assertThisInitialized(_this), "trimRight", function (text) {
|
|
159
116
|
return text.replace(/\s+$/, '');
|
|
160
117
|
});
|
|
161
|
-
|
|
162
118
|
_defineProperty(_assertThisInitialized(_this), "createMarkup", function (str) {
|
|
163
119
|
return /*#__PURE__*/_react.default.createElement("span", {
|
|
164
120
|
dangerouslySetInnerHTML: {
|
|
@@ -166,43 +122,38 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
166
122
|
}
|
|
167
123
|
});
|
|
168
124
|
});
|
|
169
|
-
|
|
170
125
|
_defineProperty(_assertThisInitialized(_this), "getLines", function () {
|
|
171
126
|
var _assertThisInitialize2 = _assertThisInitialized(_this),
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
127
|
+
elements = _assertThisInitialize2.elements,
|
|
128
|
+
_assertThisInitialize3 = _assertThisInitialize2.props,
|
|
129
|
+
numLines = _assertThisInitialize3.lines,
|
|
130
|
+
ellipsis = _assertThisInitialize3.ellipsis,
|
|
131
|
+
trimWhitespace = _assertThisInitialize3.trimWhitespace,
|
|
132
|
+
targetWidth = _assertThisInitialize2.state.targetWidth,
|
|
133
|
+
innerText = _assertThisInitialize2.innerText,
|
|
134
|
+
measureWidth = _assertThisInitialize2.measureWidth,
|
|
135
|
+
onTruncate = _assertThisInitialize2.onTruncate,
|
|
136
|
+
trimRight = _assertThisInitialize2.trimRight,
|
|
137
|
+
renderLine = _assertThisInitialize2.renderLine,
|
|
138
|
+
restoreReplacedLinks = _assertThisInitialize2.restoreReplacedLinks;
|
|
185
139
|
var lines = [];
|
|
186
140
|
var text = innerText(elements.text);
|
|
187
141
|
var textLines = text.split('\n').map(function (line) {
|
|
188
142
|
return line.split(' ');
|
|
189
143
|
});
|
|
190
144
|
var didTruncate = true;
|
|
191
|
-
|
|
192
145
|
var ellipsisWidth = _this.ellipsisWidth(_this.elements.ellipsis);
|
|
193
|
-
|
|
194
146
|
for (var line = 1; line <= numLines; line++) {
|
|
195
|
-
var textWords = textLines[0];
|
|
147
|
+
var textWords = textLines[0];
|
|
196
148
|
|
|
149
|
+
// Handle newline
|
|
197
150
|
if (textWords.length === 0) {
|
|
198
151
|
lines.push();
|
|
199
152
|
textLines.shift();
|
|
200
153
|
line--;
|
|
201
154
|
continue;
|
|
202
155
|
}
|
|
203
|
-
|
|
204
156
|
var resultLine = textWords.join(' ');
|
|
205
|
-
|
|
206
157
|
if (measureWidth(resultLine) <= targetWidth) {
|
|
207
158
|
if (textLines.length === 1) {
|
|
208
159
|
// Line is end of text and fits without truncating
|
|
@@ -212,39 +163,33 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
212
163
|
break;
|
|
213
164
|
}
|
|
214
165
|
}
|
|
215
|
-
|
|
216
166
|
if (line === numLines) {
|
|
217
167
|
// Binary search determining the longest possible line inluding truncate string
|
|
218
168
|
var textRest = textWords.join(' ');
|
|
219
169
|
var lower = 0;
|
|
220
170
|
var upper = textRest.length - 1;
|
|
221
|
-
|
|
222
171
|
while (lower <= upper) {
|
|
223
172
|
var middle = Math.floor((lower + upper) / 2);
|
|
224
173
|
var testLine = textRest.slice(0, middle + 1);
|
|
225
|
-
|
|
226
174
|
if (measureWidth(testLine) + ellipsisWidth <= targetWidth) {
|
|
227
175
|
lower = middle + 1;
|
|
228
176
|
} else {
|
|
229
177
|
upper = middle - 1;
|
|
230
178
|
}
|
|
231
179
|
}
|
|
232
|
-
|
|
233
180
|
var lastLineText = textRest.slice(0, lower);
|
|
234
|
-
|
|
235
181
|
if (trimWhitespace) {
|
|
236
|
-
lastLineText = trimRight(lastLineText);
|
|
182
|
+
lastLineText = trimRight(lastLineText);
|
|
237
183
|
|
|
184
|
+
// Remove blank lines from the end of text
|
|
238
185
|
while (!lastLineText.length && lines.length) {
|
|
239
186
|
var prevLine = lines.pop();
|
|
240
187
|
lastLineText = trimRight(prevLine);
|
|
241
188
|
}
|
|
242
189
|
}
|
|
243
|
-
|
|
244
190
|
if (lastLineText.substr(lastLineText.length - 2) === '][') {
|
|
245
191
|
lastLineText = lastLineText.substring(0, lastLineText.length - 1);
|
|
246
192
|
}
|
|
247
|
-
|
|
248
193
|
;
|
|
249
194
|
lastLineText = lastLineText.replace(/\[@+$/, '');
|
|
250
195
|
lastLineText = restoreReplacedLinks(lastLineText);
|
|
@@ -252,40 +197,32 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
252
197
|
} else {
|
|
253
198
|
// Binary search determining when the line breaks
|
|
254
199
|
var _lower = 0;
|
|
255
|
-
|
|
256
200
|
var _upper = textWords.length - 1;
|
|
257
|
-
|
|
258
201
|
while (_lower <= _upper) {
|
|
259
202
|
var _middle = Math.floor((_lower + _upper) / 2);
|
|
260
|
-
|
|
261
203
|
var _testLine = textWords.slice(0, _middle + 1).join(' ');
|
|
262
|
-
|
|
263
204
|
if (measureWidth(_testLine) <= targetWidth) {
|
|
264
205
|
_lower = _middle + 1;
|
|
265
206
|
} else {
|
|
266
207
|
_upper = _middle - 1;
|
|
267
208
|
}
|
|
268
|
-
}
|
|
269
|
-
|
|
209
|
+
}
|
|
270
210
|
|
|
211
|
+
// The first word of this line is too long to fit it
|
|
271
212
|
if (_lower === 0) {
|
|
272
213
|
// Jump to processing of last line
|
|
273
214
|
line = numLines - 1;
|
|
274
215
|
continue;
|
|
275
216
|
}
|
|
276
|
-
|
|
277
217
|
resultLine = textWords.slice(0, _lower).join(' ');
|
|
278
218
|
resultLine = restoreReplacedLinks(resultLine);
|
|
279
219
|
textLines[0].splice(0, _lower);
|
|
280
220
|
}
|
|
281
|
-
|
|
282
221
|
lines.push(resultLine);
|
|
283
222
|
}
|
|
284
|
-
|
|
285
223
|
onTruncate(didTruncate);
|
|
286
224
|
return lines.map(renderLine);
|
|
287
225
|
});
|
|
288
|
-
|
|
289
226
|
_defineProperty(_assertThisInitialized(_this), "renderLine", function (line, i, arr) {
|
|
290
227
|
if (i === arr.length - 1) {
|
|
291
228
|
return /*#__PURE__*/_react.default.createElement("span", {
|
|
@@ -295,7 +232,6 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
295
232
|
var br = /*#__PURE__*/_react.default.createElement("br", {
|
|
296
233
|
key: i + 'br'
|
|
297
234
|
});
|
|
298
|
-
|
|
299
235
|
if (line) {
|
|
300
236
|
return [/*#__PURE__*/_react.default.createElement("span", {
|
|
301
237
|
key: i
|
|
@@ -305,7 +241,6 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
305
241
|
}
|
|
306
242
|
}
|
|
307
243
|
});
|
|
308
|
-
|
|
309
244
|
_defineProperty(_assertThisInitialized(_this), "styles", {
|
|
310
245
|
ellipsis: {
|
|
311
246
|
position: 'fixed',
|
|
@@ -314,23 +249,21 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
314
249
|
left: 0
|
|
315
250
|
}
|
|
316
251
|
});
|
|
317
|
-
|
|
318
252
|
_this.elements = {};
|
|
319
253
|
_this.replacedLinks = [];
|
|
320
254
|
return _this;
|
|
321
255
|
}
|
|
322
|
-
|
|
323
256
|
_createClass(Truncate, [{
|
|
324
257
|
key: "componentDidMount",
|
|
325
258
|
value: function componentDidMount() {
|
|
326
259
|
var text = this.elements.text,
|
|
327
|
-
|
|
328
|
-
|
|
260
|
+
calcTargetWidth = this.calcTargetWidth,
|
|
261
|
+
onResize = this.onResize;
|
|
329
262
|
var canvas = document.createElement('canvas');
|
|
330
263
|
this.canvasContext = canvas.getContext('2d');
|
|
331
264
|
calcTargetWidth(function () {
|
|
332
265
|
// Node not needed in document tree to read its content
|
|
333
|
-
if (text) {
|
|
266
|
+
if (text && text.parentNode) {
|
|
334
267
|
text.parentNode.removeChild(text);
|
|
335
268
|
}
|
|
336
269
|
});
|
|
@@ -342,9 +275,9 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
342
275
|
// Render was based on outdated refs and needs to be rerun
|
|
343
276
|
if (this.props.children !== prevProps.children) {
|
|
344
277
|
this.forceUpdate();
|
|
345
|
-
}
|
|
346
|
-
|
|
278
|
+
}
|
|
347
279
|
|
|
280
|
+
// If the width prop has changed, recalculate size of contents
|
|
348
281
|
if (this.props.width !== prevProps.width) {
|
|
349
282
|
this.calcTargetWidth();
|
|
350
283
|
}
|
|
@@ -353,9 +286,11 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
353
286
|
key: "componentWillUnmount",
|
|
354
287
|
value: function componentWillUnmount() {
|
|
355
288
|
var ellipsis = this.elements.ellipsis,
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
ellipsis.parentNode
|
|
289
|
+
onResize = this.onResize,
|
|
290
|
+
timeout = this.timeout;
|
|
291
|
+
if (ellipsis.parentNode) {
|
|
292
|
+
ellipsis.parentNode.removeChild(ellipsis);
|
|
293
|
+
}
|
|
359
294
|
window.removeEventListener('resize', onResize);
|
|
360
295
|
window.cancelAnimationFrame(timeout);
|
|
361
296
|
}
|
|
@@ -363,20 +298,17 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
363
298
|
key: "render",
|
|
364
299
|
value: function render() {
|
|
365
300
|
var _this2 = this;
|
|
366
|
-
|
|
367
301
|
var target = this.elements.target,
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
302
|
+
_this$props = this.props,
|
|
303
|
+
children = _this$props.children,
|
|
304
|
+
ellipsis = _this$props.ellipsis,
|
|
305
|
+
lines = _this$props.lines,
|
|
306
|
+
spanProps = _objectWithoutProperties(_this$props, _excluded),
|
|
307
|
+
targetWidth = this.state.targetWidth,
|
|
308
|
+
getLines = this.getLines,
|
|
309
|
+
onTruncate = this.onTruncate;
|
|
377
310
|
var text;
|
|
378
311
|
var mounted = !!(target && targetWidth);
|
|
379
|
-
|
|
380
312
|
if (typeof window !== 'undefined' && mounted) {
|
|
381
313
|
if (lines > 0) {
|
|
382
314
|
text = getLines();
|
|
@@ -385,7 +317,6 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
385
317
|
onTruncate(false);
|
|
386
318
|
}
|
|
387
319
|
}
|
|
388
|
-
|
|
389
320
|
delete spanProps.onTruncate;
|
|
390
321
|
delete spanProps.trimWhitespace;
|
|
391
322
|
return /*#__PURE__*/_react.default.createElement("span", _extends({}, spanProps, {
|
|
@@ -404,12 +335,9 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
|
|
|
404
335
|
}, ellipsis));
|
|
405
336
|
}
|
|
406
337
|
}]);
|
|
407
|
-
|
|
408
338
|
return Truncate;
|
|
409
339
|
}(_react.default.Component);
|
|
410
|
-
|
|
411
340
|
exports.default = Truncate;
|
|
412
|
-
|
|
413
341
|
_defineProperty(Truncate, "propTypes", {
|
|
414
342
|
children: _propTypes.default.node,
|
|
415
343
|
ellipsis: _propTypes.default.node,
|
|
@@ -418,7 +346,6 @@ _defineProperty(Truncate, "propTypes", {
|
|
|
418
346
|
width: _propTypes.default.number,
|
|
419
347
|
onTruncate: _propTypes.default.func
|
|
420
348
|
});
|
|
421
|
-
|
|
422
349
|
_defineProperty(Truncate, "defaultProps", {
|
|
423
350
|
children: '',
|
|
424
351
|
ellipsis: '…',
|
|
@@ -426,5 +353,4 @@ _defineProperty(Truncate, "defaultProps", {
|
|
|
426
353
|
trimWhitespace: false,
|
|
427
354
|
width: 0
|
|
428
355
|
});
|
|
429
|
-
|
|
430
356
|
;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-show-more-text",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.2",
|
|
4
4
|
"description": "The text surrounded by the component will be truncated. Anything surrounded by the component could be evaluated as text. The component react-show-more-text/ShowMoreText is fork of react-show-more/ShowMore, applied improvements, works with React 16.x.x, added onClick event.",
|
|
5
5
|
"main": "lib/ShowMoreText.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib"
|
|
8
8
|
],
|
|
9
9
|
"scripts": {
|
|
10
|
-
"compile": "./node_modules/.bin/babel src/ShowMoreText.js src/Truncate.js --out-dir lib",
|
|
10
|
+
"compile": "./node_modules/.bin/babel src/ShowMoreText.js src/Truncate.js --out-dir lib && cp -R src/ShowMoreText.css lib/",
|
|
11
11
|
"compile:watch": "onchange \"src/**/*.js\" -- npm run compile --silent --source-maps",
|
|
12
12
|
"coverage": "nyc npm test && nyc report --reporter=text-lcov",
|
|
13
13
|
"lint": "eslint .",
|
|
@@ -49,12 +49,12 @@
|
|
|
49
49
|
"@babel/preset-env": "^7.12.1",
|
|
50
50
|
"@babel/preset-react": "^7.12.1",
|
|
51
51
|
"@storybook/addon-actions": "^6.5.12",
|
|
52
|
-
"@storybook/addon-essentials": "^6.
|
|
52
|
+
"@storybook/addon-essentials": "^6.4.22",
|
|
53
53
|
"@storybook/addon-interactions": "^6.5.12",
|
|
54
54
|
"@storybook/addon-links": "^6.5.12",
|
|
55
55
|
"@storybook/builder-webpack4": "^6.5.12",
|
|
56
56
|
"@storybook/manager-webpack4": "^6.5.12",
|
|
57
|
-
"@storybook/react": "^6.
|
|
57
|
+
"@storybook/react": "^6.1.21",
|
|
58
58
|
"@storybook/testing-library": "^0.0.13",
|
|
59
59
|
"babel-eslint": "^10.1.0",
|
|
60
60
|
"babel-loader": "^8.2.5",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"coveralls": "^3.0.7",
|
|
65
65
|
"enzyme": "^3.11.0",
|
|
66
66
|
"enzyme-adapter-react-16": "^1.15.5",
|
|
67
|
-
"eslint": "^
|
|
67
|
+
"eslint": "^8.24.0",
|
|
68
68
|
"eslint-config-onelint": "^3.0.0",
|
|
69
69
|
"eslint-config-onelint-react": "^2.0.1",
|
|
70
70
|
"eslint-config-react-app": "^6.0.0",
|
|
@@ -74,8 +74,9 @@
|
|
|
74
74
|
"eslint-plugin-jsx-a11y": "^6.4.1",
|
|
75
75
|
"eslint-plugin-react": "^7.21.5",
|
|
76
76
|
"eslint-plugin-react-hooks": "^4.2.0",
|
|
77
|
-
"jest": "^
|
|
78
|
-
"jest-
|
|
77
|
+
"jest": "^29.1.2",
|
|
78
|
+
"jest-environment-jsdom": "^29.1.2",
|
|
79
|
+
"jest-enzyme": "^4.2.0",
|
|
79
80
|
"nyc": "^14.1.1",
|
|
80
81
|
"onchange": "^6.1.1",
|
|
81
82
|
"react": "^16.14.0",
|