Skip to content

Commit 77c43ba

Browse files
committed
Update Ellipsis compute with height & Fixed #167
1 parent 6e2d122 commit 77c43ba

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/components/Ellipsis/index.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default class Ellipsis extends Component {
4646
}
4747
}
4848

49-
componentWillReceiveProps(nextProps) {
49+
componenthillReceiveProps(nextProps) {
5050
if (this.props.lines !== nextProps.lines) {
5151
this.computeLine();
5252
}
@@ -55,17 +55,15 @@ export default class Ellipsis extends Component {
5555
computeLine = () => {
5656
const { lines } = this.props;
5757
if (lines && !isSupportLineClamp) {
58-
const fontSize = parseInt(window.getComputedStyle(this.node).fontSize, 10) || 14;
5958
const text = this.shadowChildren.innerText;
60-
const targetWidth = (this.node.offsetWidth || this.node.parentNode.offsetWidth) * lines;
59+
const targetHeight = this.root.offsetHeight;
6160
const shadowNode = this.shadow.firstChild;
6261

6362
// bisection
64-
const tw = (targetWidth - (lines * (fontSize / 2)) - fontSize);
6563
const len = text.length;
6664
const mid = Math.floor(len / 2);
6765

68-
const count = this.bisection(tw, mid, 0, len, text, shadowNode);
66+
const count = this.bisection(targetHeight, mid, 0, len, text, shadowNode);
6967

7068
this.setState({
7169
text,
@@ -74,40 +72,45 @@ export default class Ellipsis extends Component {
7472
}
7573
}
7674

77-
bisection = (tw, m, b, e, text, shadowNode) => {
75+
bisection = (th, m, b, e, text, shadowNode) => {
76+
const suffix = '...';
7877
let mid = m;
7978
let end = e;
8079
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;
8382

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) {
8887
return mid;
8988
} else {
9089
begin = mid;
9190
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);
9392
}
9493
} else {
9594
if (mid - 1 < 0) {
9695
return mid;
9796
}
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) {
101100
return mid;
102101
} else {
103102
end = mid;
104103
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);
106105
}
107106
}
108107
}
109108

110-
handleRef = (n) => {
109+
handleRoot = (n) => {
110+
this.root = n;
111+
}
112+
113+
handleNode = (n) => {
111114
this.node = n;
112115
}
113116

@@ -130,7 +133,6 @@ export default class Ellipsis extends Component {
130133
...restProps
131134
} = this.props;
132135

133-
134136
const cls = classNames(styles.ellipsis, className, {
135137
[styles.lines]: (lines && !isSupportLineClamp),
136138
[styles.lineClamp]: (lines && isSupportLineClamp),
@@ -160,7 +162,7 @@ export default class Ellipsis extends Component {
160162
}
161163

162164
const childNode = (
163-
<span>
165+
<span ref={this.handleNode}>
164166
{
165167
(targetCount > 0) && text.substring(0, targetCount)
166168
}
@@ -173,7 +175,7 @@ export default class Ellipsis extends Component {
173175
return (
174176
<div
175177
{...restProps}
176-
ref={this.handleRef}
178+
ref={this.handleRoot}
177179
className={cls}
178180
>
179181
{

src/components/Ellipsis/index.less

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
.ellipsis {
2+
overflow: hidden;
23
display: inline-block;
34
word-break: break-all;
45
}
56

67
.lines {
78
position: relative;
89
.shadow {
10+
display: block;
11+
position: relative;
912
color: transparent;
1013
opacity: 0;
11-
display: block;
12-
position: absolute;
13-
top: 0;
14-
left: 0;
15-
width: 9999px;
1614
z-index: -999;
1715
}
1816
}

0 commit comments

Comments
 (0)