@cocreate/print 1.0.0 → 1.0.2
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 +14 -0
- package/package.json +1 -1
- package/src/index.js +35 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.0.2](https://github.com/CoCreate-app/CoCreate-print/compare/v1.0.1...v1.0.2) (2024-12-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* dispatch printed event ([1acdb66](https://github.com/CoCreate-app/CoCreate-print/commit/1acdb66d9f7988fd5494a0f035c5227e1ba7d831))
|
|
7
|
+
|
|
8
|
+
## [1.0.1](https://github.com/CoCreate-app/CoCreate-print/compare/v1.0.0...v1.0.1) (2024-12-14)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* use iframe to show print dialog only ([a6e3a6d](https://github.com/CoCreate-app/CoCreate-print/commit/a6e3a6d9526654ba64d0c5d89b76701dde352bec))
|
|
14
|
+
|
|
1
15
|
# 1.0.0 (2024-12-14)
|
|
2
16
|
|
|
3
17
|
|
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,21 @@ 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
|
+
};
|
|
113
|
+
|
|
114
|
+
document.dispatchEvent(
|
|
115
|
+
new CustomEvent("printed", {
|
|
116
|
+
detail: { content }
|
|
117
|
+
})
|
|
118
|
+
);
|
|
101
119
|
}
|
|
102
120
|
|
|
103
121
|
init();
|
|
@@ -107,7 +125,7 @@ Actions.init([
|
|
|
107
125
|
name: "print",
|
|
108
126
|
endEvent: "printed",
|
|
109
127
|
callback: (action) => {
|
|
110
|
-
|
|
128
|
+
queryPrintElement(action.element);
|
|
111
129
|
}
|
|
112
130
|
}
|
|
113
131
|
]);
|