Skip to content

Commit c981784

Browse files
Solpatiumjeetiss
andauthored
Break on containers whose children can not fit on a page (#2198)
* Break on containers whose children can not fit on a page * changeset --------- Co-authored-by: Dmitry Ivakhnenko <jeetiss@yandex.ru>
1 parent a803986 commit c981784

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

.changeset/moody-bottles-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-pdf/layout': minor
3+
---
4+
5+
Break on containers whose children can not fit on a page

packages/layout/src/steps/resolvePagination.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ const splitNodes = (height, contentArea, nodes) => {
9595
if (shouldSplit) {
9696
const [currentChild, nextChild] = split(child, height, contentArea);
9797

98+
// All children are moved to the next page, it doesn't make sense to show the parent on the current page
99+
if (child.children.length > 0 && currentChild.children.length === 0) {
100+
const box = Object.assign({}, child.box, {
101+
top: child.box.top - height,
102+
});
103+
const next = Object.assign({}, child, { box });
104+
105+
currentChildren.push(...futureFixedNodes);
106+
nextChildren.push(next, ...futureNodes);
107+
break;
108+
}
109+
98110
if (currentChild) currentChildren.push(currentChild);
99111
if (nextChild) nextChildren.push(nextChild);
100112

packages/layout/tests/steps/resolvePagination.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,66 @@ describe('pagination step', () => {
169169

170170
expect(layout.children.length).toBe(1);
171171
});
172+
173+
test('should break on a container whose children can not fit on a page', () => {
174+
const root = {
175+
type: 'DOCUMENT',
176+
children: [
177+
{
178+
type: 'PAGE',
179+
box: {},
180+
style: {
181+
width: 5,
182+
height: 60,
183+
},
184+
185+
children: [
186+
{
187+
type: 'VIEW',
188+
box: {},
189+
style: {
190+
width: 5,
191+
height: 40,
192+
},
193+
props: {},
194+
children: [],
195+
},
196+
{
197+
type: 'VIEW',
198+
box: {},
199+
style: {
200+
width: 5,
201+
},
202+
props: {},
203+
children: [
204+
{
205+
type: 'VIEW',
206+
box: {},
207+
style: {
208+
height: 40,
209+
},
210+
props: {
211+
wrap: false,
212+
},
213+
children: [],
214+
},
215+
],
216+
},
217+
],
218+
},
219+
],
220+
};
221+
222+
const layout = calcLayout(root);
223+
console.log(layout.children[0].children);
224+
225+
const page1 = layout.children[0];
226+
const page2 = layout.children[1];
227+
228+
// Only the first view is displayed on the first page
229+
expect(page1.children.length).toBe(1);
230+
// The second page displays the second wrapper, with its full height
231+
expect(page2.children.length).toBe(1);
232+
expect(page2.children[0].box.height).toBe(40);
233+
});
172234
});

0 commit comments

Comments
 (0)