@@ -489,7 +489,19 @@ function Highlighter(options = hhDefaultOptions) {
489489
490490 activeHighlightId = highlightId ;
491491 updateSelectionUi ( 'appearance' ) ;
492- selection . setBaseAndExtent ( highlightRange . startContainer , highlightRange . startOffset , highlightRange . endContainer , highlightRange . endOffset ) ;
492+
493+ // Update the selection range if needed
494+ // In Android Chrome, sometimes selection handles don't show when a selection is updated programmatically, so it's best to only update the selection if needed.
495+ let selectionRange = selection . type === 'Range' ? selection . getRangeAt ( 0 ) : null ;
496+ if ( ! selectionRange || ! (
497+ selectionRange . startContainer === highlightRange . startContainer &&
498+ selectionRange . startOffset === highlightRange . startOffset &&
499+ selectionRange . endContainer === highlightRange . endContainer &&
500+ selectionRange . endOffset === highlightRange . endOffset )
501+ ) {
502+ selection . setBaseAndExtent ( highlightRange . startContainer , highlightRange . startOffset , highlightRange . endContainer , highlightRange . endOffset ) ;
503+ }
504+
493505 this . annotatableContainer . dispatchEvent ( new CustomEvent ( 'hh:highlightactivate' , { detail : { highlight : highlightToActivate } } ) ) ;
494506 }
495507
@@ -730,7 +742,18 @@ function Highlighter(options = hhDefaultOptions) {
730742 const selection = window . getSelection ( ) ;
731743 if ( selection . type === 'Range' && activeHighlightId && this . annotatableContainer . contains ( selection . anchorNode ) ) {
732744 const adjustedSelectionRange = snapRangeToBoundaries ( selection . getRangeAt ( 0 ) , selection . anchorNode ) ;
733- selection . setBaseAndExtent ( adjustedSelectionRange . startContainer , adjustedSelectionRange . startOffset , adjustedSelectionRange . endContainer , adjustedSelectionRange . endOffset ) ;
745+
746+ // Update the selection range if needed
747+ // In Android Chrome, sometimes selection handles don't show when a selection is updated programmatically, so it's best to only update the selection if needed.
748+ const selectionRange = selection . getRangeAt ( 0 ) ;
749+ if ( ! (
750+ adjustedSelectionRange . startContainer === selectionRange . startContainer &&
751+ adjustedSelectionRange . startOffset === selectionRange . startOffset &&
752+ adjustedSelectionRange . endContainer === selectionRange . endContainer &&
753+ adjustedSelectionRange . endOffset === selectionRange . endOffset )
754+ ) {
755+ selection . setBaseAndExtent ( adjustedSelectionRange . startContainer , adjustedSelectionRange . startOffset , adjustedSelectionRange . endContainer , adjustedSelectionRange . endOffset ) ;
756+ }
734757 }
735758 tapResult = null ;
736759 longPressTimeoutId = clearTimeout ( longPressTimeoutId ) ;
0 commit comments