@builder.io/sdk-svelte 0.0.4 → 0.0.5
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/--package.json +3 -2
- package/package.json +3 -2
- package/src/blocks/BaseText.svelte +27 -0
- package/src/blocks/button/button.svelte +36 -0
- package/src/blocks/button/component-info.ts +42 -0
- package/src/blocks/columns/columns.svelte +148 -0
- package/src/blocks/columns/component-info.ts +254 -0
- package/src/blocks/custom-code/component-info.ts +31 -0
- package/src/blocks/custom-code/custom-code.svelte +66 -0
- package/src/blocks/embed/component-info.ts +53 -0
- package/src/blocks/embed/embed.svelte +51 -0
- package/src/blocks/embed/helpers.ts +8 -0
- package/src/blocks/form/component-info.ts +282 -0
- package/src/blocks/form/form.svelte +332 -0
- package/src/blocks/fragment/component-info.ts +10 -0
- package/src/blocks/fragment/fragment.svelte +12 -0
- package/src/blocks/image/component-info.ts +170 -0
- package/src/blocks/image/image.helpers.ts +75 -0
- package/src/blocks/image/image.svelte +145 -0
- package/src/blocks/img/component-info.ts +21 -0
- package/src/blocks/img/img.svelte +51 -0
- package/src/blocks/input/component-info.ts +78 -0
- package/src/blocks/input/input.svelte +34 -0
- package/src/blocks/raw-text/component-info.ts +15 -0
- package/src/blocks/raw-text/raw-text.svelte +16 -0
- package/src/blocks/section/component-info.ts +50 -0
- package/src/blocks/section/section.svelte +34 -0
- package/src/blocks/select/component-info.ts +61 -0
- package/src/blocks/select/select.svelte +34 -0
- package/src/blocks/submit-button/component-info.ts +32 -0
- package/src/blocks/submit-button/submit-button.svelte +15 -0
- package/src/blocks/symbol/component-info.ts +43 -0
- package/src/blocks/symbol/symbol.svelte +100 -0
- package/src/blocks/text/component-info.ts +24 -0
- package/src/blocks/text/text.svelte +11 -0
- package/src/blocks/textarea/component-info.ts +48 -0
- package/src/blocks/textarea/textarea.svelte +19 -0
- package/src/blocks/util.ts +6 -0
- package/src/blocks/video/component-info.ts +109 -0
- package/src/blocks/video/video.svelte +102 -0
- package/src/components/render-block/block-styles.svelte +62 -0
- package/src/components/render-block/render-block.helpers.ts +27 -0
- package/src/components/render-block/render-block.svelte +228 -0
- package/src/components/render-block/render-component-with-context.svelte +38 -0
- package/src/components/render-block/render-component.svelte +35 -0
- package/src/components/render-block/render-repeated-block.svelte +53 -0
- package/src/components/render-block/types.ts +7 -0
- package/src/components/render-blocks.svelte +104 -0
- package/src/components/render-content/components/render-styles.svelte +80 -0
- package/src/components/render-content/index.ts +1 -0
- package/src/components/render-content/render-content.svelte +354 -0
- package/src/components/render-inlined-styles.svelte +26 -0
- package/src/constants/builder-registered-components.ts +42 -0
- package/src/constants/device-sizes.ts +28 -0
- package/src/constants/target.ts +2 -0
- package/src/context/builder.context.ts +13 -0
- package/src/context/types.ts +23 -0
- package/src/functions/camel-to-kebab-case.ts +2 -0
- package/src/functions/evaluate.ts +52 -0
- package/src/functions/event-handler-name.ts +6 -0
- package/src/functions/extract-text-styles.ts +39 -0
- package/src/functions/fast-clone.ts +5 -0
- package/src/functions/get-block-actions-handler.ts +19 -0
- package/src/functions/get-block-actions.ts +25 -0
- package/src/functions/get-block-component-options.ts +12 -0
- package/src/functions/get-block-properties.ts +11 -0
- package/src/functions/get-block-tag.ts +7 -0
- package/src/functions/get-builder-search-params/fn.test.ts +19 -0
- package/src/functions/get-builder-search-params/index.ts +48 -0
- package/src/functions/get-content/ab-testing.ts +132 -0
- package/src/functions/get-content/fn.test.ts +35 -0
- package/src/functions/get-content/index.ts +77 -0
- package/src/functions/get-content/types.ts +36 -0
- package/src/functions/get-fetch.ts +14 -0
- package/src/functions/get-global-this.ts +15 -0
- package/src/functions/get-processed-block.test.ts +33 -0
- package/src/functions/get-processed-block.ts +54 -0
- package/src/functions/get-react-native-block-styles.ts +30 -0
- package/src/functions/if-target.ts +20 -0
- package/src/functions/is-browser.ts +3 -0
- package/src/functions/is-editing.ts +7 -0
- package/src/functions/is-iframe.ts +5 -0
- package/src/functions/is-previewing.ts +14 -0
- package/src/functions/mark-mutable.ts +7 -0
- package/src/functions/on-change.test.ts +23 -0
- package/src/functions/on-change.ts +29 -0
- package/src/functions/register-component.ts +67 -0
- package/src/functions/register.ts +46 -0
- package/src/functions/sanitize-react-native-block-styles.ts +65 -0
- package/src/functions/set-editor-settings.ts +16 -0
- package/src/functions/set.test.ts +19 -0
- package/src/functions/set.ts +26 -0
- package/src/functions/track.ts +90 -0
- package/src/functions/transform-block.ts +6 -0
- package/src/helpers/ab-tests.ts +22 -0
- package/src/helpers/cookie.ts +102 -0
- package/src/helpers/css.ts +33 -0
- package/src/helpers/flatten.ts +24 -0
- package/src/helpers/localStorage.ts +38 -0
- package/src/helpers/nullable.ts +4 -0
- package/src/helpers/sessionId.ts +34 -0
- package/src/helpers/time.ts +4 -0
- package/src/helpers/url.test.ts +16 -0
- package/src/helpers/url.ts +12 -0
- package/src/helpers/uuid.ts +17 -0
- package/src/helpers/visitorId.ts +38 -0
- package/src/index-helpers/blocks-exports.ts +10 -0
- package/src/index-helpers/top-of-file.ts +4 -0
- package/src/index.ts +12 -0
- package/src/scripts/init-editing.ts +102 -0
- package/src/types/builder-block.ts +58 -0
- package/src/types/builder-content.ts +39 -0
- package/src/types/can-track.ts +3 -0
- package/src/types/components.ts +188 -0
- package/src/types/deep-partial.ts +7 -0
- package/src/types/element.ts +60 -0
- package/src/types/targets.ts +6 -0
- package/src/types/typescript.ts +7 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<script context="module" lang="ts">
|
|
2
|
+
export interface EmbedProps {
|
|
3
|
+
content: string;
|
|
4
|
+
}
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import { afterUpdate } from "svelte";
|
|
9
|
+
|
|
10
|
+
import { isJsScript } from "./helpers.js";
|
|
11
|
+
|
|
12
|
+
export let content: EmbedProps["content"];
|
|
13
|
+
|
|
14
|
+
function findAndRunScripts() {
|
|
15
|
+
if (!elem || !elem.getElementsByTagName) return;
|
|
16
|
+
const scripts = elem.getElementsByTagName("script");
|
|
17
|
+
for (let i = 0; i < scripts.length; i++) {
|
|
18
|
+
const script = scripts[i];
|
|
19
|
+
if (script.src && !scriptsInserted.includes(script.src)) {
|
|
20
|
+
scriptsInserted.push(script.src);
|
|
21
|
+
const newScript = document.createElement("script");
|
|
22
|
+
newScript.async = true;
|
|
23
|
+
newScript.src = script.src;
|
|
24
|
+
document.head.appendChild(newScript);
|
|
25
|
+
} else if (isJsScript(script) && !scriptsRun.includes(script.innerText)) {
|
|
26
|
+
try {
|
|
27
|
+
scriptsRun.push(script.innerText);
|
|
28
|
+
new Function(script.innerText)();
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.warn("`Embed`: Error running script:", error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let elem;
|
|
37
|
+
|
|
38
|
+
let scriptsInserted = [];
|
|
39
|
+
let scriptsRun = [];
|
|
40
|
+
let ranInitFn = false;
|
|
41
|
+
|
|
42
|
+
function onUpdateFn_0() {
|
|
43
|
+
if (elem && !ranInitFn) {
|
|
44
|
+
ranInitFn = true;
|
|
45
|
+
findAndRunScripts();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
$: onUpdateFn_0(...[elem, ranInitFn]);
|
|
49
|
+
</script>
|
|
50
|
+
|
|
51
|
+
<div class="builder-embed" bind:this={elem}>{@html content}</div>
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import type { ComponentInfo } from '../../types/components';
|
|
2
|
+
|
|
3
|
+
export const componentInfo: ComponentInfo = {
|
|
4
|
+
name: 'Form:Form',
|
|
5
|
+
builtIn: true,
|
|
6
|
+
// editableTags: ['builder-form-error']
|
|
7
|
+
defaults: {
|
|
8
|
+
responsiveStyles: {
|
|
9
|
+
large: {
|
|
10
|
+
marginTop: '15px',
|
|
11
|
+
paddingBottom: '15px',
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
image:
|
|
16
|
+
'https://cdn.builder.io/api/v1/image/assets%2FIsxPKMo2gPRRKeakUztj1D6uqed2%2Fef36d2a846134910b64b88e6d18c5ca5',
|
|
17
|
+
inputs: [
|
|
18
|
+
{
|
|
19
|
+
name: 'sendSubmissionsTo',
|
|
20
|
+
type: 'string',
|
|
21
|
+
// TODO: save to builder data and user can download as csv
|
|
22
|
+
// TODO: easy for mode too or computed add/remove fields form mode
|
|
23
|
+
// so you can edit details and high level mode at same time...
|
|
24
|
+
// Later - more integrations like mailchimp
|
|
25
|
+
// /api/v1/form-submit?to=mailchimp
|
|
26
|
+
enum: [
|
|
27
|
+
{
|
|
28
|
+
label: 'Send to email',
|
|
29
|
+
value: 'email',
|
|
30
|
+
helperText:
|
|
31
|
+
'Send form submissions to the email address of your choosing',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
label: 'Custom',
|
|
35
|
+
value: 'custom',
|
|
36
|
+
helperText:
|
|
37
|
+
'Handle where the form requests go manually with a little code, e.g. to your own custom backend',
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
defaultValue: 'email',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'sendSubmissionsToEmail',
|
|
44
|
+
type: 'string',
|
|
45
|
+
required: true, // TODO: required: () => options.get("sendSubmissionsTo") === "email"
|
|
46
|
+
defaultValue: 'your@email.com',
|
|
47
|
+
showIf: 'options.get("sendSubmissionsTo") === "email"',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'sendWithJs',
|
|
51
|
+
type: 'boolean',
|
|
52
|
+
helperText: 'Set to false to use basic html form action',
|
|
53
|
+
defaultValue: true,
|
|
54
|
+
showIf: 'options.get("sendSubmissionsTo") === "custom"',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'name',
|
|
58
|
+
type: 'string',
|
|
59
|
+
defaultValue: 'My form',
|
|
60
|
+
// advanced: true
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'action',
|
|
64
|
+
type: 'string',
|
|
65
|
+
helperText: 'URL to send the form data to',
|
|
66
|
+
showIf: 'options.get("sendSubmissionsTo") === "custom"',
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'contentType',
|
|
70
|
+
type: 'string',
|
|
71
|
+
defaultValue: 'application/json',
|
|
72
|
+
advanced: true,
|
|
73
|
+
// TODO: do automatically if file input
|
|
74
|
+
enum: [
|
|
75
|
+
'application/json',
|
|
76
|
+
'multipart/form-data',
|
|
77
|
+
'application/x-www-form-urlencoded',
|
|
78
|
+
],
|
|
79
|
+
showIf:
|
|
80
|
+
'options.get("sendSubmissionsTo") === "custom" && options.get("sendWithJs") === true',
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: 'method',
|
|
84
|
+
type: 'string',
|
|
85
|
+
showIf: 'options.get("sendSubmissionsTo") === "custom"',
|
|
86
|
+
defaultValue: 'POST',
|
|
87
|
+
advanced: true,
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: 'previewState',
|
|
91
|
+
type: 'string',
|
|
92
|
+
// TODO: persist: false flag
|
|
93
|
+
enum: ['unsubmitted', 'sending', 'success', 'error'],
|
|
94
|
+
defaultValue: 'unsubmitted',
|
|
95
|
+
helperText:
|
|
96
|
+
'Choose a state to edit, e.g. choose "success" to show what users see on success and edit the message',
|
|
97
|
+
showIf:
|
|
98
|
+
'options.get("sendSubmissionsTo") !== "zapier" && options.get("sendWithJs") === true',
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: 'successUrl',
|
|
102
|
+
type: 'url',
|
|
103
|
+
helperText:
|
|
104
|
+
'Optional URL to redirect the user to on form submission success',
|
|
105
|
+
showIf:
|
|
106
|
+
'options.get("sendSubmissionsTo") !== "zapier" && options.get("sendWithJs") === true',
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: 'resetFormOnSubmit',
|
|
110
|
+
type: 'boolean',
|
|
111
|
+
showIf:
|
|
112
|
+
"options.get('sendSubmissionsTo') === 'custom' && options.get('sendWithJs') === true",
|
|
113
|
+
advanced: true,
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: 'successMessage',
|
|
117
|
+
type: 'uiBlocks',
|
|
118
|
+
hideFromUI: true,
|
|
119
|
+
defaultValue: [
|
|
120
|
+
{
|
|
121
|
+
'@type': '@builder.io/sdk:Element',
|
|
122
|
+
responsiveStyles: {
|
|
123
|
+
large: {
|
|
124
|
+
marginTop: '10px',
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
component: {
|
|
128
|
+
name: 'Text',
|
|
129
|
+
options: {
|
|
130
|
+
text: '<span>Thanks!</span>',
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: 'validate',
|
|
138
|
+
type: 'boolean',
|
|
139
|
+
defaultValue: true,
|
|
140
|
+
advanced: true,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
name: 'errorMessagePath',
|
|
144
|
+
type: 'text',
|
|
145
|
+
advanced: true,
|
|
146
|
+
helperText:
|
|
147
|
+
'Path to where to get the error message from in a JSON response to display to the user, e.g. "error.message" for a response like { "error": { "message": "this username is taken" }}',
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
name: 'errorMessage',
|
|
151
|
+
type: 'uiBlocks',
|
|
152
|
+
hideFromUI: true,
|
|
153
|
+
defaultValue: [
|
|
154
|
+
{
|
|
155
|
+
'@type': '@builder.io/sdk:Element',
|
|
156
|
+
responsiveStyles: {
|
|
157
|
+
large: {
|
|
158
|
+
marginTop: '10px',
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
bindings: {
|
|
162
|
+
'component.options.text':
|
|
163
|
+
'state.formErrorMessage || block.component.options.text',
|
|
164
|
+
},
|
|
165
|
+
component: {
|
|
166
|
+
name: 'Text',
|
|
167
|
+
options: {
|
|
168
|
+
text: '<span>Form submission error :( Please check your answers and try again</span>',
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
name: 'sendingMessage',
|
|
176
|
+
type: 'uiBlocks',
|
|
177
|
+
hideFromUI: true,
|
|
178
|
+
defaultValue: [
|
|
179
|
+
{
|
|
180
|
+
'@type': '@builder.io/sdk:Element',
|
|
181
|
+
responsiveStyles: {
|
|
182
|
+
large: {
|
|
183
|
+
marginTop: '10px',
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
component: {
|
|
187
|
+
name: 'Text',
|
|
188
|
+
options: {
|
|
189
|
+
text: '<span>Sending...</span>',
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
name: 'customHeaders',
|
|
197
|
+
type: 'map',
|
|
198
|
+
valueType: {
|
|
199
|
+
type: 'string',
|
|
200
|
+
},
|
|
201
|
+
advanced: true,
|
|
202
|
+
showIf:
|
|
203
|
+
'options.get("sendSubmissionsTo") === "custom" && options.get("sendWithJs") === true',
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
noWrap: true,
|
|
207
|
+
canHaveChildren: true,
|
|
208
|
+
defaultChildren: [
|
|
209
|
+
{
|
|
210
|
+
'@type': '@builder.io/sdk:Element',
|
|
211
|
+
responsiveStyles: {
|
|
212
|
+
large: {
|
|
213
|
+
marginTop: '10px',
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
component: {
|
|
217
|
+
name: 'Text',
|
|
218
|
+
options: {
|
|
219
|
+
text: '<span>Enter your name</span>',
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
'@type': '@builder.io/sdk:Element',
|
|
225
|
+
responsiveStyles: {
|
|
226
|
+
large: {
|
|
227
|
+
marginTop: '10px',
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
component: {
|
|
231
|
+
name: 'Form:Input',
|
|
232
|
+
options: {
|
|
233
|
+
name: 'name',
|
|
234
|
+
placeholder: 'Jane Doe',
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
'@type': '@builder.io/sdk:Element',
|
|
240
|
+
responsiveStyles: {
|
|
241
|
+
large: {
|
|
242
|
+
marginTop: '10px',
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
component: {
|
|
246
|
+
name: 'Text',
|
|
247
|
+
options: {
|
|
248
|
+
text: '<span>Enter your email</span>',
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
'@type': '@builder.io/sdk:Element',
|
|
254
|
+
responsiveStyles: {
|
|
255
|
+
large: {
|
|
256
|
+
marginTop: '10px',
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
component: {
|
|
260
|
+
name: 'Form:Input',
|
|
261
|
+
options: {
|
|
262
|
+
name: 'email',
|
|
263
|
+
placeholder: 'jane@doe.com',
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
'@type': '@builder.io/sdk:Element',
|
|
269
|
+
responsiveStyles: {
|
|
270
|
+
large: {
|
|
271
|
+
marginTop: '10px',
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
component: {
|
|
275
|
+
name: 'Form:SubmitButton',
|
|
276
|
+
options: {
|
|
277
|
+
text: 'Submit',
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
};
|
|
@@ -0,0 +1,332 @@
|
|
|
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
|
+
|
|
12
|
+
interface BuilderElement {}
|
|
13
|
+
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
15
|
+
// @ts-nocheck
|
|
16
|
+
|
|
17
|
+
/* eslint-disable */
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* This component was copied over from the old SDKs and has a lot of code pointing to invalid functions/env vars. It needs
|
|
21
|
+
* to be cleaned up before the component can actually be usable.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export interface FormProps {
|
|
25
|
+
attributes?: any;
|
|
26
|
+
name?: string;
|
|
27
|
+
action?: string;
|
|
28
|
+
validate?: boolean;
|
|
29
|
+
method?: string;
|
|
30
|
+
builderBlock?: BuilderElement;
|
|
31
|
+
sendSubmissionsTo?: string;
|
|
32
|
+
sendSubmissionsToEmail?: string;
|
|
33
|
+
sendWithJs?: boolean;
|
|
34
|
+
contentType?: string;
|
|
35
|
+
customHeaders?: {
|
|
36
|
+
[key: string]: string;
|
|
37
|
+
};
|
|
38
|
+
successUrl?: string;
|
|
39
|
+
previewState?: FormState;
|
|
40
|
+
successMessage?: BuilderElement[];
|
|
41
|
+
errorMessage?: BuilderElement[];
|
|
42
|
+
sendingMessage?: BuilderElement[];
|
|
43
|
+
resetFormOnSubmit?: boolean;
|
|
44
|
+
errorMessagePath?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
48
|
+
// @ts-nocheck
|
|
49
|
+
|
|
50
|
+
/* eslint-disable */
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* This component was copied over from the old SDKs and has a lot of code pointing to invalid functions/env vars. It needs
|
|
54
|
+
* to be cleaned up before the component can actually be usable.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
export type FormState = "unsubmitted" | "sending" | "success" | "error";
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<script lang="ts">
|
|
61
|
+
import { getContext, setContext } from "svelte";
|
|
62
|
+
|
|
63
|
+
import RenderBlock from "../../components/render-block/render-block.svelte";
|
|
64
|
+
import BuilderBlocks from "../../components/render-blocks.svelte";
|
|
65
|
+
import { isEditing } from "../../functions/is-editing.js";
|
|
66
|
+
|
|
67
|
+
export let previewState: FormProps["previewState"];
|
|
68
|
+
export let sendWithJs: FormProps["sendWithJs"];
|
|
69
|
+
export let sendSubmissionsTo: FormProps["sendSubmissionsTo"];
|
|
70
|
+
export let action: FormProps["action"];
|
|
71
|
+
export let customHeaders: FormProps["customHeaders"];
|
|
72
|
+
export let contentType: FormProps["contentType"];
|
|
73
|
+
export let sendSubmissionsToEmail: FormProps["sendSubmissionsToEmail"];
|
|
74
|
+
export let name: FormProps["name"];
|
|
75
|
+
export let method: FormProps["method"];
|
|
76
|
+
export let errorMessagePath: FormProps["errorMessagePath"];
|
|
77
|
+
export let resetFormOnSubmit: FormProps["resetFormOnSubmit"];
|
|
78
|
+
export let successUrl: FormProps["successUrl"];
|
|
79
|
+
export let validate: FormProps["validate"];
|
|
80
|
+
export let attributes: FormProps["attributes"];
|
|
81
|
+
export let builderBlock: FormProps["builderBlock"];
|
|
82
|
+
export let errorMessage: FormProps["errorMessage"];
|
|
83
|
+
export let sendingMessage: FormProps["sendingMessage"];
|
|
84
|
+
export let successMessage: FormProps["successMessage"];
|
|
85
|
+
|
|
86
|
+
let builderContext = getContext(BuilderContext.key);
|
|
87
|
+
|
|
88
|
+
function onSubmit(event) {
|
|
89
|
+
const sendWithJs = sendWithJs || sendSubmissionsTo === "email";
|
|
90
|
+
if (sendSubmissionsTo === "zapier") {
|
|
91
|
+
event.preventDefault();
|
|
92
|
+
} else if (sendWithJs) {
|
|
93
|
+
if (!(action || sendSubmissionsTo === "email")) {
|
|
94
|
+
event.preventDefault();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
const el = event.currentTarget;
|
|
99
|
+
const headers = customHeaders || {};
|
|
100
|
+
let body;
|
|
101
|
+
const formData = new FormData(el);
|
|
102
|
+
|
|
103
|
+
// TODO: maybe support null
|
|
104
|
+
const formPairs = Array.from(
|
|
105
|
+
event.currentTarget.querySelectorAll("input,select,textarea")
|
|
106
|
+
)
|
|
107
|
+
.filter((el) => !!el.name)
|
|
108
|
+
.map((el) => {
|
|
109
|
+
let value;
|
|
110
|
+
const key = el.name;
|
|
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
|
+
if (!isNaN(num)) {
|
|
125
|
+
value = num;
|
|
126
|
+
}
|
|
127
|
+
} else if (el.type === "file") {
|
|
128
|
+
// TODO: one vs multiple files
|
|
129
|
+
value = el.files;
|
|
130
|
+
} else {
|
|
131
|
+
value = el.value;
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
value = el.value;
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
key,
|
|
138
|
+
value,
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
let contentType = contentType;
|
|
142
|
+
if (sendSubmissionsTo === "email") {
|
|
143
|
+
contentType = "multipart/form-data";
|
|
144
|
+
}
|
|
145
|
+
Array.from(formPairs).forEach(({ value }) => {
|
|
146
|
+
if (
|
|
147
|
+
value instanceof File ||
|
|
148
|
+
(Array.isArray(value) && value[0] instanceof File) ||
|
|
149
|
+
value instanceof FileList
|
|
150
|
+
) {
|
|
151
|
+
contentType = "multipart/form-data";
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// TODO: send as urlEncoded or multipart by default
|
|
156
|
+
// because of ease of use and reliability in browser API
|
|
157
|
+
// for encoding the form?
|
|
158
|
+
if (contentType !== "application/json") {
|
|
159
|
+
body = formData;
|
|
160
|
+
} else {
|
|
161
|
+
// Json
|
|
162
|
+
const json = {};
|
|
163
|
+
Array.from(formPairs).forEach(({ value, key }) => {
|
|
164
|
+
set(json, key, value);
|
|
165
|
+
});
|
|
166
|
+
body = JSON.stringify(json);
|
|
167
|
+
}
|
|
168
|
+
if (contentType && contentType !== "multipart/form-data") {
|
|
169
|
+
if (
|
|
170
|
+
/* Zapier doesn't allow content-type header to be sent from browsers */
|
|
171
|
+
!(sendWithJs && action?.includes("zapier.com"))
|
|
172
|
+
) {
|
|
173
|
+
headers["content-type"] = contentType;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const presubmitEvent = new CustomEvent("presubmit", {
|
|
177
|
+
detail: {
|
|
178
|
+
body,
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
if (formRef) {
|
|
182
|
+
formRef.dispatchEvent(presubmitEvent);
|
|
183
|
+
if (presubmitEvent.defaultPrevented) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
formState = "sending";
|
|
188
|
+
const formUrl = `${
|
|
189
|
+
builder.env === "dev" ? "http://localhost:5000" : "https://builder.io"
|
|
190
|
+
}/api/v1/form-submit?apiKey=${builder.apiKey}&to=${btoa(
|
|
191
|
+
sendSubmissionsToEmail || ""
|
|
192
|
+
)}&name=${encodeURIComponent(name || "")}`;
|
|
193
|
+
fetch(
|
|
194
|
+
sendSubmissionsTo === "email"
|
|
195
|
+
? formUrl
|
|
196
|
+
: action /* TODO: throw error if no action URL */,
|
|
197
|
+
{
|
|
198
|
+
body,
|
|
199
|
+
headers,
|
|
200
|
+
method: method || "post",
|
|
201
|
+
}
|
|
202
|
+
).then(
|
|
203
|
+
async (res) => {
|
|
204
|
+
let body;
|
|
205
|
+
const contentType = res.headers.get("content-type");
|
|
206
|
+
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
207
|
+
body = await res.json();
|
|
208
|
+
} else {
|
|
209
|
+
body = await res.text();
|
|
210
|
+
}
|
|
211
|
+
if (!res.ok && errorMessagePath) {
|
|
212
|
+
/* TODO: allow supplying an error formatter function */
|
|
213
|
+
let message = get(body, errorMessagePath);
|
|
214
|
+
if (message) {
|
|
215
|
+
if (typeof message !== "string") {
|
|
216
|
+
/* TODO: ideally convert json to yaml so it woul dbe like
|
|
217
|
+
error: - email has been taken */
|
|
218
|
+
message = JSON.stringify(message);
|
|
219
|
+
}
|
|
220
|
+
formErrorMessage = message;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
responseData = body;
|
|
224
|
+
formState = res.ok ? "success" : "error";
|
|
225
|
+
if (res.ok) {
|
|
226
|
+
const submitSuccessEvent = new CustomEvent("submit:success", {
|
|
227
|
+
detail: {
|
|
228
|
+
res,
|
|
229
|
+
body,
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
if (formRef) {
|
|
233
|
+
formRef.dispatchEvent(submitSuccessEvent);
|
|
234
|
+
if (submitSuccessEvent.defaultPrevented) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
/* TODO: option to turn this on/off? */
|
|
238
|
+
if (resetFormOnSubmit !== false) {
|
|
239
|
+
formRef.reset();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/* TODO: client side route event first that can be preventDefaulted */
|
|
244
|
+
if (successUrl) {
|
|
245
|
+
if (formRef) {
|
|
246
|
+
const event = new CustomEvent("route", {
|
|
247
|
+
detail: {
|
|
248
|
+
url: successUrl,
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
formRef.dispatchEvent(event);
|
|
252
|
+
if (!event.defaultPrevented) {
|
|
253
|
+
location.href = successUrl;
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
location.href = successUrl;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
(err) => {
|
|
262
|
+
const submitErrorEvent = new CustomEvent("submit:error", {
|
|
263
|
+
detail: {
|
|
264
|
+
error: err,
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
if (formRef) {
|
|
268
|
+
formRef.dispatchEvent(submitErrorEvent);
|
|
269
|
+
if (submitErrorEvent.defaultPrevented) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
responseData = err;
|
|
274
|
+
formState = "error";
|
|
275
|
+
}
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
$: submissionState = () => {
|
|
280
|
+
return (isEditing() && previewState) || formState;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
let formRef;
|
|
284
|
+
|
|
285
|
+
let formState = "unsubmitted";
|
|
286
|
+
let responseData = null;
|
|
287
|
+
let formErrorMessage = "";
|
|
288
|
+
</script>
|
|
289
|
+
|
|
290
|
+
<form
|
|
291
|
+
{...attributes}
|
|
292
|
+
{validate}
|
|
293
|
+
bind:this={formRef}
|
|
294
|
+
action={!sendWithJs && action}
|
|
295
|
+
{method}
|
|
296
|
+
{name}
|
|
297
|
+
on:submit={(event) => {
|
|
298
|
+
onSubmit(event);
|
|
299
|
+
}}
|
|
300
|
+
>
|
|
301
|
+
{#if builderBlock && builderBlock.children}
|
|
302
|
+
{#each builderBlock?.children as block}
|
|
303
|
+
<RenderBlock {block} context={builderContext} />
|
|
304
|
+
{/each}
|
|
305
|
+
{/if}
|
|
306
|
+
|
|
307
|
+
{#if submissionState() === "error"}
|
|
308
|
+
<BuilderBlocks dataPath="errorMessage" blocks={errorMessage} />
|
|
309
|
+
{/if}
|
|
310
|
+
|
|
311
|
+
{#if submissionState() === "sending"}
|
|
312
|
+
<BuilderBlocks dataPath="sendingMessage" blocks={sendingMessage} />
|
|
313
|
+
{/if}
|
|
314
|
+
|
|
315
|
+
{#if submissionState() === "error" && responseData}
|
|
316
|
+
<pre class="builder-form-error-text pre">
|
|
317
|
+
{JSON.stringify(responseData, null, 2)}
|
|
318
|
+
</pre>
|
|
319
|
+
{/if}
|
|
320
|
+
|
|
321
|
+
{#if submissionState() === "success"}
|
|
322
|
+
<BuilderBlocks dataPath="successMessage" blocks={successMessage} />
|
|
323
|
+
{/if}
|
|
324
|
+
</form>
|
|
325
|
+
|
|
326
|
+
<style>
|
|
327
|
+
.pre {
|
|
328
|
+
padding: 10px;
|
|
329
|
+
color: red;
|
|
330
|
+
text-align: center;
|
|
331
|
+
}
|
|
332
|
+
</style>
|