UNPKG

react-grid-layout

Version:

A draggable and resizable grid layout with responsive breakpoints, for React.

98 lines (96 loc) 3.36 kB
'use strict'; // src/core/calculate.ts function calcGridColWidth(positionParams) { const { margin, containerPadding, containerWidth, cols } = positionParams; return (containerWidth - margin[0] * (cols - 1) - containerPadding[0] * 2) / cols; } function calcGridItemWHPx(gridUnits, colOrRowSize, marginPx) { if (!Number.isFinite(gridUnits)) return gridUnits; return Math.round( colOrRowSize * gridUnits + Math.max(0, gridUnits - 1) * marginPx ); } function calcGridItemPosition(positionParams, x, y, w, h, dragPosition, resizePosition) { const { margin, containerPadding, rowHeight } = positionParams; const colWidth = calcGridColWidth(positionParams); let width; let height; let top; let left; if (resizePosition) { width = Math.round(resizePosition.width); height = Math.round(resizePosition.height); } else { width = calcGridItemWHPx(w, colWidth, margin[0]); height = calcGridItemWHPx(h, rowHeight, margin[1]); } if (dragPosition) { top = Math.round(dragPosition.top); left = Math.round(dragPosition.left); } else if (resizePosition) { top = Math.round(resizePosition.top); left = Math.round(resizePosition.left); } else { top = Math.round((rowHeight + margin[1]) * y + containerPadding[1]); left = Math.round((colWidth + margin[0]) * x + containerPadding[0]); } return { top, left, width, height }; } function calcXY(positionParams, top, left, w, h) { const { margin, containerPadding, cols, rowHeight, maxRows } = positionParams; const colWidth = calcGridColWidth(positionParams); let x = Math.round((left - containerPadding[0]) / (colWidth + margin[0])); let y = Math.round((top - containerPadding[1]) / (rowHeight + margin[1])); x = clamp(x, 0, cols - w); y = clamp(y, 0, maxRows - h); return { x, y }; } function calcWH(positionParams, width, height, x, y, handle) { const { margin, maxRows, cols, rowHeight } = positionParams; const colWidth = calcGridColWidth(positionParams); const w = Math.round((width + margin[0]) / (colWidth + margin[0])); const h = Math.round((height + margin[1]) / (rowHeight + margin[1])); let _w = clamp(w, 0, cols - x); let _h = clamp(h, 0, maxRows - y); if (handle === "sw" || handle === "w" || handle === "nw") { _w = clamp(w, 0, cols); } if (handle === "nw" || handle === "n" || handle === "ne") { _h = clamp(h, 0, maxRows); } return { w: _w, h: _h }; } function clamp(num, lowerBound, upperBound) { return Math.max(Math.min(num, upperBound), lowerBound); } function calcGridCellDimensions(config) { const { width, cols, rowHeight, margin = [10, 10], containerPadding } = config; const padding = containerPadding ?? margin; const cellWidth = (width - padding[0] * 2 - margin[0] * (cols - 1)) / cols; const cellHeight = rowHeight; return { cellWidth, cellHeight, offsetX: padding[0], offsetY: padding[1], gapX: margin[0], gapY: margin[1], cols, containerWidth: width }; } exports.calcGridCellDimensions = calcGridCellDimensions; exports.calcGridColWidth = calcGridColWidth; exports.calcGridItemPosition = calcGridItemPosition; exports.calcGridItemWHPx = calcGridItemWHPx; exports.calcWH = calcWH; exports.calcXY = calcXY; exports.clamp = clamp; //# sourceMappingURL=chunk-F6NQPYKT.js.map //# sourceMappingURL=chunk-F6NQPYKT.js.map