Skip to content

Commit 6edd9b4

Browse files
committed
Fix plugin indentation
1 parent 08addfa commit 6edd9b4

File tree

2 files changed

+111
-111
lines changed

2 files changed

+111
-111
lines changed

src/plugin/hyperlinks.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,34 @@ Worker.prototype.toCanvas = function toCanvas() {
1919
};
2020

2121
function onclone_hyperlink(oncloneOrig, doc) {
22-
// Retrieve hyperlink info if the option is enabled.
23-
if (this.opt.enableLinks) {
24-
// Find all anchor tags and get the container's bounds for reference.
25-
var container = doc.body;
26-
var links = container.querySelectorAll('a');
27-
var containerRect = unitConvert(container.getBoundingClientRect(), this.prop.pageSize.k);
28-
linkInfo = [];
22+
// Retrieve hyperlink info if the option is enabled.
23+
if (this.opt.enableLinks) {
24+
// Find all anchor tags and get the container's bounds for reference.
25+
var container = doc.body;
26+
var links = container.querySelectorAll('a');
27+
var containerRect = unitConvert(container.getBoundingClientRect(), this.prop.pageSize.k);
28+
linkInfo = [];
2929

30-
// Loop through each anchor tag.
31-
Array.prototype.forEach.call(links, function(link) {
32-
// Treat each client rect as a separate link (for text-wrapping).
33-
var clientRects = link.getClientRects();
34-
for (var i=0; i<clientRects.length; i++) {
35-
var clientRect = unitConvert(clientRects[i], this.prop.pageSize.k);
36-
clientRect.left -= containerRect.left;
37-
clientRect.top -= containerRect.top;
30+
// Loop through each anchor tag.
31+
Array.prototype.forEach.call(links, function(link) {
32+
// Treat each client rect as a separate link (for text-wrapping).
33+
var clientRects = link.getClientRects();
34+
for (var i=0; i<clientRects.length; i++) {
35+
var clientRect = unitConvert(clientRects[i], this.prop.pageSize.k);
36+
clientRect.left -= containerRect.left;
37+
clientRect.top -= containerRect.top;
3838

39-
var page = Math.floor(clientRect.top / this.prop.pageSize.inner.height) + 1;
40-
var top = this.opt.margin[0] + clientRect.top % this.prop.pageSize.inner.height;
41-
var left = this.opt.margin[1] + clientRect.left;
39+
var page = Math.floor(clientRect.top / this.prop.pageSize.inner.height) + 1;
40+
var top = this.opt.margin[0] + clientRect.top % this.prop.pageSize.inner.height;
41+
var left = this.opt.margin[1] + clientRect.left;
4242

43-
linkInfo.push({ page, top, left, clientRect, link });
44-
}
45-
}, this);
46-
}
43+
linkInfo.push({ page, top, left, clientRect, link });
44+
}
45+
}, this);
46+
}
4747

48-
// Call the original onclone callback.
49-
oncloneOrig(doc);
48+
// Call the original onclone callback.
49+
oncloneOrig(doc);
5050
}
5151

