| | |
| | import type { Position } from "./utils"; |
| |
|
| | export type PositionParams = { |
| | margin: [number, number], |
| | containerPadding: [number, number], |
| | containerWidth: number, |
| | cols: number, |
| | rowHeight: number, |
| | maxRows: number |
| | }; |
| |
|
| | |
| | export function calcGridColWidth(positionParams: PositionParams): number { |
| | const { margin, containerPadding, containerWidth, cols } = positionParams; |
| | return ( |
| | (containerWidth - margin[0] * (cols - 1) - containerPadding[0] * 2) / cols |
| | ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function calcGridItemWHPx( |
| | gridUnits: number, |
| | colOrRowSize: number, |
| | marginPx: number |
| | ): number { |
| | |
| | if (!Number.isFinite(gridUnits)) return gridUnits; |
| | return Math.round( |
| | colOrRowSize * gridUnits + Math.max(0, gridUnits - 1) * marginPx |
| | ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export function calcGridItemPosition( |
| | positionParams: PositionParams, |
| | x: number, |
| | y: number, |
| | w: number, |
| | h: number, |
| | state: ?Object |
| | ): Position { |
| | const { margin, containerPadding, rowHeight } = positionParams; |
| | const colWidth = calcGridColWidth(positionParams); |
| | const out = {}; |
| |
|
| | |
| | if (state && state.resizing) { |
| | out.width = Math.round(state.resizing.width); |
| | out.height = Math.round(state.resizing.height); |
| | } |
| | |
| | else { |
| | out.width = calcGridItemWHPx(w, colWidth, margin[0]); |
| | out.height = calcGridItemWHPx(h, rowHeight, margin[1]); |
| | } |
| |
|
| | |
| | if (state && state.dragging) { |
| | out.top = Math.round(state.dragging.top); |
| | out.left = Math.round(state.dragging.left); |
| | } else if ( |
| | state && |
| | state.resizing && |
| | typeof state.resizing.top === "number" && |
| | typeof state.resizing.left === "number" |
| | ) { |
| | out.top = Math.round(state.resizing.top); |
| | out.left = Math.round(state.resizing.left); |
| | } |
| | |
| | else { |
| | out.top = Math.round((rowHeight + margin[1]) * y + containerPadding[1]); |
| | out.left = Math.round((colWidth + margin[0]) * x + containerPadding[0]); |
| | } |
| |
|
| | return out; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export function calcXY( |
| | positionParams: PositionParams, |
| | top: number, |
| | left: number, |
| | w: number, |
| | h: number |
| | ): { x: number, y: number } { |
| | 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 }; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export function calcWH( |
| | positionParams: PositionParams, |
| | width: number, |
| | height: number, |
| | x: number, |
| | y: number, |
| | handle: string |
| | ): { w: number, h: number } { |
| | const { margin, maxRows, cols, rowHeight } = positionParams; |
| | const colWidth = calcGridColWidth(positionParams); |
| |
|
| | |
| | |
| | |
| | let w = Math.round((width + margin[0]) / (colWidth + margin[0])); |
| | let h = Math.round((height + margin[1]) / (rowHeight + margin[1])); |
| |
|
| | |
| | let _w = clamp(w, 0, cols - x); |
| | let _h = clamp(h, 0, maxRows - y); |
| | if (["sw", "w", "nw"].indexOf(handle) !== -1) { |
| | _w = clamp(w, 0, cols); |
| | } |
| | if (["nw", "n", "ne"].indexOf(handle) !== -1) { |
| | _h = clamp(h, 0, maxRows); |
| | } |
| | return { w: _w, h: _h }; |
| | } |
| |
|
| | |
| | export function clamp( |
| | num: number, |
| | lowerBound: number, |
| | upperBound: number |
| | ): number { |
| | return Math.max(Math.min(num, upperBound), lowerBound); |
| | } |
| |
|