11import {
22 ChangeDetectionStrategy ,
33 ChangeDetectorRef ,
4- Component ,
4+ Component , ElementRef ,
55 HostBinding ,
6- HostListener ,
6+ HostListener , Inject ,
77 Input ,
8+ NgZone ,
89 OnDestroy ,
910 OnInit
1011} from '@angular/core' ;
1112import { NgIf } from '@angular/common' ;
13+ import { NativeWindowRef , NativeWindowService } from '../../../core/services/window.service' ;
1214
1315@Component ( {
1416 selector : 'ds-text-selection-tooltip' ,
@@ -46,19 +48,40 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
4648 @HostBinding ( 'style.right.px' )
4749 right : number ;
4850
49- @HostBinding ( 'style .bottom.px ' )
50- bottom : number ;
51+ @HostBinding ( 'class .bottom-placement ' )
52+ bottomPlacement = false ;
5153
5254 utterance : SpeechSynthesisUtterance ;
5355 isPaused = false ;
5456
55- constructor ( private changeDetectorRef : ChangeDetectorRef ) {
57+ bindedCheckPosition = this . checkPosition . bind ( this ) ;
58+
59+ constructor (
60+ @Inject ( NativeWindowService ) protected _window : NativeWindowRef ,
61+ private changeDetectorRef : ChangeDetectorRef ,
62+ private ngZone : NgZone ,
63+ private element : ElementRef ) {
5664
5765 }
5866
5967 ngOnInit ( ) : void {
60- this . top = this . rectangleTop - 6 ;
6168 this . left = this . rectangleLeft + this . rectangleWidth / 2 ;
69+ this . checkPosition ( ) ;
70+ this . ngZone . runOutsideAngular ( ( ) => {
71+ // listen to scroll event to update position
72+ window . addEventListener ( 'scroll' , this . bindedCheckPosition ) ;
73+ } ) ;
74+ }
75+
76+ checkPosition ( ) {
77+ if ( this . rectangleTop < this . _window . nativeWindow . scrollY ) {
78+ this . top = this . rectangleTop + this . rectangleHeight + 6 ;
79+ this . bottomPlacement = true ;
80+ } else {
81+ this . top = this . rectangleTop - 6 ;
82+ this . bottomPlacement = false ;
83+ }
84+ this . changeDetectorRef . detectChanges ( ) ;
6285 }
6386
6487 // listen to mousedown on host element to avoid clearing selection
@@ -83,6 +106,7 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
83106 if ( this . utterance ) {
84107 speechSynthesis . cancel ( ) ;
85108 }
109+ window . removeEventListener ( 'scroll' , this . bindedCheckPosition ) ;
86110 }
87111
88112 pauseTextToSpeech ( ) {
0 commit comments