Skip to content

Commit

Permalink
v0.0.14-hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
albertocubeddu authored Aug 14, 2024
1 parent 556a575 commit 58335b6
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/calculationXY.ts
Original file line number Diff line number Diff line change
@@ -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 };
}

0 comments on commit 58335b6

Please sign in to comment.