1- import { Component , Input , ViewChild , ElementRef , AfterViewInit , OnChanges , SimpleChange , ViewEncapsulation , Output , EventEmitter } from '@angular/core' ;
1+ import { Component , Input , ViewChild , ElementRef , AfterViewInit , OnChanges , SimpleChanges , ViewEncapsulation , Output , EventEmitter } from '@angular/core' ;
2+ import { SafeHtml } from '@angular/platform-browser' ;
23import { BrowserStorageService } from '@v3/services/storage.service' ;
34
45@Component ( {
56 selector : 'app-description' ,
67 templateUrl : 'description.component.html' ,
78 styleUrls : [ './description.component.scss' ] ,
8- encapsulation : ViewEncapsulation . ShadowDom ,
9- /*animations: [
10- trigger('truncation', [
11- state('show', style({
12- 'max-height': '1000px !important',
13- })),
14- state('hide', style({
15- 'max-height': '90px !important',
16- })),
17- transition('* <=> *', [
18- animate('0.5s ease-in-out')
19- ])
20- ]),
21- ]*/
9+ encapsulation : ViewEncapsulation . None ,
2210} )
23- export class DescriptionComponent implements OnChanges {
11+ export class DescriptionComponent implements OnChanges , AfterViewInit {
2412 heightLimit = 145 ; // more accurately adjusted
2513 isTruncating : boolean ;
2614 heightExceeded : boolean ;
2715 elementHeight : number ;
2816 hasBeenTruncated : boolean ; // prevent onChange replace the collapsed content
2917
30- @Input ( ) name ; // unique identity of parent element
31- @Input ( ) content ;
32- @Input ( ) isInPopup ;
18+ @Input ( ) name : string ; // unique identity of parent element
19+ @Input ( ) content : SafeHtml ;
20+ @Input ( ) isInPopup : boolean ;
3321 @Input ( ) nonCollapsible ?: boolean ;
34- @Output ( ) hasExpanded ? = new EventEmitter ( ) ;
22+ @Input ( ) ariaLabel ?: string ;
23+ @Output ( ) hasExpanded ? = new EventEmitter < boolean > ( ) ;
3524 @ViewChild ( 'description' ) descriptionRef : ElementRef ;
3625
3726 constructor (
@@ -40,43 +29,40 @@ export class DescriptionComponent implements OnChanges {
4029 this . hasBeenTruncated = false ;
4130 }
4231
43- ngOnChanges ( changes : { [ propKey : string ] : SimpleChange } ) {
44- // reset to default
45- if ( this . hasBeenTruncated === false ) {
32+ ngOnChanges ( changes : SimpleChanges ) {
33+ if ( changes . content && ! changes . content . firstChange ) {
34+ this . hasBeenTruncated = false ;
4635 this . isTruncating = false ;
4736 this . heightExceeded = false ;
37+ this . calculateHeight ( ) ;
4838 }
39+ }
4940
50- this . content = changes . content . currentValue ;
41+ ngAfterViewInit ( ) {
5142 this . calculateHeight ( ) ;
5243 }
5344
5445 calculateHeight ( ) : void {
55- if ( this . nonCollapsible === true ) {
46+ if ( this . nonCollapsible === true || ! this . storage . getUser ( ) . truncateDescription ) {
5647 return ;
5748 }
5849
59- if ( ! this . storage . getUser ( ) . truncateDescription ) {
60- return ;
61- }
62- setTimeout (
63- ( ) => {
50+ setTimeout ( ( ) => {
51+ if ( this . descriptionRef ?. nativeElement ) {
6452 this . elementHeight = this . descriptionRef . nativeElement . clientHeight ;
6553 this . heightExceeded = this . elementHeight >= this . heightLimit ;
6654
67- if ( this . heightExceeded ) {
55+ if ( this . heightExceeded && ! this . hasBeenTruncated ) {
6856 this . isTruncating = true ;
6957 this . hasBeenTruncated = true ;
7058 }
71- } ,
72- 700
73- ) ;
59+ }
60+ } , 300 ) ; // Reduced timeout
7461 }
7562
7663 openShut ( ) : void {
7764 this . isTruncating = ! this . isTruncating ;
7865 this . hasExpanded . emit ( ! this . isTruncating ) ;
79- return ;
8066 }
8167}
8268
0 commit comments