UNPKG

react-mosaic-component2

Version:
82 lines (81 loc) 2.38 kB
// src/util/BoundingBox.ts import { assertNever } from "./assertNever.mjs"; var BoundingBox; ((BoundingBox2) => { function empty() { return { top: 0, right: 0, bottom: 0, left: 0 }; } BoundingBox2.empty = empty; function split(boundingBox, relativeSplitPercentage, direction) { const absolutePercentage = getAbsoluteSplitPercentage(boundingBox, relativeSplitPercentage, direction); if (direction === "column") { return { first: { ...boundingBox, bottom: 100 - absolutePercentage }, second: { ...boundingBox, top: absolutePercentage } }; } else if (direction === "row") { return { first: { ...boundingBox, right: 100 - absolutePercentage }, second: { ...boundingBox, left: absolutePercentage } }; } else { return assertNever(direction); } } BoundingBox2.split = split; 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); } } BoundingBox2.getAbsoluteSplitPercentage = getAbsoluteSplitPercentage; function getRelativeSplitPercentage(boundingBox, absoluteSplitPercentage, direction) { const { top, right, bottom, left } = boundingBox; if (direction === "column") { const height = 100 - top - bottom; return (absoluteSplitPercentage - top) / height * 100; } else if (direction === "row") { const width = 100 - right - left; return (absoluteSplitPercentage - left) / width * 100; } else { return assertNever(direction); } } BoundingBox2.getRelativeSplitPercentage = getRelativeSplitPercentage; function asStyles({ top, right, bottom, left }) { return { top: `${top}%`, right: `${right}%`, bottom: `${bottom}%`, left: `${left}%` }; } BoundingBox2.asStyles = asStyles; })(BoundingBox || (BoundingBox = {})); export { BoundingBox };