-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.jsx
190 lines (174 loc) · 5.08 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import React from 'react'
import { canvasStyle, mirrorProps } from './common'
import { omit } from './helpers'
function prevSibling (node, count) {
while (node && count--) {
node = node.previousElementSibling
}
return node
}
const defaultProps = {
basedOn: undefined,
className: '',
component: 'div',
ellipsis: '…', // …
maxLine: 1,
onReflow () {},
text: '',
trimRight: true,
winWidth: undefined // for the HOC
}
const usedProps = Object.keys(defaultProps)
/**
* props.text {String} the text you want to clamp
* props.maxLine {Number|String} max lines allowed
* props.ellipsis {String} the ellipsis indicator
* props.trimRight {Boolean} should we trimRight the clamped text?
* props.basedOn {String} letters|words
* props.className {String}
*/
class LinesEllipsis extends React.Component {
constructor (props) {
super(props)
this.state = {
text: props.text,
clamped: false
}
this.units = []
this.maxLine = 0
this.canvas = null
}
componentDidMount () {
this.initCanvas()
this.reflow(this.props)
}
componentDidUpdate (prevProps) {
if (prevProps.winWidth !== this.props.winWidth) {
this.copyStyleToCanvas()
}
if (this.props !== prevProps) {
this.reflow(this.props)
}
}
componentWillUnmount () {
if (this.canvas) {
this.canvas.parentNode.removeChild(this.canvas)
this.canvas = null
}
}
setState (state, callback) {
if (typeof state.clamped !== 'undefined') {
this.clamped = state.clamped
}
return super.setState(state, callback)
}
initCanvas () {
if (this.canvas) return
const canvas = this.canvas = document.createElement('div')
canvas.className = `LinesEllipsis-canvas ${this.props.className}`
canvas.setAttribute('aria-hidden', 'true')
this.copyStyleToCanvas()
Object.keys(canvasStyle).forEach((key) => {
canvas.style[key] = canvasStyle[key]
})
document.body.appendChild(canvas)
}
copyStyleToCanvas () {
const targetStyle = window.getComputedStyle(this.target)
mirrorProps.forEach((key) => {
this.canvas.style[key] = targetStyle[key]
})
}
reflow (props) {
/* eslint-disable no-control-regex */
const basedOn = props.basedOn || (/^[\x00-\x7F]+$/.test(props.text) ? 'words' : 'letters')
switch (basedOn) {
case 'words':
this.units = props.text.split(/\b|(?=\W)/)
break
case 'letters':
this.units = Array.from(props.text)
break
default:
throw new Error(`Unsupported options basedOn: ${basedOn}`)
}
this.maxLine = +props.maxLine || 1
this.canvas.innerHTML = this.units.map((c) => {
return `<span class='LinesEllipsis-unit'>${c}</span>`
}).join('')
const ellipsisIndex = this.putEllipsis(this.calcIndexes())
const clamped = ellipsisIndex > -1
const newState = {
clamped,
text: clamped ? this.units.slice(0, ellipsisIndex).join('') : props.text
}
this.setState(newState, props.onReflow.bind(this, newState))
}
calcIndexes () {
const indexes = [0]
let elt = this.canvas.firstElementChild
if (!elt) return indexes
let index = 0
let line = 1
let offsetTop = elt.offsetTop
while ((elt = elt.nextElementSibling)) {
if (elt.offsetTop > offsetTop) {
line++
indexes.push(index)
offsetTop = elt.offsetTop
}
index++
if (line > this.maxLine) {
break
}
}
return indexes
}
putEllipsis (indexes) {
if (indexes.length <= this.maxLine) return -1
const lastIndex = indexes[this.maxLine]
const units = this.units.slice(0, lastIndex)
const maxOffsetTop = this.canvas.children[lastIndex].offsetTop
this.canvas.innerHTML = units.map((c, i) => {
return `<span class='LinesEllipsis-unit'>${c}</span>`
}).join('') + `<wbr><span class='LinesEllipsis-ellipsis'>${this.props.ellipsis}</span>`
const ndEllipsis = this.canvas.lastElementChild
let ndPrevUnit = prevSibling(ndEllipsis, 2)
while (ndPrevUnit &&
(
ndEllipsis.offsetTop > maxOffsetTop || // IE & Edge: doesn't support <wbr>
ndEllipsis.offsetHeight > ndPrevUnit.offsetHeight ||
ndEllipsis.offsetTop > ndPrevUnit.offsetTop
)
) {
this.canvas.removeChild(ndPrevUnit)
ndPrevUnit = prevSibling(ndEllipsis, 2)
units.pop()
}
return units.length
}
// expose
isClamped () {
return this.clamped // do not use state.clamped. #27
}
render () {
const { text, clamped } = this.state
const { component: Component, ellipsis, trimRight, className, ...rest } = this.props
return (
<Component
className={`LinesEllipsis ${clamped ? 'LinesEllipsis--clamped' : ''} ${className}`}
ref={node => (this.target = node)}
{...omit(rest, usedProps)}
>
{clamped && trimRight
? text.replace(/[\s\uFEFF\xA0]+$/, '')
: text}
<wbr />
{clamped &&
<span className='LinesEllipsis-ellipsis'>{ellipsis}</span>}
</Component>
)
}
}
LinesEllipsis.defaultProps = defaultProps
export default LinesEllipsis