-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathhyperlinks.js
More file actions
71 lines (60 loc) · 2.5 KB
/
hyperlinks.js
File metadata and controls
71 lines (60 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import Worker from '../worker.js';
import { unitConvert } from '../utils.js';
// Add hyperlink functionality to the PDF creation.
// Main link array, and refs to original functions.
var linkInfo = [];
var orig = {
toContainer: Worker.prototype.toContainer,
toPdf: Worker.prototype.toPdf,
};
Worker.prototype.toContainer = function toContainer() {
var self = this;
return orig.toContainer.call(this).then(function toContainer_hyperlink() {
// Retrieve hyperlink info if the option is enabled.
function storeLinkInfo(container, externalPage) {
var links = container.querySelectorAll('a');
var containerRect = unitConvert(container.getBoundingClientRect(), self.prop.pageSize.k);
// Loop through each anchor tag.
Array.prototype.forEach.call(links, function(link) {
// Treat each client rect as a separ nmate link (for text-wrapping).
var clientRects = link.getClientRects();
for (var i=0; i<clientRects.length; i++) {
var clientRect = unitConvert(clientRects[i], self.prop.pageSize.k);
clientRect.left -= containerRect.left;
clientRect.top -= containerRect.top;
var page = externalPage || (Math.floor(clientRect.top / self.prop.pageSize.inner.height) + 1);
var top = self.opt.margin[0] + clientRect.top % self.prop.pageSize.inner.height;
var left = self.opt.margin[1] + clientRect.left;
linkInfo.push({ page, top, left, clientRect, link });
}
});
}
if (self.opt.enableLinks) {
// Find all anchor tags and get the container's bounds for reference.
linkInfo = [];
if (Array.isArray(self.opt.enableLinks)) {
self.opt.enableLinks.forEach(function({id, page}) {
storeLinkInfo(document.getElementById(id), page);
});
} else {
storeLinkInfo(self.prop.container);
}
}
});
};
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().
linkInfo.forEach(function(l) {
this.prop.pdf.setPage(l.page);
this.prop.pdf.link(l.left, l.top, l.clientRect.width, l.clientRect.height,
{ url: l.link.href });
}, this);
// Reset the active page of the PDF to the final page.
var nPages = this.prop.pdf.internal.getNumberOfPages();
this.prop.pdf.setPage(nPages);
}
});
};