react-shopwave-connect 0.4.4 → 0.4.6
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/README.md +35 -10
- package/dist/core/index.cjs +222 -32
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +117 -20
- package/dist/core/index.d.ts +117 -20
- package/dist/core/index.js +217 -33
- package/dist/core/index.js.map +1 -1
- package/dist/hooks/index.cjs +34 -28
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.d.cts +6 -6
- package/dist/hooks/index.d.ts +6 -6
- package/dist/hooks/index.js +34 -28
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.cjs +222 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +117 -20
- package/dist/index.d.ts +117 -20
- package/dist/index.js +217 -33
- package/dist/index.js.map +1 -1
- package/dist/next/index.cjs +2 -2
- package/dist/next/index.cjs.map +1 -1
- package/dist/next/index.d.cts +3 -3
- package/dist/next/index.d.ts +3 -3
- package/dist/next/index.js +2 -2
- package/dist/next/index.js.map +1 -1
- package/dist/server/index.cjs +2 -2
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +5 -5
- package/dist/server/index.d.ts +5 -5
- package/dist/server/index.js +2 -2
- package/dist/server/index.js.map +1 -1
- package/package.json +5 -1
package/dist/hooks/index.cjs
CHANGED
|
@@ -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
|
|
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
|
|
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?.();
|
|
@@ -593,7 +599,7 @@ const STATIC_HOST = /^https?:\/\/static\.merchantstack\.com\/images\//i;
|
|
|
593
599
|
/**
|
|
594
600
|
* Turns an image path from the API into a URL a page can load over HTTPS:
|
|
595
601
|
* `http://static.merchantstack.com/images/x.png` → `<base>/x.png`, and a
|
|
596
|
-
* relative path (`merchant/
|
|
602
|
+
* relative path (`merchant/123/logo/x.png`) → `<base>/merchant/123/logo/x.png`.
|
|
597
603
|
* Other absolute URLs are returned unchanged; empty input gives `""`.
|
|
598
604
|
*/
|
|
599
605
|
function getImageUrl(path, base = SHOPWAVE_IMAGE_BASE_URL) {
|
|
@@ -639,7 +645,7 @@ async function uploadImage(file, params, options = {}) {
|
|
|
639
645
|
//#endregion
|
|
640
646
|
//#region src/core/merchantImages.ts
|
|
641
647
|
/**
|
|
642
|
-
* Where each slot lives, its upload kind and its exact size (from
|
|
648
|
+
* Where each slot lives, its upload kind and its exact size (from the existing admin's
|
|
643
649
|
* merchant settings page).
|
|
644
650
|
*/
|
|
645
651
|
const MERCHANT_IMAGE_SLOTS = Object.freeze({
|
|
@@ -689,7 +695,7 @@ function toStored(value) {
|
|
|
689
695
|
}
|
|
690
696
|
/**
|
|
691
697
|
* The `imageIds` to send on a save: every URL is turned back into its file
|
|
692
|
-
* name (what
|
|
698
|
+
* name (what the existing admin does with images you didn't change) and empty values
|
|
693
699
|
* are dropped. No images gives `{}`.
|
|
694
700
|
*/
|
|
695
701
|
function toStoredImageIds(imageIds) {
|