@wordpress/block-editor
Version:
109 lines (108 loc) • 3.31 kB
JavaScript
// packages/block-editor/src/utils/dom.js
var BLOCK_SELECTOR = ".block-editor-block-list__block";
var APPENDER_SELECTOR = ".block-list-appender";
var BLOCK_APPENDER_CLASS = ".block-editor-button-block-appender";
function isInSameBlock(a, b) {
return a.closest(BLOCK_SELECTOR) === b.closest(BLOCK_SELECTOR);
}
function isInsideRootBlock(blockElement, element) {
const parentBlock = element.closest(
[BLOCK_SELECTOR, APPENDER_SELECTOR, BLOCK_APPENDER_CLASS].join(",")
);
return parentBlock === blockElement;
}
function getBlockClientId(node) {
while (node && node.nodeType !== node.ELEMENT_NODE) {
node = node.parentNode;
}
if (!node) {
return;
}
const elementNode = (
/** @type {Element} */
node
);
const blockNode = elementNode.closest(BLOCK_SELECTOR);
if (!blockNode) {
return;
}
return blockNode.id.slice("block-".length);
}
function rectUnion(rect1, rect2) {
const left = Math.min(rect1.left, rect2.left);
const right = Math.max(rect1.right, rect2.right);
const bottom = Math.max(rect1.bottom, rect2.bottom);
const top = Math.min(rect1.top, rect2.top);
return new window.DOMRectReadOnly(left, top, right - left, bottom - top);
}
function isElementVisible(element) {
const viewport = element.ownerDocument.defaultView;
if (!viewport) {
return false;
}
if (element.classList.contains("components-visually-hidden")) {
return false;
}
const bounds = element.getBoundingClientRect();
if (bounds.width === 0 || bounds.height === 0) {
return false;
}
if (element.checkVisibility) {
return element.checkVisibility?.({
opacityProperty: true,
contentVisibilityAuto: true,
visibilityProperty: true
});
}
const style = viewport.getComputedStyle(element);
if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0") {
return false;
}
return true;
}
function isScrollable(element) {
const style = window.getComputedStyle(element);
return style.overflowX === "auto" || style.overflowX === "scroll" || style.overflowY === "auto" || style.overflowY === "scroll";
}
var WITH_OVERFLOW_ELEMENT_BLOCKS = ["core/navigation"];
function getElementBounds(element) {
const viewport = element.ownerDocument.defaultView;
if (!viewport) {
return new window.DOMRectReadOnly();
}
let bounds = element.getBoundingClientRect();
const dataType = element.getAttribute("data-type");
if (dataType && WITH_OVERFLOW_ELEMENT_BLOCKS.includes(dataType)) {
const stack = [element];
let currentElement;
while (currentElement = stack.pop()) {
if (!isScrollable(currentElement)) {
for (const child of currentElement.children) {
if (isElementVisible(child)) {
const childBounds = child.getBoundingClientRect();
bounds = rectUnion(bounds, childBounds);
stack.push(child);
}
}
}
}
}
const left = Math.max(bounds.left, 0);
const right = Math.min(bounds.right, viewport.innerWidth);
bounds = new window.DOMRectReadOnly(
left,
bounds.top,
right - left,
bounds.height
);
return bounds;
}
export {
WITH_OVERFLOW_ELEMENT_BLOCKS,
getBlockClientId,
getElementBounds,
isInSameBlock,
isInsideRootBlock,
rectUnion
};
//# sourceMappingURL=dom.js.map