@builder.io/sdk-svelte 0.0.1-22 → 0.0.1-23
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/package.json +1 -1
- package/src/blocks/columns/component-info.js +25 -1
- package/src/blocks/embed/component-info.js +1 -1
- package/src/blocks/image/component-info.js +47 -1
- package/src/blocks/image/image.helpers.js +48 -0
- package/src/blocks/image/image.svelte +41 -5
- package/src/components/render-blocks.svelte +1 -1
package/package.json
CHANGED
|
@@ -186,7 +186,31 @@ const componentInfo = {
|
|
|
186
186
|
]
|
|
187
187
|
}
|
|
188
188
|
],
|
|
189
|
-
onChange
|
|
189
|
+
onChange(options) {
|
|
190
|
+
function clearWidths() {
|
|
191
|
+
columns.forEach((col) => {
|
|
192
|
+
col.delete("width");
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
const columns = options.get("columns");
|
|
196
|
+
if (Array.isArray(columns)) {
|
|
197
|
+
const containsColumnWithWidth = !!columns.find((col) => col.get("width"));
|
|
198
|
+
if (containsColumnWithWidth) {
|
|
199
|
+
const containsColumnWithoutWidth = !!columns.find((col) => !col.get("width"));
|
|
200
|
+
if (containsColumnWithoutWidth) {
|
|
201
|
+
clearWidths();
|
|
202
|
+
} else {
|
|
203
|
+
const sumWidths = columns.reduce((memo, col) => {
|
|
204
|
+
return memo + col.get("width");
|
|
205
|
+
}, 0);
|
|
206
|
+
const widthsDontAddUp = sumWidths !== 100;
|
|
207
|
+
if (widthsDontAddUp) {
|
|
208
|
+
clearWidths();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
190
214
|
},
|
|
191
215
|
{
|
|
192
216
|
name: "space",
|
|
@@ -9,7 +9,7 @@ const componentInfo = {
|
|
|
9
9
|
required: true,
|
|
10
10
|
defaultValue: "",
|
|
11
11
|
helperText: "e.g. enter a youtube url, google map, etc",
|
|
12
|
-
onChange
|
|
12
|
+
onChange(options) {
|
|
13
13
|
const url = options.get("url");
|
|
14
14
|
if (url) {
|
|
15
15
|
options.set("content", "Loading...");
|
|
@@ -18,7 +18,53 @@ const componentInfo = {
|
|
|
18
18
|
allowedFileTypes: ["jpeg", "jpg", "png", "svg"],
|
|
19
19
|
required: true,
|
|
20
20
|
defaultValue: "https://cdn.builder.io/api/v1/image/assets%2Fpwgjf0RoYWbdnJSbpBAjXNRMe9F2%2Ffb27a7c790324294af8be1c35fe30f4d",
|
|
21
|
-
onChange
|
|
21
|
+
onChange(options) {
|
|
22
|
+
const DEFAULT_ASPECT_RATIO = 0.7041;
|
|
23
|
+
options.delete("srcset");
|
|
24
|
+
options.delete("noWebp");
|
|
25
|
+
function loadImage(url, timeout = 6e4) {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const img = document.createElement("img");
|
|
28
|
+
let loaded = false;
|
|
29
|
+
img.onload = () => {
|
|
30
|
+
loaded = true;
|
|
31
|
+
resolve(img);
|
|
32
|
+
};
|
|
33
|
+
img.addEventListener("error", (event) => {
|
|
34
|
+
console.warn("Image load failed", event.error);
|
|
35
|
+
reject(event.error);
|
|
36
|
+
});
|
|
37
|
+
img.src = url;
|
|
38
|
+
setTimeout(() => {
|
|
39
|
+
if (!loaded) {
|
|
40
|
+
reject(new Error("Image load timed out"));
|
|
41
|
+
}
|
|
42
|
+
}, timeout);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function round(num) {
|
|
46
|
+
return Math.round(num * 1e3) / 1e3;
|
|
47
|
+
}
|
|
48
|
+
const value = options.get("image");
|
|
49
|
+
const aspectRatio = options.get("aspectRatio");
|
|
50
|
+
fetch(value).then((res) => res.blob()).then((blob) => {
|
|
51
|
+
if (blob.type.includes("svg")) {
|
|
52
|
+
options.set("noWebp", true);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
if (value && (!aspectRatio || aspectRatio === DEFAULT_ASPECT_RATIO)) {
|
|
56
|
+
return loadImage(value).then((img) => {
|
|
57
|
+
const possiblyUpdatedAspectRatio = options.get("aspectRatio");
|
|
58
|
+
if (options.get("image") === value && (!possiblyUpdatedAspectRatio || possiblyUpdatedAspectRatio === DEFAULT_ASPECT_RATIO)) {
|
|
59
|
+
if (img.width && img.height) {
|
|
60
|
+
options.set("aspectRatio", round(img.height / img.width));
|
|
61
|
+
options.set("height", img.height);
|
|
62
|
+
options.set("width", img.width);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
22
68
|
},
|
|
23
69
|
{
|
|
24
70
|
name: "backgroundSize",
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
function removeProtocol(path) {
|
|
2
|
+
return path.replace(/http(s)?:/, "");
|
|
3
|
+
}
|
|
4
|
+
function updateQueryParam(uri = "", key, value) {
|
|
5
|
+
const re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
|
|
6
|
+
const separator = uri.indexOf("?") !== -1 ? "&" : "?";
|
|
7
|
+
if (uri.match(re)) {
|
|
8
|
+
return uri.replace(re, "$1" + key + "=" + encodeURIComponent(value) + "$2");
|
|
9
|
+
}
|
|
10
|
+
return uri + separator + key + "=" + encodeURIComponent(value);
|
|
11
|
+
}
|
|
12
|
+
function getShopifyImageUrl(src, size) {
|
|
13
|
+
if (!src || !(src == null ? void 0 : src.match(/cdn\.shopify\.com/)) || !size) {
|
|
14
|
+
return src;
|
|
15
|
+
}
|
|
16
|
+
if (size === "master") {
|
|
17
|
+
return removeProtocol(src);
|
|
18
|
+
}
|
|
19
|
+
const match = src.match(/(_\d+x(\d+)?)?(\.(jpg|jpeg|gif|png|bmp|bitmap|tiff|tif)(\?v=\d+)?)/i);
|
|
20
|
+
if (match) {
|
|
21
|
+
const prefix = src.split(match[0]);
|
|
22
|
+
const suffix = match[3];
|
|
23
|
+
const useSize = size.match("x") ? size : `${size}x`;
|
|
24
|
+
return removeProtocol(`${prefix[0]}_${useSize}${suffix}`);
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
function getSrcSet(url) {
|
|
29
|
+
if (!url) {
|
|
30
|
+
return url;
|
|
31
|
+
}
|
|
32
|
+
const sizes = [100, 200, 400, 800, 1200, 1600, 2e3];
|
|
33
|
+
if (url.match(/builder\.io/)) {
|
|
34
|
+
let srcUrl = url;
|
|
35
|
+
const widthInSrc = Number(url.split("?width=")[1]);
|
|
36
|
+
if (!isNaN(widthInSrc)) {
|
|
37
|
+
srcUrl = `${srcUrl} ${widthInSrc}w`;
|
|
38
|
+
}
|
|
39
|
+
return sizes.filter((size) => size !== widthInSrc).map((size) => `${updateQueryParam(url, "width", size)} ${size}w`).concat([srcUrl]).join(", ");
|
|
40
|
+
}
|
|
41
|
+
if (url.match(/cdn\.shopify\.com/)) {
|
|
42
|
+
return sizes.map((size) => [getShopifyImageUrl(url, `${size}x${size}`), size]).filter(([sizeUrl]) => !!sizeUrl).map(([sizeUrl, size]) => `${sizeUrl} ${size}w`).concat([url]).join(", ");
|
|
43
|
+
}
|
|
44
|
+
return url;
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
getSrcSet
|
|
48
|
+
};
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
<script>
|
|
2
|
+
import { getSrcSet } from "./image.helpers";
|
|
3
|
+
|
|
4
|
+
export let image;
|
|
5
|
+
export let src;
|
|
6
|
+
export let srcset;
|
|
7
|
+
export let noWebp;
|
|
2
8
|
export let altText;
|
|
3
9
|
export let backgroundSize;
|
|
4
10
|
export let className;
|
|
5
|
-
export let image;
|
|
6
|
-
export let srcset;
|
|
7
11
|
export let sizes;
|
|
8
12
|
export let aspectRatio;
|
|
9
13
|
export let fitContent;
|
|
@@ -14,10 +18,41 @@
|
|
|
14
18
|
node.style[p] = v;
|
|
15
19
|
});
|
|
16
20
|
}
|
|
21
|
+
|
|
22
|
+
$: srcSetToUse = () => {
|
|
23
|
+
const imageToUse = image || src;
|
|
24
|
+
const url = imageToUse;
|
|
25
|
+
|
|
26
|
+
if (
|
|
27
|
+
!url || // We can auto add srcset for cdn.builder.io and shopify
|
|
28
|
+
// images, otherwise you can supply this prop manually
|
|
29
|
+
!(url.match(/builder\.io/) || url.match(/cdn\.shopify\.com/))
|
|
30
|
+
) {
|
|
31
|
+
return srcset;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (srcset && image?.includes("builder.io/api/v1/image")) {
|
|
35
|
+
if (!srcset.includes(image.split("?")[0])) {
|
|
36
|
+
console.debug("Removed given srcset");
|
|
37
|
+
return getSrcSet(url);
|
|
38
|
+
}
|
|
39
|
+
} else if (image && !srcset) {
|
|
40
|
+
return getSrcSet(url);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return getSrcSet(url);
|
|
44
|
+
};
|
|
17
45
|
</script>
|
|
18
46
|
|
|
19
47
|
<div class="div">
|
|
20
48
|
<picture>
|
|
49
|
+
{#if srcSetToUse?.()?.match(/builder\.io/) && !noWebp}
|
|
50
|
+
<source
|
|
51
|
+
type="image/webp"
|
|
52
|
+
srcset={srcSetToUse?.()?.replace(/\?/g, "?format=webp&")}
|
|
53
|
+
/>
|
|
54
|
+
{/if}
|
|
55
|
+
|
|
21
56
|
<img
|
|
22
57
|
use:mitosis_styling={{
|
|
23
58
|
objectPosition: backgroundSize || "center",
|
|
@@ -26,18 +61,19 @@
|
|
|
26
61
|
loading="lazy"
|
|
27
62
|
alt={altText}
|
|
28
63
|
role={altText ? "presentation" : undefined}
|
|
29
|
-
class={"builder-image" + (className ? " " + className : "") + "img"}
|
|
64
|
+
class={"builder-image" + (className ? " " + className : "") + " img"}
|
|
30
65
|
src={image}
|
|
31
|
-
{
|
|
66
|
+
srcset={srcSetToUse()}
|
|
32
67
|
{sizes}
|
|
33
68
|
/>
|
|
34
69
|
|
|
35
|
-
<source {
|
|
70
|
+
<source srcset={srcSetToUse()} />
|
|
36
71
|
</picture>
|
|
37
72
|
|
|
38
73
|
{#if aspectRatio && !(fitContent && builderBlock?.children?.length)}
|
|
39
74
|
<div
|
|
40
75
|
use:mitosis_styling={{
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
41
77
|
paddingTop: aspectRatio * 100 + "%",
|
|
42
78
|
}}
|
|
43
79
|
class="builder-image-sizer div-2"
|