From 58335b66795d1278adc8fe7d5d91923a8f16b71d Mon Sep 17 00:00:00 2001 From: Alberto Cubeddu Date: Thu, 15 Aug 2024 07:48:15 +1000 Subject: [PATCH] v0.0.14-hotfix --- lib/calculationXY.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/calculationXY.ts diff --git a/lib/calculationXY.ts b/lib/calculationXY.ts new file mode 100644 index 0000000..214da30 --- /dev/null +++ b/lib/calculationXY.ts @@ -0,0 +1,58 @@ +interface XYCoords { + pageX: number; + pageY: number; + clientX: number; + clientY: number; +} + +export function getRealXY(e: MouseEvent): XYCoords { + let pageX = e.pageX; + let pageY = e.pageY; + let clientX = e.clientX; //horizontal coordinate within the application's viewport + let clientY = e.clientY; //vertical coordinate within the application's viewport + + // Adjust for horizontal overflow + if (clientX < 0) { + //Hoverflow Left + pageX = e.pageX - e.clientX; + clientX = 0; + } else if (clientX > window.innerWidth) { + //Hoverflow Right + pageX = e.pageX - (e.clientX - window.innerWidth); + clientX = window.innerWidth; + } + + // Adjust for vertical overflow + if (clientY < 0) { + //Hoverflow up + pageY = e.pageY - e.clientY; + clientY = 0; + } else if (clientY > window.innerHeight) { + //Hoverflow down + pageY = e.pageY - (e.clientY - window.innerHeight); + clientY = window.innerHeight; + } + + return { pageX, pageY, clientX, clientY }; +} + +export function adjustXYSelectionMenu(coords: XYCoords) { + const viewportWidth = window.innerWidth; + const menuHeight = 250; // approximate height of your menu (adjust as needed) + const menuWidth = 250; // approximate width of your menu (adjust as needed) + const visualGap = 10; // E.g. i select whole paragraph with triple click + + // // Check for right edge case + if (coords.clientX + menuWidth > viewportWidth) { + coords.pageX = Math.max(coords.pageX - menuWidth, 0); + coords.pageY += visualGap; + } else { + coords.pageX += visualGap; + } + + // Check for bottom edge case + if (coords.clientY + menuHeight > window.innerHeight) { + coords.pageY = Math.max(coords.pageY - menuHeight, 0); + } + return { xPos: coords.pageX, yPos: coords.pageY }; +}