Skip to content

Commit

Permalink
Cleaned up the code a little
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Wright committed May 31, 2018
1 parent f23478a commit 68ef807
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
8 changes: 4 additions & 4 deletions pages/Truncate.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
.TextBox()
.data([d])
.select(this)
.fontResize(true)
.fontResize(d.resize)
.height(radius)
.width(radius)
.text(d => d.name)
Expand All @@ -35,9 +35,9 @@
};

const circles = [
{ x: 150, y: 150, name: "ABCDEFGHIJKLMNOPQRSTUVWXYZ12" },
{ x: 500, y: 150, name: "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" },
{ x: 150, y: 500, name: "Bacon ipsum dolor amet venison ball tip meatloaf, shank sirloin pig filet mignon pastrami pork belly pork chop. Strip steak ham spare ribs sausage. Fatback venison burgdoggen bacon pancetta kevin shoulder porchetta spare ribs. Venison tongue short loin, pork belly pork loin jowl ham hock t-bone sausage doner. Rump bacon beef ribs short loin brisket buffalo turducken beef. Cupim swine burgdoggen kevin. Boudin tongue tri-tip sirloin, biltong short ribs pancetta pork loin. Boudin short loin brisket, shoulder porchetta biltong short ribs drumstick ground round jerky kevin andouille. Ham ribeye swine pork loin pork chop tenderloin, shoulder pork salami sausage ham hock pancetta. Tail andouille ham strip steak meatball ribeye. Bresaola pork chop tongue tri-tip. Pig tail ground round pork belly beef ribs meatball turducken kevin ball tip capicola salami pork chop hamburger brisket. Beef ribs venison turducken doner shankle landjaeger salami biltong cupim beef. Short loin kevin chicken andouille capicola tongue. Porchetta shank pastrami sirloin, fatback venison tri-tip." },
{ x: 150, y: 150, resize: true, name: "ABCDEFGHIJKLMNOPQRSTUVWXYZ12" },
{ x: 500, y: 150, resize: true, name: "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" },
{ x: 150, y: 500, resize: true, name: "Bacon ipsum dolor amet venison ball tip meatloaf, shank sirloin pig filet mignon pastrami pork belly pork chop. Strip steak ham spare ribs sausage. Fatback venison burgdoggen bacon pancetta kevin shoulder porchetta spare ribs. Venison tongue short loin, pork belly pork loin jowl ham hock t-bone sausage doner. Rump bacon beef ribs short loin brisket buffalo turducken beef. Cupim swine burgdoggen kevin. Boudin tongue tri-tip sirloin, biltong short ribs pancetta pork loin. Boudin short loin brisket, shoulder porchetta biltong short ribs drumstick ground round jerky kevin andouille. Ham ribeye swine pork loin pork chop tenderloin, shoulder pork salami sausage ham hock pancetta. Tail andouille ham strip steak meatball ribeye. Bresaola pork chop tongue tri-tip. Pig tail ground round pork belly beef ribs meatball turducken kevin ball tip capicola salami pork chop hamburger brisket. Beef ribs venison turducken doner shankle landjaeger salami biltong cupim beef. Short loin kevin chicken andouille capicola tongue. Porchetta shank pastrami sirloin, fatback venison tri-tip." },
];

const join = d3
Expand Down
26 changes: 14 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 @@ -120,12 +121,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 = Math.max(fS, fMin);
fS = Math.min(fS, fMax);

if (resize) {
lH = fS * lHRatio;
Expand All @@ -141,18 +144,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

0 comments on commit 68ef807

Please sign in to comment.