@builder.io/sdk-svelte 0.0.1-3 → 0.0.1-32
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 +19 -0
- package/package.json +8 -1
- package/src/blocks/button/button.svelte +35 -48
- package/src/blocks/columns/columns.svelte +137 -103
- package/src/blocks/columns/component-info.js +26 -1
- package/src/blocks/custom-code/custom-code.svelte +58 -53
- package/src/blocks/embed/component-info.js +21 -1
- package/src/blocks/embed/embed.svelte +45 -57
- package/src/blocks/embed/helpers.js +9 -0
- package/src/blocks/form/form.svelte +313 -296
- package/src/blocks/fragment/fragment.svelte +9 -25
- package/src/blocks/image/component-info.js +48 -1
- package/src/blocks/image/image.helpers.js +48 -0
- package/src/blocks/image/image.svelte +146 -117
- package/src/blocks/img/img.svelte +48 -36
- package/src/blocks/input/input.svelte +30 -31
- package/src/blocks/raw-text/raw-text.svelte +12 -26
- package/src/blocks/section/section.svelte +30 -36
- package/src/blocks/select/select.svelte +30 -37
- package/src/blocks/submit-button/submit-button.svelte +12 -30
- package/src/blocks/symbol/component-info.js +1 -0
- package/src/blocks/symbol/symbol.svelte +88 -53
- package/src/blocks/text/text.svelte +8 -25
- package/src/blocks/textarea/textarea.svelte +16 -29
- package/src/blocks/util.js +7 -0
- package/src/blocks/video/video.svelte +88 -41
- package/src/components/render-block/block-styles.svelte +58 -51
- package/src/components/render-block/render-block.helpers.js +23 -0
- package/src/components/render-block/render-block.svelte +204 -160
- package/src/components/render-block/render-component.svelte +30 -0
- package/src/components/render-block/render-repeated-block.svelte +45 -0
- package/src/components/render-block/types.js +0 -0
- package/src/components/render-blocks.svelte +88 -80
- package/src/components/render-content/components/render-styles.svelte +59 -63
- package/src/components/render-content/index.js +4 -0
- package/src/components/render-content/render-content.svelte +316 -207
- package/src/components/render-inlined-styles.svelte +26 -48
- package/src/constants/builder-registered-components.js +51 -0
- package/src/constants/device-sizes.js +3 -21
- package/src/context/builder.context.js +7 -1
- package/src/functions/camel-to-kebab-case.js +4 -0
- package/src/functions/convert-style-object.js +6 -0
- package/src/functions/get-block-component-options.js +6 -1
- package/src/functions/get-block-styles.js +7 -4
- package/src/functions/get-builder-search-params/index.js +12 -1
- package/src/functions/get-content/ab-testing.js +38 -0
- package/src/functions/get-content/fn.test.js +1 -1
- package/src/functions/get-content/index.js +16 -58
- package/src/functions/get-content/types.js +0 -0
- package/src/functions/get-fetch.js +2 -2
- package/src/functions/get-processed-block.js +19 -8
- package/src/functions/get-processed-block.test.js +2 -1
- package/src/functions/register-component.js +45 -26
- package/src/functions/sanitize-styles.js +5 -0
- package/src/functions/track.js +71 -2
- package/src/helpers/cookie.js +59 -0
- package/src/helpers/css.js +12 -0
- package/src/helpers/flatten.js +34 -0
- package/src/helpers/localStorage.js +34 -0
- package/src/helpers/nullable.js +4 -0
- package/src/helpers/sessionId.js +26 -0
- package/src/helpers/time.js +5 -0
- package/src/helpers/url.js +10 -0
- package/src/helpers/url.test.js +15 -0
- package/src/helpers/uuid.js +13 -0
- package/src/helpers/visitorId.js +33 -0
- package/src/index-helpers/blocks-exports.js +10 -8
- package/src/scripts/init-editing.js +4 -5
- package/src/types/can-track.js +0 -0
- package/src/types/components.js +0 -0
- package/src/types/element.js +0 -0
- package/src/blocks/button/index.js +0 -7
- package/src/blocks/columns/index.js +0 -7
- package/src/blocks/custom-code/index.js +0 -7
- package/src/blocks/embed/index.js +0 -7
- package/src/blocks/form/index.js +0 -7
- package/src/blocks/fragment/index.js +0 -7
- package/src/blocks/image/index.js +0 -7
- package/src/blocks/img/index.js +0 -7
- package/src/blocks/input/index.js +0 -7
- package/src/blocks/raw-text/index.js +0 -7
- package/src/blocks/section/index.js +0 -7
- package/src/blocks/select/index.js +0 -7
- package/src/blocks/submit-button/index.js +0 -7
- package/src/blocks/symbol/index.js +0 -7
- package/src/blocks/text/index.js +0 -7
- package/src/blocks/textarea/index.js +0 -7
- package/src/blocks/video/index.js +0 -7
- package/src/components/error-boundary.svelte +0 -26
- package/src/functions/macro-eval.js +0 -5
- package/src/functions/previewing-model-name.js +0 -11
|
@@ -1,333 +1,350 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
1
|
+
<script context="module" lang="ts">
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
|
|
5
|
+
/* eslint-disable */
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* This component was copied over from the old SDKs and has a lot of code pointing to invalid functions/env vars. It needs
|
|
9
|
+
* to be cleaned up before the component can actually be usable.
|
|
10
|
+
*/
|
|
11
|
+
interface BuilderElement {}
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
|
+
// @ts-nocheck
|
|
15
|
+
|
|
16
|
+
/* eslint-disable */
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* This component was copied over from the old SDKs and has a lot of code pointing to invalid functions/env vars. It needs
|
|
20
|
+
* to be cleaned up before the component can actually be usable.
|
|
21
|
+
*/
|
|
22
|
+
export interface FormProps {
|
|
23
|
+
attributes?: any;
|
|
24
|
+
name?: string;
|
|
25
|
+
action?: string;
|
|
26
|
+
validate?: boolean;
|
|
27
|
+
method?: string;
|
|
28
|
+
builderBlock?: BuilderElement;
|
|
29
|
+
sendSubmissionsTo?: string;
|
|
30
|
+
sendSubmissionsToEmail?: string;
|
|
31
|
+
sendWithJs?: boolean;
|
|
32
|
+
contentType?: string;
|
|
33
|
+
customHeaders?: {
|
|
34
|
+
[key: string]: string;
|
|
35
|
+
};
|
|
36
|
+
successUrl?: string;
|
|
37
|
+
previewState?: FormState;
|
|
38
|
+
successMessage?: BuilderElement[];
|
|
39
|
+
errorMessage?: BuilderElement[];
|
|
40
|
+
sendingMessage?: BuilderElement[];
|
|
41
|
+
resetFormOnSubmit?: boolean;
|
|
42
|
+
errorMessagePath?: string;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
46
|
+
// @ts-nocheck
|
|
47
|
+
|
|
48
|
+
/* eslint-disable */
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* This component was copied over from the old SDKs and has a lot of code pointing to invalid functions/env vars. It needs
|
|
52
|
+
* to be cleaned up before the component can actually be usable.
|
|
53
|
+
*/
|
|
54
|
+
export type FormState = "unsubmitted" | "sending" | "success" | "error";
|
|
55
|
+
</script>
|
|
56
|
+
|
|
57
|
+
<script lang="ts">
|
|
58
|
+
import RenderBlock from "../../components/render-block/render-block.svelte";
|
|
59
|
+
import BuilderBlocks from "../../components/render-blocks.svelte";
|
|
60
|
+
import { isEditing } from "../../functions/is-editing.js";
|
|
61
|
+
|
|
62
|
+
import { getContext, setContext } from "svelte";
|
|
63
|
+
|
|
64
|
+
export let formRef: FormProps["formRef"];
|
|
65
|
+
export let previewState: FormProps["previewState"];
|
|
66
|
+
export let sendWithJs: FormProps["sendWithJs"];
|
|
67
|
+
export let sendSubmissionsTo: FormProps["sendSubmissionsTo"];
|
|
68
|
+
export let action: FormProps["action"];
|
|
69
|
+
export let customHeaders: FormProps["customHeaders"];
|
|
70
|
+
export let contentType: FormProps["contentType"];
|
|
71
|
+
export let sendSubmissionsToEmail: FormProps["sendSubmissionsToEmail"];
|
|
72
|
+
export let name: FormProps["name"];
|
|
73
|
+
export let method: FormProps["method"];
|
|
74
|
+
export let errorMessagePath: FormProps["errorMessagePath"];
|
|
75
|
+
export let resetFormOnSubmit: FormProps["resetFormOnSubmit"];
|
|
76
|
+
export let successUrl: FormProps["successUrl"];
|
|
77
|
+
export let validate: FormProps["validate"];
|
|
78
|
+
export let attributes: FormProps["attributes"];
|
|
79
|
+
export let builderBlock: FormProps["builderBlock"];
|
|
80
|
+
export let errorMessage: FormProps["errorMessage"];
|
|
81
|
+
export let sendingMessage: FormProps["sendingMessage"];
|
|
82
|
+
export let successMessage: FormProps["successMessage"];
|
|
83
|
+
|
|
84
|
+
let builderContext = getContext(BuilderContext.key);
|
|
85
|
+
|
|
86
|
+
const onSubmit = function onSubmit(event) {
|
|
87
|
+
const sendWithJs = sendWithJs || sendSubmissionsTo === "email";
|
|
88
|
+
|
|
89
|
+
if (sendSubmissionsTo === "zapier") {
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
} else if (sendWithJs) {
|
|
92
|
+
if (!(action || sendSubmissionsTo === "email")) {
|
|
93
|
+
event.preventDefault();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
50
96
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
const el = event.currentTarget;
|
|
99
|
+
const headers = customHeaders || {};
|
|
100
|
+
let body;
|
|
101
|
+
const formData = new FormData(el); // TODO: maybe support null
|
|
102
|
+
|
|
103
|
+
const formPairs = Array.from(
|
|
104
|
+
event.currentTarget.querySelectorAll("input,select,textarea")
|
|
105
|
+
)
|
|
106
|
+
.filter((el) => !!el.name)
|
|
107
|
+
.map((el) => {
|
|
108
|
+
let value;
|
|
109
|
+
const key = el.name;
|
|
110
|
+
|
|
111
|
+
if (el instanceof HTMLInputElement) {
|
|
112
|
+
if (el.type === "radio") {
|
|
113
|
+
if (el.checked) {
|
|
114
|
+
value = el.name;
|
|
115
|
+
return {
|
|
116
|
+
key,
|
|
117
|
+
value,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
} else if (el.type === "checkbox") {
|
|
121
|
+
value = el.checked;
|
|
122
|
+
} else if (el.type === "number" || el.type === "range") {
|
|
123
|
+
const num = el.valueAsNumber;
|
|
124
|
+
|
|
125
|
+
if (!isNaN(num)) {
|
|
126
|
+
value = num;
|
|
127
|
+
}
|
|
128
|
+
} else if (el.type === "file") {
|
|
129
|
+
// TODO: one vs multiple files
|
|
130
|
+
value = el.files;
|
|
131
|
+
} else {
|
|
132
|
+
value = el.value;
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
value = el.value;
|
|
136
|
+
}
|
|
54
137
|
|
|
55
|
-
if (el instanceof HTMLInputElement) {
|
|
56
|
-
if (el.type === 'radio') {
|
|
57
|
-
if (el.checked) {
|
|
58
|
-
value = el.name;
|
|
59
138
|
return {
|
|
60
139
|
key,
|
|
61
|
-
value
|
|
140
|
+
value,
|
|
62
141
|
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
value = el.checked;
|
|
66
|
-
} else if (el.type === 'number' || el.type === 'range') {
|
|
67
|
-
const num = el.valueAsNumber;
|
|
142
|
+
});
|
|
143
|
+
let contentType = contentType;
|
|
68
144
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
} else if (el.type === 'file') {
|
|
73
|
-
// TODO: one vs multiple files
|
|
74
|
-
value = el.files;
|
|
75
|
-
} else {
|
|
76
|
-
value = el.value;
|
|
145
|
+
if (sendSubmissionsTo === "email") {
|
|
146
|
+
contentType = "multipart/form-data";
|
|
77
147
|
}
|
|
78
|
-
} else {
|
|
79
|
-
value = el.value;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
key,
|
|
84
|
-
value
|
|
85
|
-
};
|
|
86
|
-
});
|
|
87
|
-
let contentType = contentType;
|
|
88
|
-
|
|
89
|
-
if (sendSubmissionsTo === 'email') {
|
|
90
|
-
contentType = 'multipart/form-data';
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
Array.from(formPairs).forEach(({
|
|
94
|
-
value
|
|
95
|
-
}) => {
|
|
96
|
-
if (value instanceof File || Array.isArray(value) && value[0] instanceof File || value instanceof FileList) {
|
|
97
|
-
contentType = 'multipart/form-data';
|
|
98
|
-
}
|
|
99
|
-
}); // TODO: send as urlEncoded or multipart by default
|
|
100
|
-
// because of ease of use and reliability in browser API
|
|
101
|
-
// for encoding the form?
|
|
102
|
-
|
|
103
|
-
if (contentType !== 'application/json') {
|
|
104
|
-
body = formData;
|
|
105
|
-
} else {
|
|
106
|
-
// Json
|
|
107
|
-
const json = {};
|
|
108
|
-
Array.from(formPairs).forEach(({
|
|
109
|
-
value,
|
|
110
|
-
key
|
|
111
|
-
}) => {
|
|
112
|
-
set(json, key, value);
|
|
113
|
-
});
|
|
114
|
-
body = JSON.stringify(json);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (contentType && contentType !== 'multipart/form-data') {
|
|
118
|
-
if (
|
|
119
|
-
/* Zapier doesn't allow content-type header to be sent from browsers */
|
|
120
|
-
!(sendWithJs && action?.includes('zapier.com'))) {
|
|
121
|
-
headers['content-type'] = contentType;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const presubmitEvent = new CustomEvent('presubmit', {
|
|
126
|
-
detail: {
|
|
127
|
-
body
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
if (formRef) {
|
|
132
|
-
formRef.dispatchEvent(presubmitEvent);
|
|
133
|
-
|
|
134
|
-
if (presubmitEvent.defaultPrevented) {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
148
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
method: method || 'post'
|
|
147
|
-
}).then(async res => {
|
|
148
|
-
let body;
|
|
149
|
-
const contentType = res.headers.get('content-type');
|
|
150
|
-
|
|
151
|
-
if (contentType && contentType.indexOf('application/json') !== -1) {
|
|
152
|
-
body = await res.json();
|
|
153
|
-
} else {
|
|
154
|
-
body = await res.text();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (!res.ok && errorMessagePath) {
|
|
158
|
-
/* TODO: allow supplying an error formatter function */
|
|
159
|
-
let message = get(body, errorMessagePath);
|
|
160
|
-
|
|
161
|
-
if (message) {
|
|
162
|
-
if (typeof message !== 'string') {
|
|
163
|
-
/* TODO: ideally convert json to yaml so it woul dbe like
|
|
164
|
-
error: - email has been taken */
|
|
165
|
-
message = JSON.stringify(message);
|
|
149
|
+
Array.from(formPairs).forEach(({ value }) => {
|
|
150
|
+
if (
|
|
151
|
+
value instanceof File ||
|
|
152
|
+
(Array.isArray(value) && value[0] instanceof File) ||
|
|
153
|
+
value instanceof FileList
|
|
154
|
+
) {
|
|
155
|
+
contentType = "multipart/form-data";
|
|
166
156
|
}
|
|
157
|
+
}); // TODO: send as urlEncoded or multipart by default
|
|
158
|
+
// because of ease of use and reliability in browser API
|
|
159
|
+
// for encoding the form?
|
|
167
160
|
|
|
168
|
-
|
|
161
|
+
if (contentType !== "application/json") {
|
|
162
|
+
body = formData;
|
|
163
|
+
} else {
|
|
164
|
+
// Json
|
|
165
|
+
const json = {};
|
|
166
|
+
Array.from(formPairs).forEach(({ value, key }) => {
|
|
167
|
+
set(json, key, value);
|
|
168
|
+
});
|
|
169
|
+
body = JSON.stringify(json);
|
|
169
170
|
}
|
|
170
|
-
}
|
|
171
171
|
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
if (contentType && contentType !== "multipart/form-data") {
|
|
173
|
+
if (
|
|
174
|
+
/* Zapier doesn't allow content-type header to be sent from browsers */
|
|
175
|
+
!(sendWithJs && action?.includes("zapier.com"))
|
|
176
|
+
) {
|
|
177
|
+
headers["content-type"] = contentType;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
174
180
|
|
|
175
|
-
|
|
176
|
-
const submitSuccessEvent = new CustomEvent('submit:success', {
|
|
181
|
+
const presubmitEvent = new CustomEvent("presubmit", {
|
|
177
182
|
detail: {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
183
|
+
body,
|
|
184
|
+
},
|
|
181
185
|
});
|
|
182
186
|
|
|
183
187
|
if (formRef) {
|
|
184
|
-
formRef.dispatchEvent(
|
|
188
|
+
formRef.dispatchEvent(presubmitEvent);
|
|
185
189
|
|
|
186
|
-
if (
|
|
190
|
+
if (presubmitEvent.defaultPrevented) {
|
|
187
191
|
return;
|
|
188
192
|
}
|
|
189
|
-
/* TODO: option to turn this on/off? */
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (resetFormOnSubmit !== false) {
|
|
193
|
-
formRef.reset();
|
|
194
|
-
}
|
|
195
193
|
}
|
|
196
|
-
/* TODO: client side route event first that can be preventDefaulted */
|
|
197
194
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
} else {
|
|
212
|
-
location.href = successUrl;
|
|
195
|
+
formState = "sending";
|
|
196
|
+
const formUrl = `${
|
|
197
|
+
builder.env === "dev" ? "http://localhost:5000" : "https://builder.io"
|
|
198
|
+
}/api/v1/form-submit?apiKey=${builder.apiKey}&to=${btoa(
|
|
199
|
+
sendSubmissionsToEmail || ""
|
|
200
|
+
)}&name=${encodeURIComponent(name || "")}`;
|
|
201
|
+
fetch(
|
|
202
|
+
sendSubmissionsTo === "email" ? formUrl : action,
|
|
203
|
+
/* TODO: throw error if no action URL */
|
|
204
|
+
{
|
|
205
|
+
body,
|
|
206
|
+
headers,
|
|
207
|
+
method: method || "post",
|
|
213
208
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
formRef.dispatchEvent(submitErrorEvent);
|
|
225
|
-
|
|
226
|
-
if (submitErrorEvent.defaultPrevented) {
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
responseData = err;
|
|
232
|
-
formState = 'error';
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
$: submissionState = () => {
|
|
237
|
-
return isEditing() && previewState || formState;
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
let formState = 'unsubmitted';
|
|
243
|
-
let responseData = null;
|
|
244
|
-
let formErrorMessage = '';
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
</script>
|
|
252
|
-
|
|
253
|
-
<form {...attributes} validate={validate} bind:this={formRef} action={!sendWithJs && action} method={method} name={name} on:submit="{event => onSubmit(event)}" >
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
{#if builderBlock && builderBlock.children }
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
{#each builderBlock?.children as block, index }
|
|
261
|
-
<RenderBlock block={block} ></RenderBlock>
|
|
262
|
-
{/each}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
{/if}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
{#if submissionState() === 'error' }
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
<svelte:component dataPath="errorMessage" blocks={errorMessage} this={BuilderBlocks} ></svelte:component>
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
{/if}
|
|
282
|
-
|
|
209
|
+
).then(
|
|
210
|
+
async (res) => {
|
|
211
|
+
let body;
|
|
212
|
+
const contentType = res.headers.get("content-type");
|
|
213
|
+
|
|
214
|
+
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
215
|
+
body = await res.json();
|
|
216
|
+
} else {
|
|
217
|
+
body = await res.text();
|
|
218
|
+
}
|
|
283
219
|
|
|
284
|
-
|
|
220
|
+
if (!res.ok && errorMessagePath) {
|
|
221
|
+
/* TODO: allow supplying an error formatter function */
|
|
222
|
+
let message = get(body, errorMessagePath);
|
|
285
223
|
|
|
286
|
-
|
|
224
|
+
if (message) {
|
|
225
|
+
if (typeof message !== "string") {
|
|
226
|
+
/* TODO: ideally convert json to yaml so it woul dbe like
|
|
227
|
+
error: - email has been taken */
|
|
228
|
+
message = JSON.stringify(message);
|
|
229
|
+
}
|
|
287
230
|
|
|
288
|
-
|
|
289
|
-
|
|
231
|
+
formErrorMessage = message;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
290
234
|
|
|
291
|
-
|
|
235
|
+
responseData = body;
|
|
236
|
+
formState = res.ok ? "success" : "error";
|
|
292
237
|
|
|
238
|
+
if (res.ok) {
|
|
239
|
+
const submitSuccessEvent = new CustomEvent("submit:success", {
|
|
240
|
+
detail: {
|
|
241
|
+
res,
|
|
242
|
+
body,
|
|
243
|
+
},
|
|
244
|
+
});
|
|
293
245
|
|
|
294
|
-
{
|
|
246
|
+
if (formRef) {
|
|
247
|
+
formRef.dispatchEvent(submitSuccessEvent);
|
|
295
248
|
|
|
249
|
+
if (submitSuccessEvent.defaultPrevented) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
/* TODO: option to turn this on/off? */
|
|
296
253
|
|
|
297
|
-
|
|
254
|
+
if (resetFormOnSubmit !== false) {
|
|
255
|
+
formRef.reset();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/* TODO: client side route event first that can be preventDefaulted */
|
|
259
|
+
|
|
260
|
+
if (successUrl) {
|
|
261
|
+
if (formRef) {
|
|
262
|
+
const event = new CustomEvent("route", {
|
|
263
|
+
detail: {
|
|
264
|
+
url: successUrl,
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
formRef.dispatchEvent(event);
|
|
268
|
+
|
|
269
|
+
if (!event.defaultPrevented) {
|
|
270
|
+
location.href = successUrl;
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
location.href = successUrl;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
(err) => {
|
|
279
|
+
const submitErrorEvent = new CustomEvent("submit:error", {
|
|
280
|
+
detail: {
|
|
281
|
+
error: err,
|
|
282
|
+
},
|
|
283
|
+
});
|
|
298
284
|
|
|
299
|
-
|
|
285
|
+
if (formRef) {
|
|
286
|
+
formRef.dispatchEvent(submitErrorEvent);
|
|
300
287
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
288
|
+
if (submitErrorEvent.defaultPrevented) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
305
292
|
|
|
293
|
+
responseData = err;
|
|
294
|
+
formState = "error";
|
|
295
|
+
}
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
$: submissionState = () => {
|
|
300
|
+
return (isEditing() && previewState) || formState;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
let formState = "unsubmitted";
|
|
304
|
+
let responseData = null;
|
|
305
|
+
let formErrorMessage = "";
|
|
306
|
+
</script>
|
|
307
|
+
|
|
308
|
+
<form
|
|
309
|
+
{...attributes}
|
|
310
|
+
{validate}
|
|
311
|
+
bind:this={formRef}
|
|
312
|
+
action={!sendWithJs && action}
|
|
313
|
+
{method}
|
|
314
|
+
{name}
|
|
315
|
+
on:submit={(event) => {
|
|
316
|
+
onSubmit(event);
|
|
317
|
+
}}
|
|
318
|
+
>
|
|
319
|
+
{#if builderBlock && builderBlock.children}
|
|
320
|
+
{#each builderBlock?.children as block}
|
|
321
|
+
<RenderBlock {block} context={builderContext} />
|
|
322
|
+
{/each}
|
|
323
|
+
{/if}
|
|
324
|
+
|
|
325
|
+
{#if submissionState() === "error"}
|
|
326
|
+
<BuilderBlocks dataPath="errorMessage" blocks={errorMessage} />
|
|
327
|
+
{/if}
|
|
328
|
+
|
|
329
|
+
{#if submissionState() === "sending"}
|
|
330
|
+
<BuilderBlocks dataPath="sendingMessage" blocks={sendingMessage} />
|
|
331
|
+
{/if}
|
|
332
|
+
|
|
333
|
+
{#if submissionState() === "error" && responseData}
|
|
334
|
+
<pre class="builder-form-error-text pre">
|
|
335
|
+
{JSON.stringify(responseData, null, 2)}
|
|
306
336
|
</pre>
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
{/if}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
{/if}
|
|
325
|
-
|
|
326
|
-
</form>
|
|
327
|
-
|
|
328
|
-
<style>
|
|
329
|
-
.pre {
|
|
330
|
-
padding: 10px;
|
|
331
|
-
color: red;
|
|
332
|
-
text-align: center; }
|
|
333
|
-
</style>
|
|
337
|
+
{/if}
|
|
338
|
+
|
|
339
|
+
{#if submissionState() === "success"}
|
|
340
|
+
<BuilderBlocks dataPath="successMessage" blocks={successMessage} />
|
|
341
|
+
{/if}
|
|
342
|
+
</form>
|
|
343
|
+
|
|
344
|
+
<style>
|
|
345
|
+
.pre {
|
|
346
|
+
padding: 10px;
|
|
347
|
+
color: red;
|
|
348
|
+
text-align: center;
|
|
349
|
+
}
|
|
350
|
+
</style>
|
|
@@ -1,28 +1,12 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
<script context="module" lang="ts">
|
|
2
|
+
export interface FragmentProps {
|
|
3
|
+
maxWidth?: number;
|
|
4
|
+
attributes?: any;
|
|
5
|
+
children?: any;
|
|
6
|
+
}
|
|
7
|
+
</script>
|
|
24
8
|
|
|
25
|
-
|
|
9
|
+
<script lang="ts">
|
|
26
10
|
</script>
|
|
27
11
|
|
|
28
|
-
<span
|
|
12
|
+
<span><slot /></span>
|