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.
@@ -515,6 +515,35 @@ function coverCrop(srcWidth, srcHeight, dstWidth, dstHeight) {
515
515
  function requireBrowserImages(fn) {
516
516
  if (typeof createImageBitmap !== "function") throw new Error(`${fn} needs a browser (createImageBitmap is not available)`);
517
517
  }
518
+ /** Draws `crop` of the bitmap at `width × height` and encodes it (one resample). */
519
+ async function drawCrop(bitmap, crop, width, height, type, quality, fn) {
520
+ const { sx, sy, sw, sh } = crop;
521
+ if (typeof OffscreenCanvas === "function") {
522
+ const canvas$1 = new OffscreenCanvas(width, height);
523
+ const ctx$1 = canvas$1.getContext("2d");
524
+ if (!ctx$1) throw new Error(`${fn}: no 2D canvas context`);
525
+ ctx$1.imageSmoothingQuality = "high";
526
+ ctx$1.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
527
+ return canvas$1.convertToBlob({
528
+ type,
529
+ quality
530
+ });
531
+ }
532
+ const canvas = document.createElement("canvas");
533
+ canvas.width = width;
534
+ canvas.height = height;
535
+ const ctx = canvas.getContext("2d");
536
+ if (!ctx) throw new Error(`${fn}: no 2D canvas context`);
537
+ ctx.imageSmoothingQuality = "high";
538
+ ctx.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
539
+ return new Promise((resolve, reject) => canvas.toBlob((b) => b ? resolve(b) : reject(/* @__PURE__ */ new Error(`${fn}: encoding failed`)), type, quality));
540
+ }
541
+ /** The input's name with the output type's extension. */
542
+ function outputName(file, type) {
543
+ const name = file.name || "image";
544
+ const ext = type === "image/jpeg" ? "jpg" : type.split("/")[1];
545
+ return `${name.replace(/\.[^.]+$/, "")}.${ext}`;
546
+ }
518
547
  /**
519
548
  * Returns the image at exactly `width × height`: scaled and centre-cropped
520
549
  * (never stretched). Browser only. The result is a `File` named like the
@@ -523,34 +552,11 @@ function requireBrowserImages(fn) {
523
552
  async function fitImageToSize(file, width, height, options = {}) {
524
553
  requireBrowserImages("fitImageToSize");
525
554
  const { type = "image/png", quality, keepIfExact = true } = options;
526
- const name = file.name || "image";
527
- const ext = type === "image/jpeg" ? "jpg" : type.split("/")[1];
528
- const outName = `${name.replace(/\.[^.]+$/, "")}.${ext}`;
555
+ const outName = outputName(file, type);
529
556
  const bitmap = await createImageBitmap(file);
530
557
  try {
531
558
  if (keepIfExact && bitmap.width === width && bitmap.height === height && file.type === type) return file instanceof File ? file : new File([file], outName, { type });
532
- const { sx, sy, sw, sh } = coverCrop(bitmap.width, bitmap.height, width, height);
533
- let blob;
534
- if (typeof OffscreenCanvas === "function") {
535
- const canvas = new OffscreenCanvas(width, height);
536
- const ctx = canvas.getContext("2d");
537
- if (!ctx) throw new Error("fitImageToSize: no 2D canvas context");
538
- ctx.imageSmoothingQuality = "high";
539
- ctx.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
540
- blob = await canvas.convertToBlob({
541
- type,
542
- quality
543
- });
544
- } else {
545
- const canvas = document.createElement("canvas");
546
- canvas.width = width;
547
- canvas.height = height;
548
- const ctx = canvas.getContext("2d");
549
- if (!ctx) throw new Error("fitImageToSize: no 2D canvas context");
550
- ctx.imageSmoothingQuality = "high";
551
- ctx.drawImage(bitmap, sx, sy, sw, sh, 0, 0, width, height);
552
- blob = await new Promise((resolve, reject) => canvas.toBlob((b) => b ? resolve(b) : reject(/* @__PURE__ */ new Error("fitImageToSize: encoding failed")), type, quality));
553
- }
559
+ const blob = await drawCrop(bitmap, coverCrop(bitmap.width, bitmap.height, width, height), width, height, type, quality, "fitImageToSize");
554
560
  return new File([blob], outName, { type });
555
561
  } finally {
556
562
  bitmap.close?.();