@ai-matrx/print 0.12.39 → 0.12.40

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/dist/markdown.js CHANGED
@@ -28,8 +28,27 @@ function withMathStyles(htmlDocument) {
28
28
  const head = htmlDocument.search(/<\/head>/i);
29
29
  return head >= 0 ? htmlDocument.slice(0, head) + style + htmlDocument.slice(head) : style + htmlDocument;
30
30
  }
31
+ var PRINT_HOST = /* @__PURE__ */ Symbol.for("ai-matrx.print.host");
32
+ function getPrintHost() {
33
+ return globalThis[PRINT_HOST] ?? null;
34
+ }
35
+ function handToPrintHost(htmlDocument, title) {
36
+ const host = getPrintHost();
37
+ if (!host) return false;
38
+ try {
39
+ const done = host(htmlDocument, { title });
40
+ if (done && typeof done.catch === "function") {
41
+ done.catch((error) => console.error("[@ai-matrx/print] the host's print failed", error));
42
+ }
43
+ return true;
44
+ } catch (error) {
45
+ console.error("[@ai-matrx/print] the host's print threw; using the print window instead", error);
46
+ return false;
47
+ }
48
+ }
31
49
  function openPrintWindow(rawDocument, filename = "print", windowFeatures = "width=920,height=720,scrollbars=yes") {
32
50
  const htmlDocument = withMathStyles(rawDocument);
51
+ if (handToPrintHost(htmlDocument, filename)) return "opened";
33
52
  const win = openOutbound("", "_blank", windowFeatures);
34
53
  if (!win) return openPrintWindowFallback(htmlDocument, filename);
35
54
  win.document.open();
@@ -38,7 +57,7 @@ function openPrintWindow(rawDocument, filename = "print", windowFeatures = "widt
38
57
  return "opened";
39
58
  }
40
59
  function openPendingPrintWindow(filename = "print", windowFeatures = "width=920,height=720,scrollbars=yes") {
41
- const win = openOutbound("", "_blank", windowFeatures);
60
+ const win = getPrintHost() ? null : openOutbound("", "_blank", windowFeatures);
42
61
  if (win) {
43
62
  win.document.open();
44
63
  win.document.write(
@@ -49,6 +68,7 @@ function openPendingPrintWindow(filename = "print", windowFeatures = "width=920,
49
68
  return {
50
69
  write(rawDocument) {
51
70
  const htmlDocument = withMathStyles(rawDocument);
71
+ if (!win && handToPrintHost(htmlDocument, filename)) return "opened";
52
72
  if (!win || win.closed) return openPrintWindowFallback(htmlDocument, filename);
53
73
  win.document.open();
54
74
  win.document.write(htmlDocument);