Merge pull request #12259 from ChantsovaEkaterina/bug/fix-truncate-with-tooltip-directive

Fixed truncateWithTooltip directive behavior on different mouse events
This commit is contained in:
Igor Kulikov 2024-12-17 12:44:02 +02:00 committed by GitHub
commit c7d564dd67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,78 +14,55 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { import { booleanAttribute, Directive, ElementRef, input, OnInit, Renderer2 } from '@angular/core';
AfterViewInit,
Directive,
ElementRef,
Input,
OnDestroy,
OnInit,
Renderer2,
} from '@angular/core';
import { fromEvent, Subject } from 'rxjs';
import { filter, takeUntil, tap } from 'rxjs/operators';
import { MatTooltip, TooltipPosition } from '@angular/material/tooltip'; import { MatTooltip, TooltipPosition } from '@angular/material/tooltip';
import { coerceBoolean } from '@shared/decorators/coercion'; import { ContentObserver } from '@angular/cdk/observers';
import { merge } from 'rxjs';
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
@Directive({ @Directive({
selector: '[tbTruncateWithTooltip]', selector: '[tbTruncateWithTooltip]',
providers: [MatTooltip], hostDirectives: [{
directive: MatTooltip,
inputs: ['matTooltipClass', 'matTooltipTouchGestures'],
}]
}) })
export class TruncateWithTooltipDirective implements OnInit, AfterViewInit, OnDestroy { export class TruncateWithTooltipDirective implements OnInit {
@Input('tbTruncateWithTooltip') text = input<string>(undefined, {alias: 'tbTruncateWithTooltip'});
text: string;
@Input() tooltipEnabled = input(true, {transform: booleanAttribute});
@coerceBoolean()
tooltipEnabled = true;
@Input() position = input<TooltipPosition>('above');
position: TooltipPosition = 'above';
private destroy$ = new Subject<void>();
constructor( constructor(
private elementRef: ElementRef, private elementRef: ElementRef<HTMLElement>,
private renderer: Renderer2, private renderer: Renderer2,
private tooltip: MatTooltip private tooltip: MatTooltip,
) {} private contentObserver: ContentObserver
) {
merge(toObservable(this.text), this.contentObserver.observe(this.elementRef)).pipe(
takeUntilDestroyed()
).subscribe(() => {
this.tooltip.message = this.text() || this.elementRef.nativeElement.innerText
})
}
ngOnInit(): void { ngOnInit(): void {
this.observeMouseEvents();
this.applyTruncationStyles(); this.applyTruncationStyles();
this.tooltip.position = this.position();
this.showTooltipOnOverflow(this);
} }
ngAfterViewInit(): void { private showTooltipOnOverflow(ctx: TruncateWithTooltipDirective) {
this.tooltip.position = this.position; ctx.tooltip.show = (function(old) {
} function extendsFunction() {
if (ctx.tooltipEnabled() && ctx.isOverflown()) {
ngOnDestroy(): void { old.apply(ctx.tooltip, arguments);
if (this.tooltip._isTooltipVisible()) { }
this.hideTooltip(); }
} return extendsFunction;
this.destroy$.next(); })(ctx.tooltip.show);
this.destroy$.complete();
}
private observeMouseEvents(): void {
fromEvent(this.elementRef.nativeElement, 'mouseenter')
.pipe(
filter(() => this.tooltipEnabled),
filter(() => this.isOverflown(this.elementRef.nativeElement)),
tap(() => this.showTooltip()),
takeUntil(this.destroy$),
)
.subscribe();
fromEvent(this.elementRef.nativeElement, 'mouseleave')
.pipe(
filter(() => this.tooltipEnabled),
filter(() => this.tooltip._isTooltipVisible()),
tap(() => this.hideTooltip()),
takeUntil(this.destroy$),
)
.subscribe();
} }
private applyTruncationStyles(): void { private applyTruncationStyles(): void {
@ -94,16 +71,7 @@ export class TruncateWithTooltipDirective implements OnInit, AfterViewInit, OnDe
this.renderer.setStyle(this.elementRef.nativeElement, 'text-overflow', 'ellipsis'); this.renderer.setStyle(this.elementRef.nativeElement, 'text-overflow', 'ellipsis');
} }
private isOverflown(element: HTMLElement): boolean { private isOverflown(): boolean {
return element.clientWidth < element.scrollWidth; return this.elementRef.nativeElement.clientWidth < this.elementRef.nativeElement.scrollWidth;
}
private showTooltip(): void {
this.tooltip.message = this.text || this.elementRef.nativeElement.innerText;
this.tooltip.show();
}
private hideTooltip(): void {
this.tooltip.hide();
} }
} }