@taiga-ui/core
Version:
Core library for creating Angular components and applications using Taiga UI
92 lines (86 loc) • 3.58 kB
JavaScript
import { svgNodeFilter, CHAR_NO_BREAK_SPACE, CHAR_ZERO_WIDTH_SPACE } from '@taiga-ui/cdk/constants';
function tuiCheckFixedPosition(element) {
return (!!element && (isFixed(element) || tuiCheckFixedPosition(element.parentElement)));
}
function isFixed(element) {
return (element.ownerDocument.defaultView
?.getComputedStyle(element)
.getPropertyValue('position') === 'fixed');
}
/**
* @description:
* Cross-browser @media (height/width)
*
* 1. window.innerWidth/Width
* 1.1. gets CSS viewport @media (height/width) which include scrollbars
* 1.2. initial-scale and zoom variations may cause mobile values to
* wrongly scale down to what PPK calls the visual
* viewport and be smaller than the @media values
* 1.3. zoom may cause values to be 1px off due to native rounding
*
* 2. document.documentElement.clientHeight/Width
* 2.1. equals CSS viewport width minus scrollbar width
* 2.2. matches @media (height) when there is no scrollbar
* 2.3. available cross-browser
* 2.4. inaccurate if doctype is missing
*/
function tuiGetViewportHeight({ document, innerHeight }) {
return Math.max(document.documentElement.clientHeight || 0, innerHeight || 0);
}
function tuiGetViewportWidth({ document, innerWidth }) {
return Math.max(document.documentElement.clientWidth || 0, innerWidth || 0);
}
/**
* Creates a cloned range with its boundaries set at word boundaries
*
* @param currentRange a range to clone
* @return modified range
*/
function tuiGetWordRange(currentRange) {
const range = currentRange.cloneRange();
const { startContainer, startOffset, endContainer, endOffset } = range;
const { ownerDocument } = startContainer;
if (!ownerDocument) {
return range;
}
const treeWalker = ownerDocument.createTreeWalker(ownerDocument.body, NodeFilter.SHOW_TEXT, svgNodeFilter);
treeWalker.currentNode = startContainer;
do {
const container = treeWalker.currentNode;
const textContent = container.textContent || '';
const content = container === startContainer
? textContent.slice(0, Math.max(0, startOffset + 1))
: textContent;
const offset = Math.max(content.lastIndexOf(' '), content.lastIndexOf('\n'), content.lastIndexOf(CHAR_NO_BREAK_SPACE), content.lastIndexOf(CHAR_ZERO_WIDTH_SPACE)) + 1;
range.setStart(container, 0);
if (offset) {
range.setStart(container, offset);
break;
}
} while (treeWalker.previousNode());
treeWalker.currentNode = endContainer;
do {
const container = treeWalker.currentNode;
const textContent = container.textContent || '';
const content = container === endContainer ? textContent.slice(endOffset + 1) : textContent;
const offset = [
content.indexOf(' '),
content.lastIndexOf('\n'),
content.indexOf(CHAR_NO_BREAK_SPACE),
content.indexOf(CHAR_ZERO_WIDTH_SPACE),
].reduce((result, item) => result === -1 || item === -1
? Math.max(result, item)
: Math.min(result, item), -1);
range.setEnd(container, textContent.length);
if (offset !== -1) {
range.setEnd(container, offset + textContent.length - content.length);
break;
}
} while (treeWalker.nextNode());
return range;
}
/**
* Generated bundle index. Do not edit.
*/
export { tuiCheckFixedPosition, tuiGetViewportHeight, tuiGetViewportWidth, tuiGetWordRange };
//# sourceMappingURL=taiga-ui-core-utils-dom.mjs.map