UNPKG

@zag-js/splitter

Version:

Core logic for the splitter widget implemented as a state machine

92 lines (90 loc) 3.24 kB
"use strict"; 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); // src/utils/preserve-fixed-panel-sizes.ts var preserve_fixed_panel_sizes_exports = {}; __export(preserve_fixed_panel_sizes_exports, { preserveFixedPanelSizes: () => preserveFixedPanelSizes }); module.exports = __toCommonJS(preserve_fixed_panel_sizes_exports); var import_fuzzy = require("./fuzzy.js"); function preserveFixedPanelSizes({ panels, prevLayout, prevGroupSize, nextGroupSize }) { if (prevGroupSize <= 0 || nextGroupSize <= 0) { return prevLayout; } const nextLayout = [...prevLayout]; const relativeIndices = []; let fixedTotal = 0; let relativeTotal = 0; panels.forEach((panel, index) => { if (panel.resizeBehavior === "preserve-pixel-size") { const prevPixelSize = prevLayout[index] / 100 * prevGroupSize; const nextPercentSize = prevPixelSize / nextGroupSize * 100; nextLayout[index] = nextPercentSize; fixedTotal += nextPercentSize; } else { relativeIndices.push(index); relativeTotal += prevLayout[index]; } }); if (relativeIndices.length === 0) { const total2 = nextLayout.reduce((accumulated, current) => accumulated + current, 0); if ((0, import_fuzzy.fuzzyNumbersEqual)(total2, 100)) { return nextLayout; } if (total2 <= 0) { return prevLayout; } const scale2 = 100 / total2; return nextLayout.map((size) => size * scale2); } const remainingSize = 100 - fixedTotal; if (remainingSize <= 0) { const total2 = nextLayout.reduce((accumulated, current) => accumulated + current, 0); if ((0, import_fuzzy.fuzzyNumbersEqual)(total2, 100)) { return nextLayout; } const scale2 = 100 / Math.max(total2, 1); return nextLayout.map((size) => size * scale2); } if ((0, import_fuzzy.fuzzyNumbersEqual)(relativeTotal, 0)) { const size = remainingSize / relativeIndices.length; relativeIndices.forEach((index) => { nextLayout[index] = size; }); return nextLayout; } relativeIndices.forEach((index) => { nextLayout[index] = prevLayout[index] / relativeTotal * remainingSize; }); const total = nextLayout.reduce((accumulated, current) => accumulated + current, 0); if ((0, import_fuzzy.fuzzyNumbersEqual)(total, 100)) { return nextLayout; } const scale = 100 / total; return nextLayout.map((size) => size * scale); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { preserveFixedPanelSizes });