From beb4d7fa789991b1564a694ff73f205655d1bd77 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 29 Mar 2024 19:54:07 -0300 Subject: client: WikiRect.fromMouse, fromElementUnderMouse --- src/static/client3.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/static/client3.js b/src/static/client3.js index 764bdaa8..6aea86ad 100644 --- a/src/static/client3.js +++ b/src/static/client3.js @@ -193,6 +193,34 @@ class WikiRect extends DOMRect { return this.fromRect(element.getBoundingClientRect()); } + static fromMouse() { + const {clientX, clientY} = liveMousePositionInfo.state; + + return WikiRect.fromRect({ + x: clientX, + y: clientY, + width: 0, + height: 0, + }); + } + + static fromElementUnderMouse(element) { + const mouseRect = WikiRect.fromMouse(); + + const rects = + Array.from(element.getClientRects()) + .map(rect => WikiRect.fromRect(rect)); + + const rectUnderMouse = + rects.find(rect => rect.contains(mouseRect)); + + if (rectUnderMouse) { + return rectUnderMouse; + } else { + return rects[0]; + } + } + static leftOf(origin, offset = 0) { // Returns a rectangle representing everywhere to the left of the provided // point or rectangle (with no top or bottom bounds), towards negative x. -- cgit 1.3.0-6-gf8a5