@@ -566,6 +566,7 @@ function Highlighter(options = hhDefaultOptions) {
566566 this . loadHighlights ( [ ] ) ;
567567 this . annotatableContainer . querySelectorAll ( '.hh-svg-background, .hh-selection-handle' ) . forEach ( el => el . remove ( ) )
568568 removeStylesheets ( this . stylesheets ) ;
569+ resizeObserver . disconnect ( ) ;
569570 controller . abort ( ) ;
570571
571572 this . annotatableContainer . highlighter = undefined ;
@@ -723,19 +724,23 @@ function Highlighter(options = hhDefaultOptions) {
723724 } , { signal : controller . signal } ) ;
724725 }
725726
726- // Window resize
727- let previousContainerWidth = this . annotatableContainer . clientWidth ;
728- const respondToWindowResize = ( ) => {
729- // Only respond if the annotatable container width changed
730- if ( this . annotatableContainer . clientWidth === previousContainerWidth ) return ;
731- if ( options . drawingMode === 'svg' ) {
732- this . drawHighlights ( ) ;
733- if ( previousSelectionRange ) updateSelectionUi ( 'bounds' ) ;
727+ // Annotatable container resize (debounced)
728+ let computedStyle = window . getComputedStyle ( this . annotatableContainer ) ;
729+ let previousWidth = this . annotatableContainer . clientWidth - parseInt ( computedStyle . getPropertyValue ( 'padding-left' ) ) - parseInt ( computedStyle . getPropertyValue ( 'padding-right' ) ) ;
730+ const resizeObserver = new ResizeObserver ( debounce ( ( entries ) => {
731+ for ( const entry of entries ) {
732+ const width = entry . contentBoxSize [ 0 ] . inlineSize ;
733+ // Only respond if the annotatable content width changed
734+ if ( width !== previousWidth ) {
735+ if ( options . drawingMode === 'svg' ) {
736+ this . drawHighlights ( ) ;
737+ if ( previousSelectionRange ) updateSelectionUi ( 'bounds' ) ;
738+ }
739+ previousWidth = width ;
740+ }
734741 }
735- previousContainerWidth = this . annotatableContainer . clientWidth ;
736- }
737- const debouncedRespondToWindowResize = debounce ( ( ) => respondToWindowResize ( ) , Math . floor ( Object . keys ( highlightsById ) . length / 20 ) ) ;
738- window . addEventListener ( 'resize' , debouncedRespondToWindowResize , { signal : controller . signal } ) ;
742+ } , Math . floor ( Object . keys ( highlightsById ) . length / 20 ) ) ) ;
743+ resizeObserver . observe ( this . annotatableContainer ) ;
739744
740745
741746 // -------- UTILITY FUNCTIONS --------
0 commit comments