Skip to content

Commit

Permalink
fixed rendering empty ToC
Browse files Browse the repository at this point in the history
  • Loading branch information
liborm85 committed Jul 27, 2019
1 parent 08e61f3 commit 388e6ea
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
54 changes: 28 additions & 26 deletions src/DocMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,32 +166,34 @@ class DocMeasure {
node.toc.title = this.measureNode(node.toc.title);
}

let body = [];
let textStyle = node.toc.textStyle || {};
let numberStyle = node.toc.numberStyle || textStyle;
let textMargin = node.toc.textMargin || [0, 0, 0, 0];
for (let i = 0, l = node.toc._items.length; i < l; i++) {
let item = node.toc._items[i];
let lineStyle = item._textNodeRef.tocStyle || textStyle;
let lineMargin = item._textNodeRef.tocMargin || textMargin;
let lineNumberStyle = item._textNodeRef.tocNumberStyle || numberStyle;
let destination = getNodeId(item._nodeRef);
body.push([
{ text: item._textNodeRef.text, linkToDestination: destination, alignment: 'left', style: lineStyle, margin: lineMargin },
{ text: '00000', linkToDestination: destination, alignment: 'right', _tocItemRef: item._nodeRef, style: lineNumberStyle, margin: [0, lineMargin[1], 0, lineMargin[3]] }
]);
}

node.toc._table = {
table: {
dontBreakRows: true,
widths: ['*', 'auto'],
body: body
},
layout: 'noBorders'
};

node.toc._table = this.measureNode(node.toc._table);
if (node.toc._items.length > 0) {
let body = [];
let textStyle = node.toc.textStyle || {};
let numberStyle = node.toc.numberStyle || textStyle;
let textMargin = node.toc.textMargin || [0, 0, 0, 0];
for (let i = 0, l = node.toc._items.length; i < l; i++) {
let item = node.toc._items[i];
let lineStyle = item._textNodeRef.tocStyle || textStyle;
let lineMargin = item._textNodeRef.tocMargin || textMargin;
let lineNumberStyle = item._textNodeRef.tocNumberStyle || numberStyle;
let destination = getNodeId(item._nodeRef);
body.push([
{ text: item._textNodeRef.text, linkToDestination: destination, alignment: 'left', style: lineStyle, margin: lineMargin },
{ text: '00000', linkToDestination: destination, alignment: 'right', _tocItemRef: item._nodeRef, style: lineNumberStyle, margin: [0, lineMargin[1], 0, lineMargin[3]] }
]);
}

node.toc._table = {
table: {
dontBreakRows: true,
widths: ['*', 'auto'],
body: body
},
layout: 'noBorders'
};

node.toc._table = this.measureNode(node.toc._table);
}

return node;
}
Expand Down
4 changes: 3 additions & 1 deletion src/LayoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,9 @@ class LayoutBuilder {
if (node.toc.title) {
this.processNode(node.toc.title);
}
this.processNode(node.toc._table);
if (node.toc._table) {
this.processNode(node.toc._table);
}
}

buildNextLine(textNode) {
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/LayoutBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,6 @@ describe('LayoutBuilder', function () {
it.skip('should render lines to pdf in a single call if style is the same');
it.skip('should support document encryption');
it.skip('should support document permissions');
it.skip('should support TOC');
it.skip('should support in-document-references');
it.skip('should support uppercase text transforms');
it.skip('should support lowercase text transforms');
Expand Down Expand Up @@ -2009,4 +2008,20 @@ describe('LayoutBuilder', function () {
assert.deepEqual(pageBreakBeforeFunction.getCall(3).args[0].pageNumbers, [2]);
});
});

describe('table of content', function () {
it('should render empty ToC', function () {
var desc = [
{
toc: {
title: { text: 'INDEX' }
}
}
];

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

assert.equal(pages.length, 1);
});
});
});

0 comments on commit 388e6ea

Please sign in to comment.