@@ -6,12 +6,14 @@ import {
66 ElementRef ,
77 EmbeddedViewRef ,
88 EnvironmentInjector ,
9+ Inject ,
910 Input ,
1011 NgZone ,
1112 OnDestroy ,
1213 OnInit
1314} from '@angular/core' ;
1415import { TextSelectionTooltipComponent } from './text-selection-tooltip/text-selection-tooltip.component' ;
16+ import { DOCUMENT } from '@angular/common' ;
1517
1618@Directive ( {
1719 selector : '[dsTextSelectTooltip]' ,
@@ -28,6 +30,7 @@ export class TextSelectDirective implements OnInit, OnDestroy {
2830
2931 // Initialize the directive.
3032 constructor (
33+ @Inject ( DOCUMENT ) private _document : Document ,
3134 private elementRef : ElementRef ,
3235 private zone : NgZone ,
3336 private appRef : ApplicationRef ,
@@ -38,7 +41,7 @@ export class TextSelectDirective implements OnInit, OnDestroy {
3841 // Clean up when the directive is destroyed.
3942 ngOnDestroy ( ) : void {
4043 this . elementRef . nativeElement . removeEventListener ( 'mousedown' , this . handleMousedown , false ) ;
41- document . removeEventListener ( 'mouseup' , this . handleMouseup , false ) ;
44+ this . _document . removeEventListener ( 'mouseup' , this . handleMouseup , false ) ;
4245 }
4346
4447 // Set up event listeners when the directive is initialized.
@@ -59,12 +62,12 @@ export class TextSelectDirective implements OnInit, OnDestroy {
5962
6063 // Handle mousedown events inside the current element.
6164 handleMousedown = ( ) : void => {
62- document . addEventListener ( 'mouseup' , this . handleMouseup , false ) ;
65+ this . _document . addEventListener ( 'mouseup' , this . handleMouseup , false ) ;
6366 } ;
6467
6568 // Handle mouseup events anywhere in the document.
6669 private handleMouseup = ( ) : void => {
67- document . removeEventListener ( 'mouseup' , this . handleMouseup , false ) ;
70+ this . _document . removeEventListener ( 'mouseup' , this . handleMouseup , false ) ;
6871 this . processSelection ( ) ;
6972 } ;
7073
@@ -73,7 +76,7 @@ export class TextSelectDirective implements OnInit, OnDestroy {
7376 }
7477
7578 processSelection ( ) : void {
76- const selection = document . getSelection ( ) ;
79+ const selection = this . _document . getSelection ( ) ;
7780 const stringSelection = selection . toString ( ) . trim ( ) ;
7881 const previousSelection = this . selectedText ;
7982
@@ -106,7 +109,7 @@ export class TextSelectDirective implements OnInit, OnDestroy {
106109 this . appRef . attachView ( this . componentRef . hostView ) ;
107110
108111 const domElem = ( this . componentRef . hostView as EmbeddedViewRef < any > ) . rootNodes [ 0 ] as HTMLElement ;
109- document . body . appendChild ( domElem ) ;
112+ this . _document . body . appendChild ( domElem ) ;
110113
111114 this . componentRef . instance . elementRectangleLeft = viewportRectangle . left + window . scrollX ;
112115 this . componentRef . instance . elementRectangleTop = viewportRectangle . top + window . scrollY ;
0 commit comments