Unverified Commit 5a972e9d authored by niko's avatar niko Committed by GitHub

Update Ellipsis compute through height (#171)

* Update Ellipsis compute with height & Fixed https://github.com/ant-design/ant-design-pro/issues/167
parent 1800435a
...@@ -55,17 +55,15 @@ export default class Ellipsis extends Component { ...@@ -55,17 +55,15 @@ export default class Ellipsis extends Component {
computeLine = () => { computeLine = () => {
const { lines } = this.props; const { lines } = this.props;
if (lines && !isSupportLineClamp) { if (lines && !isSupportLineClamp) {
const fontSize = parseInt(window.getComputedStyle(this.node).fontSize, 10) || 14;
const text = this.shadowChildren.innerText; const text = this.shadowChildren.innerText;
const targetWidth = (this.node.offsetWidth || this.node.parentNode.offsetWidth) * lines; const targetHeight = this.root.offsetHeight;
const shadowNode = this.shadow.firstChild; const shadowNode = this.shadow.firstChild;
// bisection // bisection
const tw = (targetWidth - (lines * (fontSize / 2)) - fontSize);
const len = text.length; const len = text.length;
const mid = Math.floor(len / 2); const mid = Math.floor(len / 2);
const count = this.bisection(tw, mid, 0, len, text, shadowNode); const count = this.bisection(targetHeight, mid, 0, len, text, shadowNode);
this.setState({ this.setState({
text, text,
...@@ -74,40 +72,45 @@ export default class Ellipsis extends Component { ...@@ -74,40 +72,45 @@ export default class Ellipsis extends Component {
} }
} }
bisection = (tw, m, b, e, text, shadowNode) => { bisection = (th, m, b, e, text, shadowNode) => {
const suffix = '...';
let mid = m; let mid = m;
let end = e; let end = e;
let begin = b; let begin = b;
shadowNode.innerHTML = text.substring(0, mid); shadowNode.innerHTML = text.substring(0, mid) + suffix;
let sw = shadowNode.offsetWidth; let sh = shadowNode.offsetHeight;
if (sw < tw) { if (sh < th) {
shadowNode.innerHTML = text.substring(0, mid + 1); shadowNode.innerHTML = text.substring(0, mid + 1) + suffix;
sw = shadowNode.offsetWidth; sh = shadowNode.offsetHeight;
if (sw >= tw) { if (sh >= th) {
return mid; return mid;
} else { } else {
begin = mid; begin = mid;
mid = Math.floor((end - begin) / 2) + begin; mid = Math.floor((end - begin) / 2) + begin;
return this.bisection(tw, mid, begin, end, text, shadowNode); return this.bisection(th, mid, begin, end, text, shadowNode);
} }
} else { } else {
if (mid - 1 < 0) { if (mid - 1 < 0) {
return mid; return mid;
} }
shadowNode.innerHTML = text.substring(0, mid - 1); shadowNode.innerHTML = text.substring(0, mid - 1) + suffix;
sw = shadowNode.offsetWidth; sh = shadowNode.offsetHeight;
if (sw <= tw) { if (sh <= th) {
return mid; return mid;
} else { } else {
end = mid; end = mid;
mid = Math.floor((end - begin) / 2) + begin; mid = Math.floor((end - begin) / 2) + begin;
return this.bisection(tw, mid, begin, end, text, shadowNode); return this.bisection(th, mid, begin, end, text, shadowNode);
} }
} }
} }
handleRef = (n) => { handleRoot = (n) => {
this.root = n;
}
handleNode = (n) => {
this.node = n; this.node = n;
} }
...@@ -130,7 +133,6 @@ export default class Ellipsis extends Component { ...@@ -130,7 +133,6 @@ export default class Ellipsis extends Component {
...restProps ...restProps
} = this.props; } = this.props;
const cls = classNames(styles.ellipsis, className, { const cls = classNames(styles.ellipsis, className, {
[styles.lines]: (lines && !isSupportLineClamp), [styles.lines]: (lines && !isSupportLineClamp),
[styles.lineClamp]: (lines && isSupportLineClamp), [styles.lineClamp]: (lines && isSupportLineClamp),
...@@ -160,7 +162,7 @@ export default class Ellipsis extends Component { ...@@ -160,7 +162,7 @@ export default class Ellipsis extends Component {
} }
const childNode = ( const childNode = (
<span> <span ref={this.handleNode}>
{ {
(targetCount > 0) && text.substring(0, targetCount) (targetCount > 0) && text.substring(0, targetCount)
} }
...@@ -173,7 +175,7 @@ export default class Ellipsis extends Component { ...@@ -173,7 +175,7 @@ export default class Ellipsis extends Component {
return ( return (
<div <div
{...restProps} {...restProps}
ref={this.handleRef} ref={this.handleRoot}
className={cls} className={cls}
> >
{ {
......
.ellipsis { .ellipsis {
overflow: hidden;
display: inline-block; display: inline-block;
word-break: break-all; word-break: break-all;
} }
...@@ -6,13 +7,10 @@ ...@@ -6,13 +7,10 @@
.lines { .lines {
position: relative; position: relative;
.shadow { .shadow {
display: block;
position: relative;
color: transparent; color: transparent;
opacity: 0; opacity: 0;
display: block;
position: absolute;
top: 0;
left: 0;
width: 9999px;
z-index: -999; z-index: -999;
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment