slate/components/core/Selectable/doObjectsCollide.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

const getBoundsForNode = (node) => {
const rect = node.getBoundingClientRect();
return {
top: rect.top + document.body.scrollTop,
left: rect.left + document.body.scrollLeft,
offsetWidth: node.offsetWidth,
offsetHeight: node.offsetHeight,
};
};
const coordsCollide = (aTop, aLeft, bTop, bLeft, aWidth, aHeight, bWidth, bHeight, tolerance) => {
return !(
// 'a' bottom doesn't touch 'b' top
(
aTop + aHeight - tolerance < bTop ||
// 'a' top doesn't touch 'b' bottom
aTop + tolerance > bTop + bHeight ||
// 'a' right doesn't touch 'b' left
aLeft + aWidth - tolerance < bLeft ||
// 'a' left doesn't touch 'b' right
aLeft + tolerance > bLeft + bWidth
)
);
};
2021-06-09 01:53:30 +03:00
export default function DoObjectsCollide(a, b, tolerance = 0) {
const aObj = a instanceof HTMLElement ? getBoundsForNode(a) : a;
const bObj = b instanceof HTMLElement ? getBoundsForNode(b) : b;
return coordsCollide(
aObj.top,
aObj.left,
bObj.top,
bObj.left,
aObj.offsetWidth,
aObj.offsetHeight,
bObj.offsetWidth,
bObj.offsetHeight,
tolerance
);
2021-06-09 01:53:30 +03:00
}