From 31934aa2c547d235a581a5f825aea7fa19ec1a90 Mon Sep 17 00:00:00 2001 From: Aljaz Grilc Date: Fri, 24 May 2024 11:07:55 +0200 Subject: [PATCH 1/3] Optimize scroll event handling by limiting updates to rect positions only --- service2.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/service2.js b/service2.js index 0cf2d42..03c80eb 100644 --- a/service2.js +++ b/service2.js @@ -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"
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. * From 94847d44f2627da178d9eb2322dc9fb2d376be1f Mon Sep 17 00:00:00 2001 From: Aljaz Grilc Date: Fri, 24 May 2024 11:11:57 +0200 Subject: [PATCH 2/3] Update TODO comment --- service2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service2.js b/service2.js index 03c80eb..67e97c5 100644 --- a/service2.js +++ b/service2.js @@ -1,5 +1,5 @@ // TODO: Implement