Skip to content

Commit 6f0e8d2

Browse files
authored
fix: use styles for forcing nodes height after split (#1659)
* fix: use styles for forcing nodes height after split * remove console.log * add names for test * add changeset
1 parent d341ae6 commit 6f0e8d2

3 files changed

Lines changed: 134 additions & 40 deletions

File tree

.changeset/angry-bikes-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-pdf/layout': patch
3+
---
4+
5+
use styles for forcing nodes height after split

packages/layout/src/node/splitNode.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ const splitNode = (node, height) => {
2424
borderBottomRightRadius: zero,
2525
}),
2626
box: {
27-
height: R.always(height - nodeTop),
2827
borderBottomWidth: zero,
2928
},
3029
})(node);
3130

31+
// TODO: force height without style mutation
32+
current.style.height = height - nodeTop;
33+
3234
const nextHeight = R.ifElse(
3335
hasFixedHeight,
3436
subtractHeight(height - nodeTop),
@@ -46,11 +48,15 @@ const splitNode = (node, height) => {
4648
}),
4749
box: {
4850
top: zero,
49-
height: R.always(nextHeight),
5051
borderTopWidth: zero,
5152
},
5253
})(node);
5354

55+
// TODO: force height without style mutation
56+
if (nextHeight) {
57+
next.style.height = nextHeight;
58+
}
59+
5460
return [current, next];
5561
};
5662

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

Lines changed: 121 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,47 @@ import resolveDimensions from '../../src/steps/resolveDimensions';
55
const calcLayout = node => resolvePagination(resolveDimensions(node));
66

77
describe('pagination step', () => {
8-
const root = {
9-
type: 'DOCUMENT',
10-
children: [
11-
{
12-
type: 'PAGE',
13-
box: {},
14-
style: {
15-
width: 100,
16-
height: 100,
17-
},
18-
children: [
19-
{
20-
type: 'VIEW',
21-
box: {},
22-
style: {
23-
position: 'absolute',
24-
width: '50%',
25-
top: 0,
26-
bottom: 0,
27-
},
28-
props: {},
29-
children: [],
8+
test('should stretch absolute block to full page size', () => {
9+
const root = {
10+
type: 'DOCUMENT',
11+
children: [
12+
{
13+
type: 'PAGE',
14+
box: {},
15+
style: {
16+
width: 100,
17+
height: 100,
3018
},
31-
{
32-
type: 'TEXT',
33-
box: {},
34-
style: {},
35-
props: {},
36-
children: [
37-
{
38-
type: 'TEXT_INSTANCE',
39-
value: 'hello world',
19+
children: [
20+
{
21+
type: 'VIEW',
22+
box: {},
23+
style: {
24+
position: 'absolute',
25+
width: '50%',
26+
top: 0,
27+
bottom: 0,
4028
},
41-
],
42-
},
43-
],
44-
},
45-
],
46-
};
29+
props: {},
30+
children: [],
31+
},
32+
{
33+
type: 'TEXT',
34+
box: {},
35+
style: {},
36+
props: {},
37+
children: [
38+
{
39+
type: 'TEXT_INSTANCE',
40+
value: 'hello world',
41+
},
42+
],
43+
},
44+
],
45+
},
46+
],
47+
};
4748

48-
test('should stretch absolute block to full page size', () => {
4949
const layout = calcLayout(root);
5050

5151
const page = layout.children[0];
@@ -54,4 +54,87 @@ describe('pagination step', () => {
5454
expect(page.box.height).toBe(100);
5555
expect(view.box.height).toBe(100);
5656
});
57+
58+
test('should force new height for split nodes', () => {
59+
const root = {
60+
type: 'DOCUMENT',
61+
children: [
62+
{
63+
type: 'PAGE',
64+
box: {},
65+
style: {
66+
width: 15,
67+
height: 60,
68+
},
69+
70+
children: [
71+
{
72+
type: 'VIEW',
73+
box: {},
74+
style: {},
75+
props: {},
76+
children: [
77+
{
78+
type: 'TEXT',
79+
box: {},
80+
style: {},
81+
props: {},
82+
children: [
83+
{
84+
type: 'TEXT_INSTANCE',
85+
value: 'a a a a',
86+
},
87+
],
88+
},
89+
],
90+
},
91+
],
92+
},
93+
],
94+
};
95+
96+
const layout = calcLayout(root);
97+
98+
const view1 = layout.children[0].children[0];
99+
const view2 = layout.children[1].children[0];
100+
101+
expect(view1.box.height).toBe(60);
102+
expect(view2.box.height).not.toBe(60);
103+
});
104+
105+
test('should force new height for split nodes with fixed height', () => {
106+
const root = {
107+
type: 'DOCUMENT',
108+
children: [
109+
{
110+
type: 'PAGE',
111+
box: {},
112+
style: {
113+
width: 5,
114+
height: 60,
115+
},
116+
117+
children: [
118+
{
119+
type: 'VIEW',
120+
box: {},
121+
style: { height: 130 },
122+
props: {},
123+
children: [],
124+
},
125+
],
126+
},
127+
],
128+
};
129+
130+
const layout = calcLayout(root);
131+
132+
const view1 = layout.children[0].children[0];
133+
const view2 = layout.children[1].children[0];
134+
const view3 = layout.children[2].children[0];
135+
136+
expect(view1.box.height).toBe(60);
137+
expect(view2.box.height).toBe(60);
138+
expect(view3.box.height).toBe(10);
139+
});
57140
});

0 commit comments

Comments
 (0)