@builder.io/sdk-solid 0.4.5 → 0.5.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/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/src/blocks/BaseText.jsx +1 -1
- package/src/blocks/button/button.jsx +5 -3
- package/src/blocks/button/component-info.js +16 -22
- package/src/blocks/columns/columns.jsx +12 -21
- package/src/blocks/columns/component-info.js +203 -226
- package/src/blocks/custom-code/component-info.js +19 -25
- package/src/blocks/embed/component-info.js +31 -37
- package/src/blocks/embed/helpers.js +3 -9
- package/src/blocks/form/component-info.js +174 -212
- package/src/blocks/form/form.jsx +1 -268
- package/src/blocks/fragment/component-info.js +1 -3
- package/src/blocks/helpers.js +27 -0
- package/src/blocks/image/component-info.js +105 -133
- package/src/blocks/image/image.helpers.js +3 -5
- package/src/blocks/img/component-info.js +8 -12
- package/src/blocks/img/img.jsx +2 -0
- package/src/blocks/input/component-info.js +29 -57
- package/src/blocks/input/input.jsx +2 -0
- package/src/blocks/raw-text/component-info.js +7 -11
- package/src/blocks/raw-text/raw-text.jsx +2 -2
- package/src/blocks/section/component-info.js +24 -31
- package/src/blocks/section/section.jsx +3 -0
- package/src/blocks/select/component-info.js +34 -48
- package/src/blocks/select/select.jsx +2 -0
- package/src/blocks/submit-button/component-info.js +6 -10
- package/src/blocks/submit-button/submit-button.jsx +3 -1
- package/src/blocks/symbol/component-info.js +30 -37
- package/src/blocks/symbol/symbol.helpers.js +60 -0
- package/src/blocks/symbol/symbol.jsx +31 -68
- package/src/blocks/text/component-info.js +10 -13
- package/src/blocks/text/text.jsx +1 -1
- package/src/blocks/textarea/component-info.js +22 -30
- package/src/blocks/textarea/textarea.jsx +3 -0
- package/src/blocks/video/component-info.js +74 -96
- package/src/blocks/video/video.jsx +1 -0
- package/src/components/{render-block/render-block.helpers.js → block/block.helpers.js} +26 -44
- package/src/components/{render-block/render-block.jsx → block/block.jsx} +65 -80
- package/src/components/{render-block → block/components}/block-styles.jsx +16 -16
- package/src/components/block/components/block-wrapper.jsx +50 -0
- package/src/components/block/components/component-ref/component-ref.helpers.js +41 -0
- package/src/components/block/components/component-ref/component-ref.jsx +58 -0
- package/src/components/block/components/interactive-element.jsx +30 -0
- package/src/components/block/components/repeated-block.jsx +20 -0
- package/src/components/blocks/blocks-wrapper.jsx +66 -0
- package/src/components/blocks/blocks.jsx +57 -0
- package/src/components/{render-content/render-content.jsx → content/components/enable-editor.jsx} +86 -175
- package/src/components/{render-content/components/render-styles.helpers.js → content/components/styles.helpers.js} +6 -7
- package/src/components/{render-content/components/render-styles.jsx → content/components/styles.jsx} +4 -4
- package/src/components/{render-content/render-content.helpers.js → content/content.helpers.js} +15 -15
- package/src/components/content/content.jsx +136 -0
- package/src/components/content/index.js +2 -0
- package/src/components/content/wrap-component-ref.js +2 -0
- package/src/components/{render-content-variants/render-content-variants.jsx → content-variants/content-variants.jsx} +19 -22
- package/src/components/{render-content-variants → content-variants}/helpers.js +28 -40
- package/src/constants/builder-registered-components.js +34 -25
- package/src/constants/device-sizes.js +6 -6
- package/src/constants/sdk-version.js +1 -1
- package/src/context/builder.context.js +1 -1
- package/src/context/components.context.js +5 -0
- package/src/functions/apply-patch-with-mutation.js +66 -0
- package/src/functions/camel-to-kebab-case.js +2 -4
- package/src/functions/evaluate/acorn.js +1595 -0
- package/src/functions/{evaluate.js → evaluate/evaluate.js} +36 -10
- package/src/functions/evaluate/index.js +2 -0
- package/src/functions/evaluate/interpreter.js +2801 -0
- package/src/functions/evaluate/non-node-runtime.js +92 -0
- package/src/functions/evaluate/types.js +0 -0
- package/src/functions/event-handler-name.js +2 -4
- package/src/functions/extract-text-styles.js +4 -12
- package/src/functions/fast-clone.js +2 -4
- package/src/functions/get-block-actions-handler.js +3 -5
- package/src/functions/get-block-actions.js +15 -4
- package/src/functions/get-block-component-options.js +11 -12
- package/src/functions/get-block-properties.js +29 -19
- package/src/functions/get-builder-search-params/index.js +5 -10
- package/src/functions/get-content/generate-content-url.js +17 -19
- package/src/functions/get-content/index.js +43 -29
- package/src/functions/get-fetch.js +1 -3
- package/src/functions/get-global-this.js +1 -3
- package/src/functions/get-processed-block.js +12 -13
- package/src/functions/get-react-native-block-styles.js +11 -12
- package/src/functions/if-target.js +1 -3
- package/src/functions/is-browser.js +1 -3
- package/src/functions/is-editing.js +1 -3
- package/src/functions/is-iframe.js +1 -3
- package/src/functions/is-non-node-server.js +9 -0
- package/src/functions/is-previewing.js +1 -3
- package/src/functions/on-change.js +1 -4
- package/src/functions/register-component.js +34 -42
- package/src/functions/register.js +1 -3
- package/src/functions/sanitize-react-native-block-styles.js +22 -17
- package/src/functions/set-editor-settings.js +1 -3
- package/src/functions/set.js +1 -3
- package/src/functions/track/helpers.js +3 -5
- package/src/functions/track/index.js +45 -43
- package/src/functions/track/interaction.js +11 -7
- package/src/functions/transform-block-properties.js +1 -3
- package/src/functions/transform-block.js +1 -3
- package/src/helpers/ab-tests.js +45 -28
- package/src/helpers/canTrack.js +3 -5
- package/src/helpers/cookie.js +15 -24
- package/src/helpers/css.js +3 -7
- package/src/helpers/flatten.js +15 -18
- package/src/helpers/localStorage.js +1 -4
- package/src/helpers/logger.js +1 -3
- package/src/helpers/nullable.js +2 -4
- package/src/helpers/preview-lru-cache/get.js +8 -0
- package/src/helpers/preview-lru-cache/helpers.js +10 -0
- package/src/helpers/preview-lru-cache/init.js +10 -0
- package/src/helpers/preview-lru-cache/set.js +35 -0
- package/src/helpers/preview-lru-cache/types.js +0 -0
- package/src/helpers/sessionId.js +14 -11
- package/src/helpers/time.js +1 -3
- package/src/helpers/url.js +2 -4
- package/src/helpers/uuid.js +4 -6
- package/src/helpers/visitorId.js +8 -7
- package/src/index-helpers/blocks-exports.js +3 -14
- package/src/index-helpers/top-of-file.js +1 -3
- package/src/index.js +2 -17
- package/src/scripts/init-editing.js +62 -48
- package/src/types/api-version.js +1 -3
- package/src/types/builder-props.js +0 -0
- package/src/blocks/util.js +0 -8
- package/src/components/render-block/render-component.jsx +0 -40
- package/src/components/render-block/render-repeated-block.jsx +0 -16
- package/src/components/render-blocks.jsx +0 -100
- package/src/components/render-content/builder-editing.jsx +0 -5
- package/src/components/render-content/index.js +0 -4
- package/src/components/render-content/wrap-component-ref.js +0 -4
- package/src/functions/evaluate.test.js +0 -17
- package/src/functions/get-builder-search-params/fn.test.js +0 -13
- package/src/functions/get-content/generate-content-url.test.js +0 -97
- package/src/functions/get-processed-block.test.js +0 -34
- package/src/functions/on-change.test.js +0 -19
- package/src/functions/set.test.js +0 -16
- package/src/helpers/url.test.js +0 -21
- /package/src/components/{render-block → block}/types.js +0 -0
- /package/src/components/{render-content/render-content.types.js → content/content.types.js} +0 -0
- /package/src/components/{render-content-variants/render-content-variants.types.js → content-variants/content-variants.types.js} +0 -0
package/src/blocks/form/form.jsx
CHANGED
|
@@ -1,272 +1,5 @@
|
|
|
1
|
-
import { useContext, Show, For, createSignal } from "solid-js";
|
|
2
|
-
|
|
3
|
-
import { css } from "solid-styled-components";
|
|
4
|
-
|
|
5
|
-
import RenderBlock from "../../components/render-block/render-block.jsx";
|
|
6
|
-
import BuilderBlocks from "../../components/render-blocks.jsx";
|
|
7
|
-
import { isEditing } from "../../functions/is-editing.js";
|
|
8
|
-
|
|
9
1
|
function FormComponent(props) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const [responseData, setResponseData] = createSignal(null);
|
|
13
|
-
|
|
14
|
-
const [formErrorMessage, setFormErrorMessage] = createSignal("");
|
|
15
|
-
|
|
16
|
-
function submissionState() {
|
|
17
|
-
return (isEditing() && props.previewState) || formState();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function onSubmit(event) {
|
|
21
|
-
const sendWithJs = props.sendWithJs || props.sendSubmissionsTo === "email";
|
|
22
|
-
if (props.sendSubmissionsTo === "zapier") {
|
|
23
|
-
event.preventDefault();
|
|
24
|
-
} else if (sendWithJs) {
|
|
25
|
-
if (!(props.action || props.sendSubmissionsTo === "email")) {
|
|
26
|
-
event.preventDefault();
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
event.preventDefault();
|
|
30
|
-
const el = event.currentTarget;
|
|
31
|
-
const headers = props.customHeaders || {};
|
|
32
|
-
let body;
|
|
33
|
-
const formData = new FormData(el);
|
|
34
|
-
|
|
35
|
-
// TODO: maybe support null
|
|
36
|
-
const formPairs = Array.from(
|
|
37
|
-
event.currentTarget.querySelectorAll("input,select,textarea")
|
|
38
|
-
)
|
|
39
|
-
.filter((el) => !!el.name)
|
|
40
|
-
.map((el) => {
|
|
41
|
-
let value;
|
|
42
|
-
const key = el.name;
|
|
43
|
-
if (el instanceof HTMLInputElement) {
|
|
44
|
-
if (el.type === "radio") {
|
|
45
|
-
if (el.checked) {
|
|
46
|
-
value = el.name;
|
|
47
|
-
return {
|
|
48
|
-
key,
|
|
49
|
-
value,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
} else if (el.type === "checkbox") {
|
|
53
|
-
value = el.checked;
|
|
54
|
-
} else if (el.type === "number" || el.type === "range") {
|
|
55
|
-
const num = el.valueAsNumber;
|
|
56
|
-
if (!isNaN(num)) {
|
|
57
|
-
value = num;
|
|
58
|
-
}
|
|
59
|
-
} else if (el.type === "file") {
|
|
60
|
-
// TODO: one vs multiple files
|
|
61
|
-
value = el.files;
|
|
62
|
-
} else {
|
|
63
|
-
value = el.value;
|
|
64
|
-
}
|
|
65
|
-
} else {
|
|
66
|
-
value = el.value;
|
|
67
|
-
}
|
|
68
|
-
return {
|
|
69
|
-
key,
|
|
70
|
-
value,
|
|
71
|
-
};
|
|
72
|
-
});
|
|
73
|
-
let contentType = props.contentType;
|
|
74
|
-
if (props.sendSubmissionsTo === "email") {
|
|
75
|
-
contentType = "multipart/form-data";
|
|
76
|
-
}
|
|
77
|
-
Array.from(formPairs).forEach(({ value }) => {
|
|
78
|
-
if (
|
|
79
|
-
value instanceof File ||
|
|
80
|
-
(Array.isArray(value) && value[0] instanceof File) ||
|
|
81
|
-
value instanceof FileList
|
|
82
|
-
) {
|
|
83
|
-
contentType = "multipart/form-data";
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// TODO: send as urlEncoded or multipart by default
|
|
88
|
-
// because of ease of use and reliability in browser API
|
|
89
|
-
// for encoding the form?
|
|
90
|
-
if (contentType !== "application/json") {
|
|
91
|
-
body = formData;
|
|
92
|
-
} else {
|
|
93
|
-
// Json
|
|
94
|
-
const json = {};
|
|
95
|
-
Array.from(formPairs).forEach(({ value, key }) => {
|
|
96
|
-
set(json, key, value);
|
|
97
|
-
});
|
|
98
|
-
body = JSON.stringify(json);
|
|
99
|
-
}
|
|
100
|
-
if (contentType && contentType !== "multipart/form-data") {
|
|
101
|
-
if (
|
|
102
|
-
/* Zapier doesn't allow content-type header to be sent from browsers */
|
|
103
|
-
!(sendWithJs && props.action?.includes("zapier.com"))
|
|
104
|
-
) {
|
|
105
|
-
headers["content-type"] = contentType;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
const presubmitEvent = new CustomEvent("presubmit", {
|
|
109
|
-
detail: {
|
|
110
|
-
body,
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
if (formRef) {
|
|
114
|
-
formRef.dispatchEvent(presubmitEvent);
|
|
115
|
-
if (presubmitEvent.defaultPrevented) {
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
setFormState("sending");
|
|
120
|
-
const formUrl = `${
|
|
121
|
-
builder.env === "dev" ? "http://localhost:5000" : "https://builder.io"
|
|
122
|
-
}/api/v1/form-submit?apiKey=${builder.apiKey}&to=${btoa(
|
|
123
|
-
props.sendSubmissionsToEmail || ""
|
|
124
|
-
)}&name=${encodeURIComponent(props.name || "")}`;
|
|
125
|
-
fetch(
|
|
126
|
-
props.sendSubmissionsTo === "email"
|
|
127
|
-
? formUrl
|
|
128
|
-
: props.action /* TODO: throw error if no action URL */,
|
|
129
|
-
{
|
|
130
|
-
body,
|
|
131
|
-
headers,
|
|
132
|
-
method: props.method || "post",
|
|
133
|
-
}
|
|
134
|
-
).then(
|
|
135
|
-
async (res) => {
|
|
136
|
-
let body;
|
|
137
|
-
const contentType = res.headers.get("content-type");
|
|
138
|
-
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
139
|
-
body = await res.json();
|
|
140
|
-
} else {
|
|
141
|
-
body = await res.text();
|
|
142
|
-
}
|
|
143
|
-
if (!res.ok && props.errorMessagePath) {
|
|
144
|
-
/* TODO: allow supplying an error formatter function */
|
|
145
|
-
let message = get(body, props.errorMessagePath);
|
|
146
|
-
if (message) {
|
|
147
|
-
if (typeof message !== "string") {
|
|
148
|
-
/* TODO: ideally convert json to yaml so it woul dbe like
|
|
149
|
-
error: - email has been taken */
|
|
150
|
-
message = JSON.stringify(message);
|
|
151
|
-
}
|
|
152
|
-
setFormErrorMessage(message);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
setResponseData(body);
|
|
156
|
-
setFormState(res.ok ? "success" : "error");
|
|
157
|
-
if (res.ok) {
|
|
158
|
-
const submitSuccessEvent = new CustomEvent("submit:success", {
|
|
159
|
-
detail: {
|
|
160
|
-
res,
|
|
161
|
-
body,
|
|
162
|
-
},
|
|
163
|
-
});
|
|
164
|
-
if (formRef) {
|
|
165
|
-
formRef.dispatchEvent(submitSuccessEvent);
|
|
166
|
-
if (submitSuccessEvent.defaultPrevented) {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
/* TODO: option to turn this on/off? */
|
|
170
|
-
if (props.resetFormOnSubmit !== false) {
|
|
171
|
-
formRef.reset();
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/* TODO: client side route event first that can be preventDefaulted */
|
|
176
|
-
if (props.successUrl) {
|
|
177
|
-
if (formRef) {
|
|
178
|
-
const event = new CustomEvent("route", {
|
|
179
|
-
detail: {
|
|
180
|
-
url: props.successUrl,
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
formRef.dispatchEvent(event);
|
|
184
|
-
if (!event.defaultPrevented) {
|
|
185
|
-
location.href = props.successUrl;
|
|
186
|
-
}
|
|
187
|
-
} else {
|
|
188
|
-
location.href = props.successUrl;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
},
|
|
193
|
-
(err) => {
|
|
194
|
-
const submitErrorEvent = new CustomEvent("submit:error", {
|
|
195
|
-
detail: {
|
|
196
|
-
error: err,
|
|
197
|
-
},
|
|
198
|
-
});
|
|
199
|
-
if (formRef) {
|
|
200
|
-
formRef.dispatchEvent(submitErrorEvent);
|
|
201
|
-
if (submitErrorEvent.defaultPrevented) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
setResponseData(err);
|
|
206
|
-
setFormState("error");
|
|
207
|
-
}
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
let formRef;
|
|
213
|
-
|
|
214
|
-
const builderContext = useContext(BuilderContext);
|
|
215
|
-
|
|
216
|
-
return (
|
|
217
|
-
<form
|
|
218
|
-
validate={props.validate}
|
|
219
|
-
ref={formRef}
|
|
220
|
-
action={!props.sendWithJs && props.action}
|
|
221
|
-
method={props.method}
|
|
222
|
-
name={props.name}
|
|
223
|
-
onSubmit={(event) => onSubmit(event)}
|
|
224
|
-
{...props.attributes}
|
|
225
|
-
>
|
|
226
|
-
<Show when={props.builderBlock && props.builderBlock.children}>
|
|
227
|
-
<For each={props.builderBlock?.children}>
|
|
228
|
-
{(block, _index) => {
|
|
229
|
-
const index = _index();
|
|
230
|
-
return (
|
|
231
|
-
<RenderBlock block={block} context={builderContext}></RenderBlock>
|
|
232
|
-
);
|
|
233
|
-
}}
|
|
234
|
-
</For>
|
|
235
|
-
</Show>
|
|
236
|
-
<Show when={submissionState() === "error"}>
|
|
237
|
-
<BuilderBlocks
|
|
238
|
-
dataPath="errorMessage"
|
|
239
|
-
blocks={props.errorMessage}
|
|
240
|
-
></BuilderBlocks>
|
|
241
|
-
</Show>
|
|
242
|
-
<Show when={submissionState() === "sending"}>
|
|
243
|
-
<BuilderBlocks
|
|
244
|
-
dataPath="sendingMessage"
|
|
245
|
-
blocks={props.sendingMessage}
|
|
246
|
-
></BuilderBlocks>
|
|
247
|
-
</Show>
|
|
248
|
-
<Show when={submissionState() === "error" && responseData()}>
|
|
249
|
-
<pre
|
|
250
|
-
class={
|
|
251
|
-
"builder-form-error-text " +
|
|
252
|
-
css({
|
|
253
|
-
padding: "10px",
|
|
254
|
-
color: "red",
|
|
255
|
-
textAlign: "center",
|
|
256
|
-
})
|
|
257
|
-
}
|
|
258
|
-
>
|
|
259
|
-
{JSON.stringify(responseData(), null, 2)}
|
|
260
|
-
</pre>
|
|
261
|
-
</Show>
|
|
262
|
-
<Show when={submissionState() === "success"}>
|
|
263
|
-
<BuilderBlocks
|
|
264
|
-
dataPath="successMessage"
|
|
265
|
-
blocks={props.successMessage}
|
|
266
|
-
></BuilderBlocks>
|
|
267
|
-
</Show>
|
|
268
|
-
</form>
|
|
269
|
-
);
|
|
2
|
+
return <></>;
|
|
270
3
|
}
|
|
271
4
|
|
|
272
5
|
export default FormComponent;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
function filterAttrs(attrs = {}, prefix, isEvent) {
|
|
2
|
+
const result = {};
|
|
3
|
+
for (const attr in attrs) {
|
|
4
|
+
if (!attrs[attr]) continue;
|
|
5
|
+
if (isEvent && !attr.startsWith(prefix)) continue;
|
|
6
|
+
const eventName = isEvent ? attr.replace(prefix, "") : attr;
|
|
7
|
+
result[eventName] = attrs[attr];
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
function setAttrs(node, attrs = {}) {
|
|
12
|
+
const attrKeys = Object.keys(attrs);
|
|
13
|
+
const setup = attr => node.addEventListener(attr, attrs[attr]);
|
|
14
|
+
const teardown = attr => node.removeEventListener(attr, attrs[attr]);
|
|
15
|
+
attrKeys.forEach(setup);
|
|
16
|
+
return {
|
|
17
|
+
update(attrs2 = {}) {
|
|
18
|
+
const attrKeys2 = Object.keys(attrs2);
|
|
19
|
+
attrKeys2.forEach(teardown);
|
|
20
|
+
attrKeys2.forEach(setup);
|
|
21
|
+
},
|
|
22
|
+
destroy() {
|
|
23
|
+
attrKeys.forEach(teardown);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export { filterAttrs, setAttrs }
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { serializeFn } from "../util.js";
|
|
2
1
|
const componentInfo = {
|
|
3
2
|
name: "Image",
|
|
4
3
|
static: true,
|
|
@@ -10,141 +9,114 @@ const componentInfo = {
|
|
|
10
9
|
overflow: "hidden"
|
|
11
10
|
},
|
|
12
11
|
canHaveChildren: true,
|
|
13
|
-
inputs: [
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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);
|
|
12
|
+
inputs: [{
|
|
13
|
+
name: "image",
|
|
14
|
+
type: "file",
|
|
15
|
+
bubble: true,
|
|
16
|
+
allowedFileTypes: ["jpeg", "jpg", "png", "svg"],
|
|
17
|
+
required: true,
|
|
18
|
+
defaultValue: "https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F72c80f114dc149019051b6852a9e3b7a",
|
|
19
|
+
onChange: options => {
|
|
20
|
+
const DEFAULT_ASPECT_RATIO = 0.7041;
|
|
21
|
+
options.delete("srcset");
|
|
22
|
+
options.delete("noWebp");
|
|
23
|
+
function loadImage(url, timeout = 6e4) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
const img = document.createElement("img");
|
|
26
|
+
let loaded = false;
|
|
27
|
+
img.onload = () => {
|
|
28
|
+
loaded = true;
|
|
29
|
+
resolve(img);
|
|
30
|
+
};
|
|
31
|
+
img.addEventListener("error", event => {
|
|
32
|
+
console.warn("Image load failed", event.error);
|
|
33
|
+
reject(event.error);
|
|
43
34
|
});
|
|
35
|
+
img.src = url;
|
|
36
|
+
setTimeout(() => {
|
|
37
|
+
if (!loaded) {
|
|
38
|
+
reject(new Error("Image load timed out"));
|
|
39
|
+
}
|
|
40
|
+
}, timeout);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function round(num) {
|
|
44
|
+
return Math.round(num * 1e3) / 1e3;
|
|
45
|
+
}
|
|
46
|
+
const value = options.get("image");
|
|
47
|
+
const aspectRatio = options.get("aspectRatio");
|
|
48
|
+
fetch(value).then(res => res.blob()).then(blob => {
|
|
49
|
+
if (blob.type.includes("svg")) {
|
|
50
|
+
options.set("noWebp", true);
|
|
44
51
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
});
|
|
53
|
+
if (value && (!aspectRatio || aspectRatio === DEFAULT_ASPECT_RATIO)) {
|
|
54
|
+
return loadImage(value).then(img => {
|
|
55
|
+
const possiblyUpdatedAspectRatio = options.get("aspectRatio");
|
|
56
|
+
if (options.get("image") === value && (!possiblyUpdatedAspectRatio || possiblyUpdatedAspectRatio === DEFAULT_ASPECT_RATIO)) {
|
|
57
|
+
if (img.width && img.height) {
|
|
58
|
+
options.set("aspectRatio", round(img.height / img.width));
|
|
59
|
+
options.set("height", img.height);
|
|
60
|
+
options.set("width", img.width);
|
|
61
|
+
}
|
|
53
62
|
}
|
|
54
63
|
});
|
|
55
|
-
|
|
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
|
-
})
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
name: "backgroundSize",
|
|
71
|
-
type: "text",
|
|
72
|
-
defaultValue: "cover",
|
|
73
|
-
enum: [
|
|
74
|
-
{
|
|
75
|
-
label: "contain",
|
|
76
|
-
value: "contain",
|
|
77
|
-
helperText: "The image should never get cropped"
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
label: "cover",
|
|
81
|
-
value: "cover",
|
|
82
|
-
helperText: "The image should fill it's box, cropping when needed"
|
|
83
|
-
}
|
|
84
|
-
]
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
name: "backgroundPosition",
|
|
88
|
-
type: "text",
|
|
89
|
-
defaultValue: "center",
|
|
90
|
-
enum: [
|
|
91
|
-
"center",
|
|
92
|
-
"top",
|
|
93
|
-
"left",
|
|
94
|
-
"right",
|
|
95
|
-
"bottom",
|
|
96
|
-
"top left",
|
|
97
|
-
"top right",
|
|
98
|
-
"bottom left",
|
|
99
|
-
"bottom right"
|
|
100
|
-
]
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
name: "altText",
|
|
104
|
-
type: "string",
|
|
105
|
-
helperText: "Text to display when the user has images off"
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
name: "height",
|
|
109
|
-
type: "number",
|
|
110
|
-
hideFromUI: true
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
name: "width",
|
|
114
|
-
type: "number",
|
|
115
|
-
hideFromUI: true
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
name: "sizes",
|
|
119
|
-
type: "string",
|
|
120
|
-
hideFromUI: true
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
name: "srcset",
|
|
124
|
-
type: "string",
|
|
125
|
-
hideFromUI: true
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
name: "lazy",
|
|
129
|
-
type: "boolean",
|
|
130
|
-
defaultValue: true,
|
|
131
|
-
hideFromUI: true
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
name: "fitContent",
|
|
135
|
-
type: "boolean",
|
|
136
|
-
helperText: "When child blocks are provided, fit to them instead of using the image's aspect ratio",
|
|
137
|
-
defaultValue: true
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
name: "aspectRatio",
|
|
141
|
-
type: "number",
|
|
142
|
-
helperText: "This is the ratio of height/width, e.g. set to 1.5 for a 300px wide and 200px tall photo. Set to 0 to not force the image to maintain it's aspect ratio",
|
|
143
|
-
advanced: true,
|
|
144
|
-
defaultValue: 0.7041
|
|
64
|
+
}
|
|
145
65
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
66
|
+
}, {
|
|
67
|
+
name: "backgroundSize",
|
|
68
|
+
type: "text",
|
|
69
|
+
defaultValue: "cover",
|
|
70
|
+
enum: [{
|
|
71
|
+
label: "contain",
|
|
72
|
+
value: "contain",
|
|
73
|
+
helperText: "The image should never get cropped"
|
|
74
|
+
}, {
|
|
75
|
+
label: "cover",
|
|
76
|
+
value: "cover",
|
|
77
|
+
helperText: "The image should fill it's box, cropping when needed"
|
|
78
|
+
}]
|
|
79
|
+
}, {
|
|
80
|
+
name: "backgroundPosition",
|
|
81
|
+
type: "text",
|
|
82
|
+
defaultValue: "center",
|
|
83
|
+
enum: ["center", "top", "left", "right", "bottom", "top left", "top right", "bottom left", "bottom right"]
|
|
84
|
+
}, {
|
|
85
|
+
name: "altText",
|
|
86
|
+
type: "string",
|
|
87
|
+
helperText: "Text to display when the user has images off"
|
|
88
|
+
}, {
|
|
89
|
+
name: "height",
|
|
90
|
+
type: "number",
|
|
91
|
+
hideFromUI: true
|
|
92
|
+
}, {
|
|
93
|
+
name: "width",
|
|
94
|
+
type: "number",
|
|
95
|
+
hideFromUI: true
|
|
96
|
+
}, {
|
|
97
|
+
name: "sizes",
|
|
98
|
+
type: "string",
|
|
99
|
+
hideFromUI: true
|
|
100
|
+
}, {
|
|
101
|
+
name: "srcset",
|
|
102
|
+
type: "string",
|
|
103
|
+
hideFromUI: true
|
|
104
|
+
}, {
|
|
105
|
+
name: "lazy",
|
|
106
|
+
type: "boolean",
|
|
107
|
+
defaultValue: true,
|
|
108
|
+
hideFromUI: true
|
|
109
|
+
}, {
|
|
110
|
+
name: "fitContent",
|
|
111
|
+
type: "boolean",
|
|
112
|
+
helperText: "When child blocks are provided, fit to them instead of using the image's aspect ratio",
|
|
113
|
+
defaultValue: true
|
|
114
|
+
}, {
|
|
115
|
+
name: "aspectRatio",
|
|
116
|
+
type: "number",
|
|
117
|
+
helperText: "This is the ratio of height/width, e.g. set to 1.5 for a 300px wide and 200px tall photo. Set to 0 to not force the image to maintain it's aspect ratio",
|
|
118
|
+
advanced: true,
|
|
119
|
+
defaultValue: 0.7041
|
|
120
|
+
}]
|
|
150
121
|
};
|
|
122
|
+
export { componentInfo }
|
|
@@ -36,13 +36,11 @@ function getSrcSet(url) {
|
|
|
36
36
|
if (!isNaN(widthInSrc)) {
|
|
37
37
|
srcUrl = `${srcUrl} ${widthInSrc}w`;
|
|
38
38
|
}
|
|
39
|
-
return sizes.filter(
|
|
39
|
+
return sizes.filter(size => size !== widthInSrc).map(size => `${updateQueryParam(url, "width", size)} ${size}w`).concat([srcUrl]).join(", ");
|
|
40
40
|
}
|
|
41
41
|
if (url.match(/cdn\.shopify\.com/)) {
|
|
42
|
-
return sizes.map(
|
|
42
|
+
return sizes.map(size => [getShopifyImageUrl(url, `${size}x${size}`), size]).filter(([sizeUrl]) => !!sizeUrl).map(([sizeUrl, size]) => `${sizeUrl} ${size}w`).concat([url]).join(", ");
|
|
43
43
|
}
|
|
44
44
|
return url;
|
|
45
45
|
}
|
|
46
|
-
export {
|
|
47
|
-
getSrcSet
|
|
48
|
-
};
|
|
46
|
+
export { getSrcSet }
|
|
@@ -2,18 +2,14 @@ const componentInfo = {
|
|
|
2
2
|
name: "Raw:Img",
|
|
3
3
|
hideFromInsertMenu: true,
|
|
4
4
|
image: "https://firebasestorage.googleapis.com/v0/b/builder-3b0a2.appspot.com/o/images%2Fbaseline-insert_photo-24px.svg?alt=media&token=4e5d0ef4-f5e8-4e57-b3a9-38d63a9b9dc4",
|
|
5
|
-
inputs: [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
],
|
|
5
|
+
inputs: [{
|
|
6
|
+
name: "image",
|
|
7
|
+
bubble: true,
|
|
8
|
+
type: "file",
|
|
9
|
+
allowedFileTypes: ["jpeg", "jpg", "png", "svg", "gif", "webp"],
|
|
10
|
+
required: true
|
|
11
|
+
}],
|
|
14
12
|
noWrap: true,
|
|
15
13
|
static: true
|
|
16
14
|
};
|
|
17
|
-
export {
|
|
18
|
-
componentInfo
|
|
19
|
-
};
|
|
15
|
+
export { componentInfo }
|
package/src/blocks/img/img.jsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isEditing } from "../../functions/is-editing.js";
|
|
2
|
+
import { filterAttrs } from "../helpers.js";
|
|
2
3
|
|
|
3
4
|
function ImgComponent(props) {
|
|
4
5
|
return (
|
|
@@ -10,6 +11,7 @@ function ImgComponent(props) {
|
|
|
10
11
|
key={(isEditing() && props.imgSrc) || "default-key"}
|
|
11
12
|
alt={props.altText}
|
|
12
13
|
src={props.imgSrc || props.image}
|
|
14
|
+
{...{}}
|
|
13
15
|
{...props.attributes}
|
|
14
16
|
/>
|
|
15
17
|
);
|