diff --git a/service.js b/service.js index 4340ee4..fae2d39 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. */ @@ -473,6 +475,26 @@ class BesService { return false } } + + /** + * 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 + }) + } } /**************************************************************************************************