react-shopwave-connect 0.4.4 → 0.4.5

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.
@@ -539,6 +539,35 @@ function coverCrop(srcWidth, srcHeight, dstWidth, dstHeight) {
539
539
  function requireBrowserImages(fn) {
540
540
  if (typeof createImageBitmap !== "function") throw new Error(`${fn} needs a browser (createImageBitmap is not available)`);
541
541
  }
542
+ /** Draws `crop` of the bitmap at `width × height` and encodes it (one resample). */
543
+ async function drawCrop(bitmap, crop, width, height, type, quality, fn) {
544
+ const { sx, sy, sw, sh } = crop;
545
+ if (typeof OffscreenCanvas === "function") {
546
+ const canvas$1 = new OffscreenCanvas(width, height);
547
+ const ctx$1 = canvas$1.getContext("2d");
548
+ if (!ctx$1) throw new Error(`${fn}: no 2D canvas context`);
549
+ ctx$1.imageSmoothingQuality = "high";
550
+ ctx$1.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
551
+ return canvas$1.convertToBlob({
552
+ type,
553
+ quality
554
+ });
555
+ }
556
+ const canvas = document.createElement("canvas");
557
+ canvas.width = width;
558
+ canvas.height = height;
559
+ const ctx = canvas.getContext("2d");
560
+ if (!ctx) throw new Error(`${fn}: no 2D canvas context`);
561
+ ctx.imageSmoothingQuality = "high";
562
+ ctx.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
563
+ return new Promise((resolve, reject) => canvas.toBlob((b) => b ? resolve(b) : reject(/* @__PURE__ */ new Error(`${fn}: encoding failed`)), type, quality));
564
+ }
565
+ /** The input's name with the output type's extension. */
566
+ function outputName(file, type) {
567
+ const name = file.name || "image";
568
+ const ext = type === "image/jpeg" ? "jpg" : type.split("/")[1];
569
+ return `${name.replace(/\.[^.]+$/, "")}.${ext}`;
570
+ }
542
571
  /**
543
572
  * Returns the image at exactly `width × height`: scaled and centre-cropped
544
573
  * (never stretched). Browser only. The result is a `File` named like the
@@ -547,34 +576,11 @@ function requireBrowserImages(fn) {
547
576
  async function fitImageToSize(file, width, height, options = {}) {
548
577
  requireBrowserImages("fitImageToSize");
549
578
  const { type = "image/png", quality, keepIfExact = true } = options;
550
- const name = file.name || "image";
551
- const ext = type === "image/jpeg" ? "jpg" : type.split("/")[1];
552
- const outName = `${name.replace(/\.[^.]+$/, "")}.${ext}`;
579
+ const outName = outputName(file, type);
553
580
  const bitmap = await createImageBitmap(file);
554
581
  try {
555
582
  if (keepIfExact && bitmap.width === width && bitmap.height === height && file.type === type) return file instanceof File ? file : new File([file], outName, { type });
556
- const { sx, sy, sw, sh } = coverCrop(bitmap.width, bitmap.height, width, height);
557
- let blob;
558
- if (typeof OffscreenCanvas === "function") {
559
- const canvas = new OffscreenCanvas(width, height);
560
- const ctx = canvas.getContext("2d");
561
- if (!ctx) throw new Error("fitImageToSize: no 2D canvas context");
562
- ctx.imageSmoothingQuality = "high";
563
- ctx.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
564
- blob = await canvas.convertToBlob({
565
- type,
566
- quality
567
- });
568
- } else {
569
- const canvas = document.createElement("canvas");
570
- canvas.width = width;
571
- canvas.height = height;
572
- const ctx = canvas.getContext("2d");
573
- if (!ctx) throw new Error("fitImageToSize: no 2D canvas context");
574
- ctx.imageSmoothingQuality = "high";
575
- ctx.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
576
- blob = await new Promise((resolve, reject) => canvas.toBlob((b) => b ? resolve(b) : reject(/* @__PURE__ */ new Error("fitImageToSize: encoding failed")), type, quality));
577
- }
583
+ const blob = await drawCrop(bitmap, coverCrop(bitmap.width, bitmap.height, width, height), width, height, type, quality, "fitImageToSize");
578
584
  return new File([blob], outName, { type });
579
585
  } finally {
580
586
  bitmap.close?.();