Skip to content

Commit bcf2da7

Browse files
fix: add missing breakableViewChild method
1 parent 50ae3da commit bcf2da7

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

packages/layout/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ats-form-react-pdf-layout",
3-
"version": "4.4.4",
3+
"version": "4.4.5",
44
"license": "MIT",
55
"description": "Resolve document component's layout",
66
"author": "Diego Muracciole <diegomuracciole@gmail.com>",

packages/layout/src/steps/resolvePagination.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ const warnUnavailableSpace = (node: SafeNode) => {
5656
);
5757
};
5858

59+
const breakableViewChild = (children, height, path = '', currentChildren) => {
60+
for (let i = 0; i < children.length; i += 1) {
61+
if (children[i].type !== 'VIEW') continue;
62+
63+
if (
64+
shouldNodeBreak(
65+
children[i],
66+
children.slice(i + 1, height),
67+
height,
68+
currentChildren,
69+
)
70+
) {
71+
return {
72+
child: children[i],
73+
path: `${path}/${i}`,
74+
};
75+
}
76+
77+
if (children[i].children && children[i].children.length > 0) {
78+
const breakable = breakableViewChild(
79+
children[i].children,
80+
height,
81+
`${path}/${i}`,
82+
currentChildren,
83+
);
84+
if (breakable) return breakable;
85+
}
86+
}
87+
return null;
88+
};
89+
5990
const splitNodes = (height: number, contentArea: number, nodes: SafeNode[]) => {
6091
const currentChildren: SafeNode[] = [];
6192
const nextChildren: SafeNode[] = [];
@@ -74,6 +105,12 @@ const splitNodes = (height: number, contentArea: number, nodes: SafeNode[]) => {
74105
height,
75106
currentChildren,
76107
);
108+
109+
const firstBreakableViewChild =
110+
child.children &&
111+
child.children.length > 0 &&
112+
breakableViewChild(child.children, height, '', currentChildren);
113+
77114
const prevChild = nodes.length > 0 && i > 0 ? nodes[i - 1] : undefined;
78115
const shouldSplit = height + SAFETY_THRESHOLD < nodeTop + nodeHeight;
79116
const canWrap = canNodeWrap(child, prevChild, currentChildren);
@@ -97,6 +134,7 @@ const splitNodes = (height: number, contentArea: number, nodes: SafeNode[]) => {
97134
currentChildren.push(child);
98135
nextChildren.push(...futureNodes);
99136
warnUnavailableSpace(child);
137+
break;
100138
} else {
101139
// We don't want to break non wrapable nodes, so we just let them be.
102140
const box = Object.assign({}, child.box, {
@@ -110,8 +148,8 @@ const splitNodes = (height: number, contentArea: number, nodes: SafeNode[]) => {
110148

111149
currentChildren.push(...futureFixedNodes);
112150
nextChildren.push(next, ...futureNodes);
151+
break;
113152
}
114-
break;
115153
}
116154

117155
if (shouldBreak) {
@@ -127,7 +165,7 @@ const splitNodes = (height: number, contentArea: number, nodes: SafeNode[]) => {
127165
break;
128166
}
129167

130-
if (shouldSplit) {
168+
if (shouldSplit || firstBreakableViewChild) {
131169
const [currentChild, nextChild] = split(child, height, contentArea);
132170

133171
// All children are moved to the next page, it doesn't make sense to show the parent on the current page
@@ -137,10 +175,10 @@ const splitNodes = (height: number, contentArea: number, nodes: SafeNode[]) => {
137175
// currentChildren.push(child, ...futureFixedNodes);
138176
// nextChildren.push(...futureNodes);
139177
// } else {
140-
const box = Object.assign({}, child.box, {
141-
top: child.box.top - height,
178+
const box = Object.assign({}, nextChild.box, {
179+
top: nextChild.box.top - height,
142180
});
143-
const next = Object.assign({}, child, { box });
181+
const next = Object.assign({}, nextChild, { box });
144182

145183
currentChildren.push(...futureFixedNodes);
146184
nextChildren.push(next, ...futureNodes);

packages/renderer/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ats-form-react-pdf-renderer",
3-
"version": "4.3.4",
3+
"version": "4.3.5",
44
"license": "MIT",
55
"description": "Create PDF files on the browser and server",
66
"author": "Diego Muracciole <diegomuracciole@gmail.com>",
@@ -26,7 +26,7 @@
2626
"@babel/runtime": "^7.20.13",
2727
"@react-pdf/fns": "3.1.2",
2828
"@react-pdf/font": "^4.0.3",
29-
"ats-form-react-pdf-layout": "^4.4.4",
29+
"ats-form-react-pdf-layout": "^4.4.5",
3030
"@react-pdf/pdfkit": "^4.0.4",
3131
"@react-pdf/primitives": "^4.1.1",
3232
"@react-pdf/reconciler": "^1.1.4",

0 commit comments

Comments
 (0)