From 63d37c872b786abe9233d70b2eff0362582cbc3a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 16 May 2022 21:45:22 -0400 Subject: Popup positioning improvements (#2135) * Rename elementRect to sourceRect * Add getRects function to TextSourceElement and TextSourceRange * Add jsdocs * Remove unnecessary valid parameter * Remove default parameter * Make optionsContext optional * Remove unnecessary checks * Update sourceRect to use left/right rather than x/y * Update the return type of Popup*.getFrameRect * Rename some unrelated rect vars for disambiguation * Disambiguate between Popup.Rect and Popup.ValidRect * Move sourceRect destructuring * Pass multiple source rects * Simplify * Change Rect to use right/bottom rather than width/height * Update how popup offset is applied * Simplify frame offset * Remove _applyFrameOffset * Use right/bottom rather than width/height * Simplify some positioning settings * Update parameter names for clarity * Fix typos * Refactor data type for _getPosition* functions * Support using multiple source rects * Combine _getPosition functions * Refactor * Expose after dataset value * Consistently use this's property * Add jsdoc --- ext/js/app/popup-factory.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'ext/js/app/popup-factory.js') diff --git a/ext/js/app/popup-factory.js b/ext/js/app/popup-factory.js index fb4a7b8b..bef687ff 100644 --- a/ext/js/app/popup-factory.js +++ b/ext/js/app/popup-factory.js @@ -250,7 +250,9 @@ class PopupFactory { async _onApiContainsPoint({id, x, y}) { const popup = this._getPopup(id); - [x, y] = this._convertPopupPointToRootPagePoint(popup, x, y); + const offset = this._getPopupOffset(popup); + x += offset.x; + y += offset.y; return await popup.containsPoint(x, y); } @@ -258,9 +260,13 @@ class PopupFactory { const popup = this._getPopup(id); if (!this._popupCanShow(popup)) { return; } - const {elementRect} = details; - if (typeof elementRect !== 'undefined') { - [elementRect.x, elementRect.y] = this._convertPopupPointToRootPagePoint(popup, elementRect.x, elementRect.y); + const offset = this._getPopupOffset(popup); + const {sourceRects} = details; + for (const sourceRect of sourceRects) { + sourceRect.left += offset.x; + sourceRect.top += offset.y; + sourceRect.right += offset.x; + sourceRect.bottom += offset.y; } return await popup.showContent(details, displayDetails); @@ -311,16 +317,15 @@ class PopupFactory { return popup; } - _convertPopupPointToRootPagePoint(popup, x, y) { + _getPopupOffset(popup) { const {parent} = popup; if (parent !== null) { const popupRect = parent.getFrameRect(); if (popupRect.valid) { - x += popupRect.x; - y += popupRect.y; + return {x: popupRect.left, y: popupRect.top}; } } - return [x, y]; + return {x: 0, y: 0}; } _popupCanShow(popup) { -- cgit v1.2.3