Skip to content

Commit 08addfa

Browse files
committed
Move plugin behaviour into html2canvas onclone
1 parent 225fbde commit 08addfa

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/plugin/hyperlinks.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ import { unitConvert } from '../utils.js';
66
// Main link array, and refs to original functions.
77
var linkInfo = [];
88
var orig = {
9-
toContainer: Worker.prototype.toContainer,
9+
toCanvas: Worker.prototype.toCanvas,
1010
toPdf: Worker.prototype.toPdf,
1111
};
1212

13-
Worker.prototype.toContainer = function toContainer() {
14-
return orig.toContainer.call(this).then(function toContainer_hyperlink() {
13+
Worker.prototype.toCanvas = function toCanvas() {
14+
return this.then(function toCanvas_hyperlink() {
15+
// Attach extra behaviour to the html2canvas onclone property.
16+
var oncloneOrig = this.opt.html2canvas.onclone || function () {};
17+
this.opt.html2canvas.onclone = onclone_hyperlink.bind(this, oncloneOrig);
18+
}).then(orig.toCanvas.bind(this));
19+
};
20+
21+
function onclone_hyperlink(oncloneOrig, doc) {
1522
// Retrieve hyperlink info if the option is enabled.
1623
if (this.opt.enableLinks) {
1724
// Find all anchor tags and get the container's bounds for reference.
18-
var container = this.prop.container;
25+
var container = doc.body;
1926
var links = container.querySelectorAll('a');
2027
var containerRect = unitConvert(container.getBoundingClientRect(), this.prop.pageSize.k);
2128
linkInfo = [];
@@ -37,14 +44,16 @@ Worker.prototype.toContainer = function toContainer() {
3744
}
3845
}, this);
3946
}
40-
});
41-
};
47+
48+
// Call the original onclone callback.
49+
oncloneOrig(doc);
50+
}
4251

4352
Worker.prototype.toPdf = function toPdf() {
4453
return orig.toPdf.call(this).then(function toPdf_hyperlink() {
4554
// Add hyperlinks if the option is enabled.
4655
if (this.opt.enableLinks) {
47-
// Attach each anchor tag based on info from toContainer().
56+
// Attach each anchor tag based on info from the cloned document.
4857
linkInfo.forEach(function(l) {
4958
this.prop.pdf.setPage(l.page);
5059
this.prop.pdf.link(l.left, l.top, l.clientRect.width, l.clientRect.height,

src/plugin/pagebreaks.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { objType, createElement } from '../utils.js';
2525

2626
// Refs to original functions.
2727
var orig = {
28-
toContainer: Worker.prototype.toContainer
28+
toCanvas: Worker.prototype.toCanvas
2929
};
3030

3131
// Add pagebreak default options to the Worker template.
@@ -36,10 +36,17 @@ Worker.template.opt.pagebreak = {
3636
avoid: []
3737
};
3838

39-
Worker.prototype.toContainer = function toContainer() {
40-
return orig.toContainer.call(this).then(function toContainer_pagebreak() {
39+
Worker.prototype.toCanvas = function toCanvas() {
40+
return this.then(function toCanvas_pagebreak() {
41+
// Attach extra behaviour to the html2canvas onclone property.
42+
var oncloneOrig = this.opt.html2canvas.onclone || function () {};
43+
this.opt.html2canvas.onclone = onclone_pagebreak.bind(this, oncloneOrig);
44+
}).then(orig.toCanvas.bind(this));
45+
};
46+
47+
function onclone_pagebreak(oncloneOrig, doc) {
4148
// Setup root element and inner page height.
42-
var root = this.prop.container;
49+
var root = doc.body;
4350
var pxPageHeight = this.prop.pageSize.inner.px.height;
4451

4552
// Check all requested modes.
@@ -130,5 +137,7 @@ Worker.prototype.toContainer = function toContainer() {
130137
el.parentNode.insertBefore(pad, el.nextSibling);
131138
}
132139
});
133-
});
134-
};
140+
141+
// Call the original onclone callback.
142+
oncloneOrig(doc);
143+
}

0 commit comments

Comments
 (0)