UNPKG

@zag-js/splitter

Version:

Core logic for the splitter widget implemented as a state machine

69 lines (67 loc) 2.11 kB
import "../chunk-QZ7TP4HQ.mjs"; // src/utils/preserve-fixed-panel-sizes.ts import { fuzzyNumbersEqual } from "./fuzzy.mjs"; 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 (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 (fuzzyNumbersEqual(total2, 100)) { return nextLayout; } const scale2 = 100 / Math.max(total2, 1); return nextLayout.map((size) => size * scale2); } if (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 (fuzzyNumbersEqual(total, 100)) { return nextLayout; } const scale = 100 / total; return nextLayout.map((size) => size * scale); } export { preserveFixedPanelSizes };