-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
index.js
189 lines (145 loc) · 5.4 KB
/
index.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React from "react";
import { findDOMNode } from "react-dom";
import PropTypes from "prop-types";
class ReactToPrint extends React.Component {
static propTypes = {
/** Copy styles over into print window. default: true */
copyStyles: PropTypes.bool,
/** Trigger action used to open browser print */
trigger: PropTypes.func.isRequired,
/** Content to be printed */
content: PropTypes.func.isRequired,
/** Callback function to trigger before print */
onBeforePrint: PropTypes.func,
/** Callback function to trigger after print */
onAfterPrint: PropTypes.func,
/** Close the print window after action */
closeAfterPrint: PropTypes.bool,
/** Optional class to pass to the print window body */
bodyClass: PropTypes.string,
/** Debug Mode */
debug: PropTypes.bool
};
static defaultProps = {
copyStyles: true,
closeAfterPrint: true,
bodyClass: '',
debug: false
};
triggerPrint(target) {
if (this.props.onBeforePrint) {
this.props.onBeforePrint();
}
setTimeout(() => {
if (!this.props.debug) {
target.print();
if (this.props.closeAfterPrint) {
target.close();
}
}
}, 500);
}
handlePrint = () => {
const {
content,
copyStyles,
onAfterPrint
} = this.props;
let printWindow = window.open("", "Print", "status=no, toolbar=no, scrollbars=yes", "false");
if (onAfterPrint) {
printWindow.onbeforeunload = onAfterPrint;
}
const contentEl = content();
const contentNodes = findDOMNode(contentEl);
const imageNodes = [...contentNodes.getElementsByTagName("img")];
const linkNodes = document.querySelectorAll('link[rel="stylesheet"]');
this.imageTotal = imageNodes.length;
this.imageLoaded = 0;
this.linkTotal = linkNodes.length;
this.linkLoaded = 0;
const markLoaded = (type) => {
if (type === 'image')
this.imageLoaded++;
else if (type === 'link')
this.linkLoaded++;
if (this.imageLoaded === this.imageTotal && this.linkLoaded === this.linkTotal) {
this.triggerPrint(printWindow);
}
};
[...imageNodes].forEach((child) => {
/** Workaround for Safari if the image has base64 data as a source */
if (/^data:/.test(child.src)) {
child.crossOrigin = 'anonymous';
}
child.setAttribute('src', child.src);
child.onload = markLoaded.bind(null, 'image');
child.onerror = markLoaded.bind(null, 'image');
child.crossOrigin = 'use-credentials';
});
/*
* IE does not seem to allow appendChild from different window contexts correctly. They seem to come back
* as plain objects. In order to get around this each tag is re-created into the printWindow
* https://stackoverflow.com/questions/38708840/calling-adoptnode-and-importnode-on-a-child-window-fails-in-ie-and-edge
*/
if (copyStyles !== false) {
const headEls = document.querySelectorAll('style, link[rel="stylesheet"]');
[...headEls].forEach(node => {
const doc = printWindow.contentDocument || printWindow.document;
let newHeadEl = doc.createElement(node.tagName);
if (node.textContent)
newHeadEl.textContent = node.textContent;
else if (node.innerText)
newHeadEl.innerText = node.innerText;
let attributes = [...node.attributes];
attributes.forEach(attr => {
let nodeValue = attr.nodeValue;
if (
attr.nodeName === 'href' &&
/^https?:\/\//.test(attr.nodeValue) === false &&
/^blob:/.test(attr.nodeValue) === false
) {
const relPath = attr.nodeValue.substr(0, 3) === "../"
? document.location.pathname.replace(/[^/]*$/, '')
: "/";
nodeValue = nodeValue.replace(/\/+/, '');
nodeValue = document.location.protocol + '//' + document.location.host + relPath + nodeValue;
}
newHeadEl.setAttribute(attr.nodeName, nodeValue);
});
if (node.tagName === 'LINK') {
newHeadEl.onload = markLoaded.bind(null, 'link');
newHeadEl.onerror = markLoaded.bind(null, 'link');
}
printWindow.document.head.appendChild(newHeadEl);
});
}
if (document.body.className) {
const bodyClasses = document.body.className.split(" ");
bodyClasses
.filter(item => item)
.map(item => printWindow.document.body.classList.add(item));
}
if (this.props.bodyClass.length) {
printWindow.document.body.classList.add(this.props.bodyClass);
}
/* remove date/time from top */
let styleEl = printWindow.document.createElement('style');
styleEl.appendChild(printWindow.document.createTextNode("@page { size: auto; margin: 0mm; } @media print { body { -webkit-print-color-adjust: exact; } }"));
printWindow.document.head.appendChild(styleEl);
printWindow.document.body.innerHTML = contentNodes.outerHTML;
if (this.imageTotal === 0 || copyStyles === false) {
this.triggerPrint(printWindow);
}
if (this.props.debug) {
console.log("** DEBUG MODE **");
console.log(printWindow.document);
}
}
render() {
return React.cloneElement(this.props.trigger(), {
ref: (el) => this.triggerRef = el,
onClick: this.handlePrint
});
}
}
export default ReactToPrint;