refactor(utils): cleanup

This commit is contained in:
moklick
2021-11-04 16:10:35 +01:00
parent b4b15612a3
commit cc7debf8de
9 changed files with 116 additions and 145 deletions
+25 -1
View File
@@ -1,4 +1,4 @@
import { Dimensions, XYPosition, NodeExtent } from '../types';
import { Dimensions, XYPosition, NodeExtent, Box, Rect } from '../types';
export const getDimensions = (node: HTMLDivElement): Dimensions => ({
width: node.offsetWidth,
@@ -14,3 +14,27 @@ export const clampPosition = (position: XYPosition, extent: NodeExtent) => ({
export const getHostForElement = (element: HTMLElement): Document | ShadowRoot =>
(element.getRootNode?.() as Document | ShadowRoot) || window?.document;
export const getBoundsOfBoxes = (box1: Box, box2: Box): Box => ({
x: Math.min(box1.x, box2.x),
y: Math.min(box1.y, box2.y),
x2: Math.max(box1.x2, box2.x2),
y2: Math.max(box1.y2, box2.y2),
});
export const rectToBox = ({ x, y, width, height }: Rect): Box => ({
x,
y,
x2: x + width,
y2: y + height,
});
export const boxToRect = ({ x, y, x2, y2 }: Box): Rect => ({
x,
y,
width: x2 - x,
height: y2 - y,
});
export const getBoundsofRects = (rect1: Rect, rect2: Rect): Rect =>
boxToRect(getBoundsOfBoxes(rectToBox(rect1), rectToBox(rect2)));