Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clamp the string when there are no spaces #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class LinesEllipsis extends React.Component {
this.canvas.innerHTML = this.units.map((c) => {
return `<span class='LinesEllipsis-unit'>${c}</span>`
}).join('')
const ellipsisIndex = this.putEllipsis(this.calcIndexes())
const ellipsisIndex = this.putEllipsis(this.calcIndexes(basedOn))
const clamped = ellipsisIndex > -1
const newState = {
clamped,
Expand All @@ -111,19 +111,28 @@ class LinesEllipsis extends React.Component {
this.setState(newState, props.onReflow.bind(this, newState))
}

calcIndexes () {
calcIndexes (basedOn) {
const indexes = [0]
let elt = this.canvas.firstElementChild
if (!elt) return indexes

let index = 0
let line = 1
let offsetTop = elt.offsetTop
let canvasWidth = this.canvas.offsetWidth
let sumElementsWidth = 0
while ((elt = elt.nextElementSibling)) {
if (elt.offsetTop === offsetTop) {
sumElementsWidth += elt.offsetWidth
}
if (elt.offsetTop > offsetTop) {
line++
indexes.push(index)
offsetTop = elt.offsetTop
sumElementsWidth = 0
} else if (sumElementsWidth > canvasWidth && basedOn === 'letters') {
line++
indexes.push(index)
}
index++
if (line > this.maxLine) {
Expand Down