@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 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/print",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A simple print component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "print",
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
- qeueryPrintElement(element);
43
+ queryPrintElement(element);
44
44
  }
45
45
  });
46
46
  }
47
47
 
48
- function qeueryPrintElement(element) {
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(element) {
57
- if (element) return;
56
+ function print(elements) {
57
+ if (!elements) return;
58
58
  if (
59
- !(element instanceof HTMLCollection) &&
60
- !(element instanceof NodeList) &&
61
- !Array.isArray(element)
59
+ !(elements instanceof HTMLCollection) &&
60
+ !(elements instanceof NodeList) &&
61
+ !Array.isArray(elements)
62
62
  ) {
63
- element = [element];
63
+ elements = [elements];
64
64
  }
65
65
 
66
66
  let content = "";
67
- element.forEach((target) => {
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
- // Open a new window for printing
86
- const printWindow = window.open("", "", "width=800,height=600");
87
- printWindow.document.write(`
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
- printWindow.document.close();
97
- printWindow.print();
104
+ iframeDoc.close();
105
+
106
+ // Wait for iframe content to load before triggering print
107
+ iframe.onload = () => {
108
+ iframe.contentWindow.print();
98
109
 
99
- // Trigger the "printed" event
100
- // action.end();
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
- print(action);
128
+ queryPrintElement(action.element);
111
129
  }
112
130
  }
113
131
  ]);