index.js 4.56 KB
Newer Older
1
import React, { Component } from 'react';
niko's avatar
niko committed
2 3 4 5 6 7 8
import { Tooltip } from 'antd';
import classNames from 'classnames';
import styles from './index.less';

/* eslint react/no-did-mount-set-state: 0 */
/* eslint no-param-reassign: 0 */

9 10
const isSupportLineClamp = (document.body.style.webkitLineClamp !== undefined);

niko's avatar
niko committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
const EllipsisText = ({ text, length, tooltip, ...other }) => {
  if (typeof text !== 'string') {
    throw new Error('Ellipsis children must be string.');
  }
  if (text.length <= length || length < 0) {
    return <span {...other}>{text}</span>;
  }
  const tail = '...';
  let displayText;
  if (length - tail.length <= 0) {
    displayText = '';
  } else {
    displayText = text.slice(0, (length - tail.length));
  }

  if (tooltip) {
27
    return <Tooltip title={text}><span>{displayText}{tail}</span></Tooltip>;
niko's avatar
niko committed
28 29 30 31 32 33 34 35 36
  }

  return (
    <span {...other}>
      {displayText}{tail}
    </span>
  );
};

37
export default class Ellipsis extends Component {
niko's avatar
niko committed
38 39 40 41 42 43 44 45 46 47 48 49
  state = {
    text: '',
    targetCount: 0,
  }

  componentDidMount() {
    if (this.node) {
      this.computeLine();
    }
  }

  componentWillReceiveProps(nextProps) {
50
    if (this.props.lines !== nextProps.lines) {
niko's avatar
niko committed
51 52 53 54 55
      this.computeLine();
    }
  }

  computeLine = () => {
56 57
    const { lines } = this.props;
    if (lines && !isSupportLineClamp) {
niko's avatar
niko committed
58
      const text = this.shadowChildren.innerText;
59
      const targetHeight = this.root.offsetHeight;
niko's avatar
niko committed
60 61 62 63 64 65
      const shadowNode = this.shadow.firstChild;

      // bisection
      const len = text.length;
      const mid = Math.floor(len / 2);

66
      const count = this.bisection(targetHeight, mid, 0, len, text, shadowNode);
niko's avatar
niko committed
67 68 69 70 71 72 73 74

      this.setState({
        text,
        targetCount: count,
      });
    }
  }

75 76
  bisection = (th, m, b, e, text, shadowNode) => {
    const suffix = '...';
niko's avatar
niko committed
77 78 79
    let mid = m;
    let end = e;
    let begin = b;
80 81
    shadowNode.innerHTML = text.substring(0, mid) + suffix;
    let sh = shadowNode.offsetHeight;
niko's avatar
niko committed
82

83 84 85 86
    if (sh < th) {
      shadowNode.innerHTML = text.substring(0, mid + 1) + suffix;
      sh = shadowNode.offsetHeight;
      if (sh >= th) {
niko's avatar
niko committed
87 88 89 90
        return mid;
      } else {
        begin = mid;
        mid = Math.floor((end - begin) / 2) + begin;
91
        return this.bisection(th, mid, begin, end, text, shadowNode);
niko's avatar
niko committed
92 93 94 95 96
      }
    } else {
      if (mid - 1 < 0) {
        return mid;
      }
97 98 99
      shadowNode.innerHTML = text.substring(0, mid - 1) + suffix;
      sh = shadowNode.offsetHeight;
      if (sh <= th) {
niko's avatar
niko committed
100 101 102 103
        return mid;
      } else {
        end = mid;
        mid = Math.floor((end - begin) / 2) + begin;
104
        return this.bisection(th, mid, begin, end, text, shadowNode);
niko's avatar
niko committed
105 106 107 108
      }
    }
  }

109 110 111 112 113
  handleRoot = (n) => {
    this.root = n;
  }

  handleNode = (n) => {
niko's avatar
niko committed
114 115 116 117 118 119 120 121 122 123 124 125
    this.node = n;
  }

  handleShadow = (n) => {
    this.shadow = n;
  }

  handleShadowChildren = (n) => {
    this.shadowChildren = n;
  }

  render() {
126
    const { text, targetCount } = this.state;
niko's avatar
niko committed
127 128 129 130 131 132 133 134 135 136
    const {
      children,
      lines,
      length,
      className,
      tooltip,
      ...restProps
    } = this.props;

    const cls = classNames(styles.ellipsis, className, {
137 138
      [styles.lines]: (lines && !isSupportLineClamp),
      [styles.lineClamp]: (lines && isSupportLineClamp),
niko's avatar
niko committed
139 140 141 142 143 144 145 146 147 148 149
    });

    if (!lines && !length) {
      return (<span className={cls} {...restProps}>{children}</span>);
    }

    // length
    if (!lines) {
      return (<EllipsisText className={cls} length={length} text={children || ''} tooltip={tooltip} {...restProps} />);
    }

150 151 152 153 154
    const id = `antd-pro-ellipsis-${`${new Date().getTime()}${Math.floor(Math.random() * 100)}`}`;

    // support document.body.style.webkitLineClamp
    if (isSupportLineClamp) {
      const style = `#${id}{-webkit-line-clamp:${lines};}`;
niko's avatar
niko committed
155
      return (
156
        <div id={id} className={cls} {...restProps}>
niko's avatar
niko committed
157
          <style>{style}</style>
158 159 160 161
          {
            tooltip ? (<Tooltip title={text}>{children}</Tooltip>) : children
          }
        </div>);
niko's avatar
niko committed
162 163
    }

164
    const childNode = (
165
      <span ref={this.handleNode}>
166 167 168 169 170 171 172 173
        {
          (targetCount > 0) && text.substring(0, targetCount)
        }
        {
          (targetCount > 0) && (targetCount < text.length) && '...'
        }
      </span>
    );
niko's avatar
niko committed
174 175 176 177

    return (
      <div
        {...restProps}
178
        ref={this.handleRoot}
niko's avatar
niko committed
179 180 181
        className={cls}
      >
        {
182 183 184
          tooltip ? (
            <Tooltip title={text}>{childNode}</Tooltip>
          ) : childNode
niko's avatar
niko committed
185 186 187 188 189 190 191
        }
        <div className={styles.shadow} ref={this.handleShadowChildren}>{children}</div>
        <div className={styles.shadow} ref={this.handleShadow}><span>{text}</span></div>
      </div>
    );
  }
}