@ai-matrx/print 0.2.0 → 0.2.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/dist/react.cjs CHANGED
@@ -46,8 +46,8 @@ var import_tailwind_merge = require("tailwind-merge");
46
46
  function escapeHtml(str) {
47
47
  return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
48
48
  }
49
- function openPrintWindow(htmlDocument, filename = "print") {
50
- const win = window.open("", "_blank", "width=920,height=720,scrollbars=yes");
49
+ function openPrintWindow(htmlDocument, filename = "print", windowFeatures = "width=920,height=720,scrollbars=yes") {
50
+ const win = window.open("", "_blank", windowFeatures);
51
51
  if (!win) {
52
52
  const blob = new Blob([htmlDocument], { type: "text/html;charset=utf-8" });
53
53
  const url = URL.createObjectURL(blob);
@@ -57,12 +57,13 @@ function openPrintWindow(htmlDocument, filename = "print") {
57
57
  document.body.appendChild(a);
58
58
  a.click();
59
59
  document.body.removeChild(a);
60
- URL.revokeObjectURL(url);
61
- return;
60
+ setTimeout(() => URL.revokeObjectURL(url), 0);
61
+ return "downloaded";
62
62
  }
63
63
  win.document.open();
64
64
  win.document.write(htmlDocument);
65
65
  win.document.close();
66
+ return "opened";
66
67
  }
67
68
 
68
69
  // src/qr.ts
@@ -590,7 +591,7 @@ ${bodyHtml}
590
591
  ${withFitScript ? LP_FIT_SCRIPT : ""}
591
592
  </body>
592
593
  </html>`;
593
- openPrintWindow(html, title);
594
+ return openPrintWindow(html, title);
594
595
  }
595
596
  function expandLabels(labels, s) {
596
597
  const from = s.rangeFrom > 0 ? s.rangeFrom : 1;
@@ -628,7 +629,7 @@ function buildCalibrationBody(t) {
628
629
  return `<div class="lp-page">${banner}${marks}${cells.join("\n")}</div>`;
629
630
  }
630
631
  function printCalibrationSheet(template) {
631
- openSheetWindow(
632
+ return openSheetWindow(
632
633
  template,
633
634
  buildCalibrationBody(template),
634
635
  "Label calibration",
@@ -695,18 +696,16 @@ async function printQrLabelSheet(data, variantId, rawSettings) {
695
696
  const t = resolveTemplate(data, variantId);
696
697
  const s = resolveSettings(rawSettings);
697
698
  if (s.calibrationPage) {
698
- printCalibrationSheet(t);
699
- return;
699
+ return printCalibrationSheet(t);
700
700
  }
701
701
  const labels = expandLabels(data.labels ?? [], s);
702
702
  if (!labels.length) {
703
- openSheetWindow(
703
+ return openSheetWindow(
704
704
  t,
705
705
  `<div class="lp-page"><div style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:11pt;">No labels to print.</div></div>`,
706
706
  "QR labels",
707
707
  false
708
708
  );
709
- return;
710
709
  }
711
710
  const hasBadge = labels.some((l) => (l.badge ?? "").trim() !== "");
712
711
  const layout = computeCellLayout(t, s.showCaption, hasBadge);
@@ -736,7 +735,7 @@ async function printQrLabelSheet(data, variantId, rawSettings) {
736
735
  const tail = cells.length % perPage;
737
736
  if (tail > 0)
738
737
  for (let i = 0; i < perPage - tail; i++) cells.push({ html: "" });
739
- openSheetWindow(
738
+ return openSheetWindow(
740
739
  t,
741
740
  renderPages(t, cells),
742
741
  "QR labels",
@@ -1136,7 +1135,8 @@ function visibleSettings(settings, variantId) {
1136
1135
  function PrintOptionsContent({
1137
1136
  printer,
1138
1137
  data,
1139
- onClose
1138
+ onClose,
1139
+ onPrinted
1140
1140
  }) {
1141
1141
  const [selectedVariant, setSelectedVariant] = (0, import_react.useState)(
1142
1142
  printer.variants[0]?.id ?? ""
@@ -1160,7 +1160,8 @@ function PrintOptionsContent({
1160
1160
  setIsPrinting(true);
1161
1161
  try {
1162
1162
  const settings = useDefaults ? buildDefaultSettings(allSettings) : settingValues;
1163
- await printer.print(data, variantId, settings);
1163
+ const outcome = await printer.print(data, variantId, settings);
1164
+ onPrinted?.(outcome);
1164
1165
  } finally {
1165
1166
  setIsPrinting(false);
1166
1167
  onClose();
@@ -1388,7 +1389,8 @@ function PrintOptionsDialog({
1388
1389
  printer,
1389
1390
  data,
1390
1391
  open,
1391
- onOpenChange
1392
+ onOpenChange,
1393
+ onPrinted
1392
1394
  }) {
1393
1395
  const isMobile = useIsMobile();
1394
1396
  const handleClose = () => onOpenChange(false);
@@ -1415,7 +1417,8 @@ function PrintOptionsDialog({
1415
1417
  {
1416
1418
  printer,
1417
1419
  data,
1418
- onClose: handleClose
1420
+ onClose: handleClose,
1421
+ onPrinted
1419
1422
  }
1420
1423
  ) })
1421
1424
  ] }) });
@@ -1433,18 +1436,20 @@ function PrintOptionsDialog({
1433
1436
  {
1434
1437
  printer,
1435
1438
  data,
1436
- onClose: handleClose
1439
+ onClose: handleClose,
1440
+ onPrinted
1437
1441
  }
1438
1442
  ) })
1439
1443
  ] }) });
1440
1444
  }
1441
- function usePrintOptions(printer, data) {
1445
+ function usePrintOptions(printer, data, onPrinted) {
1442
1446
  const [open, setOpen] = (0, import_react.useState)(false);
1443
1447
  const triggerPrint = async () => {
1444
1448
  if (!printer) return;
1445
1449
  const hasOptions = printer.variants.length > 0 || (printer.settings?.length ?? 0) > 0;
1446
1450
  if (!hasOptions) {
1447
- await printer.print(data);
1451
+ const outcome = await printer.print(data);
1452
+ onPrinted?.(outcome);
1448
1453
  } else {
1449
1454
  setOpen(true);
1450
1455
  }