Skip to content

Commit d0c9480

Browse files
author
Andrea Barbasso
committed
[UXP-126] cleanup and refactor code
1 parent 306718a commit d0c9480

2 files changed

Lines changed: 11 additions & 57 deletions

File tree

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

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,6 @@ import {
1313
} from '@angular/core';
1414
import { TextSelectionTooltipComponent } from './text-selection-tooltip/text-selection-tooltip.component';
1515

16-
// Define the structure of the event that will be emitted when text is selected.
17-
export interface TextSelectEvent {
18-
text: string;
19-
viewportRectangle: SelectionRectangle | null;
20-
hostRectangle: SelectionRectangle | null;
21-
}
22-
23-
// Define the structure of the selection rectangle.
24-
interface SelectionRectangle {
25-
left: number;
26-
top: number;
27-
width: number;
28-
height: number;
29-
}
30-
31-
32-
// This directive emits an event when the user selects text within the host element.
3316
@Directive({
3417
selector: '[dsTextSelectTooltip]',
3518
})
@@ -60,7 +43,6 @@ export class TextSelectDirective implements OnInit, OnDestroy {
6043

6144
// Set up event listeners when the directive is initialized.
6245
public ngOnInit(): void {
63-
this.elementRef.nativeElement.style.position = 'relative';
6446
this.zone.runOutsideAngular(() => {
6547
this.elementRef.nativeElement.addEventListener('mousedown', this.handleMousedown, false);
6648
});
@@ -87,15 +69,11 @@ export class TextSelectDirective implements OnInit, OnDestroy {
8769
};
8870

8971

90-
// Inspect the document's current selection and check to see if it should be
91-
// emitted as a TextSelectEvent within the current element.
9272
private processSelection(): void {
93-
// setting up a zero-delay timeout to wait for the selection to be cleared
94-
// this solves the issue of the previous selection not being cleared before the mouseup event
95-
// setTimeout(() => {
9673
const selection = document.getSelection();
9774
const stringSelection = selection.toString().trim();
9875
const previousSelection = this.selectedText;
76+
9977
if (this.hasSelection) {
10078
this.zone.runGuarded(() => {
10179
this.hasSelection = false;
@@ -112,26 +90,25 @@ export class TextSelectDirective implements OnInit, OnDestroy {
11290
}
11391
let range = selection.getRangeAt(0);
11492
let rangeContainer = this.getRangeContainer(range);
93+
// check if the range container is inside the current element
94+
// (to avoid showing the tooltip when selecting text in other elements)
11595
if (this.elementRef.nativeElement.contains(rangeContainer)) {
11696
let viewportRectangle = range.getBoundingClientRect();
117-
let bodyRelativeRectangle = this.rectangleRelativeToBody(viewportRectangle, rangeContainer);
11897
if (stringSelection) {
11998
this.zone.runGuarded(() => {
12099
this.hasSelection = true;
121100
if (this.componentRef === null) {
122101
this.componentRef = createComponent(TextSelectionTooltipComponent, {environmentInjector: this.injector});
123102
this.appRef.attachView(this.componentRef.hostView);
124103

125-
const domElem =
126-
(this.componentRef.hostView as EmbeddedViewRef<any>)
127-
.rootNodes[0] as HTMLElement;
104+
const domElem = (this.componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement;
128105

129106
document.body.appendChild(domElem);
130107

131-
this.componentRef.instance.elementRectangleLeft = bodyRelativeRectangle.left;
132-
this.componentRef.instance.elementRectangleTop = bodyRelativeRectangle.top;
133-
this.componentRef.instance.elementRectangleWidth = bodyRelativeRectangle.width;
134-
this.componentRef.instance.elementRectangleHeight = bodyRelativeRectangle.height;
108+
this.componentRef.instance.elementRectangleLeft = viewportRectangle.left + window.scrollX;
109+
this.componentRef.instance.elementRectangleTop = viewportRectangle.top + window.scrollY;
110+
this.componentRef.instance.elementRectangleWidth = viewportRectangle.width;
111+
this.componentRef.instance.elementRectangleHeight = viewportRectangle.height;
135112
this.componentRef.instance.text = stringSelection;
136113

137114
this.componentRef.instance.showTTSControls = this.showTTSControls;
@@ -141,28 +118,5 @@ export class TextSelectDirective implements OnInit, OnDestroy {
141118
});
142119
}
143120
}
144-
// });
145-
}
146-
147-
// Convert the given viewport-relative rectangle to a body-relative rectangle.
148-
private rectangleRelativeToBody(
149-
viewportRectangle: SelectionRectangle,
150-
rangeContainer: Node
151-
): SelectionRectangle {
152-
let host = document.body;
153-
let hostRectangle = host.getBoundingClientRect();
154-
let localLeft = (viewportRectangle.left - hostRectangle.left);
155-
let localTop = (viewportRectangle.top - hostRectangle.top);
156-
let node = rangeContainer;
157-
do {
158-
localLeft += (<Element>node).scrollLeft;
159-
localTop += (<Element>node).scrollTop;
160-
} while ((node !== host) && (node = node.parentNode));
161-
return ({
162-
left: localLeft,
163-
top: localTop,
164-
width: viewportRectangle.width,
165-
height: viewportRectangle.height
166-
});
167121
}
168122
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
5959
utterance: SpeechSynthesisUtterance;
6060
isPaused = false;
6161

62-
bindedCheckPosition = this.checkPosition.bind(this);
62+
boundCheckPosition = this.checkPosition.bind(this);
6363

6464
constructor(
6565
@Inject(NativeWindowService) protected _window: NativeWindowRef,
@@ -74,7 +74,7 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
7474
this.checkPosition();
7575
this.ngZone.runOutsideAngular(() => {
7676
// listen to scroll event to update position
77-
window.addEventListener('scroll', this.bindedCheckPosition);
77+
window.addEventListener('scroll', this.boundCheckPosition);
7878
});
7979
}
8080

@@ -112,7 +112,7 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
112112
if (this.utterance) {
113113
speechSynthesis.cancel();
114114
}
115-
window.removeEventListener('scroll', this.bindedCheckPosition);
115+
window.removeEventListener('scroll', this.boundCheckPosition);
116116
}
117117

118118
pauseTextToSpeech() {

0 commit comments

Comments
 (0)