@@ -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