@builder.io/sdk-react 0.2.1 → 0.2.2
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 +6 -0
- package/dist/react/blocks/symbol/symbol.js +2 -1
- package/dist/react/components/render-block/render-block.js +2 -3
- package/dist/react/components/render-content/render-content.js +8 -7
- package/dist/react/components/render-content-variants/helpers.js +98 -0
- package/dist/react/components/render-content-variants/render-content-variants.js +45 -0
- package/dist/react/functions/get-block-properties.js +4 -1
- package/dist/react/functions/get-content/index.js +22 -15
- package/dist/react/functions/track/index.js +2 -1
- package/dist/react/helpers/logger.js +7 -0
- package/dist/rsc/blocks/symbol/symbol.js +2 -1
- package/dist/rsc/components/render-block/render-block.js +2 -3
- package/dist/rsc/components/render-content/render-content.js +1 -0
- package/dist/rsc/components/render-content-variants/helpers.js +99 -0
- package/dist/rsc/components/render-content-variants/render-content-variants.js +45 -0
- package/dist/rsc/functions/get-block-properties.js +4 -1
- package/dist/rsc/functions/get-content/index.js +22 -15
- package/dist/rsc/functions/track/index.js +2 -1
- package/dist/rsc/helpers/logger.js +7 -0
- package/package.json +1 -1
- package/packages/react/src/blocks/symbol/symbol.jsx +2 -1
- package/packages/react/src/components/render-block/render-block.jsx +2 -3
- package/packages/react/src/components/render-content/render-content.jsx +11 -8
- package/packages/react/src/functions/get-block-properties.js +4 -1
- package/packages/react/src/functions/get-content/index.js +21 -15
- package/packages/react/src/functions/track/index.js +2 -1
- package/packages/react/src/helpers/logger.js +9 -0
- package/packages/rsc/src/blocks/symbol/symbol.jsx +2 -4
- package/packages/rsc/src/components/render-block/render-block.jsx +2 -3
- package/packages/rsc/src/components/render-content/render-content.jsx +1 -0
- package/packages/rsc/src/functions/get-block-properties.js +4 -1
- package/packages/rsc/src/functions/get-content/index.js +21 -15
- package/packages/rsc/src/functions/track/index.js +2 -1
- package/packages/rsc/src/helpers/logger.js +9 -0
- package/src/blocks/BaseText.jsx +0 -9
- package/src/blocks/button/button.jsx +0 -27
- package/src/blocks/button/component-info.js +0 -41
- package/src/blocks/columns/columns.jsx +0 -172
- package/src/blocks/columns/component-info.js +0 -242
- package/src/blocks/custom-code/component-info.js +0 -31
- package/src/blocks/custom-code/custom-code.jsx +0 -64
- package/src/blocks/embed/component-info.js +0 -44
- package/src/blocks/embed/embed.jsx +0 -49
- package/src/blocks/embed/helpers.js +0 -9
- package/src/blocks/form/component-info.js +0 -262
- package/src/blocks/form/form.jsx +0 -253
- package/src/blocks/fragment/component-info.js +0 -11
- package/src/blocks/fragment/fragment.jsx +0 -5
- package/src/blocks/image/component-info.js +0 -151
- package/src/blocks/image/image.helpers.js +0 -48
- package/src/blocks/image/image.jsx +0 -122
- package/src/blocks/img/component-info.js +0 -20
- package/src/blocks/img/img.jsx +0 -17
- package/src/blocks/input/component-info.js +0 -74
- package/src/blocks/input/input.jsx +0 -19
- package/src/blocks/raw-text/component-info.js +0 -16
- package/src/blocks/raw-text/raw-text.jsx +0 -10
- package/src/blocks/section/component-info.js +0 -49
- package/src/blocks/section/section.jsx +0 -27
- package/src/blocks/select/component-info.js +0 -59
- package/src/blocks/select/select.jsx +0 -20
- package/src/blocks/submit-button/component-info.js +0 -28
- package/src/blocks/submit-button/submit-button.jsx +0 -9
- package/src/blocks/symbol/component-info.js +0 -43
- package/src/blocks/symbol/symbol.jsx +0 -84
- package/src/blocks/text/component-info.js +0 -24
- package/src/blocks/text/text.jsx +0 -10
- package/src/blocks/textarea/component-info.js +0 -47
- package/src/blocks/textarea/textarea.jsx +0 -13
- package/src/blocks/util.js +0 -8
- package/src/blocks/video/component-info.js +0 -106
- package/src/blocks/video/video.jsx +0 -58
- package/src/components/render-block/block-styles.jsx +0 -69
- package/src/components/render-block/render-block.helpers.js +0 -23
- package/src/components/render-block/render-block.jsx +0 -252
- package/src/components/render-block/render-component-with-context.jsx +0 -38
- package/src/components/render-block/render-component.jsx +0 -33
- package/src/components/render-block/render-repeated-block.jsx +0 -33
- package/src/components/render-block/types.js +0 -0
- package/src/components/render-blocks.jsx +0 -89
- package/src/components/render-content/builder-editing.jsx +0 -5
- package/src/components/render-content/components/render-styles.jsx +0 -64
- package/src/components/render-content/index.js +0 -4
- package/src/components/render-content/render-content.jsx +0 -370
- package/src/components/render-inlined-styles.jsx +0 -28
- package/src/constants/builder-registered-components.js +0 -54
- package/src/constants/device-sizes.js +0 -48
- package/src/constants/target.js +0 -4
- package/src/context/builder.context.js +0 -12
- package/src/context/types.js +0 -0
- package/src/functions/camel-to-kebab-case.js +0 -4
- package/src/functions/evaluate.js +0 -29
- package/src/functions/event-handler-name.js +0 -7
- package/src/functions/extract-text-styles.js +0 -22
- package/src/functions/fast-clone.js +0 -4
- package/src/functions/get-block-actions-handler.js +0 -11
- package/src/functions/get-block-actions.js +0 -18
- package/src/functions/get-block-component-options.js +0 -28
- package/src/functions/get-block-properties.js +0 -50
- package/src/functions/get-block-tag.js +0 -6
- package/src/functions/get-builder-search-params/fn.test.js +0 -13
- package/src/functions/get-builder-search-params/index.js +0 -38
- package/src/functions/get-content/ab-testing.js +0 -99
- package/src/functions/get-content/generate-content-url.js +0 -55
- package/src/functions/get-content/generate-content-url.test.js +0 -31
- package/src/functions/get-content/index.js +0 -65
- package/src/functions/get-content/types.js +0 -0
- package/src/functions/get-fetch.js +0 -14
- package/src/functions/get-global-this.js +0 -18
- package/src/functions/get-processed-block.js +0 -59
- package/src/functions/get-processed-block.test.js +0 -32
- package/src/functions/get-react-native-block-styles.js +0 -33
- package/src/functions/if-target.js +0 -15
- package/src/functions/is-browser.js +0 -6
- package/src/functions/is-editing.js +0 -7
- package/src/functions/is-iframe.js +0 -7
- package/src/functions/is-previewing.js +0 -14
- package/src/functions/on-change.js +0 -27
- package/src/functions/on-change.test.js +0 -19
- package/src/functions/register-component.js +0 -68
- package/src/functions/register.js +0 -29
- package/src/functions/sanitize-react-native-block-styles.js +0 -66
- package/src/functions/set-editor-settings.js +0 -15
- package/src/functions/set.js +0 -11
- package/src/functions/set.test.js +0 -16
- package/src/functions/track.js +0 -124
- package/src/functions/transform-block-properties.js +0 -6
- package/src/functions/transform-block.js +0 -6
- package/src/helpers/ab-tests.js +0 -16
- package/src/helpers/cookie.js +0 -81
- package/src/helpers/css.js +0 -34
- package/src/helpers/flatten.js +0 -34
- package/src/helpers/localStorage.js +0 -35
- package/src/helpers/nullable.js +0 -4
- package/src/helpers/sessionId.js +0 -52
- package/src/helpers/time.js +0 -5
- package/src/helpers/url.js +0 -13
- package/src/helpers/url.test.js +0 -21
- package/src/helpers/uuid.js +0 -13
- package/src/helpers/visitorId.js +0 -34
- package/src/index-helpers/blocks-exports.js +0 -22
- package/src/index-helpers/top-of-file.js +0 -4
- package/src/index.js +0 -14
- package/src/scripts/init-editing.js +0 -93
- package/src/types/builder-block.js +0 -0
- package/src/types/builder-content.js +0 -0
- package/src/types/can-track.js +0 -0
- package/src/types/components.js +0 -0
- package/src/types/deep-partial.js +0 -0
- package/src/types/element.js +0 -0
- package/src/types/targets.js +0 -0
- package/src/types/typescript.js +0 -0
package/src/blocks/form/form.jsx
DELETED
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { useState, useContext, useRef } from "react";
|
|
3
|
-
import RenderBlock from "../../components/render-block/render-block.js";
|
|
4
|
-
import BuilderBlocks from "../../components/render-blocks.js";
|
|
5
|
-
import { isEditing } from "../../functions/is-editing.js";
|
|
6
|
-
|
|
7
|
-
export default function FormComponent(props) {
|
|
8
|
-
const formRef = useRef(null);
|
|
9
|
-
const [formState, setFormState] = useState(() => "unsubmitted");
|
|
10
|
-
|
|
11
|
-
const [responseData, setResponseData] = useState(() => null);
|
|
12
|
-
|
|
13
|
-
const [formErrorMessage, setFormErrorMessage] = useState(() => "");
|
|
14
|
-
|
|
15
|
-
function submissionState() {
|
|
16
|
-
return (isEditing() && props.previewState) || formState;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function onSubmit(event) {
|
|
20
|
-
const sendWithJs = props.sendWithJs || props.sendSubmissionsTo === "email";
|
|
21
|
-
if (props.sendSubmissionsTo === "zapier") {
|
|
22
|
-
event.preventDefault();
|
|
23
|
-
} else if (sendWithJs) {
|
|
24
|
-
if (!(props.action || props.sendSubmissionsTo === "email")) {
|
|
25
|
-
event.preventDefault();
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
event.preventDefault();
|
|
29
|
-
const el = event.currentTarget;
|
|
30
|
-
const headers = props.customHeaders || {};
|
|
31
|
-
let body;
|
|
32
|
-
const formData = new FormData(el);
|
|
33
|
-
|
|
34
|
-
// TODO: maybe support null
|
|
35
|
-
const formPairs = Array.from(
|
|
36
|
-
event.currentTarget.querySelectorAll("input,select,textarea")
|
|
37
|
-
)
|
|
38
|
-
.filter((el) => !!el.name)
|
|
39
|
-
.map((el) => {
|
|
40
|
-
let value;
|
|
41
|
-
const key = el.name;
|
|
42
|
-
if (el instanceof HTMLInputElement) {
|
|
43
|
-
if (el.type === "radio") {
|
|
44
|
-
if (el.checked) {
|
|
45
|
-
value = el.name;
|
|
46
|
-
return {
|
|
47
|
-
key,
|
|
48
|
-
value,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
} else if (el.type === "checkbox") {
|
|
52
|
-
value = el.checked;
|
|
53
|
-
} else if (el.type === "number" || el.type === "range") {
|
|
54
|
-
const num = el.valueAsNumber;
|
|
55
|
-
if (!isNaN(num)) {
|
|
56
|
-
value = num;
|
|
57
|
-
}
|
|
58
|
-
} else if (el.type === "file") {
|
|
59
|
-
// TODO: one vs multiple files
|
|
60
|
-
value = el.files;
|
|
61
|
-
} else {
|
|
62
|
-
value = el.value;
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
value = el.value;
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
key,
|
|
69
|
-
value,
|
|
70
|
-
};
|
|
71
|
-
});
|
|
72
|
-
let contentType = props.contentType;
|
|
73
|
-
if (props.sendSubmissionsTo === "email") {
|
|
74
|
-
contentType = "multipart/form-data";
|
|
75
|
-
}
|
|
76
|
-
Array.from(formPairs).forEach(({ value }) => {
|
|
77
|
-
if (
|
|
78
|
-
value instanceof File ||
|
|
79
|
-
(Array.isArray(value) && value[0] instanceof File) ||
|
|
80
|
-
value instanceof FileList
|
|
81
|
-
) {
|
|
82
|
-
contentType = "multipart/form-data";
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
// TODO: send as urlEncoded or multipart by default
|
|
87
|
-
// because of ease of use and reliability in browser API
|
|
88
|
-
// for encoding the form?
|
|
89
|
-
if (contentType !== "application/json") {
|
|
90
|
-
body = formData;
|
|
91
|
-
} else {
|
|
92
|
-
// Json
|
|
93
|
-
const json = {};
|
|
94
|
-
Array.from(formPairs).forEach(({ value, key }) => {
|
|
95
|
-
set(json, key, value);
|
|
96
|
-
});
|
|
97
|
-
body = JSON.stringify(json);
|
|
98
|
-
}
|
|
99
|
-
if (contentType && contentType !== "multipart/form-data") {
|
|
100
|
-
if (
|
|
101
|
-
/* Zapier doesn't allow content-type header to be sent from browsers */ !(
|
|
102
|
-
sendWithJs && props.action?.includes("zapier.com")
|
|
103
|
-
)
|
|
104
|
-
) {
|
|
105
|
-
headers["content-type"] = contentType;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
const presubmitEvent = new CustomEvent("presubmit", { detail: { body } });
|
|
109
|
-
if (formRef.current) {
|
|
110
|
-
formRef.current.dispatchEvent(presubmitEvent);
|
|
111
|
-
if (presubmitEvent.defaultPrevented) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
setFormState("sending");
|
|
116
|
-
const formUrl = `${
|
|
117
|
-
builder.env === "dev" ? "http://localhost:5000" : "https://builder.io"
|
|
118
|
-
}/api/v1/form-submit?apiKey=${builder.apiKey}&to=${btoa(
|
|
119
|
-
props.sendSubmissionsToEmail || ""
|
|
120
|
-
)}&name=${encodeURIComponent(props.name || "")}`;
|
|
121
|
-
fetch(
|
|
122
|
-
props.sendSubmissionsTo === "email"
|
|
123
|
-
? formUrl
|
|
124
|
-
: props.action /* TODO: throw error if no action URL */,
|
|
125
|
-
{ body, headers, method: props.method || "post" }
|
|
126
|
-
).then(
|
|
127
|
-
async (res) => {
|
|
128
|
-
let body;
|
|
129
|
-
const contentType = res.headers.get("content-type");
|
|
130
|
-
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
131
|
-
body = await res.json();
|
|
132
|
-
} else {
|
|
133
|
-
body = await res.text();
|
|
134
|
-
}
|
|
135
|
-
if (!res.ok && props.errorMessagePath) {
|
|
136
|
-
/* TODO: allow supplying an error formatter function */ let message =
|
|
137
|
-
get(body, props.errorMessagePath);
|
|
138
|
-
if (message) {
|
|
139
|
-
if (typeof message !== "string") {
|
|
140
|
-
/* TODO: ideally convert json to yaml so it woul dbe like error: - email has been taken */ message =
|
|
141
|
-
JSON.stringify(message);
|
|
142
|
-
}
|
|
143
|
-
setFormErrorMessage(message);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
setResponseData(body);
|
|
147
|
-
setFormState(res.ok ? "success" : "error");
|
|
148
|
-
if (res.ok) {
|
|
149
|
-
const submitSuccessEvent = new CustomEvent("submit:success", {
|
|
150
|
-
detail: { res, body },
|
|
151
|
-
});
|
|
152
|
-
if (formRef.current) {
|
|
153
|
-
formRef.current.dispatchEvent(submitSuccessEvent);
|
|
154
|
-
if (submitSuccessEvent.defaultPrevented) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
/* TODO: option to turn this on/off? */ if (
|
|
158
|
-
props.resetFormOnSubmit !== false
|
|
159
|
-
) {
|
|
160
|
-
formRef.current.reset();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
/* TODO: client side route event first that can be preventDefaulted */ if (
|
|
164
|
-
props.successUrl
|
|
165
|
-
) {
|
|
166
|
-
if (formRef.current) {
|
|
167
|
-
const event = new CustomEvent("route", {
|
|
168
|
-
detail: { url: props.successUrl },
|
|
169
|
-
});
|
|
170
|
-
formRef.current.dispatchEvent(event);
|
|
171
|
-
if (!event.defaultPrevented) {
|
|
172
|
-
location.href = props.successUrl;
|
|
173
|
-
}
|
|
174
|
-
} else {
|
|
175
|
-
location.href = props.successUrl;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
},
|
|
180
|
-
(err) => {
|
|
181
|
-
const submitErrorEvent = new CustomEvent("submit:error", {
|
|
182
|
-
detail: { error: err },
|
|
183
|
-
});
|
|
184
|
-
if (formRef.current) {
|
|
185
|
-
formRef.current.dispatchEvent(submitErrorEvent);
|
|
186
|
-
if (submitErrorEvent.defaultPrevented) {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
setResponseData(err);
|
|
191
|
-
setFormState("error");
|
|
192
|
-
}
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
const builderContext = useContext(BuilderContext);
|
|
197
|
-
return (
|
|
198
|
-
<>
|
|
199
|
-
{" "}
|
|
200
|
-
<form
|
|
201
|
-
validate={props.validate}
|
|
202
|
-
ref={formRef}
|
|
203
|
-
action={!props.sendWithJs && props.action}
|
|
204
|
-
method={props.method}
|
|
205
|
-
name={props.name}
|
|
206
|
-
onSubmit={(event) => onSubmit(event)}
|
|
207
|
-
{...props.attributes}
|
|
208
|
-
>
|
|
209
|
-
{" "}
|
|
210
|
-
{props.builderBlock && props.builderBlock.children ? (
|
|
211
|
-
<>
|
|
212
|
-
{props.builderBlock?.children?.map((block) => (
|
|
213
|
-
<RenderBlock block={block} context={builderContext} />
|
|
214
|
-
))}
|
|
215
|
-
</>
|
|
216
|
-
) : null}{" "}
|
|
217
|
-
{submissionState() === "error" ? (
|
|
218
|
-
<>
|
|
219
|
-
<BuilderBlocks
|
|
220
|
-
dataPath="errorMessage"
|
|
221
|
-
blocks={props.errorMessage}
|
|
222
|
-
/>
|
|
223
|
-
</>
|
|
224
|
-
) : null}{" "}
|
|
225
|
-
{submissionState() === "sending" ? (
|
|
226
|
-
<>
|
|
227
|
-
<BuilderBlocks
|
|
228
|
-
dataPath="sendingMessage"
|
|
229
|
-
blocks={props.sendingMessage}
|
|
230
|
-
/>
|
|
231
|
-
</>
|
|
232
|
-
) : null}{" "}
|
|
233
|
-
{submissionState() === "error" && responseData ? (
|
|
234
|
-
<>
|
|
235
|
-
<pre className="builder-form-error-text pre-4bdc7532">
|
|
236
|
-
{" "}
|
|
237
|
-
{JSON.stringify(responseData, null, 2)}{" "}
|
|
238
|
-
</pre>
|
|
239
|
-
</>
|
|
240
|
-
) : null}{" "}
|
|
241
|
-
{submissionState() === "success" ? (
|
|
242
|
-
<>
|
|
243
|
-
<BuilderBlocks
|
|
244
|
-
dataPath="successMessage"
|
|
245
|
-
blocks={props.successMessage}
|
|
246
|
-
/>
|
|
247
|
-
</>
|
|
248
|
-
) : null}{" "}
|
|
249
|
-
</form>{" "}
|
|
250
|
-
<style>{`.pre-4bdc7532 { padding: 10px; color: red; text-align: center; }`}</style>{" "}
|
|
251
|
-
</>
|
|
252
|
-
);
|
|
253
|
-
}
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { serializeFn } from "../util.js";
|
|
2
|
-
const componentInfo = {
|
|
3
|
-
name: "Image",
|
|
4
|
-
static: true,
|
|
5
|
-
builtIn: true,
|
|
6
|
-
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",
|
|
7
|
-
defaultStyles: {
|
|
8
|
-
position: "relative",
|
|
9
|
-
minHeight: "20px",
|
|
10
|
-
minWidth: "20px",
|
|
11
|
-
overflow: "hidden"
|
|
12
|
-
},
|
|
13
|
-
canHaveChildren: true,
|
|
14
|
-
inputs: [
|
|
15
|
-
{
|
|
16
|
-
name: "image",
|
|
17
|
-
type: "file",
|
|
18
|
-
bubble: true,
|
|
19
|
-
allowedFileTypes: ["jpeg", "jpg", "png", "svg"],
|
|
20
|
-
required: true,
|
|
21
|
-
defaultValue: "https://cdn.builder.io/api/v1/image/assets%2Fpwgjf0RoYWbdnJSbpBAjXNRMe9F2%2Ffb27a7c790324294af8be1c35fe30f4d",
|
|
22
|
-
onChange: serializeFn((options) => {
|
|
23
|
-
const DEFAULT_ASPECT_RATIO = 0.7041;
|
|
24
|
-
options.delete("srcset");
|
|
25
|
-
options.delete("noWebp");
|
|
26
|
-
function loadImage(url, timeout = 6e4) {
|
|
27
|
-
return new Promise((resolve, reject) => {
|
|
28
|
-
const img = document.createElement("img");
|
|
29
|
-
let loaded = false;
|
|
30
|
-
img.onload = () => {
|
|
31
|
-
loaded = true;
|
|
32
|
-
resolve(img);
|
|
33
|
-
};
|
|
34
|
-
img.addEventListener("error", (event) => {
|
|
35
|
-
console.warn("Image load failed", event.error);
|
|
36
|
-
reject(event.error);
|
|
37
|
-
});
|
|
38
|
-
img.src = url;
|
|
39
|
-
setTimeout(() => {
|
|
40
|
-
if (!loaded) {
|
|
41
|
-
reject(new Error("Image load timed out"));
|
|
42
|
-
}
|
|
43
|
-
}, timeout);
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
function round(num) {
|
|
47
|
-
return Math.round(num * 1e3) / 1e3;
|
|
48
|
-
}
|
|
49
|
-
const value = options.get("image");
|
|
50
|
-
const aspectRatio = options.get("aspectRatio");
|
|
51
|
-
fetch(value).then((res) => res.blob()).then((blob) => {
|
|
52
|
-
if (blob.type.includes("svg")) {
|
|
53
|
-
options.set("noWebp", true);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
if (value && (!aspectRatio || aspectRatio === DEFAULT_ASPECT_RATIO)) {
|
|
57
|
-
return loadImage(value).then((img) => {
|
|
58
|
-
const possiblyUpdatedAspectRatio = options.get("aspectRatio");
|
|
59
|
-
if (options.get("image") === value && (!possiblyUpdatedAspectRatio || possiblyUpdatedAspectRatio === DEFAULT_ASPECT_RATIO)) {
|
|
60
|
-
if (img.width && img.height) {
|
|
61
|
-
options.set("aspectRatio", round(img.height / img.width));
|
|
62
|
-
options.set("height", img.height);
|
|
63
|
-
options.set("width", img.width);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
})
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
name: "backgroundSize",
|
|
72
|
-
type: "text",
|
|
73
|
-
defaultValue: "cover",
|
|
74
|
-
enum: [
|
|
75
|
-
{
|
|
76
|
-
label: "contain",
|
|
77
|
-
value: "contain",
|
|
78
|
-
helperText: "The image should never get cropped"
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
label: "cover",
|
|
82
|
-
value: "cover",
|
|
83
|
-
helperText: "The image should fill it's box, cropping when needed"
|
|
84
|
-
}
|
|
85
|
-
]
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
name: "backgroundPosition",
|
|
89
|
-
type: "text",
|
|
90
|
-
defaultValue: "center",
|
|
91
|
-
enum: [
|
|
92
|
-
"center",
|
|
93
|
-
"top",
|
|
94
|
-
"left",
|
|
95
|
-
"right",
|
|
96
|
-
"bottom",
|
|
97
|
-
"top left",
|
|
98
|
-
"top right",
|
|
99
|
-
"bottom left",
|
|
100
|
-
"bottom right"
|
|
101
|
-
]
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
name: "altText",
|
|
105
|
-
type: "string",
|
|
106
|
-
helperText: "Text to display when the user has images off"
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
name: "height",
|
|
110
|
-
type: "number",
|
|
111
|
-
hideFromUI: true
|
|
112
|
-
},
|
|
113
|
-
{
|
|
114
|
-
name: "width",
|
|
115
|
-
type: "number",
|
|
116
|
-
hideFromUI: true
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
name: "sizes",
|
|
120
|
-
type: "string",
|
|
121
|
-
hideFromUI: true
|
|
122
|
-
},
|
|
123
|
-
{
|
|
124
|
-
name: "srcset",
|
|
125
|
-
type: "string",
|
|
126
|
-
hideFromUI: true
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
name: "lazy",
|
|
130
|
-
type: "boolean",
|
|
131
|
-
defaultValue: true,
|
|
132
|
-
hideFromUI: true
|
|
133
|
-
},
|
|
134
|
-
{
|
|
135
|
-
name: "fitContent",
|
|
136
|
-
type: "boolean",
|
|
137
|
-
helperText: "When child blocks are provided, fit to them instead of using the image's aspect ratio",
|
|
138
|
-
defaultValue: true
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
name: "aspectRatio",
|
|
142
|
-
type: "number",
|
|
143
|
-
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",
|
|
144
|
-
advanced: true,
|
|
145
|
-
defaultValue: 0.7041
|
|
146
|
-
}
|
|
147
|
-
]
|
|
148
|
-
};
|
|
149
|
-
export {
|
|
150
|
-
componentInfo
|
|
151
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
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,122 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { getSrcSet } from "./image.helpers.js";
|
|
3
|
-
|
|
4
|
-
export default function Image(props) {
|
|
5
|
-
function srcSetToUse() {
|
|
6
|
-
const imageToUse = props.image || props.src;
|
|
7
|
-
const url = imageToUse;
|
|
8
|
-
if (
|
|
9
|
-
!url ||
|
|
10
|
-
// We can auto add srcset for cdn.builder.io and shopify
|
|
11
|
-
// images, otherwise you can supply this prop manually
|
|
12
|
-
!(url.match(/builder\.io/) || url.match(/cdn\.shopify\.com/))
|
|
13
|
-
) {
|
|
14
|
-
return props.srcset;
|
|
15
|
-
}
|
|
16
|
-
if (props.srcset && props.image?.includes("builder.io/api/v1/image")) {
|
|
17
|
-
if (!props.srcset.includes(props.image.split("?")[0])) {
|
|
18
|
-
console.debug("Removed given srcset");
|
|
19
|
-
return getSrcSet(url);
|
|
20
|
-
}
|
|
21
|
-
} else if (props.image && !props.srcset) {
|
|
22
|
-
return getSrcSet(url);
|
|
23
|
-
}
|
|
24
|
-
return getSrcSet(url);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function webpSrcSet() {
|
|
28
|
-
if (srcSetToUse?.()?.match(/builder\.io/) && !props.noWebp) {
|
|
29
|
-
return srcSetToUse().replace(/\?/g, "?format=webp&");
|
|
30
|
-
} else {
|
|
31
|
-
return "";
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function aspectRatioCss() {
|
|
36
|
-
const aspectRatioStyles = {
|
|
37
|
-
position: "absolute",
|
|
38
|
-
height: "100%",
|
|
39
|
-
width: "100%",
|
|
40
|
-
left: "0px",
|
|
41
|
-
top: "0px",
|
|
42
|
-
};
|
|
43
|
-
const out = props.aspectRatio ? aspectRatioStyles : undefined;
|
|
44
|
-
return out;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
<>
|
|
49
|
-
<>
|
|
50
|
-
<picture>
|
|
51
|
-
{webpSrcSet() ? (
|
|
52
|
-
<>
|
|
53
|
-
<source type="image/webp" srcSet={webpSrcSet()} />
|
|
54
|
-
</>
|
|
55
|
-
) : null}
|
|
56
|
-
|
|
57
|
-
<img
|
|
58
|
-
loading="lazy"
|
|
59
|
-
alt={props.altText}
|
|
60
|
-
role={props.altText ? "presentation" : undefined}
|
|
61
|
-
style={{
|
|
62
|
-
objectPosition: props.backgroundPosition || "center",
|
|
63
|
-
objectFit: props.backgroundSize || "cover",
|
|
64
|
-
...aspectRatioCss(),
|
|
65
|
-
}}
|
|
66
|
-
className={
|
|
67
|
-
"builder-image" +
|
|
68
|
-
(props.className ? " " + props.className : "") +
|
|
69
|
-
" img-35d6dc22"
|
|
70
|
-
}
|
|
71
|
-
src={props.image}
|
|
72
|
-
srcSet={srcSetToUse()}
|
|
73
|
-
sizes={props.sizes}
|
|
74
|
-
/>
|
|
75
|
-
|
|
76
|
-
<source srcSet={srcSetToUse()} />
|
|
77
|
-
</picture>
|
|
78
|
-
|
|
79
|
-
{props.aspectRatio &&
|
|
80
|
-
!(props.builderBlock?.children?.length && props.fitContent) ? (
|
|
81
|
-
<>
|
|
82
|
-
<div
|
|
83
|
-
className="builder-image-sizer div-35d6dc22"
|
|
84
|
-
style={{
|
|
85
|
-
paddingTop:
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
87
|
-
props.aspectRatio * 100 + "%",
|
|
88
|
-
}}
|
|
89
|
-
/>
|
|
90
|
-
</>
|
|
91
|
-
) : null}
|
|
92
|
-
|
|
93
|
-
{props.builderBlock?.children?.length && props.fitContent ? (
|
|
94
|
-
<>{props.children}</>
|
|
95
|
-
) : null}
|
|
96
|
-
|
|
97
|
-
{!props.fitContent && props.children ? (
|
|
98
|
-
<>
|
|
99
|
-
<div className="div-35d6dc22-2">{props.children}</div>
|
|
100
|
-
</>
|
|
101
|
-
) : null}
|
|
102
|
-
</>
|
|
103
|
-
<style>{`.img-35d6dc22 {
|
|
104
|
-
opacity: 1;
|
|
105
|
-
transition: opacity 0.2s ease-in-out;
|
|
106
|
-
}.div-35d6dc22 {
|
|
107
|
-
width: 100%;
|
|
108
|
-
pointer-events: none;
|
|
109
|
-
font-size: 0;
|
|
110
|
-
}.div-35d6dc22-2 {
|
|
111
|
-
display: flex;
|
|
112
|
-
flex-direction: column;
|
|
113
|
-
align-items: stretch;
|
|
114
|
-
position: absolute;
|
|
115
|
-
top: 0;
|
|
116
|
-
left: 0;
|
|
117
|
-
width: 100%;
|
|
118
|
-
height: 100%;
|
|
119
|
-
}`}</style>
|
|
120
|
-
</>
|
|
121
|
-
);
|
|
122
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const componentInfo = {
|
|
2
|
-
name: "Raw:Img",
|
|
3
|
-
hideFromInsertMenu: true,
|
|
4
|
-
builtIn: true,
|
|
5
|
-
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",
|
|
6
|
-
inputs: [
|
|
7
|
-
{
|
|
8
|
-
name: "image",
|
|
9
|
-
bubble: true,
|
|
10
|
-
type: "file",
|
|
11
|
-
allowedFileTypes: ["jpeg", "jpg", "png", "svg"],
|
|
12
|
-
required: true
|
|
13
|
-
}
|
|
14
|
-
],
|
|
15
|
-
noWrap: true,
|
|
16
|
-
static: true
|
|
17
|
-
};
|
|
18
|
-
export {
|
|
19
|
-
componentInfo
|
|
20
|
-
};
|
package/src/blocks/img/img.jsx
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { isEditing } from "../../functions/is-editing.js";
|
|
3
|
-
|
|
4
|
-
export default function ImgComponent(props) {
|
|
5
|
-
return (
|
|
6
|
-
<img
|
|
7
|
-
style={{
|
|
8
|
-
objectFit: props.backgroundSize || "cover",
|
|
9
|
-
objectPosition: props.backgroundPosition || "center",
|
|
10
|
-
}}
|
|
11
|
-
key={(isEditing() && props.imgSrc) || "default-key"}
|
|
12
|
-
alt={props.altText}
|
|
13
|
-
src={props.imgSrc || props.image}
|
|
14
|
-
{...props.attributes}
|
|
15
|
-
/>
|
|
16
|
-
);
|
|
17
|
-
}
|