From df4d293ffbc961999deefb0859c885810d1ae7ae Mon Sep 17 00:00:00 2001 From: Aljaz Grilc Date: Thu, 20 Jun 2024 09:47:36 +0200 Subject: [PATCH] Implement position observer to monitor if hostElement moves it's position --- service.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/service.js b/service.js index b7f2de2..2b9937c 100644 --- a/service.js +++ b/service.js @@ -66,6 +66,9 @@ class BesService { this.resizeObserver = new ResizeObserver(this.onResize.bind(this)) this.resizeObserver.observe(this.hostElement) + this.initialPosRect = this.hostElement.getBoundingClientRect() + this.initializePositionObserver() + besServices.push(this) } @@ -275,7 +278,6 @@ class BesService { return rect.left <= x && x < rect.right && rect.top <= y && y < rect.bottom } - // TODO: Monitor if hostElement moves in DOM and reposition correction panel and status icon. /** * Creates auxiliary DOM elements for text adornments. */ @@ -464,6 +466,26 @@ class BesService { this.statusIcon.classList.add(status) this.statusDiv.title = title } + + /** + * Initializes position observer. This is necessary to monitor host element movements in DOM. + */ + initializePositionObserver() { + const positionObserver = new MutationObserver(() => { + const currentPosRect = this.hostElement.getBoundingClientRect() + if ( + currentPosRect.top !== this.initialPosRect.top || + currentPosRect.left !== this.initialPosRect.left + ) { + this.onReposition() + this.initialPosRect = currentPosRect + } + }) + positionObserver.observe(document.body, { + childList: true, + subtree: true + }) + } } /**************************************************************************************************