-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
shouldBreak.js
48 lines (37 loc) · 1.51 KB
/
shouldBreak.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* eslint-disable no-continue */
import getWrap from './getWrap';
const getBreak = (node) => node.props?.break || false;
const getMinPresenceAhead = (node) => node.props?.minPresenceAhead || 0;
const getFurthestEnd = (elements) =>
Math.max(...elements.map((node) => node.box.top + node.box.height));
const getEndOfMinPresenceAhead = (child) => {
return (
child.box.top +
child.box.height +
child.box.marginBottom +
getMinPresenceAhead(child)
);
};
const getEndOfPresence = (child, futureElements) => {
const afterMinPresenceAhead = getEndOfMinPresenceAhead(child);
const endOfFurthestFutureElement = getFurthestEnd(
futureElements.filter((node) => !node.props?.fixed),
);
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 && endOfPresence > height && breakingImprovesPresence)
);
};
export default shouldBreak;