5252
Worker.prototype.toPdf = function toPdf() {

src/plugin/pagebreaks.js

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -45,99 +45,99 @@ Worker.prototype.toCanvas = function toCanvas() {
4545
};
4646

4747
function onclone_pagebreak(oncloneOrig, doc) {
48-
// Setup root element and inner page height.
49-
var root = doc.body;
50-
var pxPageHeight = this.prop.pageSize.inner.px.height;
51-
52-
// Check all requested modes.
53-
var modeSrc = [].concat(this.opt.pagebreak.mode);
54-
var mode = {
55-
avoidAll: modeSrc.indexOf('avoid-all') !== -1,
56-
css: modeSrc.indexOf('css') !== -1,
57-
legacy: modeSrc.indexOf('legacy') !== -1
48+
// Setup root element and inner page height.
49+
var root = doc.body;
50+
var pxPageHeight = this.prop.pageSize.inner.px.height;
51+
52+
// Check all requested modes.
53+
var modeSrc = [].concat(this.opt.pagebreak.mode);
54+
var mode = {
55+
avoidAll: modeSrc.indexOf('avoid-all') !== -1,
56+
css: modeSrc.indexOf('css') !== -1,
57+
legacy: modeSrc.indexOf('legacy') !== -1
58+
};
59+
60+
// Get arrays of all explicitly requested elements.
61+
var select = {};
62+
var self = this;
63+
['before', 'after', 'avoid'].forEach(function(key) {
64+
var all = mode.avoidAll && key === 'avoid';
65+
select[key] = all ? [] : [].concat(self.opt.pagebreak[key] || []);
66+
if (select[key].length > 0) {
67+
select[key] = Array.prototype.slice.call(
68+
root.querySelectorAll(select[key].join(', ')));
69+
}
70+
});
71+
72+
// Get all legacy page-break elements.
73+
var legacyEls = root.querySelectorAll('.html2pdf__page-break');
74+
legacyEls = Array.prototype.slice.call(legacyEls);
75+
76+
// Loop through all elements.
77+
var els = root.querySelectorAll('*');
78+
Array.prototype.forEach.call(els, function pagebreak_loop(el) {
79+
// Setup pagebreak rules based on legacy and avoidAll modes.
80+
var rules = {
81+
before: false,
82+
after: mode.legacy && legacyEls.indexOf(el) !== -1,
83+
avoid: mode.avoidAll
5884
};
5985

60-
// Get arrays of all explicitly requested elements.
61-
var select = {};
62-
var self = this;
63-
['before', 'after', 'avoid'].forEach(function(key) {
64-
var all = mode.avoidAll && key === 'avoid';
65-
select[key] = all ? [] : [].concat(self.opt.pagebreak[key] || []);
66-
if (select[key].length > 0) {
67-
select[key] = Array.prototype.slice.call(
68-
root.querySelectorAll(select[key].join(', ')));
69-
}
70-
});
71-
72-
// Get all legacy page-break elements.
73-
var legacyEls = root.querySelectorAll('.html2pdf__page-break');
74-
legacyEls = Array.prototype.slice.call(legacyEls);
75-
76-
// Loop through all elements.
77-
var els = root.querySelectorAll('*');
78-
Array.prototype.forEach.call(els, function pagebreak_loop(el) {
79-
// Setup pagebreak rules based on legacy and avoidAll modes.
80-
var rules = {
81-
before: false,
82-
after: mode.legacy && legacyEls.indexOf(el) !== -1,
83-
avoid: mode.avoidAll
86+
// Add rules for css mode.
87+
if (mode.css) {
88+
// TODO: Check if this is valid with iFrames.
89+
var style = window.getComputedStyle(el);
90+
// TODO: Handle 'left' and 'right' correctly.
91+
// TODO: Add support for 'avoid' on breakBefore/After.
92+
var breakOpt = ['always', 'page', 'left', 'right'];
93+
var avoidOpt = ['avoid', 'avoid-page'];
94+
rules = {
95+
before: rules.before || breakOpt.indexOf(style.breakBefore || style.pageBreakBefore) !== -1,
96+
after: rules.after || breakOpt.indexOf(style.breakAfter || style.pageBreakAfter) !== -1,
97+
avoid: rules.avoid || avoidOpt.indexOf(style.breakInside || style.pageBreakInside) !== -1
8498
};
99+
}
85100

86-
// Add rules for css mode.
87-
if (mode.css) {
88-
// TODO: Check if this is valid with iFrames.
89-
var style = window.getComputedStyle(el);
90-
// TODO: Handle 'left' and 'right' correctly.
91-
// TODO: Add support for 'avoid' on breakBefore/After.
92-
var breakOpt = ['always', 'page', 'left', 'right'];
93-
var avoidOpt = ['avoid', 'avoid-page'];
94-
rules = {
95-
before: rules.before || breakOpt.indexOf(style.breakBefore || style.pageBreakBefore) !== -1,
96-
after: rules.after || breakOpt.indexOf(style.breakAfter || style.pageBreakAfter) !== -1,
97-
avoid: rules.avoid || avoidOpt.indexOf(style.breakInside || style.pageBreakInside) !== -1
98-
};
99-
}
101+
// Add rules for explicit requests.
102+
Object.keys(rules).forEach(function(key) {
103+
rules[key] = rules[key] || select[key].indexOf(el) !== -1;
104+
});
100105

101-
// Add rules for explicit requests.
102-
Object.keys(rules).forEach(function(key) {
103-
rules[key] = rules[key] || select[key].indexOf(el) !== -1;
104-
});
105-
106-
// Get element position on the screen.
107-
// TODO: Subtract the top of the container from clientRect.top/bottom?
108-
var clientRect = el.getBoundingClientRect();
109-
110-
// Avoid: Check if a break happens mid-element.
111-
if (rules.avoid && !rules.before) {
112-
var startPage = Math.floor(clientRect.top / pxPageHeight);
113-
var endPage = Math.floor(clientRect.bottom / pxPageHeight);
114-
var nPages = Math.abs(clientRect.bottom - clientRect.top) / pxPageHeight;
115-
116-
// Turn on rules.before if the el is broken and is at most one page long.
117-
if (endPage !== startPage && nPages <= 1) {
118-
rules.before = true;
119-
}
120-
}
106+
// Get element position on the screen.
107+
// TODO: Subtract the top of the container from clientRect.top/bottom?
108+
var clientRect = el.getBoundingClientRect();
121109

122-
// Before: Create a padding div to push the element to the next page.
123-
if (rules.before) {
124-
var pad = createElement('div', {style: {
125-
display: 'block',
126-
height: pxPageHeight - (clientRect.top % pxPageHeight) + 'px'
127-
}});
128-
el.parentNode.insertBefore(pad, el);
129-
}
110+
// Avoid: Check if a break happens mid-element.
111+
if (rules.avoid && !rules.before) {
112+
var startPage = Math.floor(clientRect.top / pxPageHeight);
113+
var endPage = Math.floor(clientRect.bottom / pxPageHeight);
114+
var nPages = Math.abs(clientRect.bottom - clientRect.top) / pxPageHeight;
130115

131-
// After: Create a padding div to fill the remaining page.
132-
if (rules.after) {
133-
var pad = createElement('div', {style: {
134-
display: 'block',
135-
height: pxPageHeight - (clientRect.bottom % pxPageHeight) + 'px'
136-
}});
137-
el.parentNode.insertBefore(pad, el.nextSibling);
116+
// Turn on rules.before if the el is broken and is at most one page long.
117+
if (endPage !== startPage && nPages <= 1) {
118+
rules.before = true;
138119
}
139-
});
140-
141-
// Call the original onclone callback.
142-
oncloneOrig(doc);
120+
}
121+
122+
// Before: Create a padding div to push the element to the next page.
123+
if (rules.before) {
124+
var pad = createElement('div', {style: {
125+
display: 'block',
126+
height: pxPageHeight - (clientRect.top % pxPageHeight) + 'px'
127+
}});
128+
el.parentNode.insertBefore(pad, el);
129+
}
130+
131+
// After: Create a padding div to fill the remaining page.
132+
if (rules.after) {
133+
var pad = createElement('div', {style: {
134+
display: 'block',
135+
height: pxPageHeight - (clientRect.bottom % pxPageHeight) + 'px'
136+
}});
137+
el.parentNode.insertBefore(pad, el.nextSibling);
138+
}
139+
});
140+
141+
// Call the original onclone callback.
142+
oncloneOrig(doc);
143143
}

0 commit comments

Comments
 (0)