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

Support for text without spaces #85

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Hint: (Generally with React) if you want to preserve newlines from plain text, y
| children | string, React node | | The text to be truncated. Anything that can be evaluated as text. | `'Some text'`, `<p>Some paragraph <a/>with other text-based inline elements<a></p>`, `<span>Some</span><span>siblings</span>` |
| onTruncate | function | | Gets invoked on each render. Gets called with `true` when text got truncated and ellipsis was injected, and with `false` otherwise. | `isTruncated => isTruncated !== this.state.isTruncated && this.setState({ isTruncated })` |
| trimWhitespace | boolean | false | If `true`, whitespace will be removed from before the ellipsis (e.g. `words ...` will become `words...` instead) | `<Truncate trimWhitespace>{longText}</Truncate>` |
| breakAll | boolean | false | If `true`, lines will break at any place, not only on spaces (useful for example for truncating multiline URLs). | `<Truncate breakAll lines={3}>{longText}</Truncate>` |

## Known issues
- Text exceeding horizontal boundaries when "viewport" meta tag is not set accordingly for mobile devices (font boosting leads to wrong calculations). See [issue](https://github.com/One-com/react-truncate/issues/4#issuecomment-226703499)
Expand Down
19 changes: 12 additions & 7 deletions src/Truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ export default class Truncate extends Component {
PropTypes.number
]),
trimWhitespace: PropTypes.bool,
breakAll: PropTypes.bool,
onTruncate: PropTypes.func
};

static defaultProps = {
children: '',
ellipsis: '…',
lines: 1,
trimWhitespace: false
trimWhitespace: false,
breakAll: false
};

state = {};
Expand Down Expand Up @@ -175,7 +177,8 @@ export default class Truncate extends Component {
props: {
lines: numLines,
ellipsis,
trimWhitespace
trimWhitespace,
breakAll
},
state: {
targetWidth
Expand All @@ -188,7 +191,8 @@ export default class Truncate extends Component {

const lines = [];
const text = innerText(elements.text);
const textLines = text.split('\n').map(line => line.split(' '));
const wordSeparator = breakAll ? '' : ' ';
const textLines = text.split('\n').map(line => line.split(wordSeparator));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having a breakAll property, we could introduce a separator property, that defaults to ' ' and use it here. What do you think?

let didTruncate = true;
const ellipsisWidth = this.ellipsisWidth(this.elements.ellipsis);

Expand All @@ -203,7 +207,7 @@ export default class Truncate extends Component {
continue;
}

let resultLine = textWords.join(' ');
let resultLine = textWords.join(wordSeparator);

if (measureWidth(resultLine) <= targetWidth) {
if (textLines.length === 1) {
Expand All @@ -217,7 +221,7 @@ export default class Truncate extends Component {

if (line === numLines) {
// Binary search determining the longest possible line inluding truncate string
const textRest = textWords.join(' ');
const textRest = textWords.join(wordSeparator);

let lower = 0;
let upper = textRest.length - 1;
Expand Down Expand Up @@ -256,7 +260,7 @@ export default class Truncate extends Component {
while (lower <= upper) {
const middle = Math.floor((lower + upper) / 2);

const testLine = textWords.slice(0, middle + 1).join(' ');
const testLine = textWords.slice(0, middle + 1).join(wordSeparator);

if (measureWidth(testLine) <= targetWidth) {
lower = middle + 1;
Expand All @@ -272,7 +276,7 @@ export default class Truncate extends Component {
continue;
}

resultLine = textWords.slice(0, lower).join(' ');
resultLine = textWords.slice(0, lower).join(wordSeparator);
textLines[0].splice(0, lower);
}

Expand Down Expand Up @@ -336,6 +340,7 @@ export default class Truncate extends Component {

delete spanProps.onTruncate;
delete spanProps.trimWhitespace;
delete spanProps.breakAll;

return (
<span {...spanProps} ref={(targetEl) => { this.elements.target = targetEl; }}>
Expand Down