Skip to content

Commit 6c27fd5

Browse files
committed
[UXP-126] Fix reference to window and document object
1 parent 187f4fc commit 6c27fd5

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/app/directives/text-select/text-select.directive.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TextSelectDirective } from './text-select.directive';
22
import { ApplicationRef, ElementRef, NgZone } from '@angular/core';
33
import { TestBed } from '@angular/core/testing';
4+
import { DOCUMENT } from '@angular/common';
45

56
describe('TextSelectDirective', () => {
67
let directive: TextSelectDirective;
@@ -12,6 +13,7 @@ describe('TextSelectDirective', () => {
1213
TestBed.configureTestingModule({
1314
providers: [
1415
TextSelectDirective,
16+
{ provide: Document, useExisting: DOCUMENT },
1517
{ provide: ApplicationRef, useValue: { attachView: () => ({ rootNodes: [{}] }) } },
1618
{ provide: ElementRef, useValue: { nativeElement: document.createElement('div') } },
1719
{ provide: NgZone, useValue: new NgZone({}) },

src/app/directives/text-select/text-select.directive.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
1415
import { 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;

src/app/directives/text-select/text-selection-tooltip/text-selection-tooltip.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
7676
this.checkPosition();
7777
this.ngZone.runOutsideAngular(() => {
7878
// listen to scroll event to update position
79-
window.addEventListener('scroll', this.boundCheckPosition);
79+
this._window.nativeWindow.addEventListener('scroll', this.boundCheckPosition);
8080
});
8181
}
8282

@@ -114,7 +114,7 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
114114
if (this.utterance) {
115115
speechSynthesis.cancel();
116116
}
117-
window.removeEventListener('scroll', this.boundCheckPosition);
117+
this._window.nativeWindow.removeEventListener('scroll', this.boundCheckPosition);
118118
}
119119

120120
pauseTextToSpeech() {

0 commit comments

Comments
 (0)