Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table): add customizable table page break handler #2151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,46 @@ var docDefinition = {
],
],
},
},
{ text: 'onPageBreak action', pageBreak: 'before', style: 'subheader' },
{
table: {
dontBreakRows: true, // important for the current implementation -> onPageBreak only called once
onPageBreak: function (pageInfo, tableInfo, processor, layoutBuilder) {
var tableNode = processor.tableNode;

var additionalRowWrapper = layoutBuilder.docMeasure.measureTable({
table: {
widths: ['*', '*', '*'],
body: [
[{
text: 'Additional row on break',
colSpan: 3,
}]
]
}
});

var additionalRow = additionalRowWrapper.table.body[0];

tableNode.table.body.splice(tableInfo.rowPosition, 0, additionalRow);

layoutBuilder.insertTableRow(tableInfo.rowPosition, additionalRow, processor);
tableInfo.rowPosition++;
},
headerRows: 1,
widths: ['*', '*', '*'],
body: [
['Title', 'Title', 'Title'],
...(function () {
var rows = [];
for (var i = 0; i < 40; i++) {
rows.push(['Row ' + i, 'Row ' + i, 'Row ' + i]);
}
return rows;
})()
],
},
}
],
styles: {
Expand Down
54 changes: 36 additions & 18 deletions src/LayoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,32 +619,50 @@ class LayoutBuilder {

processor.beginTable(this.writer);

let rowHeights = tableNode.table.heights;
for (let i = 0, l = tableNode.table.body.length; i < l; i++) {
processor.beginRow(i, this.writer);

let height;
if (isFunction(rowHeights)) {
height = rowHeights(i);
} else if (isArray(rowHeights)) {
height = rowHeights[i];
} else {
height = rowHeights;
}
const tableInfo = {
rowPosition: 0
};

if (height === 'auto') {
height = undefined;
}
const pageBreakAction = (prevPage) => {
processor.onPageBreak(prevPage, tableInfo, processor, this);
};

let result = this.processRow(tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i, height);
addAll(tableNode.positions, result.positions);
this.writer.addListener("pageChanged", pageBreakAction);

processor.endRow(i, this.writer, result.pageBreaks);
while (tableInfo.rowPosition < tableNode.table.body.length) {
let rowContent = tableNode.table.body[tableInfo.rowPosition];
this.insertTableRow(tableInfo.rowPosition, rowContent, processor);
tableInfo.rowPosition++;
}

this.writer.removeListener("pageChanged", pageBreakAction);
processor.endTable(this.writer);
}

insertTableRow(rowPosition, rowContent, processor) {
processor.beginRow(rowPosition, this.writer);

let rowHeights = processor.tableNode.table.heights;

let height;
if (isFunction(rowHeights)) {
height = rowHeights(rowPosition);
} else if (isArray(rowHeights)) {
height = rowHeights[rowPosition];
} else {
height = rowHeights;
}

if (height === 'auto') {
height = undefined;
}

let result = this.processRow(rowContent, processor.tableNode.table.widths, processor.tableNode._offsets.offsets, processor.tableNode.table.body, rowPosition, height);
addAll(processor.tableNode.positions, result.positions);

processor.endRow(rowPosition, this.writer, result.pageBreaks);
}

// leafs (texts)
processLeaf(node) {
let line = this.buildNextLine(node);
Expand Down
7 changes: 7 additions & 0 deletions src/TableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ class TableProcessor {
this.rowsWithoutPageBreak = this.headerRows + (tableNode.table.keepWithHeaderRows || 0);
this.dontBreakRows = tableNode.table.dontBreakRows || false;

this.onPageBreak = () => {
};
if (tableNode.table && tableNode.table.onPageBreak && isFunction(tableNode.table.onPageBreak)) {
this.onPageBreak = tableNode.table.onPageBreak;
}

if (this.rowsWithoutPageBreak) {
writer.beginUnbreakableBlock();
}
Expand All @@ -123,6 +129,7 @@ class TableProcessor {
};
}


beginRow(rowIndex, writer) {
this.topLineWidth = this.layout.hLineWidth(rowIndex, this.tableNode);
this.rowPaddingTop = this.layout.paddingTop(rowIndex, this.tableNode);
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/LayoutBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,35 @@ describe('LayoutBuilder', function () {
});
});

it('should repeat be able to handle table page breaks externally', function () {
const pageBreaks = [];

var desc = [{
table: {
headerRows: 1,
widths: 'auto',
onPageBreak: function (pageInfo) {
pageBreaks.push(pageInfo);
},
body: [
['h1', 'h2', 'h3']
]
},
layout: emptyTableLayout
}];

for (var i = 0; i < 590; i++) {
desc[0].table.body.push(['a', 'b', 'c']);
}

var pages = builder.layoutDocument(desc, sampleTestProvider);

assert.equal(pages.length, 10);
const expectedPageChanges = 9;
const columns = 3;
assert.equal(pageBreaks.length, expectedPageChanges * columns);
});

it('should not change x positions of repeated table headers, if context.x has changed (bugfix)', function () {
var desc = [{
table: {
Expand Down