@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 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/print",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
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,15 @@ 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
+ };
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
- print(action);
122
+ queryPrintElement(action.element);
111
123
  }
112
124
  }
113
125
  ]);