Comparing version 0.2.0 to 0.2.1
@@ -33,5 +33,2 @@ (function (global, factory) { | ||
// If already short enough, we're done | ||
if (el.offsetHeight < maxHeight) continue; | ||
var fullText = el.textContent; | ||
@@ -43,2 +40,15 @@ var words = spaces ? fullText.split(' ') : fullText; | ||
// Temporarily remove any CSS height for text height calculation | ||
var heightStyle = el.style.height; | ||
el.style.height = 'auto'; | ||
var maxHeightStyle = el.style.maxHeight; | ||
el.style.maxHeight = 'none'; | ||
// If already short enough, we're done | ||
if (el.offsetHeight < maxHeight) { | ||
el.style.height = heightStyle; | ||
el.style.maxHeight = maxHeightStyle; | ||
continue; | ||
} | ||
// Binary search for number of words which can fit in allotted height | ||
@@ -60,2 +70,5 @@ var max = words.length - 1; | ||
el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>'); | ||
el.style.height = heightStyle; | ||
el.style.maxHeight = maxHeightStyle; | ||
} | ||
@@ -67,7 +80,6 @@ } | ||
if (plugin) { | ||
plugin.fn.extend({ | ||
shave: function shaveFunc(maxHeight, opts) { | ||
return shave(this, maxHeight, opts); | ||
} | ||
}); | ||
plugin.fn.shave = function shavePlugin(maxHeight, opts) { | ||
shave(this, maxHeight, opts); | ||
return this; | ||
}; | ||
} | ||
@@ -74,0 +86,0 @@ } |
@@ -7,2 +7,2 @@ /** | ||
* @license ISC */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.shave=t()}(this,function(){"use strict";function e(e,t,n){if(!t)throw Error("maxHeight is required");var s="string"==typeof e?document.querySelectorAll(e):e;"length"in s||(s=[s]);for(var r={character:"…",classname:"js-shave",spaces:!0},i=n&&n.character||r.character,o=n&&n.classname||r.classname,a=(!n||n.spaces!==!1)&&r.spaces,c='<span class="js-shave-char">'+i+"</span>",f=0;f<s.length;f++){var d=s[f],l=d.querySelector("."+o);if(l&&(d.removeChild(d.querySelector(".js-shave-char")),d.textContent=d.textContent),!(d.offsetHeight<t)){var h=d.textContent,u=a?h.split(" "):h;if(!(u.length<2)){for(var p=u.length-1,v=0,j=void 0;v<p;)j=v+p+1>>1,d.textContent=a?u.slice(0,j).join(" "):u.slice(0,j),d.insertAdjacentHTML("beforeend",c),d.offsetHeight>t?p=a?j-1:j-2:v=j;d.textContent=a?u.slice(0,p).join(" "):u.slice(0,p),d.insertAdjacentHTML("beforeend",c);var y=a?u.slice(p+1).join(" "):u.slice(p);d.insertAdjacentHTML("beforeend",'<span class="'+o+'" style="display:none;">'+y+"</span>")}}}}if("undefined"!=typeof window){var t=window.$||window.jQuery||window.Zepto;t&&t.fn.extend({shave:function(t,n){return e(this,t,n)}})}return e}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.shave=t()}(this,function(){"use strict";function e(e,t,n){if(!t)throw Error("maxHeight is required");var s="string"==typeof e?document.querySelectorAll(e):e;"length"in s||(s=[s]);for(var i={character:"…",classname:"js-shave",spaces:!0},a=n&&n.character||i.character,r=n&&n.classname||i.classname,o=(!n||n.spaces!==!1)&&i.spaces,c='<span class="js-shave-char">'+a+"</span>",l=0;l<s.length;l++){var h=s[l],f=h.querySelector("."+r);f&&(h.removeChild(h.querySelector(".js-shave-char")),h.textContent=h.textContent);var d=h.textContent,u=o?d.split(" "):d;if(!(u.length<2)){var y=h.style.height;h.style.height="auto";var p=h.style.maxHeight;if(h.style.maxHeight="none",h.offsetHeight<t)h.style.height=y,h.style.maxHeight=p;else{for(var g=u.length-1,v=0,m=void 0;v<g;)m=v+g+1>>1,h.textContent=o?u.slice(0,m).join(" "):u.slice(0,m),h.insertAdjacentHTML("beforeend",c),h.offsetHeight>t?g=o?m-1:m-2:v=m;h.textContent=o?u.slice(0,g).join(" "):u.slice(0,g),h.insertAdjacentHTML("beforeend",c);var x=o?u.slice(g+1).join(" "):u.slice(g);h.insertAdjacentHTML("beforeend",'<span class="'+r+'" style="display:none;">'+x+"</span>"),h.style.height=y,h.style.maxHeight=p}}}}if("undefined"!=typeof window){var t=window.$||window.jQuery||window.Zepto;t&&(t.fn.shave=function(t,n){return e(this,t,n),this})}return e}); |
{ | ||
"name": "shave", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Shave is a javascript plugin that truncates multi-line text within a html element based on set max height", | ||
@@ -5,0 +5,0 @@ "main": "dist/shave.js", |
@@ -28,5 +28,9 @@  | ||
bower | ||
``` | ||
```sh | ||
bower install shave --save-dev | ||
``` | ||
yarn | ||
```sh | ||
yarn add shave --dev | ||
``` | ||
@@ -33,0 +37,0 @@ ## Usage |
11240
76
93