Skip to content

Commit f84d2b4

Browse files
author
Andrea Barbasso
committed
[UXP-126] fix error on click of already selected text
1 parent 9595f41 commit f84d2b4

2 files changed

Lines changed: 44 additions & 45 deletions

File tree

src/app/app.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,8 @@ export class AppComponent implements OnInit, AfterViewInit {
173173
console.warn('Viewport rectangle:', event.viewportRectangle);
174174
console.warn('Host rectangle:', event.hostRectangle);
175175
console.groupEnd();
176+
177+
const utterance = new SpeechSynthesisUtterance(event.text);
178+
speechSynthesis.speak(utterance);
176179
}
177180
}

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

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ export class TextSelectDirective implements OnInit, OnDestroy {
3838
public ngOnDestroy(): void {
3939
this.elementRef.nativeElement.removeEventListener('mousedown', this.handleMousedown, false);
4040
document.removeEventListener('mouseup', this.handleMouseup, false);
41-
document.removeEventListener('selectionchange', this.handleSelectionchange, false);
4241
}
4342

4443
// Set up event listeners when the directive is initialized.
4544
public ngOnInit(): void {
4645
this.zone.runOutsideAngular(() => {
4746
this.elementRef.nativeElement.addEventListener('mousedown', this.handleMousedown, false);
48-
document.addEventListener('selectionchange', this.handleSelectionchange, false);
4947
});
5048
}
5149

@@ -69,57 +67,55 @@ export class TextSelectDirective implements OnInit, OnDestroy {
6967
this.processSelection();
7068
};
7169

72-
// Handle selectionchange events anywhere in the document.
73-
private handleSelectionchange = (): void => {
74-
if (this.hasSelection) {
75-
this.processSelection();
76-
}
77-
};
7870

7971
// Inspect the document's current selection and check to see if it should be
8072
// emitted as a TextSelectEvent within the current element.
8173
private processSelection(): void {
82-
let selection = document.getSelection();
83-
if (this.hasSelection) {
84-
this.zone.runGuarded(() => {
85-
this.hasSelection = false;
86-
this.dsTextSelect.next({
87-
text: '',
88-
viewportRectangle: null,
89-
hostRectangle: null
90-
});
91-
});
92-
}
93-
if (!selection.rangeCount || !selection.toString()) {
94-
return;
95-
}
96-
let range = selection.getRangeAt(0);
97-
let rangeContainer = this.getRangeContainer(range);
98-
if (this.elementRef.nativeElement.contains(rangeContainer)) {
99-
let viewportRectangle = range.getBoundingClientRect();
100-
let localRectangle = this.viewportToHost(viewportRectangle, rangeContainer);
101-
const stringSelection = selection.toString();
102-
if (stringSelection) {
74+
// setting up a zero-delay timeout to wait for the selection to be cleared
75+
// this solves the issue of the previous selection not being cleared before the mouseup event
76+
setTimeout(() => {
77+
const selection = document.getSelection();
78+
if (this.hasSelection) {
10379
this.zone.runGuarded(() => {
104-
this.hasSelection = true;
105-
this.dsTextSelect.emit({
106-
text: stringSelection,
107-
viewportRectangle: {
108-
left: viewportRectangle.left,
109-
top: viewportRectangle.top,
110-
width: viewportRectangle.width,
111-
height: viewportRectangle.height
112-
},
113-
hostRectangle: {
114-
left: localRectangle.left,
115-
top: localRectangle.top,
116-
width: localRectangle.width,
117-
height: localRectangle.height
118-
}
80+
this.hasSelection = false;
81+
this.dsTextSelect.next({
82+
text: '',
83+
viewportRectangle: null,
84+
hostRectangle: null
11985
});
12086
});
12187
}
122-
}
88+
if (!selection.rangeCount || !selection.toString()) {
89+
return;
90+
}
91+
let range = selection.getRangeAt(0);
92+
let rangeContainer = this.getRangeContainer(range);
93+
if (this.elementRef.nativeElement.contains(rangeContainer)) {
94+
let viewportRectangle = range.getBoundingClientRect();
95+
let localRectangle = this.viewportToHost(viewportRectangle, rangeContainer);
96+
const stringSelection = selection.toString();
97+
if (stringSelection) {
98+
this.zone.runGuarded(() => {
99+
this.hasSelection = true;
100+
this.dsTextSelect.emit({
101+
text: stringSelection,
102+
viewportRectangle: {
103+
left: viewportRectangle.left,
104+
top: viewportRectangle.top,
105+
width: viewportRectangle.width,
106+
height: viewportRectangle.height
107+
},
108+
hostRectangle: {
109+
left: localRectangle.left,
110+
top: localRectangle.top,
111+
width: localRectangle.width,
112+
height: localRectangle.height
113+
}
114+
});
115+
});
116+
}
117+
}
118+
});
123119
}
124120

125121
// Convert the given viewport-relative rectangle to a host-relative rectangle.

0 commit comments

Comments
 (0)