UNPKG

@wordpress/block-editor

Version:
75 lines (73 loc) 3.01 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); // packages/block-editor/src/utils/math.js var math_exports = {}; __export(math_exports, { getDistanceFromPointToEdge: () => getDistanceFromPointToEdge, getDistanceToNearestEdge: () => getDistanceToNearestEdge, isPointContainedByRect: () => isPointContainedByRect, isPointWithinTopAndBottomBoundariesOfRect: () => isPointWithinTopAndBottomBoundariesOfRect }); module.exports = __toCommonJS(math_exports); function getDistanceFromPointToEdge(point, rect, edge) { const isHorizontal = edge === "top" || edge === "bottom"; const { x, y } = point; const pointLateralPosition = isHorizontal ? x : y; const pointForwardPosition = isHorizontal ? y : x; const edgeStart = isHorizontal ? rect.left : rect.top; const edgeEnd = isHorizontal ? rect.right : rect.bottom; const edgeForwardPosition = rect[edge]; let edgeLateralPosition; if (pointLateralPosition >= edgeStart && pointLateralPosition <= edgeEnd) { edgeLateralPosition = pointLateralPosition; } else if (pointLateralPosition < edgeEnd) { edgeLateralPosition = edgeStart; } else { edgeLateralPosition = edgeEnd; } return Math.sqrt( (pointLateralPosition - edgeLateralPosition) ** 2 + (pointForwardPosition - edgeForwardPosition) ** 2 ); } function getDistanceToNearestEdge(point, rect, allowedEdges = ["top", "bottom", "left", "right"]) { let candidateDistance; let candidateEdge; allowedEdges.forEach((edge) => { const distance = getDistanceFromPointToEdge(point, rect, edge); if (candidateDistance === void 0 || distance < candidateDistance) { candidateDistance = distance; candidateEdge = edge; } }); return [candidateDistance, candidateEdge]; } function isPointContainedByRect(point, rect) { return rect.left <= point.x && rect.right >= point.x && rect.top <= point.y && rect.bottom >= point.y; } function isPointWithinTopAndBottomBoundariesOfRect(point, rect) { return rect.top <= point.y && rect.bottom >= point.y; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getDistanceFromPointToEdge, getDistanceToNearestEdge, isPointContainedByRect, isPointWithinTopAndBottomBoundariesOfRect }); //# sourceMappingURL=math.js.map