Merge pull request #5138 from tsiegleauq/more-nested-pdf-lists

Add nested PDF lists with line numbering
This commit is contained in:
Emanuel Schütze 2019-11-26 15:24:48 +01:00 committed by GitHub
commit 7d52f12770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -129,7 +129,7 @@ export class HtmlToPdfService {
return this.P_MARGIN_BOTTOM; return this.P_MARGIN_BOTTOM;
} }
case 'li': { case 'li': {
return this.LI_MARGIN_BOTTOM; return this.P_MARGIN_BOTTOM;
} }
default: { default: {
return this.P_MARGIN_BOTTOM; return this.P_MARGIN_BOTTOM;
@ -234,12 +234,7 @@ export class HtmlToPdfService {
case 'div': { case 'div': {
const children = this.parseChildren(element, styles); const children = this.parseChildren(element, styles);
// this introduces a bug with rendering sub-lists in PDF if (this.lineNumberingMode === LineNumberingMode.Outside && !classes.includes('insert')) {
if (
this.lineNumberingMode === LineNumberingMode.Outside &&
!this.isInsideAList(element) &&
!classes.includes('insert')
) {
newParagraph = this.create('stack'); newParagraph = this.create('stack');
newParagraph.stack = children; newParagraph.stack = children;
} else { } else {
@ -268,7 +263,7 @@ export class HtmlToPdfService {
} }
// if the list ends (usually due to a new insert cr) prevent margins // if the list ends (usually due to a new insert cr) prevent margins
if (classes.includes('os-split-after')) { if (classes.includes('os-split-after') || this.withSublist(element)) {
newParagraph.margin[3] = 0; newParagraph.margin[3] = 0;
} }
@ -324,7 +319,10 @@ export class HtmlToPdfService {
break; break;
} }
case 'br': { case 'br': {
if (this.lineNumberingMode === LineNumberingMode.None && classes.includes('os-line-break')) { if (
(this.lineNumberingMode === LineNumberingMode.None && classes.includes('os-line-break')) ||
(this.lineNumberingMode === LineNumberingMode.Outside && this.isInsideAList(element))
) {
break; break;
} else { } else {
newParagraph = this.create('text'); newParagraph = this.create('text');
@ -347,7 +345,7 @@ export class HtmlToPdfService {
} }
// in case of line numbers and only of the list is not nested in another list. // in case of line numbers and only of the list is not nested in another list.
if (this.lineNumberingMode === LineNumberingMode.Outside && !this.isInsideAList(element)) { if (this.lineNumberingMode === LineNumberingMode.Outside) {
const lines = this.extractLineNumbers(element); const lines = this.extractLineNumbers(element);
const cleanedChildDom = this.cleanLineNumbers(element); const cleanedChildDom = this.cleanLineNumbers(element);
@ -474,6 +472,17 @@ export class HtmlToPdfService {
}; };
} }
/**
* Checks if a given LI has a sublist
*/
private withSublist(element: Element): boolean {
if (element.nodeName.toLowerCase() === 'li') {
const hasUl = Array.from(element.children).some(child => child.nodeName.toLowerCase() === 'ul');
return hasUl;
}
return false;
}
/** /**
* Cleans the elements children from line-number spans * Cleans the elements children from line-number spans
* *
@ -534,6 +543,8 @@ export class HtmlToPdfService {
// If this is an list item, add some space to the lineNumbers: // If this is an list item, add some space to the lineNumbers:
if (childrenLineNumbers.length && element.nodeName === 'LI') { if (childrenLineNumbers.length && element.nodeName === 'LI') {
childrenLineNumbers[childrenLineNumbers.length - 1].marginBottom = this.LI_MARGIN_BOTTOM; childrenLineNumbers[childrenLineNumbers.length - 1].marginBottom = this.LI_MARGIN_BOTTOM;
} else if (childrenLineNumbers.length && element.parentNode.nodeName === 'LI') {
childrenLineNumbers[childrenLineNumbers.length - 1].marginBottom = this.P_MARGIN_BOTTOM;
} }
foundLineNumbers = foundLineNumbers.concat(childrenLineNumbers); foundLineNumbers = foundLineNumbers.concat(childrenLineNumbers);