Skip to content

Commit

Permalink
Refactor for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobeltrame committed Sep 22, 2023
1 parent 4bddd93 commit ff69234
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions packages/layout/src/node/shouldBreak.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,42 @@ const getBreak = node => node.props?.break || false;

const getMinPresenceAhead = node => node.props?.minPresenceAhead || 0;

const shouldBreak = (child, futureElements, height) => {
if (child.props?.fixed) return false;
const getFurthestEnd = elements =>
Math.max(...elements.map(node => node.box.top + node.box.height));

const shouldSplit = height < child.box.top + child.box.height;
const canWrap = getWrap(child);
const minPresenceAhead = getMinPresenceAhead(child);
const afterMinPresenceAhead =
const getEndOfMinPresenceAhead = child => {
return (
child.box.top +
child.box.height +
child.box.marginBottom +
minPresenceAhead;
const nonFixedFutureElements = futureElements.filter(
node => !node.props?.fixed,
getMinPresenceAhead(child)
);
const endOfLastFutureElement = Math.max(
...nonFixedFutureElements.map(node => node.box.top + node.box.height),
};

const getEndOfPresence = (child, futureElements) => {
const afterMinPresenceAhead = getEndOfMinPresenceAhead(child);
const endOfFurthestFutureElement = getFurthestEnd(
futureElements.filter(node => !node.props?.fixed),
);
const afterPresence = Math.min(afterMinPresenceAhead, endOfLastFutureElement);
// If the child is already at the top of the page, breaking won't improve presence
return Math.min(afterMinPresenceAhead, endOfFurthestFutureElement);
};

const shouldBreak = (child, futureElements, height) => {
if (child.props?.fixed) return false;

const shouldSplit = height < child.box.top + child.box.height;
const canWrap = getWrap(child);

// Calculate the y coordinate where the desired presence of the child ends
const endOfPresence = getEndOfPresence(child, futureElements);
// If the child is already at the top of the page, breaking won't improve its presence
// (as long as react-pdf does not support breaking into differently sized containers)
const breakingImprovesPresence = child.box.top > child.box.marginTop;

return (
getBreak(child) ||
(shouldSplit && !canWrap) ||
(!shouldSplit && afterPresence > height && breakingImprovesPresence)
(!shouldSplit && endOfPresence > height && breakingImprovesPresence)
);
};

Expand Down

0 comments on commit ff69234

Please sign in to comment.