ngx-truncate-text
Version:
This module is for shortening the text and also includes some useful features for text manipulation like finding hashtags, highlight words with favorite color and so on after version 1.0.0.
2 lines • 6 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core")):"function"==typeof define&&define.amd?define("ngx-truncate-text",["exports","@angular/core"],t):t(e["ngx-truncate-text"]={},e.ng.core)}(this,function(e,t){"use strict";function H(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&n>=e.length?void 0:e)&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}var r=(n.prototype.checkWordCut=function(e,t){for(var r=0,r=e;r<t.length&&" "!=t[r];r++);return t.substring(0,r)},n.prototype.findHashtag=function(e){return e=e.replace(/(^|\s)(#[\p{Pc}\p{N}\p{L}\u200cÀ-ÖØ-öø-ʸ(_)]+)/gimu,function(e){return'<span class="hashtag" style="color:#1b95e0">'+e+"</span>"})},n.prototype.applyCondition=function(e,t,r,n){void 0===n&&(n=!1);var i;return(r=void 0===r?!1:r)&&n?(i=this.checkWordCut(t,e),this.findHashtag(i)):r?this.checkWordCut(t,e):n?(i=e.substring(0,t),this.findHashtag(i)):e.substring(0,t)},n.prototype.highlight=function(r,e,t){var n,i,o,h,s,l,a,c;if(!t.length)return r;if("exactly"==e){try{for(var p=H(t),u=p.next();!u.done;u=p.next())!function(t){var e="";r=t.hasOwnProperty("name")?(e="(^|(?<!\\p{L}))("+t.name+")(?!(\\p{L}))",r.replace(new RegExp(e,"gmu"),function(e){return'<span style="background:'+t.color+'">'+e+"</span>"})):(e="(^|(?<!\\p{L}))("+t+")(?!(\\p{L}))",r.replace(new RegExp(e,"gmu"),function(e){return'<span style="background:yellow">'+e+"</span>"}))}(y=u.value)}catch(T){n={error:T}}finally{try{u&&!u.done&&(i=p["return"])&&i.call(p)}finally{if(n)throw n.error}}return r}try{for(var g=H(t),f=g.next();!f.done;f=g.next()){var d,y=f.value;if(y.hasOwnProperty("name")){if(d=r.match(new RegExp(y.name,"gmi")))try{for(var m=H(d),x=m.next();!x.done;x=m.next()){var v=x.value;r=r.replace(new RegExp(y.name,"gmi"),'<span style="background-color:'+y.color+'">'+v+"</span>")}}catch(b){s={error:b}}finally{try{x&&!x.done&&(l=m["return"])&&l.call(m)}finally{if(s)throw s.error}}}else if(d=r.match(new RegExp(y,"gmi")))try{for(var w=H(d),S=w.next();!S.done;S=w.next()){v=S.value;r=r.replace(new RegExp(y,"gmi"),'<span style="background-color:yellow">'+v+"</span>")}}catch(L){a={error:L}}finally{try{S&&!S.done&&(c=w["return"])&&c.call(w)}finally{if(a)throw a.error}}}}catch(C){o={error:C}}finally{try{f&&!f.done&&(h=g["return"])&&h.call(g)}finally{if(o)throw o.error}}return r},n.decorators=[{type:t.Injectable}],n.ctorParameters=function(){return[]},n);function n(){}var i=(Object.defineProperty(o.prototype,"highlightList",{set:function(e){this._highlightQuery=JSON.parse(e)},enumerable:!0,configurable:!0}),o.prototype.ngOnChanges=function(e){this._highlightQuery.sort(function(e,t){if(e.hasOwnProperty("name"))return t.name.length-e.name.length}),e&&e.content&&e.content.previousValue&&this.fill(e.content.currentValue)},o.prototype.ngAfterViewInit=function(){this.fill(this.element.innerHTML)},o.prototype.fill=function(e){var t=this;this.hasLiteral||(e=e.replace(/(\r\n\t|\n|\r\t)/gm," "));var r,n;(this.text=e).length>this.number?(r=this.trunService.applyCondition(e,this.number,this.completeWord,this.hashtag),r=this.trunService.highlight(r,this.highlightCondition,this._highlightQuery),this.replace=!0,this.element.innerHTML=r+" ... ",(n=this.renderer.createElement("span")).innerHTML=this.more,this.renderer.setStyle(n,"color","#ff00ff"),this.renderer.setStyle(n,"cursor","pointer"),this.renderer.addClass(n,"toggleText"),this.element.innerHTML=r+"... ",this.renderer.listen(n,"click",function(e){return!0===t.replace?t.showFullText(e):t.hideSomeText(e)}),this.element.appendChild(n)):(e=this.hashtag?this.trunService.findHashtag(e):e,e=this.trunService.highlight(e,this.highlightCondition,this._highlightQuery),this.element.innerHTML=e),this.hasLiteral&&(this.element.style.whiteSpace="pre-line")},o.prototype.showFullText=function(e){var t=this,r=this.renderer.createElement("span");r.innerHTML=" "+this.less,this.renderer.setStyle(r,"color","#ff00ff"),this.renderer.setStyle(r,"cursor","pointer"),this.renderer.addClass(r,"toggleText");var n=this.hashtag?this.trunService.findHashtag(this.text):this.text;this.element.innerHTML=this.trunService.highlight(n,this.highlightCondition,this._highlightQuery),this.hasLiteral&&(this.element.style.whiteSpace="pre-line"),this.renderer.listen(r,"click",function(e){return t.hideSomeText(e)}),this.element.appendChild(r),e.stopPropagation(),this.replace=!1},o.prototype.hideSomeText=function(e){var t=this,r=this.trunService.applyCondition(this.text,this.number,this.completeWord,this.hashtag),n=this.trunService.highlight(r,this.highlightCondition,this._highlightQuery),r=this.renderer.createElement("span");this.renderer.setStyle(r,"color","#ff00ff"),this.renderer.setStyle(r,"cursor","pointer"),this.renderer.addClass(r,"toggleText"),r.innerHTML=this.more,this.element.innerHTML=n+" ... ",this.hasLiteral&&(this.element.style.whiteSpace="pre-line"),this.renderer.listen(r,"click",function(e){return t.showFullText(e)}),this.element.appendChild(r),e.stopPropagation(),this.replace=!0},o.decorators=[{type:t.Directive,args:[{selector:"[ngxTruncateText]"}]}],o.ctorParameters=function(){return[{type:t.ElementRef},{type:t.Renderer2},{type:r}]},o.propDecorators={more:[{type:t.Input}],less:[{type:t.Input}],number:[{type:t.Input}],completeWord:[{type:t.Input}],hashtag:[{type:t.Input}],hasLiteral:[{type:t.Input}],highlightCondition:[{type:t.Input}],highlightList:[{type:t.Input}]},o);function o(e,t,r){this.elRef=e,this.renderer=t,this.trunService=r,this._highlightQuery=[],this.replace=!1,(window.trun=this).element=e.nativeElement}var h=(s.decorators=[{type:t.NgModule,args:[{declarations:[i],imports:[],exports:[i],providers:[r]}]}],s);function s(){}e.NgxTruncateTextModule=h,e.NgxTruncateTextDirective=i,e.ɵa=r,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=ngx-truncate-text.umd.min.js.map