@@ -16,13 +16,7 @@ class Pointer {
1616 this . pointerModeLink = true ;
1717 this . pointerSectionId = '' ;
1818
19- this . init ( ) ;
2019 this . setupListeners ( ) ;
21- }
22-
23- init ( ) {
24- // Set up pointer by removing it
25- this . container . parentNode . removeChild ( this . container ) ;
2620
2721 // Set up clipboard
2822 new Clipboard ( this . container . querySelector ( 'button' ) ) ;
@@ -52,8 +46,7 @@ class Pointer {
5246 // Hide pointer when clicking away
5347 DOM . onEvents ( document . body , [ 'click' , 'focus' ] , event => {
5448 if ( ! this . showing || this . isSelection ) return ;
55- this . container . parentElement . removeChild ( this . container ) ;
56- this . showing = false ;
49+ this . hidePointer ( ) ;
5750 } ) ;
5851
5952 // Show pointer when selecting a single block of tagged content
@@ -67,6 +60,11 @@ class Pointer {
6760 } ) ;
6861 }
6962
63+ hidePointer ( ) {
64+ this . container . style . display = null ;
65+ this . showing = false ;
66+ }
67+
7068 /**
7169 * Move and display the pointer at the given element, targeting the given screen x-position if possible.
7270 * @param {Element } element
@@ -80,23 +78,29 @@ class Pointer {
8078 this . pointerSectionId = element . id ;
8179 this . updateForTarget ( element ) ;
8280
83- element . parentNode . insertBefore ( this . container , element ) ;
8481 this . container . style . display = 'block' ;
82+ const targetBounds = element . getBoundingClientRect ( ) ;
83+ const pointerBounds = this . container . getBoundingClientRect ( ) ;
84+
85+ const xTarget = Math . min ( Math . max ( xPosition , targetBounds . left ) , targetBounds . right ) ;
86+ const xOffset = xTarget - ( pointerBounds . width / 2 ) ;
87+ const yOffset = ( targetBounds . top - pointerBounds . height ) - 16 ;
88+
89+ this . container . style . left = `${ xOffset } px` ;
90+ this . container . style . top = `${ yOffset } px` ;
91+
8592 this . showing = true ;
8693 this . isSelection = true ;
8794
88- // Set pointer to sit near mouse-up position
89- requestAnimationFrame ( ( ) => {
90- const bookMarkBounds = element . getBoundingClientRect ( ) ;
91- const pointerLeftOffset = Math . max ( ( xPosition - bookMarkBounds . left - 164 ) , 0 ) ;
92- const pointerLeftOffsetPercent = ( pointerLeftOffset / bookMarkBounds . width ) * 100 ;
95+ setTimeout ( ( ) => {
96+ this . isSelection = false ;
97+ } , 100 ) ;
9398
94- this . container . children [ 0 ] . style . left = pointerLeftOffsetPercent + '%' ;
95-
96- setTimeout ( ( ) => {
97- this . isSelection = false ;
98- } , 100 ) ;
99- } ) ;
99+ const scrollListener = ( ) => {
100+ this . hidePointer ( ) ;
101+ window . removeEventListener ( 'scroll' , scrollListener , { passive : true } ) ;
102+ } ;
103+ window . addEventListener ( 'scroll' , scrollListener , { passive : true } ) ;
100104 }
101105
102106 /**
0 commit comments