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

enable truncation based on single long words and number of line restriction #91

Merged
merged 10 commits into from
Jun 5, 2018
Merged
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
37 changes: 25 additions & 12 deletions src/TextBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import strip from "./strip";
import textSplit from "./textSplit";
import measure from "./textWidth";
import wrap from "./textWrap";
import truncateWord from "./textTruncate";
import {trimRight} from "./trim";

/**
Expand Down Expand Up @@ -47,6 +48,7 @@ export default class TextBox extends BaseClass {
this._height = accessor("height", 200);
this._id = (d, i) => d.id || `${i}`;
this._lineHeight = (d, i) => this._fontSize(d, i) * 1.2;
this._maxLines = constant(null);
this._on = {};
this._overflow = constant(false);
this._padding = constant(0);
Expand Down Expand Up @@ -106,6 +108,7 @@ export default class TextBox extends BaseClass {
.fontSize(fS)
.fontWeight(style["font-weight"])
.lineHeight(lH)
.maxLines(this._maxLines(d, i))
.height(h)
.overflow(this._overflow(d, i))
.width(w);
Expand All @@ -120,12 +123,14 @@ export default class TextBox extends BaseClass {
@private
*/
function checkSize() {
const truncate = () => {
if (line < 1) lineData = [truncateWord(wrapResults.words[0], that._ellipsis("", line), w, style)];
else lineData[line - 1] = that._ellipsis(lineData[line - 1], line);
};

if (fS < fMin) {
lineData = [];
return;
}
else if (fS > fMax) fS = fMax;
// Constraint the font size
fS = max([fS, fMin]);
fS = min([fS, fMax]);

if (resize) {
lH = fS * lHRatio;
Expand All @@ -141,18 +146,17 @@ export default class TextBox extends BaseClass {
line = lineData.length;

if (wrapResults.truncated) {

if (resize) {
fS--;
if (fS < fMin) lineData = [];
if (fS < fMin) {
fS = fMin;
truncate();
return;
}
else checkSize();
}
else if (line < 1) lineData = [that._ellipsis("", line)];
else lineData[line - 1] = that._ellipsis(lineData[line - 1], line);

else truncate();
}


}

if (w > fMin && (h > lH || resize && h > fMin * lHRatio)) {
Expand Down Expand Up @@ -464,6 +468,15 @@ function(d, i) {
return arguments.length ? (this._lineHeight = typeof _ === "function" ? _ : constant(_), this) : this._lineHeight;
}

/**
@memberof TextBox
@desc Restricts the maximum number of lines to wrap onto, which is null (unlimited) by default.
@param {Function|Number} [*value*]
*/
maxLines(_) {
return arguments.length ? (this._maxLines = typeof _ === "function" ? _ : constant(_), this) : this._maxLines;
}

/**
@memberof TextBox
@desc Sets the text overflow to the specified accessor function or static boolean.
Expand Down
22 changes: 22 additions & 0 deletions src/textTruncate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import measure from "./textWidth";

/**
@function textTruncate
@desc Truncate a single word with ellipsis until if fits within the given width
@param {String} text The word to truncate
@param {String} ellipsis The ellipsis to append
@param {Number} maxWidth The maximum width that the text can take
@param {Object} style The style object to apply
@return {String} The resultant text with ellipsis
*/
export default function(text, ellipsis, maxWidth, style) {
for (let i = text.length; i > 0; i--) {
const shortened = text.slice(0, i) + ellipsis;
const width = measure(shortened, style);
if (width < maxWidth) {
return shortened;
}
}

return ellipsis;
}
12 changes: 11 additions & 1 deletion src/textWrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function() {
fontWeight = 400,
height = 200,
lineHeight,
maxLines = null,
overflow = false,
split = defaultSplit,
width = 200;
Expand Down Expand Up @@ -57,7 +58,7 @@ export default function() {
}
lineData[line - 1] = trimRight(lineData[line - 1]);
line++;
if (lineHeight * line > height || wordWidth > width && !overflow) {
if (lineHeight * line > height || wordWidth > width && !overflow || (maxLines && line > maxLines)) {
truncated = true;
break;
}
Expand Down Expand Up @@ -125,6 +126,15 @@ export default function() {
return arguments.length ? (lineHeight = _, textWrap) : lineHeight;
};

/**
@memberof textWrap
@desc If *value* is specified, sets the maximum number of lines allowed when wrapping.
@param {Function|Number} [*value*]
*/
textWrap.maxLines = function(_) {
return arguments.length ? (maxLines = _, textWrap) : maxLines;
};

/**
@memberof textWrap
@desc If *value* is specified, sets the overflow to the specified boolean and returns this generator. If *value* is not specified, returns the current overflow value.
Expand Down