UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 5.1 kB
{ "version": 3, "sources": ["../../src/utils/math.js"], "sourcesContent": ["/**\n * A string representing the name of an edge.\n *\n * @typedef {'top'|'right'|'bottom'|'left'} WPEdgeName\n */\n\n/**\n * @typedef {Object} WPPoint\n * @property {number} x The horizontal position.\n * @property {number} y The vertical position.\n */\n\n/**\n * Given a point, a DOMRect and the name of an edge, returns the distance to\n * that edge of the rect.\n *\n * This function works for edges that are horizontal or vertical (e.g. not\n * rotated), the following terms are used so that the function works in both\n * orientations:\n *\n * - Forward, meaning the axis running horizontally when an edge is vertical\n * and vertically when an edge is horizontal.\n * - Lateral, meaning the axis running vertically when an edge is vertical\n * and horizontally when an edge is horizontal.\n *\n * @param {WPPoint} point The point to measure distance from.\n * @param {DOMRect} rect A DOM Rect containing edge positions.\n * @param {WPEdgeName} edge The edge to measure to.\n */\nexport function getDistanceFromPointToEdge( point, rect, edge ) {\n\tconst isHorizontal = edge === 'top' || edge === 'bottom';\n\tconst { x, y } = point;\n\tconst pointLateralPosition = isHorizontal ? x : y;\n\tconst pointForwardPosition = isHorizontal ? y : x;\n\tconst edgeStart = isHorizontal ? rect.left : rect.top;\n\tconst edgeEnd = isHorizontal ? rect.right : rect.bottom;\n\tconst edgeForwardPosition = rect[ edge ];\n\n\t// Measure the straight line distance to the edge of the rect, when the\n\t// point is adjacent to the edge.\n\t// Else, if the point is positioned diagonally to the edge of the rect,\n\t// measure diagonally to the nearest corner that the edge meets.\n\tlet edgeLateralPosition;\n\tif (\n\t\tpointLateralPosition >= edgeStart &&\n\t\tpointLateralPosition <= edgeEnd\n\t) {\n\t\tedgeLateralPosition = pointLateralPosition;\n\t} else if ( pointLateralPosition < edgeEnd ) {\n\t\tedgeLateralPosition = edgeStart;\n\t} else {\n\t\tedgeLateralPosition = edgeEnd;\n\t}\n\n\treturn Math.sqrt(\n\t\t( pointLateralPosition - edgeLateralPosition ) ** 2 +\n\t\t\t( pointForwardPosition - edgeForwardPosition ) ** 2\n\t);\n}\n\n/**\n * Given a point, a DOMRect and a list of allowed edges returns the name of and\n * distance to the nearest edge.\n *\n * @param {WPPoint} point The point to measure distance from.\n * @param {DOMRect} rect A DOM Rect containing edge positions.\n * @param {WPEdgeName[]} allowedEdges A list of the edges included in the\n * calculation. Defaults to all edges.\n *\n * @return {[number, string]} An array where the first value is the distance\n * and a second is the edge name.\n */\nexport function getDistanceToNearestEdge(\n\tpoint,\n\trect,\n\tallowedEdges = [ 'top', 'bottom', 'left', 'right' ]\n) {\n\tlet candidateDistance;\n\tlet candidateEdge;\n\n\tallowedEdges.forEach( ( edge ) => {\n\t\tconst distance = getDistanceFromPointToEdge( point, rect, edge );\n\n\t\tif ( candidateDistance === undefined || distance < candidateDistance ) {\n\t\t\tcandidateDistance = distance;\n\t\t\tcandidateEdge = edge;\n\t\t}\n\t} );\n\n\treturn [ candidateDistance, candidateEdge ];\n}\n\n/**\n * Is the point contained by the rectangle.\n *\n * @param {WPPoint} point The point.\n * @param {DOMRect} rect The rectangle.\n *\n * @return {boolean} True if the point is contained by the rectangle, false otherwise.\n */\nexport function isPointContainedByRect( point, rect ) {\n\treturn (\n\t\trect.left <= point.x &&\n\t\trect.right >= point.x &&\n\t\trect.top <= point.y &&\n\t\trect.bottom >= point.y\n\t);\n}\n\n/**\n * Is the point within the top and bottom boundaries of the rectangle.\n *\n * @param {WPPoint} point The point.\n * @param {DOMRect} rect The rectangle.\n *\n * @return {boolean} True if the point is within top and bottom of rectangle, false otherwise.\n */\nexport function isPointWithinTopAndBottomBoundariesOfRect( point, rect ) {\n\treturn rect.top <= point.y && rect.bottom >= point.y;\n}\n"], "mappings": ";AA6BO,SAAS,2BAA4B,OAAO,MAAM,MAAO;AAC/D,QAAM,eAAe,SAAS,SAAS,SAAS;AAChD,QAAM,EAAE,GAAG,EAAE,IAAI;AACjB,QAAM,uBAAuB,eAAe,IAAI;AAChD,QAAM,uBAAuB,eAAe,IAAI;AAChD,QAAM,YAAY,eAAe,KAAK,OAAO,KAAK;AAClD,QAAM,UAAU,eAAe,KAAK,QAAQ,KAAK;AACjD,QAAM,sBAAsB,KAAM,IAAK;AAMvC,MAAI;AACJ,MACC,wBAAwB,aACxB,wBAAwB,SACvB;AACD,0BAAsB;AAAA,EACvB,WAAY,uBAAuB,SAAU;AAC5C,0BAAsB;AAAA,EACvB,OAAO;AACN,0BAAsB;AAAA,EACvB;AAEA,SAAO,KAAK;AAAA,KACT,uBAAuB,wBAAyB,KAC/C,uBAAuB,wBAAyB;AAAA,EACpD;AACD;AAcO,SAAS,yBACf,OACA,MACA,eAAe,CAAE,OAAO,UAAU,QAAQ,OAAQ,GACjD;AACD,MAAI;AACJ,MAAI;AAEJ,eAAa,QAAS,CAAE,SAAU;AACjC,UAAM,WAAW,2BAA4B,OAAO,MAAM,IAAK;AAE/D,QAAK,sBAAsB,UAAa,WAAW,mBAAoB;AACtE,0BAAoB;AACpB,sBAAgB;AAAA,IACjB;AAAA,EACD,CAAE;AAEF,SAAO,CAAE,mBAAmB,aAAc;AAC3C;AAUO,SAAS,uBAAwB,OAAO,MAAO;AACrD,SACC,KAAK,QAAQ,MAAM,KACnB,KAAK,SAAS,MAAM,KACpB,KAAK,OAAO,MAAM,KAClB,KAAK,UAAU,MAAM;AAEvB;AAUO,SAAS,0CAA2C,OAAO,MAAO;AACxE,SAAO,KAAK,OAAO,MAAM,KAAK,KAAK,UAAU,MAAM;AACpD;", "names": [] }