Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Last active September 24, 2020 13:43
Show Gist options
  • Save pamelafox/4674841 to your computer and use it in GitHub Desktop.
Save pamelafox/4674841 to your computer and use it in GitHub Desktop.
truncate based off offscreen
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) + "&hellip;";
}
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