Skip to content

Commit 2cfaaca

Browse files
author
Andrea Barbasso
committed
[UXP-126] fix positioning for elements that are not app-component
1 parent 5b023d5 commit 2cfaaca

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class TextSelectDirective implements OnInit, OnDestroy {
110110
let rangeContainer = this.getRangeContainer(range);
111111
if (this.elementRef.nativeElement.contains(rangeContainer)) {
112112
let viewportRectangle = range.getBoundingClientRect();
113-
let localRectangle = this.viewportToHost(viewportRectangle, rangeContainer);
113+
let bodyRelativeRectangle = this.rectangleRelativeToBody(viewportRectangle, rangeContainer);
114114
if (stringSelection) {
115115
this.zone.runGuarded(() => {
116116
this.hasSelection = true;
@@ -127,10 +127,10 @@ export class TextSelectDirective implements OnInit, OnDestroy {
127127

128128
document.body.appendChild(domElem);
129129

130-
this.componentRef.instance.rectangleLeft = localRectangle.left;
131-
this.componentRef.instance.rectangleTop = localRectangle.top;
132-
this.componentRef.instance.rectangleWidth = localRectangle.width;
133-
this.componentRef.instance.rectangleHeight = localRectangle.height;
130+
this.componentRef.instance.elementRectangleLeft = bodyRelativeRectangle.left;
131+
this.componentRef.instance.elementRectangleTop = bodyRelativeRectangle.top;
132+
this.componentRef.instance.elementRectangleWidth = bodyRelativeRectangle.width;
133+
this.componentRef.instance.elementRectangleHeight = bodyRelativeRectangle.height;
134134
this.componentRef.instance.text = stringSelection;
135135

136136
this.selectedText = stringSelection;
@@ -141,12 +141,12 @@ export class TextSelectDirective implements OnInit, OnDestroy {
141141
// });
142142
}
143143

144-
// Convert the given viewport-relative rectangle to a host-relative rectangle.
145-
private viewportToHost(
144+
// Convert the given viewport-relative rectangle to a body-relative rectangle.
145+
private rectangleRelativeToBody(
146146
viewportRectangle: SelectionRectangle,
147147
rangeContainer: Node
148148
): SelectionRectangle {
149-
let host = this.elementRef.nativeElement;
149+
let host = document.body;
150150
let hostRectangle = host.getBoundingClientRect();
151151
let localLeft = (viewportRectangle.left - hostRectangle.left);
152152
let localTop = (viewportRectangle.top - hostRectangle.top);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ import { TranslateService } from '@ngx-translate/core';
2727
export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
2828

2929
@Input()
30-
rectangleLeft = 0;
30+
elementRectangleLeft = 0;
3131

3232
@Input()
33-
rectangleTop = 0;
33+
elementRectangleTop = 0;
3434

3535
@Input()
36-
rectangleWidth = 0;
36+
elementRectangleWidth = 0;
3737

3838
@Input()
39-
rectangleHeight = 0;
39+
elementRectangleHeight = 0;
4040

4141
@Input()
4242
text: string;
@@ -67,7 +67,7 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
6767
}
6868

6969
ngOnInit(): void {
70-
this.left = this.rectangleLeft + this.rectangleWidth / 2;
70+
this.left = this.elementRectangleLeft + this.elementRectangleWidth / 2;
7171
this.checkPosition();
7272
this.ngZone.runOutsideAngular(() => {
7373
// listen to scroll event to update position
@@ -76,11 +76,11 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
7676
}
7777

7878
checkPosition() {
79-
if (this.rectangleTop < this._window.nativeWindow.scrollY) {
80-
this.top = this.rectangleTop + this.rectangleHeight + 6;
79+
if (this.elementRectangleTop < this._window.nativeWindow.scrollY) {
80+
this.top = this.elementRectangleTop + this.elementRectangleHeight + 6;
8181
this.bottomPlacement = true;
8282
} else {
83-
this.top = this.rectangleTop - 6;
83+
this.top = this.elementRectangleTop - 6;
8484
this.bottomPlacement = false;
8585
}
8686
this.changeDetectorRef.detectChanges();

0 commit comments

Comments
 (0)