Last active
September 24, 2020 13:43
-
-
Save pamelafox/4674841 to your computer and use it in GitHub Desktop.
truncate based off offscreen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function truncateText(string, nMaxChars) { | |
if (string.length <= nMaxChars) | |
return string; | |
var xMaxFit = nMaxChars - 3; | |
var xTruncateAt = string.lastIndexOf(' ', xMaxFit); | |
if (xTruncateAt == -1 || xTruncateAt < nMaxChars / 2) | |
xTruncateAt = xMaxFit; | |
return string.substr(0, xTruncateAt) + "…"; | |
} | |
function makeKnob(width, label) { | |
var knob = $('<div class="colorslider-knob-wrapper"><div class="colorslider-knob"></div>'); | |
var offscreen = $('<div></div>').css({'position': 'absolute', 'top': '-999px', 'left': '-999px'}).appendTo('body'); | |
offscreen.width(knob.width()).html(label); | |
if (offscreen.height() > 40) { | |
label = ED.util.truncateText(label, 20); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment