UNPKG

react-truncate-markup

Version:
6 lines 17.4 kB
/*! * react-truncate-markup v5.1.2 * Apache-2.0 Licensed */ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports.ReactTruncateMarkup=e(require("react")):t.ReactTruncateMarkup=e(t.React)}(this,function(t){return function(t){function e(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},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="",e(e.s=0)}([function(t,e,n){t.exports=n(1)},function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(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 o(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}),n.d(e,"default",function(){return O});var s,c,u=n(2),a=n.n(u),h=n(3),l=n(4),f=n.n(l),p=n(6),d=n(8),v=n(9),y=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},g=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},m={LEFT:!0,RIGHT:!1},_=function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(!e)return n;if("string"==typeof e)return n+e;if(Object(v.c)(e))return n+v.a;var r=Array.isArray(e)?e:e.props.children||"";return n+a.a.Children.map(r,function(e){return t(e)}).join("")},w=function(t,e,n,r){return g({},t,{props:g({},t.props,{style:g({},t.props.style,function(){return n?{display:(t.props.style||{}).display||"block"}:2===r?{display:(t.props.style||{}).display||"inline-block"}:{}}()),children:e})})},T=function t(e){return!(null!=e&&!["string","number"].includes(void 0===e?"undefined":b(e))&&!Object(v.c)(e))||"function"!=typeof e.type&&(!e.props||!e.props.children||a.a.Children.toArray(e.props.children).reduce(function(e,n){return e&&t(n)},!0))},O=(c=s=function(t){function e(n){r(this,e);var o=i(this,t.call(this,n));return o.lineHeight=null,o.splitDirectionSeq=[],o.shouldTruncate=!0,o.wasLastCharTested=!1,o.endFound=!1,o.latestThatFits=null,o.onTruncateCalled=!1,o.toStringMemo=Object(h.a)(_),o.childrenWithRefMemo=Object(h.a)(o.childrenElementWithRef),o.validateTreeMemo=Object(h.a)(T),o.onTruncate=function(t){o.onTruncateCalled||(o.onTruncateCalled=!0,o.props.onTruncate(t))},o.handleResize=function(t,e){if(e&&e.disconnect(),!t)return null;var n=!0,r=function(){n?n=!1:(o.shouldTruncate=!1,o.latestThatFits=null,o.setState({text:o.origText},function(){o.shouldTruncate=!0,o.onTruncateCalled=!1,o.truncate()}))},i=e||new p.a(r);return i.observe(t),i},o.setRef=function(t){var e=o.el!==t;o.el=t,e&&(o.resizeObserver=o.handleResize(t,o.resizeObserver))},o.state={text:o.childrenWithRefMemo(o.props.children)},o}return o(e,t),e.prototype.componentDidMount=function(){this.isValid&&(this.lineHeight=this.props.lineHeight||f()(this.el),this.truncate())},e.prototype.UNSAFE_componentWillReceiveProps=function(t){var e=this;this.shouldTruncate=!1,this.latestThatFits=null,this.setState({text:this.childrenWithRefMemo(t.children)},function(){e.isValid&&(e.lineHeight=t.lineHeight||f()(e.el),e.shouldTruncate=!0,e.truncate())})},e.prototype.componentDidUpdate=function(){if(!1!==this.shouldTruncate&&!1!==this.isValid)return this.endFound?null!==this.latestThatFits&&this.state.text!==this.latestThatFits?void this.setState({text:this.latestThatFits}):void this.onTruncate(!0):void(this.splitDirectionSeq.length&&(this.fits()?(this.latestThatFits=this.state.text,this.splitDirectionSeq.splice(this.splitDirectionSeq.length-1,1,m.RIGHT,m.LEFT)):this.splitDirectionSeq.push(m.LEFT),this.tryToFit(this.origText,this.splitDirectionSeq)))},e.prototype.componentWillUnmount=function(){this.lineHeight=null,this.latestThatFits=null,this.splitDirectionSeq=[]},e.prototype.truncate=function(){if(this.fits())return this.shouldTruncate=!1,void this.onTruncate(!1);this.truncateOriginalText()},e.prototype.childrenElementWithRef=function(t){var e=a.a.Children.only(t);return a.a.cloneElement(e,{ref:this.setRef,style:g({wordWrap:"break-word"},e.props.style)})},e.prototype.truncateOriginalText=function(){this.endFound=!1,this.splitDirectionSeq=[m.LEFT],this.wasLastCharTested=!1,this.tryToFit(this.origText,this.splitDirectionSeq)},e.prototype.tryToFit=function(t,e){if(t.props.children){var n=this.split(t,e,!0),r="function"==typeof this.props.ellipsis?this.props.ellipsis(n):this.props.ellipsis;r="object"===(void 0===r?"undefined":b(r))?a.a.cloneElement(r,{key:"ellipsis"}):r;var i=n.props.children,o=[].concat(i,r),s=_(i)!==this.toStringMemo(this.props.children);this.setState({text:g({},n,{props:g({},n.props,{children:s?o:i})})})}},e.prototype.split=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1;if(!t||Object(v.c)(t))return this.endFound=!0,t;if("string"==typeof t)return this.splitString(t,e,r);if(Array.isArray(t))return this.splitArray(t,e,r);var i=this.split(t.props.children,e,!1,r+1);return w(t,i,n,r)},e.prototype.splitString=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=arguments[2];if(!e.length)return t;if(e.length&&this.policy.isAtomic(t))return this.wasLastCharTested?this.endFound=!0:this.wasLastCharTested=!0,t;if(this.policy.tokenizeString){return this.splitArray(this.policy.tokenizeString(t),e,n).join("")}var r=e[0],i=e.slice(1),o=Math.ceil(t.length/2),s=t.substring(0,o);if(r===m.LEFT)return this.splitString(s,i,n);var c=t.substring(o);return s+this.splitString(c,i,n)},e.prototype.splitArray=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=arguments[2];if(!e.length)return t;if(1===t.length)return[this.split(t[0],e,!1,n)];var r=e[0],i=e.slice(1),o=Math.ceil(t.length/2),s=t.slice(0,o);if(r===m.LEFT)return this.splitArray(s,i,n);var c=t.slice(o);return s.concat(this.splitArray(c,i,n))},e.prototype.fits=function(){var t=this.props.lines,e=this.el.getBoundingClientRect(),n=e.height;return t>=Math.round(n/parseFloat(this.lineHeight))},e.prototype.render=function(){return this.state.text},y(e,[{key:"isValid",get:function(){return this.validateTreeMemo(this.props.children)}},{key:"origText",get:function(){return this.childrenWithRefMemo(this.props.children)}},{key:"policy",get:function(){return d.a[this.props.tokenize]||d.a.characters}}]),e}(a.a.Component),s.Atom=v.b,s.defaultProps={lines:1,ellipsis:"...",lineHeight:"",onTruncate:function(){},tokenize:"characters"},c)},function(e,n){e.exports=t},function(t,e,n){"use strict";function r(t,e){if(t.length!==e.length)return!1;for(var n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function i(t,e){function n(){for(var n=[],r=0;r<arguments.length;r++)n[r]=arguments[r];return c&&i===this&&e(n,s)?o:(o=t.apply(this,n),c=!0,i=this,s=n,o)}void 0===e&&(e=r);var i,o,s=[],c=!1;return n}e.a=i},function(t,e,n){function r(t){var e=i(t,"line-height"),n=parseFloat(e,10);if(e===n+""){var r=t.style.lineHeight;t.style.lineHeight=e+"em",e=i(t,"line-height"),n=parseFloat(e,10),r?t.style.lineHeight=r:delete t.style.lineHeight}if(-1!==e.indexOf("pt")?(n*=4,n/=3):-1!==e.indexOf("mm")?(n*=96,n/=25.4):-1!==e.indexOf("cm")?(n*=96,n/=2.54):-1!==e.indexOf("in")?n*=96:-1!==e.indexOf("pc")&&(n*=16),n=Math.round(n),"normal"===e){var o=t.nodeName,s=document.createElement(o);s.innerHTML="&nbsp;","TEXTAREA"===o.toUpperCase()&&s.setAttribute("rows","1");var c=i(t,"font-size");s.style.fontSize=c,s.style.padding="0px",s.style.border="0px";var u=document.body;u.appendChild(s);n=s.offsetHeight,u.removeChild(s)}return n}var i=n(5);t.exports=r},function(t,e){var n=function(t,e,n){return n=window.getComputedStyle,(n?n(t):t.currentStyle)[e.replace(/-(\w)/gi,function(t,e){return e.toUpperCase()})]};t.exports=n},function(t,e,n){"use strict";(function(t){function n(t,e){function n(){o&&(o=!1,t()),s&&i()}function r(){v(n)}function i(){var t=Date.now();if(o){if(t-c<y)return;s=!0}else o=!0,s=!1,setTimeout(r,e);c=t}var o=!1,s=!1,c=0;return i}function r(t){return parseFloat(t)||0}function i(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];return e.reduce(function(e,n){return e+r(t["border-"+n+"-width"])},0)}function o(t){for(var e=["top","right","bottom","left"],n={},i=0,o=e;i<o.length;i++){var s=o[i],c=t["padding-"+s];n[s]=r(c)}return n}function s(t){var e=t.getBBox();return l(0,0,e.width,e.height)}function c(t){var e=t.clientWidth,n=t.clientHeight;if(!e&&!n)return O;var s=T(t).getComputedStyle(t),c=o(s),a=c.left+c.right,h=c.top+c.bottom,f=r(s.width),p=r(s.height);if("border-box"===s.boxSizing&&(Math.round(f+a)!==e&&(f-=i(s,"left","right")+a),Math.round(p+h)!==n&&(p-=i(s,"top","bottom")+h)),!u(t)){var d=Math.round(f+a)-e,v=Math.round(p+h)-n;1!==Math.abs(d)&&(f-=d),1!==Math.abs(v)&&(p-=v)}return l(c.left,c.top,f,p)}function u(t){return t===T(t).document.documentElement}function a(t){return p?E(t)?s(t):c(t):O}function h(t){var e=t.x,n=t.y,r=t.width,i=t.height,o="undefined"!=typeof DOMRectReadOnly?DOMRectReadOnly:Object,s=Object.create(o.prototype);return w(s,{x:e,y:n,width:r,height:i,top:n,right:e+r,bottom:i+n,left:e}),s}function l(t,e,n,r){return{x:t,y:e,width:n,height:r}}var f=function(){function t(t,e){var n=-1;return t.some(function(t,r){return t[0]===e&&(n=r,!0)}),n}return"undefined"!=typeof Map?Map:function(){function e(){this.__entries__=[]}return Object.defineProperty(e.prototype,"size",{get:function(){return this.__entries__.length},enumerable:!0,configurable:!0}),e.prototype.get=function(e){var n=t(this.__entries__,e),r=this.__entries__[n];return r&&r[1]},e.prototype.set=function(e,n){var r=t(this.__entries__,e);~r?this.__entries__[r][1]=n:this.__entries__.push([e,n])},e.prototype.delete=function(e){var n=this.__entries__,r=t(n,e);~r&&n.splice(r,1)},e.prototype.has=function(e){return!!~t(this.__entries__,e)},e.prototype.clear=function(){this.__entries__.splice(0)},e.prototype.forEach=function(t,e){void 0===e&&(e=null);for(var n=0,r=this.__entries__;n<r.length;n++){var i=r[n];t.call(e,i[1],i[0])}},e}()}(),p="undefined"!=typeof window&&"undefined"!=typeof document&&window.document===document,d=function(){return void 0!==t&&t.Math===Math?t:"undefined"!=typeof self&&self.Math===Math?self:"undefined"!=typeof window&&window.Math===Math?window:Function("return this")()}(),v=function(){return"function"==typeof requestAnimationFrame?requestAnimationFrame.bind(d):function(t){return setTimeout(function(){return t(Date.now())},1e3/60)}}(),y=2,b=20,g=["top","right","bottom","left","width","height","size","weight"],m="undefined"!=typeof MutationObserver,_=function(){function t(){this.connected_=!1,this.mutationEventsAdded_=!1,this.mutationsObserver_=null,this.observers_=[],this.onTransitionEnd_=this.onTransitionEnd_.bind(this),this.refresh=n(this.refresh.bind(this),b)}return t.prototype.addObserver=function(t){~this.observers_.indexOf(t)||this.observers_.push(t),this.connected_||this.connect_()},t.prototype.removeObserver=function(t){var e=this.observers_,n=e.indexOf(t);~n&&e.splice(n,1),!e.length&&this.connected_&&this.disconnect_()},t.prototype.refresh=function(){this.updateObservers_()&&this.refresh()},t.prototype.updateObservers_=function(){var t=this.observers_.filter(function(t){return t.gatherActive(),t.hasActive()});return t.forEach(function(t){return t.broadcastActive()}),t.length>0},t.prototype.connect_=function(){p&&!this.connected_&&(document.addEventListener("transitionend",this.onTransitionEnd_),window.addEventListener("resize",this.refresh),m?(this.mutationsObserver_=new MutationObserver(this.refresh),this.mutationsObserver_.observe(document,{attributes:!0,childList:!0,characterData:!0,subtree:!0})):(document.addEventListener("DOMSubtreeModified",this.refresh),this.mutationEventsAdded_=!0),this.connected_=!0)},t.prototype.disconnect_=function(){p&&this.connected_&&(document.removeEventListener("transitionend",this.onTransitionEnd_),window.removeEventListener("resize",this.refresh),this.mutationsObserver_&&this.mutationsObserver_.disconnect(),this.mutationEventsAdded_&&document.removeEventListener("DOMSubtreeModified",this.refresh),this.mutationsObserver_=null,this.mutationEventsAdded_=!1,this.connected_=!1)},t.prototype.onTransitionEnd_=function(t){var e=t.propertyName,n=void 0===e?"":e;g.some(function(t){return!!~n.indexOf(t)})&&this.refresh()},t.getInstance=function(){return this.instance_||(this.instance_=new t),this.instance_},t.instance_=null,t}(),w=function(t,e){for(var n=0,r=Object.keys(e);n<r.length;n++){var i=r[n];Object.defineProperty(t,i,{value:e[i],enumerable:!1,writable:!1,configurable:!0})}return t},T=function(t){return t&&t.ownerDocument&&t.ownerDocument.defaultView||d},O=l(0,0,0,0),E=function(){return"undefined"!=typeof SVGGraphicsElement?function(t){return t instanceof T(t).SVGGraphicsElement}:function(t){return t instanceof T(t).SVGElement&&"function"==typeof t.getBBox}}(),x=function(){function t(t){this.broadcastWidth=0,this.broadcastHeight=0,this.contentRect_=l(0,0,0,0),this.target=t}return t.prototype.isActive=function(){var t=a(this.target);return this.contentRect_=t,t.width!==this.broadcastWidth||t.height!==this.broadcastHeight},t.prototype.broadcastRect=function(){var t=this.contentRect_;return this.broadcastWidth=t.width,this.broadcastHeight=t.height,t},t}(),S=function(){function t(t,e){var n=h(e);w(this,{target:t,contentRect:n})}return t}(),M=function(){function t(t,e,n){if(this.activeObservations_=[],this.observations_=new f,"function"!=typeof t)throw new TypeError("The callback provided as parameter 1 is not a function.");this.callback_=t,this.controller_=e,this.callbackCtx_=n}return t.prototype.observe=function(t){if(!arguments.length)throw new TypeError("1 argument required, but only 0 present.");if("undefined"!=typeof Element&&Element instanceof Object){if(!(t instanceof T(t).Element))throw new TypeError('parameter 1 is not of type "Element".');var e=this.observations_;e.has(t)||(e.set(t,new x(t)),this.controller_.addObserver(this),this.controller_.refresh())}},t.prototype.unobserve=function(t){if(!arguments.length)throw new TypeError("1 argument required, but only 0 present.");if("undefined"!=typeof Element&&Element instanceof Object){if(!(t instanceof T(t).Element))throw new TypeError('parameter 1 is not of type "Element".');var e=this.observations_;e.has(t)&&(e.delete(t),e.size||this.controller_.removeObserver(this))}},t.prototype.disconnect=function(){this.clearActive(),this.observations_.clear(),this.controller_.removeObserver(this)},t.prototype.gatherActive=function(){var t=this;this.clearActive(),this.observations_.forEach(function(e){e.isActive()&&t.activeObservations_.push(e)})},t.prototype.broadcastActive=function(){if(this.hasActive()){var t=this.callbackCtx_,e=this.activeObservations_.map(function(t){return new S(t.target,t.broadcastRect())});this.callback_.call(t,e,t),this.clearActive()}},t.prototype.clearActive=function(){this.activeObservations_.splice(0)},t.prototype.hasActive=function(){return this.activeObservations_.length>0},t}(),A="undefined"!=typeof WeakMap?new WeakMap:new f,F=function(){function t(e){if(!(this instanceof t))throw new TypeError("Cannot call a class as a function.");if(!arguments.length)throw new TypeError("1 argument required, but only 0 present.");var n=_.getInstance(),r=new M(e,n,this);A.set(this,r)}return t}();["observe","unobserve","disconnect"].forEach(function(t){F.prototype[t]=function(){var e;return(e=A.get(this))[t].apply(e,arguments)}});var R=function(){return void 0!==d.ResizeObserver?d.ResizeObserver:F}();e.a=R}).call(e,n(7))},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){"use strict";var r={characters:{tokenizeString:null,isAtomic:function(t){return t.length<=1}},words:{tokenizeString:function(t){return t.match(/(\s*\S[\S\xA0]*)/g)},isAtomic:function(t){return/^\s*[\S\xA0]*\s*$/.test(t)}}};e.a=r},function(t,e,n){"use strict";n.d(e,"b",function(){return r}),n.d(e,"c",function(){return i}),n.d(e,"a",function(){return o});var r=function(t){return t.children||null};r.__rtm_atom=!0;var i=function(t){return!(!t||!t.type||!0!==t.type.__rtm_atom)},o="<Atom>"}])}); //# sourceMappingURL=react-truncate-markup.min.js.map