|
1 | 1 | import Worker from '../worker.js';
|
| 2 | +import { objType, createElement } from './utils.js'; |
2 | 3 |
|
| 4 | +// Add page-break functionality. |
| 5 | + |
| 6 | +// Refs to original functions. |
3 | 7 | var orig = {
|
4 | 8 | toContainer: Worker.prototype.toContainer
|
5 | 9 | };
|
6 | 10 |
|
| 11 | +// Add pageBreak default options to the Worker template. |
| 12 | +Worker.template.opt.pageBreak = { |
| 13 | + mode: ['css', 'legacy'], // 'avoid-all', 'css', 'legacy', 'whiteline' |
| 14 | + before: [], |
| 15 | + after: [], |
| 16 | + avoid: [] |
| 17 | +}; |
| 18 | + |
7 | 19 | Worker.prototype.toContainer = function toContainer() {
|
8 | 20 | return orig.toContainer.call(this).then(function toContainer_pagebreak() {
|
9 |
| - // Enable page-breaks. |
10 |
| - var pageBreaks = this.prop.container.querySelectorAll('.html2pdf__page-break'); |
| 21 | + // Setup root element and inner page height. |
| 22 | + var root = this.prop.container; |
11 | 23 | var pxPageHeight = this.prop.pageSize.inner.px.height;
|
12 |
| - Array.prototype.forEach.call(pageBreaks, function pageBreak_loop(el) { |
13 |
| - el.style.display = 'block'; |
| 24 | + |
| 25 | + // Check all requested modes. |
| 26 | + var modeSrc = [].concat(this.opt.pageBreak.mode); |
| 27 | + var mode = { |
| 28 | + avoidAll: modeSrc.indexOf('avoid-all') !== -1, |
| 29 | + css: modeSrc.indexOf('css') !== -1, |
| 30 | + legacy: modeSrc.indexOf('legacy') !== -1, |
| 31 | + whiteline: modeSrc.indexOf('whiteline') !== -1 |
| 32 | + }; |
| 33 | + |
| 34 | + // Get arrays of all explicitly requested elements. |
| 35 | + var select = {}; |
| 36 | + ['before', 'after', 'avoid'].forEach(function(key) { |
| 37 | + var all = mode.avoidAll && key === 'avoid'; |
| 38 | + select[key] = all ? [] : [].concat(this.opt.pageBreak[key]); |
| 39 | + if (select[key].length > 0) { |
| 40 | + select[key] = Array.prototype.slice.call( |
| 41 | + root.querySelectorAll(select[key].join(', '))); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + // Get all legacy page-break elements. |
| 46 | + var legacyEls = root.querySelectorAll('.html2pdf__page-break'); |
| 47 | + legacyEls = Array.prototype.slice.call(legacyEls); |
| 48 | + |
| 49 | + // Loop through all elements. |
| 50 | + var els = root.querySelectorAll('*'); |
| 51 | + Array.prototype.forEach.call(els, function pageBreak_loop(el) { |
| 52 | + // Setup pagebreak rules based on legacy and avoidAll modes. |
| 53 | + var rules = { |
| 54 | + before: false, |
| 55 | + after: mode.legacy && legacyEls.indexOf(el) !== -1, |
| 56 | + avoid: mode.avoidAll |
| 57 | + }; |
| 58 | + |
| 59 | + // Add rules for css mode. |
| 60 | + if (mode.css) { |
| 61 | + // TODO: Check if this is valid with iFrames. |
| 62 | + var style = window.getComputedStyle(el); |
| 63 | + // TODO: Handle 'left' and 'right' correctly. |
| 64 | + // TODO: Add support for 'avoid' on breakBefore/After. |
| 65 | + var cssOpt = ['always', 'left', 'right']; |
| 66 | + rules = { |
| 67 | + before: rules.before || cssOpt.indexOf(style.breakBefore || style.pageBreakBefore) !== -1, |
| 68 | + after: rules.after || cssOpt.indexOf(style.breakAfter || style.pageBreakAfter) !== -1, |
| 69 | + avoid: rules.avoid || (style.breakInside || style.pageBreakInside) === 'avoid' |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + // Add rules for explicit requests. |
| 74 | + Object.keys(rules).forEach(function(key) { |
| 75 | + rules[key] = rules[key] || select[key].indexOf(el) !== -1; |
| 76 | + }); |
| 77 | + |
| 78 | + // Get element position on the screen. |
| 79 | + // TODO: Subtract the top of the container from clientRect.top/bottom? |
14 | 80 | var clientRect = el.getBoundingClientRect();
|
15 |
| - el.style.height = pxPageHeight - (clientRect.top % pxPageHeight) + 'px'; |
16 |
| - }, this); |
| 81 | + |
| 82 | + // Avoid: Check if a break happens mid-element. |
| 83 | + if (rules.avoid && !rules.before) { |
| 84 | + var startPage = Math.floor(clientRect.top / pxPageHeight); |
| 85 | + var endPage = Math.floor(clientRect.bottom / pxPageHeight); |
| 86 | + var nPages = Math.abs(clientRect.bottom - clientRect.top) / pxPageHeight; |
| 87 | + |
| 88 | + // Turn on rules.before if the el is broken and is less than a page long. |
| 89 | + if (endPage !== startPage && nPages < 1) { |
| 90 | + rules.before = true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Before: Create a padding div to push the element to the next page. |
| 95 | + if (rules.before) { |
| 96 | + var pad = createElement('div', {style: { |
| 97 | + display: 'block', |
| 98 | + height: pxPageHeight - (clientRect.top % pxPageHeight) + 'px' |
| 99 | + }}); |
| 100 | + el.parentNode.insertBefore(pad, el); |
| 101 | + } |
| 102 | + |
| 103 | + // After: Create a padding div to fill the remaining page. |
| 104 | + if (rules.after) { |
| 105 | + var pad = createElement('div', {style: { |
| 106 | + display: 'block', |
| 107 | + height: pxPageHeight - (clientRect.bottom % pxPageHeight) + 'px' |
| 108 | + }}); |
| 109 | + el.parentNode.insertAfter(pad, el); |
| 110 | + } |
| 111 | + }); |
17 | 112 | });
|
18 | 113 | };
|
0 commit comments