Optimize scroll event handling by limiting updates to rect positions only

This commit is contained in:
Aljaž Grilc 2024-05-24 11:07:55 +02:00
parent 76ba7788c0
commit 31934aa2c5

View File

@ -151,14 +151,15 @@ class BesService {
onScroll() {
// Scroll panel is "position: absolute", we need to keep it aligned with the host element.
this.scrollPanel.style.top = `${-this.hostElement.scrollTop}px`
this.scrollPanel.style.left = `${-this.hostElement.scrollLeft}px`
// Why do we need to set left to -scrollLeft? It destroys the position of highlights
// this.scrollPanel.style.left = `${-this.hostElement.scrollLeft}px`
// Markup is in a "position:absolute" <div> element requiring repositioning when scrolling host element or window.
// It is defered to reduce stress in a flood of scroll events.
// TODO: We could technically just update scrollTop and scrollLeft of all markup rects for even better performance?
if (this.scrollTimeout) clearTimeout(this.scrollTimeout)
this.scrollTimeout = setTimeout(() => {
this.repositionAllMarkup()
this.updateRects()
delete this.scrollTimeout
}, 500)
}
@ -206,6 +207,15 @@ class BesService {
return { clientRects, highlights }
}
updateRects() {
this.results.forEach(result => {
result.matches.forEach(match => {
const clientRects = match.range.getClientRects()
match.rects = clientRects
})
})
}
/**
* Tests if given coordinate is inside of a rectangle.
*