-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
556a575
commit 58335b6
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |