@lonli-lokli/react-mosaic-component
Version:
A React Tiling Window Manager
104 lines (102 loc) • 3.47 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// libs/react-mosaic-component/src/lib/util/BoundingBox.ts
var BoundingBox_exports = {};
__export(BoundingBox_exports, {
boundingBoxAsStyles: () => boundingBoxAsStyles,
emptyBoundingBox: () => emptyBoundingBox,
getAbsoluteSplitPercentage: () => getAbsoluteSplitPercentage,
getRelativeSplitPercentage: () => getRelativeSplitPercentage,
splitBoundingBox: () => splitBoundingBox
});
module.exports = __toCommonJS(BoundingBox_exports);
var import_assertNever = require("./assertNever.cjs");
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:
(0, import_assertNever.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 (0, import_assertNever.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 (0, import_assertNever.assertNever)(direction);
}
}