UNPKG

@lonli-lokli/react-mosaic-component

Version:
83 lines (82 loc) 2.35 kB
// libs/react-mosaic-component/src/lib/util/BoundingBox.ts import { assertNever } from "./assertNever.mjs"; function emptyBoundingBox() { return { top: 0, right: 0, bottom: 0, left: 0 }; } function splitBoundingBox(box, percentages, direction) { const results = []; let currentOffset = 0; for (const percentage of percentages) { const newBox = { ...box }; switch (direction) { case "row": { const totalWidth = 100 - box.left - box.right; newBox.left = box.left + totalWidth * currentOffset / 100; newBox.right = box.right + totalWidth * (100 - (currentOffset + percentage)) / 100; break; } case "column": { const totalHeight = 100 - box.top - box.bottom; newBox.top = box.top + totalHeight * currentOffset / 100; newBox.bottom = box.bottom + totalHeight * (100 - (currentOffset + percentage)) / 100; break; } default: assertNever(direction); } results.push(newBox); currentOffset += percentage; } return results; } function boundingBoxAsStyles({ top, right, bottom, left }) { return { top: `${top}%`, right: `${right}%`, bottom: `${bottom}%`, left: `${left}%` }; } function getAbsoluteSplitPercentage(boundingBox, relativeSplitPercentage, direction) { const { top, right, bottom, left } = boundingBox; if (direction === "column") { const height = 100 - top - bottom; return height * relativeSplitPercentage / 100 + top; } else if (direction === "row") { const width = 100 - right - left; return width * relativeSplitPercentage / 100 + left; } else { return assertNever(direction); } } function getRelativeSplitPercentage(boundingBox, absoluteSplitPercentage, direction) { const { top, right, bottom, left } = boundingBox; if (direction === "column") { const height = 100 - top - bottom; if (height === 0) return 0; return (absoluteSplitPercentage - top) / height * 100; } else if (direction === "row") { const width = 100 - right - left; if (width === 0) return 0; return (absoluteSplitPercentage - left) / width * 100; } else { return assertNever(direction); } } export { boundingBoxAsStyles, emptyBoundingBox, getAbsoluteSplitPercentage, getRelativeSplitPercentage, splitBoundingBox };