UNPKG

@wordpress/dom

Version:
96 lines (95 loc) 3.3 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var get_rectangle_from_range_exports = {}; __export(get_rectangle_from_range_exports, { default: () => getRectangleFromRange }); module.exports = __toCommonJS(get_rectangle_from_range_exports); var import_assert_is_defined = require("../utils/assert-is-defined"); function getRectangleFromRange(range) { if (!range.collapsed) { const rects2 = Array.from(range.getClientRects()); if (rects2.length === 1) { return rects2[0]; } const filteredRects = rects2.filter(({ width }) => width > 1); if (filteredRects.length === 0) { return range.getBoundingClientRect(); } if (filteredRects.length === 1) { return filteredRects[0]; } let { top: furthestTop, bottom: furthestBottom, left: furthestLeft, right: furthestRight } = filteredRects[0]; for (const { top, bottom, left, right } of filteredRects) { if (top < furthestTop) { furthestTop = top; } if (bottom > furthestBottom) { furthestBottom = bottom; } if (left < furthestLeft) { furthestLeft = left; } if (right > furthestRight) { furthestRight = right; } } return new window.DOMRect( furthestLeft, furthestTop, furthestRight - furthestLeft, furthestBottom - furthestTop ); } const { startContainer } = range; const { ownerDocument } = startContainer; if (startContainer.nodeName === "BR") { const { parentNode } = startContainer; (0, import_assert_is_defined.assertIsDefined)(parentNode, "parentNode"); const index = ( /** @type {Node[]} */ Array.from(parentNode.childNodes).indexOf(startContainer) ); (0, import_assert_is_defined.assertIsDefined)(ownerDocument, "ownerDocument"); range = ownerDocument.createRange(); range.setStart(parentNode, index); range.setEnd(parentNode, index); } const rects = range.getClientRects(); if (rects.length > 1) { return null; } let rect = rects[0]; if (!rect || rect.height === 0) { (0, import_assert_is_defined.assertIsDefined)(ownerDocument, "ownerDocument"); const padNode = ownerDocument.createTextNode("\u200B"); range = range.cloneRange(); range.insertNode(padNode); rect = range.getClientRects()[0]; (0, import_assert_is_defined.assertIsDefined)(padNode.parentNode, "padNode.parentNode"); padNode.parentNode.removeChild(padNode); } return rect; } //# sourceMappingURL=get-rectangle-from-range.js.map