« get me outta code hell

client: WikiRect.fromMouse, fromElementUnderMouse - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-03-29 19:54:07 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-03-29 19:54:07 -0300
commitbeb4d7fa789991b1564a694ff73f205655d1bd77 (patch)
tree19d81f52e52c5422959c48b9974679386837ff09 /src/static
parentf4f1c80ed54738ef06007df30b2c45d922870b90 (diff)
client: WikiRect.fromMouse, fromElementUnderMouse
Diffstat (limited to 'src/static')
-rw-r--r--src/static/client3.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/static/client3.js b/src/static/client3.js
index 764bdaa..6aea86a 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.