UNPKG

@dvcol/common-utils

Version:

Typescript library for common utility functions and constants

48 lines (44 loc) 2.32 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// 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 (_optionalChain([style, 'optionalAccess', _ => _.userSelect]) === "none" || _optionalChain([style, 'optionalAccess', _2 => _2["-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); }; exports.getCursorState = getCursorState;