@builder.io/sdk-svelte 0.0.1-0

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.
Files changed (67) hide show
  1. package/package.json +12 -0
  2. package/src/blocks/button.svelte +38 -0
  3. package/src/blocks/columns.svelte +95 -0
  4. package/src/blocks/custom-code.svelte +63 -0
  5. package/src/blocks/embed.svelte +62 -0
  6. package/src/blocks/form.svelte +300 -0
  7. package/src/blocks/fragment.svelte +25 -0
  8. package/src/blocks/image.svelte +99 -0
  9. package/src/blocks/img.svelte +33 -0
  10. package/src/blocks/input.svelte +32 -0
  11. package/src/blocks/raw-text.svelte +26 -0
  12. package/src/blocks/section.svelte +33 -0
  13. package/src/blocks/select.svelte +34 -0
  14. package/src/blocks/submit-button.svelte +30 -0
  15. package/src/blocks/symbol.svelte +55 -0
  16. package/src/blocks/text.svelte +25 -0
  17. package/src/blocks/textarea.svelte +29 -0
  18. package/src/blocks/video.svelte +42 -0
  19. package/src/components/block-styles.svelte +23 -0
  20. package/src/components/error-boundary.svelte +23 -0
  21. package/src/components/render-block.svelte +121 -0
  22. package/src/components/render-blocks.svelte +70 -0
  23. package/src/components/render-content/components/render-styles.svelte +95 -0
  24. package/src/components/render-content/render-content.svelte +216 -0
  25. package/src/constants/device-sizes.js +46 -0
  26. package/src/constants/target.js +11 -0
  27. package/src/context/builder.context.js +33 -0
  28. package/src/functions/evaluate.js +51 -0
  29. package/src/functions/event-handler-name.js +14 -0
  30. package/src/functions/get-block-actions.js +46 -0
  31. package/src/functions/get-block-component-options.js +29 -0
  32. package/src/functions/get-block-properties.js +35 -0
  33. package/src/functions/get-block-styles.js +52 -0
  34. package/src/functions/get-block-tag.js +13 -0
  35. package/src/functions/get-builder-search-params/fn.test.js +31 -0
  36. package/src/functions/get-builder-search-params/index.js +29 -0
  37. package/src/functions/get-content/fn.test.js +49 -0
  38. package/src/functions/get-content/index.js +158 -0
  39. package/src/functions/get-fetch.js +35 -0
  40. package/src/functions/get-global-this.js +25 -0
  41. package/src/functions/get-processed-block.js +67 -0
  42. package/src/functions/get-processed-block.test.js +49 -0
  43. package/src/functions/if-target.js +38 -0
  44. package/src/functions/is-browser.js +13 -0
  45. package/src/functions/is-editing.js +30 -0
  46. package/src/functions/is-iframe.js +30 -0
  47. package/src/functions/is-previewing.js +37 -0
  48. package/src/functions/macro-eval.js +12 -0
  49. package/src/functions/on-change.js +34 -0
  50. package/src/functions/on-change.test.js +37 -0
  51. package/src/functions/previewing-model-name.js +34 -0
  52. package/src/functions/register-component.js +74 -0
  53. package/src/functions/register.js +52 -0
  54. package/src/functions/set-editor-settings.js +38 -0
  55. package/src/functions/set.js +18 -0
  56. package/src/functions/set.test.js +34 -0
  57. package/src/functions/track.js +45 -0
  58. package/src/functions/transform-block.js +13 -0
  59. package/src/index-helpers/blocks-exports.js +43 -0
  60. package/src/index-helpers/top-of-file.js +11 -0
  61. package/src/index.js +32 -0
  62. package/src/scripts/init-editing.js +89 -0
  63. package/src/types/builder-block.js +3 -0
  64. package/src/types/builder-content.js +3 -0
  65. package/src/types/deep-partial.js +3 -0
  66. package/src/types/targets.js +3 -0
  67. package/src/types/typescript.js +3 -0
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@builder.io/sdk-svelte",
3
+ "description": "Builder.io SDK for Svelte",
4
+ "version": "0.0.1-0",
5
+ "dependencies": {
6
+ "node-fetch": "^2.6.1"
7
+ },
8
+ "main": "src/index.js",
9
+ "scripts": {
10
+ "release:dev": "npm version prerelease --no-git-tag-version && npm publish --tag dev --access public"
11
+ }
12
+ }
@@ -0,0 +1,38 @@
1
+ <script>
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ export let attributes;
12
+ export let text;
13
+ export let link;
14
+ export let openLinkInNewTab;
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+ </script>
27
+
28
+
29
+
30
+ {#if link }
31
+
32
+ <a {...attributes} role="button" href={link} target={openLinkInNewTab ? '_blank' : undefined} >
33
+
34
+ {text}
35
+
36
+ </a>
37
+
38
+ {/if}
@@ -0,0 +1,95 @@
1
+ <script>
2
+
3
+
4
+
5
+
6
+ import RenderBlocks, { } from '../components/render-blocks.lite';
7
+
8
+
9
+
10
+
11
+
12
+ export let space;
13
+ export let columns;
14
+ export let stackColumnsAt;
15
+ export let reverseColumnsWhenStacked;
16
+
17
+ function getGutterSize() {
18
+ return typeof space === 'number' ? space || 0 : 20;
19
+ }
20
+
21
+ function getColumns() {
22
+ return columns || [];
23
+ }
24
+
25
+ function getWidth(index) {
26
+ const columns = this.getColumns();
27
+ return columns[index]?.width || 100 / columns.length;
28
+ }
29
+
30
+ function getColumnCssWidth(index) {
31
+ const columns = this.getColumns();
32
+ const gutterSize = this.getGutterSize();
33
+ const subtractWidth = gutterSize * (columns.length - 1) / columns.length;
34
+ return `calc(${this.getWidth(index)}% - ${subtractWidth}px)`;
35
+ }
36
+
37
+ function maybeApplyForTablet(prop) {
38
+ const stackColumnsAt = stackColumnsAt || 'tablet';
39
+ return stackColumnsAt === 'tablet' ? prop : 'inherit';
40
+ }
41
+ $: columnsCssVars = () => {
42
+ const flexDir = stackColumnsAt === 'never' ? 'inherit' : reverseColumnsWhenStacked ? 'column-reverse' : 'column';
43
+ return {
44
+ '--flex-dir': flexDir,
45
+ '--flex-dir-tablet': this.maybeApplyForTablet(flexDir)
46
+ };
47
+ };
48
+
49
+ $: columnCssVars = () => {
50
+ const width = '100%';
51
+ const marginLeft = '0';
52
+ return {
53
+ '--column-width': width,
54
+ '--column-margin-left': marginLeft,
55
+ '--column-width-tablet': this.maybeApplyForTablet(width),
56
+ '--column-margin-left-tablet': this.maybeApplyForTablet(marginLeft)
57
+ };
58
+ };
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+ </script>
68
+
69
+ <div class="builder-columns div" style={columnsCssVars()} >
70
+
71
+ {#each columns as column }<div class="builder-column div-2" style={{
72
+ width: getColumnCssWidth(index),
73
+ marginLeft: `${index === 0 ? 0 : getGutterSize()}px`,
74
+ ...columnCssVars()
75
+ }} >
76
+
77
+ <RenderBlocks blocks={column.blocks} ></RenderBlocks>
78
+
79
+ </div>{/each}
80
+
81
+ </div>
82
+
83
+ <style>
84
+ .div {
85
+ display: flex;
86
+ align-items: stretch;
87
+ line-height: normal; }@media (max-width: 999px) { .div {
88
+ flex-direction: var(--flex-dir-tablet); } }@media (max-width: 639px) { .div {
89
+ flex-direction: var(--flex-dir); } }.div-2 {
90
+ flex-grow: 1; }@media (max-width: 999px) { .div-2 {
91
+ width: var(--column-width-tablet) !important;
92
+ margin-left: var(--column-margin-left-tablet) !important; } }@media (max-width: 639px) { .div-2 {
93
+ width: var(--column-width) !important;
94
+ margin-left: var(--column-margin-left) !important; } }
95
+ </style>
@@ -0,0 +1,63 @@
1
+ <script>
2
+ import { onMount } from 'svelte'
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ export let elem;
12
+ export let replaceNodes;
13
+ export let code;
14
+
15
+ function findAndRunScripts() {
16
+ // TODO: Move this function to standalone one in '@builder.io/utils'
17
+ if (elem && typeof window !== 'undefined') {
18
+ /** @type {HTMLScriptElement[]} */
19
+ const scripts = elem.getElementsByTagName('script');
20
+
21
+ for (let i = 0; i < scripts.length; i++) {
22
+ const script = scripts[i];
23
+
24
+ if (script.src) {
25
+ if (scriptsInserted.includes(script.src)) {
26
+ continue;
27
+ }
28
+
29
+ scriptsInserted.push(script.src);
30
+ const newScript = document.createElement('script');
31
+ newScript.async = true;
32
+ newScript.src = script.src;
33
+ document.head.appendChild(newScript);
34
+ } else if (!script.type || ['text/javascript', 'application/javascript', 'application/ecmascript'].includes(script.type)) {
35
+ if (scriptsRun.includes(script.innerText)) {
36
+ continue;
37
+ }
38
+
39
+ try {
40
+ scriptsRun.push(script.innerText);
41
+ new Function(script.innerText)();
42
+ } catch (error) {
43
+ console.warn('`CustomCode`: Error running script:', error);
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+
51
+ let scriptsInserted = [];
52
+ let scriptsRun = [];
53
+
54
+ onMount(() => {
55
+ findAndRunScripts();
56
+ });
57
+
58
+
59
+
60
+
61
+ </script>
62
+
63
+ <div bind:this={elem} class={'builder-custom-code' + (replaceNodes ? ' replace-nodes' : '')} >{@html code}</div>
@@ -0,0 +1,62 @@
1
+ <script>
2
+ import { onMount } from 'svelte'
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ export let elem;
12
+ export let content;
13
+
14
+ function findAndRunScripts() {
15
+ // TODO: Move this function to standalone one in '@builder.io/utils'
16
+ if (elem && typeof window !== 'undefined') {
17
+ /** @type {HTMLScriptElement[]} */
18
+ const scripts = elem.getElementsByTagName('script');
19
+
20
+ for (let i = 0; i < scripts.length; i++) {
21
+ const script = scripts[i];
22
+
23
+ if (script.src) {
24
+ if (scriptsInserted.includes(script.src)) {
25
+ continue;
26
+ }
27
+
28
+ scriptsInserted.push(script.src);
29
+ const newScript = document.createElement('script');
30
+ newScript.async = true;
31
+ newScript.src = script.src;
32
+ document.head.appendChild(newScript);
33
+ } else if (!script.type || ['text/javascript', 'application/javascript', 'application/ecmascript'].includes(script.type)) {
34
+ if (scriptsRun.includes(script.innerText)) {
35
+ continue;
36
+ }
37
+
38
+ try {
39
+ scriptsRun.push(script.innerText);
40
+ new Function(script.innerText)();
41
+ } catch (error) {
42
+ console.warn('`Embed`: Error running script:', error);
43
+ }
44
+ }
45
+ }
46
+ }
47
+ }
48
+
49
+
50
+ let scriptsInserted = [];
51
+ let scriptsRun = [];
52
+
53
+ onMount(() => {
54
+ findAndRunScripts();
55
+ });
56
+
57
+
58
+
59
+
60
+ </script>
61
+
62
+ <div class="builder-embed" bind:this={elem} >{@html content}</div>
@@ -0,0 +1,300 @@
1
+ <script>
2
+
3
+
4
+
5
+
6
+ import RenderBlock, { } from '../components/render-block.lite';
7
+
8
+
9
+
10
+
11
+
12
+ export let formRef;
13
+ export let previewState;
14
+ export let sendWithJs;
15
+ export let sendSubmissionsTo;
16
+ export let action;
17
+ export let customHeaders;
18
+ export let contentType;
19
+ export let sendSubmissionsToEmail;
20
+ export let name;
21
+ export let method;
22
+ export let errorMessagePath;
23
+ export let resetFormOnSubmit;
24
+ export let successUrl;
25
+ export let validate;
26
+ export let attributes;
27
+ export let builderBlock;
28
+ export let errorMessage;
29
+ export let sendingMessage;
30
+ export let successMessage;
31
+
32
+ function onSubmit(event) {
33
+ const sendWithJs = sendWithJs || sendSubmissionsTo === 'email';
34
+
35
+ if (sendSubmissionsTo === 'zapier') {
36
+ event.preventDefault();
37
+ } else if (sendWithJs) {
38
+ if (!(action || sendSubmissionsTo === 'email')) {
39
+ event.preventDefault();
40
+ return;
41
+ }
42
+
43
+ event.preventDefault();
44
+ const el = event.currentTarget;
45
+ const headers = customHeaders || {};
46
+ let body;
47
+ const formData = new FormData(el); // TODO: maybe support null
48
+
49
+ const formPairs = Array.from(event.currentTarget.querySelectorAll('input,select,textarea')).filter(el => !!el.name).map(el => {
50
+ let value;
51
+ const key = el.name;
52
+
53
+ if (el instanceof HTMLInputElement) {
54
+ if (el.type === 'radio') {
55
+ if (el.checked) {
56
+ value = el.name;
57
+ return {
58
+ key,
59
+ value
60
+ };
61
+ }
62
+ } else if (el.type === 'checkbox') {
63
+ value = el.checked;
64
+ } else if (el.type === 'number' || el.type === 'range') {
65
+ const num = el.valueAsNumber;
66
+
67
+ if (!isNaN(num)) {
68
+ value = num;
69
+ }
70
+ } else if (el.type === 'file') {
71
+ // TODO: one vs multiple files
72
+ value = el.files;
73
+ } else {
74
+ value = el.value;
75
+ }
76
+ } else {
77
+ value = el.value;
78
+ }
79
+
80
+ return {
81
+ key,
82
+ value
83
+ };
84
+ });
85
+ let contentType = contentType;
86
+
87
+ if (sendSubmissionsTo === 'email') {
88
+ contentType = 'multipart/form-data';
89
+ }
90
+
91
+ Array.from(formPairs).forEach(({
92
+ value
93
+ }) => {
94
+ if (value instanceof File || Array.isArray(value) && value[0] instanceof File || value instanceof FileList) {
95
+ contentType = 'multipart/form-data';
96
+ }
97
+ }); // TODO: send as urlEncoded or multipart by default
98
+ // because of ease of use and reliability in browser API
99
+ // for encoding the form?
100
+
101
+ if (contentType !== 'application/json') {
102
+ body = formData;
103
+ } else {
104
+ // Json
105
+ const json = {};
106
+ Array.from(formPairs).forEach(({
107
+ value,
108
+ key
109
+ }) => {
110
+ set(json, key, value);
111
+ });
112
+ body = JSON.stringify(json);
113
+ }
114
+
115
+ if (contentType && contentType !== 'multipart/form-data') {
116
+ if (
117
+ /* Zapier doesn't allow content-type header to be sent from browsers */
118
+ !(sendWithJs && action?.includes('zapier.com'))) {
119
+ headers['content-type'] = contentType;
120
+ }
121
+ }
122
+
123
+ const presubmitEvent = new CustomEvent('presubmit', {
124
+ detail: {
125
+ body
126
+ }
127
+ });
128
+
129
+ if (formRef) {
130
+ formRef.dispatchEvent(presubmitEvent);
131
+
132
+ if (presubmitEvent.defaultPrevented) {
133
+ return;
134
+ }
135
+ }
136
+
137
+ state = 'sending';
138
+ const formUrl = `${builder.env === 'dev' ? 'http://localhost:5000' : 'https://builder.io'}/api/v1/form-submit?apiKey=${builder.apiKey}&to=${btoa(sendSubmissionsToEmail || '')}&name=${encodeURIComponent(name || '')}`;
139
+ fetch(sendSubmissionsTo === 'email' ? formUrl : action
140
+ /* TODO: throw error if no action URL */
141
+ , {
142
+ body,
143
+ headers,
144
+ method: method || 'post'
145
+ }).then(async res => {
146
+ let body;
147
+ const contentType = res.headers.get('content-type');
148
+
149
+ if (contentType && contentType.indexOf('application/json') !== -1) {
150
+ body = await res.json();
151
+ } else {
152
+ body = await res.text();
153
+ }
154
+
155
+ if (!res.ok && errorMessagePath) {
156
+ /* TODO: allow supplying an error formatter function */
157
+ let message = get(body, errorMessagePath);
158
+
159
+ if (message) {
160
+ if (typeof message !== 'string') {
161
+ /* TODO: ideally convert json to yaml so it woul dbe like
162
+ error: - email has been taken */
163
+ message = JSON.stringify(message);
164
+ }
165
+
166
+ formErrorMessage = message;
167
+ }
168
+ }
169
+
170
+ responseData = body;
171
+ state = res.ok ? 'success' : 'error';
172
+
173
+ if (res.ok) {
174
+ const submitSuccessEvent = new CustomEvent('submit:success', {
175
+ detail: {
176
+ res,
177
+ body
178
+ }
179
+ });
180
+
181
+ if (formRef) {
182
+ formRef.dispatchEvent(submitSuccessEvent);
183
+
184
+ if (submitSuccessEvent.defaultPrevented) {
185
+ return;
186
+ }
187
+ /* TODO: option to turn this on/off? */
188
+
189
+
190
+ if (resetFormOnSubmit !== false) {
191
+ formRef.reset();
192
+ }
193
+ }
194
+ /* TODO: client side route event first that can be preventDefaulted */
195
+
196
+
197
+ if (successUrl) {
198
+ if (formRef) {
199
+ const event = new CustomEvent('route', {
200
+ detail: {
201
+ url: successUrl
202
+ }
203
+ });
204
+ formRef.dispatchEvent(event);
205
+
206
+ if (!event.defaultPrevented) {
207
+ location.href = successUrl;
208
+ }
209
+ } else {
210
+ location.href = successUrl;
211
+ }
212
+ }
213
+ }
214
+ }, err => {
215
+ const submitErrorEvent = new CustomEvent('submit:error', {
216
+ detail: {
217
+ error: err
218
+ }
219
+ });
220
+
221
+ if (formRef) {
222
+ formRef.dispatchEvent(submitErrorEvent);
223
+
224
+ if (submitErrorEvent.defaultPrevented) {
225
+ return;
226
+ }
227
+ }
228
+
229
+ responseData = err;
230
+ state = 'error';
231
+ });
232
+ }
233
+ }
234
+ $: submissionState = () => {
235
+ return Builder.isEditing && previewState || state;
236
+ };
237
+
238
+ let state = 'unsubmitted';
239
+ let responseData = null;
240
+ let formErrorMessage = '';
241
+
242
+
243
+
244
+
245
+
246
+
247
+ </script>
248
+
249
+ <form {...attributes} validate={validate} bind:this={formRef} action={!sendWithJs && action} method={method} name={name} on:submit="{event => onSubmit(event)}" >
250
+
251
+ {#if builderBlock && builderBlock.children }
252
+
253
+ {#each builderBlock?.children as block }<RenderBlock block={block} ></RenderBlock>{/each}
254
+
255
+ {/if}
256
+
257
+
258
+
259
+ {#if submissionState() === 'error' }
260
+
261
+ <BuilderBlocks dataPath="errorMessage" blocks={errorMessage} ></BuilderBlocks>
262
+
263
+ {/if}
264
+
265
+
266
+
267
+ {#if submissionState() === 'sending' }
268
+
269
+ <BuilderBlocks dataPath="sendingMessage" blocks={sendingMessage} ></BuilderBlocks>
270
+
271
+ {/if}
272
+
273
+
274
+
275
+ {#if submissionState() === 'error' && responseData }
276
+
277
+ <pre class="builder-form-error-text pre" >
278
+
279
+ {JSON.stringify(responseData, null, 2)}
280
+
281
+ </pre>
282
+
283
+ {/if}
284
+
285
+
286
+
287
+ {#if submissionState() === 'success' }
288
+
289
+ <BuilderBlocks dataPath="successMessage" blocks={successMessage} ></BuilderBlocks>
290
+
291
+ {/if}
292
+
293
+ </form>
294
+
295
+ <style>
296
+ .pre {
297
+ padding: 10px;
298
+ color: red;
299
+ text-align: center; }
300
+ </style>
@@ -0,0 +1,25 @@
1
+ <script>
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ export let children;
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+ </script>
24
+
25
+ <span ><slot></slot></span>
@@ -0,0 +1,99 @@
1
+ <script>
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ export let altText;
12
+ export let backgroundSize;
13
+ export let class;
14
+ export let image;
15
+ export let srcset;
16
+ export let sizes;
17
+ export let aspectRatio;
18
+ export let fitContent;
19
+ export let builderBlock;
20
+ export let children;
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+ </script>
33
+
34
+ <div class="div" >
35
+
36
+ <picture >
37
+
38
+ <img loading="lazy" class="img" alt={altText} aria-role={altText ? 'presentation' : undefined} style={{
39
+ objectPosition: backgroundSize || 'center',
40
+ objectFit: backgroundSize || 'cover'
41
+ }} class={'builder-image' + (class ? ' ' + class : '')} src={image} srcset={srcset} sizes={sizes} />
42
+
43
+
44
+ <source srcSet={srcset} />
45
+
46
+ </picture>
47
+
48
+
49
+ {#if aspectRatio && !(fitContent && builderBlock?.children?.length) }<div class="builder-image-sizer div-2" style={{
50
+ paddingTop: aspectRatio * 100 + '%'
51
+ }} >
52
+
53
+ {' '}
54
+
55
+ </div>{/if}
56
+
57
+
58
+ {#if builderBlock?.children?.length && fitContent }
59
+
60
+ <slot></slot>
61
+
62
+ {/if}
63
+
64
+
65
+
66
+ {#if !fitContent }
67
+
68
+ <div class="div-3" >
69
+
70
+ <slot></slot>
71
+
72
+ </div>
73
+
74
+ {/if}
75
+
76
+ </div>
77
+
78
+ <style>
79
+ .div {
80
+ position: relative; }.img {
81
+ opacity: 1;
82
+ transition: opacity 0.2s ease-in-out;
83
+ position: absolute;
84
+ height: 100%;
85
+ width: 100%;
86
+ top: 0px;
87
+ left: 0px; }.div-2 {
88
+ width: 100%;
89
+ pointer-events: none;
90
+ font-size: 0; }.div-3 {
91
+ display: flex;
92
+ flex-direction: column;
93
+ align-items: stretch;
94
+ position: absolute;
95
+ top: 0;
96
+ left: 0;
97
+ width: 100%;
98
+ height: 100%; }
99
+ </style>