@dvcol/common-utils
Version:
Typescript library for common utility functions and constants
48 lines (46 loc) • 1.63 kB
JavaScript
// lib/common/utils/cursor.utils.ts
var checkTextNode = (node, { clientX, clientY }) => {
if (node.nodeType !== Node.TEXT_NODE) return false;
const range = document.createRange();
range.selectNodeContents(node);
const rects = range.getClientRects();
for (const rect of rects) {
if (clientX >= rect.left && clientX <= rect.right && clientY >= rect.top && clientY <= rect.bottom) {
return true;
}
}
return false;
};
var checkTextInElement = (elem, position) => {
for (const child of elem.childNodes) {
if (checkTextNode(child, position)) return true;
if (child.nodeType === Node.ELEMENT_NODE && checkTextInElement(child, position)) return true;
}
return false;
};
var isTextInputType = (element) => {
if (element.tagName === "INPUT") return true;
if (element.tagName === "TEXTAREA") return true;
return element.isContentEditable;
};
var isCursorOverText = ({ clientX, clientY }) => {
const element = document.elementFromPoint(clientX, clientY);
if (!element) return false;
const style = getComputedStyle(element);
if (style?.userSelect === "none" || style?.["-webkit-user-select"] === "none") return false;
if (isTextInputType(element)) return true;
return checkTextInElement(element, { clientX, clientY });
};
var getCursorStyle = (target, event) => {
const style = getComputedStyle(target).cursor;
if (style === "auto" && isCursorOverText(event)) return "text";
return style;
};
var getCursorState = (e) => {
const { target } = e;
if (!target || !(target instanceof HTMLElement)) return;
return getCursorStyle(target, e);
};
export {
getCursorState
};