Skip to content

Commit

Permalink
Move plugin behaviour into html2canvas onclone
Browse files Browse the repository at this point in the history
  • Loading branch information
eKoopmans committed May 19, 2019
1 parent 225fbde commit 08addfa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
23 changes: 16 additions & 7 deletions src/plugin/hyperlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ import { unitConvert } from '../utils.js';
// Main link array, and refs to original functions.
var linkInfo = [];
var orig = {
toContainer: Worker.prototype.toContainer,
toCanvas: Worker.prototype.toCanvas,
toPdf: Worker.prototype.toPdf,
};

Worker.prototype.toContainer = function toContainer() {
return orig.toContainer.call(this).then(function toContainer_hyperlink() {
Worker.prototype.toCanvas = function toCanvas() {
return this.then(function toCanvas_hyperlink() {
// Attach extra behaviour to the html2canvas onclone property.
var oncloneOrig = this.opt.html2canvas.onclone || function () {};
this.opt.html2canvas.onclone = onclone_hyperlink.bind(this, oncloneOrig);
}).then(orig.toCanvas.bind(this));
};

function onclone_hyperlink(oncloneOrig, doc) {
// Retrieve hyperlink info if the option is enabled.
if (this.opt.enableLinks) {
// Find all anchor tags and get the container's bounds for reference.
var container = this.prop.container;
var container = doc.body;
var links = container.querySelectorAll('a');
var containerRect = unitConvert(container.getBoundingClientRect(), this.prop.pageSize.k);
linkInfo = [];
Expand All @@ -37,14 +44,16 @@ Worker.prototype.toContainer = function toContainer() {
}
}, this);
}
});
};

// Call the original onclone callback.
oncloneOrig(doc);
}

Worker.prototype.toPdf = function toPdf() {
return orig.toPdf.call(this).then(function toPdf_hyperlink() {
// Add hyperlinks if the option is enabled.
if (this.opt.enableLinks) {
// Attach each anchor tag based on info from toContainer().
// Attach each anchor tag based on info from the cloned document.
linkInfo.forEach(function(l) {
this.prop.pdf.setPage(l.page);
this.prop.pdf.link(l.left, l.top, l.clientRect.width, l.clientRect.height,
Expand Down
21 changes: 15 additions & 6 deletions src/plugin/pagebreaks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { objType, createElement } from '../utils.js';

// Refs to original functions.
var orig = {
toContainer: Worker.prototype.toContainer
toCanvas: Worker.prototype.toCanvas
};

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

Worker.prototype.toContainer = function toContainer() {
return orig.toContainer.call(this).then(function toContainer_pagebreak() {
Worker.prototype.toCanvas = function toCanvas() {
return this.then(function toCanvas_pagebreak() {
// Attach extra behaviour to the html2canvas onclone property.
var oncloneOrig = this.opt.html2canvas.onclone || function () {};
this.opt.html2canvas.onclone = onclone_pagebreak.bind(this, oncloneOrig);
}).then(orig.toCanvas.bind(this));
};

function onclone_pagebreak(oncloneOrig, doc) {
// Setup root element and inner page height.
var root = this.prop.container;
var root = doc.body;
var pxPageHeight = this.prop.pageSize.inner.px.height;

// Check all requested modes.
Expand Down Expand Up @@ -130,5 +137,7 @@ Worker.prototype.toContainer = function toContainer() {
el.parentNode.insertBefore(pad, el.nextSibling);
}
});
});
};

// Call the original onclone callback.
oncloneOrig(doc);
}

0 comments on commit 08addfa

Please sign in to comment.