react-text-ellipsis
Version:
react multi-line ellipsis
1 lines • 8.19 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define("TextEllipsis",["React"],e):"object"==typeof exports?exports.TextEllipsis=e(require("react")):t.TextEllipsis=e(t.React)}("undefined"!=typeof self?self:this,function(t){return function(t){function e(i){if(n[i])return n[i].exports;var r=n[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,i){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:i})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="/dist/",e(e.s=0)}([function(t,e,n){"use strict";function i(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var u=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),a=n(1),c=n(2),l=i(c),f=n(4),p=i(f),h=function(t){function e(t){r(this,e);var n=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return n.isSupportNativeClamp=!1,n.truncate=n.truncate.bind(n),n.process=n.process.bind(n),n.debounceProcess=(0,l.default)(n.process,n.props.debounceTimeoutOnResize),n.text="",n.lineHeight=0,n.splitOnChars=[".","-","–","—"," "],n.splitChar=n.splitOnChars[0],n.chunks=null,n.lastChunk=null,n}return s(e,t),u(e,[{key:"componentDidMount",value:function(){this.isSupportNativeClamp=!this.props.useJsOnly&&"webkitLineClamp"in document.body.style,this.text=this.container.innerHTML,this.process(),window.addEventListener("resize",this.debounceProcess,!1)}},{key:"componentDidUpdate",value:function(){this.text=this.container.innerHTML,this.process()}},{key:"componentWillUnmount",value:function(){window.removeEventListener("resize",this.debounceProcess,!1)}},{key:"onResult",value:function(){this.container.scrollWidth>this.container.clientWidth||this.container.scrollHeight>this.container.clientHeight?this.props.onResult(e.RESULT.TRUNCATED):this.props.onResult(e.RESULT.NOT_TRUNCATED)}},{key:"getLineHeight",value:function(){var t=this.computedStyle("line-height");return"normal"===t?Math.ceil(1.2*parseFloat(this.computedStyle("font-size"))):Math.ceil(parseFloat(t))}},{key:"truncate",value:function(){if(this.chunks||(this.splitOnChars.length>0?this.splitChar=this.splitOnChars.shift():this.splitChar="",this.chunks=this.container.innerHTML.split(this.splitChar)),this.chunks.length>1?(this.lastChunk=this.chunks.pop(),this.container.innerHTML=this.chunks.join(this.splitChar)+this.props.ellipsisChars):this.chunks=null,this.chunks){if(this.container.offsetHeight<=this.lineHeight*this.props.lines){if(!(this.splitOnChars.length>=0&&""!==this.splitChar))return;this.container.innerHTML=this.chunks.join(this.splitChar)+this.splitChar+this.lastChunk,this.chunks=null}}else if(""===this.splitChar)return;this.truncate()}},{key:"process",value:function(){if(this.isSupportNativeClamp){var t=this.container.style;t.overflow="hidden",t.textOverflow="ellipsis",t.webkitBoxOrient="vertical",t.display="-webkit-box",t.webkitLineClamp=this.props.lines,this.onResult()}else this.lineHeight=this.getLineHeight(),this.container.offsetHeight<this.lineHeight*this.props.lines?this.onResult():this.truncate()}},{key:"computedStyle",value:function(t){return document.defaultView.getComputedStyle(this.container,null).getPropertyValue(t)}},{key:"render",value:function(){var t=this;return(0,a.createElement)(this.props.tag,{ref:function(e){t.container=e},className:this.props.tagClass,style:Object.assign({width:"100%",wordWrap:"break-word"},this.props.style)},this.props.children)}}]),e}(a.PureComponent);h.RESULT={TRUNCATED:"TRUNCATED",NOT_TRUNCATED:"NOT_TRUNCATED"},h.propTypes={lines:p.default.number.isRequired,children:p.default.element.isRequired,tag:p.default.string,ellipsisChars:p.default.string,tagClass:p.default.string,debounceTimeoutOnResize:p.default.number,useJsOnly:p.default.bool,onResult:p.default.func,style:p.default.object},h.defaultProps={tag:"div",ellipsisChars:"...",tagClass:"",debounceTimeoutOnResize:200,useJsOnly:!1,style:{},onResult:function(){}},e.default=h},function(e,n){e.exports=t},function(t,e,n){(function(e){function n(t,e,n){function r(e){var n=y,i=b;return y=b=void 0,w=e,m=t.apply(i,n)}function o(t){return w=t,T=setTimeout(l,e),x?r(t):m}function a(t){var n=t-R,i=t-w,r=e-n;return k?O(r,v-i):r}function c(t){var n=t-R,i=t-w;return void 0===R||n>=e||n<0||k&&i>=v}function l(){var t=C();if(c(t))return f(t);T=setTimeout(l,a(t))}function f(t){return T=void 0,j&&y?r(t):(y=b=void 0,m)}function p(){void 0!==T&&clearTimeout(T),w=0,y=R=b=T=void 0}function h(){return void 0===T?m:f(C())}function d(){var t=C(),n=c(t);if(y=arguments,b=this,R=t,n){if(void 0===T)return o(R);if(k)return T=setTimeout(l,e),r(R)}return void 0===T&&(T=setTimeout(l,e)),m}var y,b,v,m,T,R,w=0,x=!1,k=!1,j=!0;if("function"!=typeof t)throw new TypeError(u);return e=s(e)||0,i(n)&&(x=!!n.leading,k="maxWait"in n,v=k?g(s(n.maxWait)||0,e):v,j="trailing"in n?!!n.trailing:j),d.cancel=p,d.flush=h,d}function i(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function r(t){return!!t&&"object"==typeof t}function o(t){return"symbol"==typeof t||r(t)&&T.call(t)==c}function s(t){if("number"==typeof t)return t;if(o(t))return a;if(i(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=i(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(l,"");var n=p.test(t);return n||h.test(t)?d(t.slice(2),n?2:8):f.test(t)?a:+t}var u="Expected a function",a=NaN,c="[object Symbol]",l=/^\s+|\s+$/g,f=/^[-+]0x[0-9a-f]+$/i,p=/^0b[01]+$/i,h=/^0o[0-7]+$/i,d=parseInt,y="object"==typeof e&&e&&e.Object===Object&&e,b="object"==typeof self&&self&&self.Object===Object&&self,v=y||b||Function("return this")(),m=Object.prototype,T=m.toString,g=Math.max,O=Math.min,C=function(){return v.Date.now()};t.exports=n}).call(e,n(3))},function(t,e){var n;n=function(){return this}();try{n=n||Function("return this")()||(0,eval)("this")}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e,n){t.exports=n(5)()},function(t,e,n){"use strict";var i=n(6),r=n(7),o=n(8);t.exports=function(){function t(t,e,n,i,s,u){u!==o&&r(!1,"Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types")}function e(){return t}t.isRequired=t;var n={array:t,bool:t,func:t,number:t,object:t,string:t,symbol:t,any:t,arrayOf:e,element:t,instanceOf:e,node:t,objectOf:e,oneOf:e,oneOfType:e,shape:e,exact:e};return n.checkPropTypes=i,n.PropTypes=n,n}},function(t,e,n){"use strict";function i(t){return function(){return t}}var r=function(){};r.thatReturns=i,r.thatReturnsFalse=i(!1),r.thatReturnsTrue=i(!0),r.thatReturnsNull=i(null),r.thatReturnsThis=function(){return this},r.thatReturnsArgument=function(t){return t},t.exports=r},function(t,e,n){"use strict";function i(t,e,n,i,o,s,u,a){if(r(e),!t){var c;if(void 0===e)c=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var l=[n,i,o,s,u,a],f=0;c=new Error(e.replace(/%s/g,function(){return l[f++]})),c.name="Invariant Violation"}throw c.framesToPop=1,c}}var r=function(t){};t.exports=i},function(t,e,n){"use strict";t.exports="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"}])});