@cocreate/print 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/index.js +29 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.0.1](https://github.com/CoCreate-app/CoCreate-print/compare/v1.0.0...v1.0.1) (2024-12-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* use iframe to show print dialog only ([a6e3a6d](https://github.com/CoCreate-app/CoCreate-print/commit/a6e3a6d9526654ba64d0c5d89b76701dde352bec))
|
|
7
|
+
|
|
1
8
|
# 1.0.0 (2024-12-14)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -40,12 +40,12 @@ function initElement(element) {
|
|
|
40
40
|
element.addEventListener("click", () => {
|
|
41
41
|
let actions = element.getAttribute("actions");
|
|
42
42
|
if (actions && !actions.includes("print")) {
|
|
43
|
-
|
|
43
|
+
queryPrintElement(element);
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
function
|
|
48
|
+
function queryPrintElement(element) {
|
|
49
49
|
let elements = queryElements({ element, prefix: "print" });
|
|
50
50
|
if (elements === false) {
|
|
51
51
|
elements = [element];
|
|
@@ -53,18 +53,18 @@ function qeueryPrintElement(element) {
|
|
|
53
53
|
print(elements);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
function print(
|
|
57
|
-
if (
|
|
56
|
+
function print(elements) {
|
|
57
|
+
if (!elements) return;
|
|
58
58
|
if (
|
|
59
|
-
!(
|
|
60
|
-
!(
|
|
61
|
-
!Array.isArray(
|
|
59
|
+
!(elements instanceof HTMLCollection) &&
|
|
60
|
+
!(elements instanceof NodeList) &&
|
|
61
|
+
!Array.isArray(elements)
|
|
62
62
|
) {
|
|
63
|
-
|
|
63
|
+
elements = [elements];
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
let content = "";
|
|
67
|
-
|
|
67
|
+
elements.forEach((target) => {
|
|
68
68
|
content += target.outerHTML; // Combine all target elements' HTML
|
|
69
69
|
});
|
|
70
70
|
|
|
@@ -82,9 +82,17 @@ function print(element) {
|
|
|
82
82
|
})
|
|
83
83
|
.join("");
|
|
84
84
|
|
|
85
|
-
//
|
|
86
|
-
const
|
|
87
|
-
|
|
85
|
+
// Create a hidden iframe for printing
|
|
86
|
+
const iframe = document.createElement("iframe");
|
|
87
|
+
iframe.style.position = "absolute";
|
|
88
|
+
iframe.style.top = "-9999px";
|
|
89
|
+
iframe.style.left = "-9999px";
|
|
90
|
+
document.body.appendChild(iframe);
|
|
91
|
+
|
|
92
|
+
// Write content to the iframe
|
|
93
|
+
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
|
|
94
|
+
iframeDoc.open();
|
|
95
|
+
iframeDoc.write(`
|
|
88
96
|
<html>
|
|
89
97
|
<head>
|
|
90
98
|
<title>Print</title>
|
|
@@ -93,11 +101,15 @@ function print(element) {
|
|
|
93
101
|
<body>${content}</body>
|
|
94
102
|
</html>
|
|
95
103
|
`);
|
|
96
|
-
|
|
97
|
-
|
|
104
|
+
iframeDoc.close();
|
|
105
|
+
|
|
106
|
+
// Wait for iframe content to load before triggering print
|
|
107
|
+
iframe.onload = () => {
|
|
108
|
+
iframe.contentWindow.print();
|
|
98
109
|
|
|
99
|
-
|
|
100
|
-
|
|
110
|
+
// Remove iframe after printing
|
|
111
|
+
iframe.parentNode.removeChild(iframe);
|
|
112
|
+
};
|
|
101
113
|
}
|
|
102
114
|
|
|
103
115
|
init();
|
|
@@ -107,7 +119,7 @@ Actions.init([
|
|
|
107
119
|
name: "print",
|
|
108
120
|
endEvent: "printed",
|
|
109
121
|
callback: (action) => {
|
|
110
|
-
|
|
122
|
+
queryPrintElement(action.element);
|
|
111
123
|
}
|
|
112
124
|
}
|
|
113
125
|
]);
|