@builder.io/sdk-svelte 0.0.1-20 → 0.0.1-23

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 (40) hide show
  1. package/package.json +1 -1
  2. package/src/blocks/button/button.svelte +17 -48
  3. package/src/blocks/columns/columns.svelte +110 -102
  4. package/src/blocks/columns/component-info.js +25 -1
  5. package/src/blocks/custom-code/custom-code.svelte +59 -61
  6. package/src/blocks/embed/component-info.js +20 -1
  7. package/src/blocks/embed/embed.svelte +38 -56
  8. package/src/blocks/embed/helpers.js +9 -0
  9. package/src/blocks/form/form.svelte +264 -294
  10. package/src/blocks/fragment/fragment.svelte +1 -26
  11. package/src/blocks/image/component-info.js +47 -1
  12. package/src/blocks/image/image.helpers.js +48 -0
  13. package/src/blocks/image/image.svelte +121 -117
  14. package/src/blocks/img/img.svelte +22 -32
  15. package/src/blocks/input/input.svelte +17 -30
  16. package/src/blocks/raw-text/raw-text.svelte +4 -26
  17. package/src/blocks/section/section.svelte +20 -36
  18. package/src/blocks/select/select.svelte +16 -36
  19. package/src/blocks/submit-button/submit-button.svelte +4 -30
  20. package/src/blocks/symbol/symbol.svelte +57 -57
  21. package/src/blocks/text/text.svelte +1 -25
  22. package/src/blocks/textarea/textarea.svelte +5 -29
  23. package/src/blocks/video/video.svelte +35 -43
  24. package/src/components/render-block/block-styles.svelte +49 -59
  25. package/src/components/render-block/render-block.svelte +197 -167
  26. package/src/components/render-block/render-component.svelte +13 -51
  27. package/src/components/render-block/render-repeated-block.svelte +29 -0
  28. package/src/components/render-blocks.svelte +74 -98
  29. package/src/components/render-content/components/render-styles.svelte +42 -62
  30. package/src/components/render-content/render-content.svelte +252 -241
  31. package/src/components/render-inlined-styles.svelte +15 -45
  32. package/src/constants/builder-registered-components.js +4 -1
  33. package/src/context/builder.context.js +7 -1
  34. package/src/functions/convert-style-object.js +6 -0
  35. package/src/functions/get-block-styles.js +6 -3
  36. package/src/functions/get-processed-block.js +15 -8
  37. package/src/functions/get-processed-block.test.js +2 -1
  38. package/src/functions/register-component.js +10 -18
  39. package/src/functions/sanitize-styles.js +5 -0
  40. package/src/functions/fast-clone.js +0 -4
@@ -1,27 +1,20 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
- import RenderInlinedStyles from '../../render-inlined-styles.svelte';
7
-
8
-
2
+ import RenderInlinedStyles from "../../render-inlined-styles.svelte";
9
3
 
10
-
4
+ export let cssCode;
5
+ export let customFonts;
11
6
 
12
-
13
- export let cssCode;
14
- export let customFonts;
15
-
16
- const getCssFromFont = function getCssFromFont(font) {
17
- // TODO: compute what font sizes are used and only load those.......
18
- const family = font.family + (font.kind && !font.kind.includes('#') ? ', ' + font.kind : '');
19
- const name = family.split(',')[0];
20
- const url = font.fileUrl ?? font?.files?.regular;
21
- let str = '';
7
+ const getCssFromFont = function getCssFromFont(font) {
8
+ // TODO: compute what font sizes are used and only load those.......
9
+ const family =
10
+ font.family +
11
+ (font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
12
+ const name = family.split(",")[0];
13
+ const url = font.fileUrl ?? font?.files?.regular;
14
+ let str = "";
22
15
 
23
- if (url && family && name) {
24
- str += `
16
+ if (url && family && name) {
17
+ str += `
25
18
  @font-face {
26
19
  font-family: "${family}";
27
20
  src: local("${name}"), url('${url}') format('woff2');
@@ -29,21 +22,20 @@ if (url && family && name) {
29
22
  font-weight: 400;
30
23
  }
31
24
  `.trim();
32
- }
33
-
34
- if (font.files) {
35
- for (const weight in font.files) {
36
- const isNumber = String(Number(weight)) === weight;
25
+ }
37
26
 
38
- if (!isNumber) {
39
- continue;
40
- } // TODO: maybe limit number loaded
27
+ if (font.files) {
28
+ for (const weight in font.files) {
29
+ const isNumber = String(Number(weight)) === weight;
41
30
 
31
+ if (!isNumber) {
32
+ continue;
33
+ } // TODO: maybe limit number loaded
42
34
 
43
- const weightUrl = font.files[weight];
35
+ const weightUrl = font.files[weight];
44
36
 
45
- if (weightUrl && weightUrl !== url) {
46
- str += `
37
+ if (weightUrl && weightUrl !== url) {
38
+ str += `
47
39
  @font-face {
48
40
  font-family: "${family}";
49
41
  src: url('${weightUrl}') format('woff2');
@@ -51,40 +43,28 @@ if (font.files) {
51
43
  font-weight: ${weight};
52
44
  }
53
45
  `.trim();
46
+ }
47
+ }
54
48
  }
55
- }
56
- }
57
49
 
58
- return str;
59
- };
50
+ return str;
51
+ };
60
52
 
61
- const getFontCss = function getFontCss({
62
- customFonts
63
- }) {
64
- // TODO: flag for this
65
- // if (!builder.allowCustomFonts) {
66
- // return '';
67
- // }
68
- // TODO: separate internal data from external
69
- return customFonts?.map(font => getCssFromFont(font))?.join(' ') || '';
70
- };
71
- $: injectedStyles = () => {
72
- return `
73
- ${cssCode || ''}
53
+ const getFontCss = function getFontCss({ customFonts }) {
54
+ // TODO: flag for this
55
+ // if (!builder.allowCustomFonts) {
56
+ // return '';
57
+ // }
58
+ // TODO: separate internal data from external
59
+ return customFonts?.map((font) => getCssFromFont(font))?.join(" ") || "";
60
+ };
61
+ $: injectedStyles = () => {
62
+ return `
63
+ ${cssCode || ""}
74
64
  ${getFontCss({
75
- customFonts: customFonts
65
+ customFonts: customFonts,
76
66
  })}`;
77
- };
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
- </script>
67
+ };
68
+ </script>
89
69
 
90
- <RenderInlinedStyles styles={injectedStyles()} ></RenderInlinedStyles>
70
+ <RenderInlinedStyles styles={injectedStyles()} />
@@ -1,267 +1,278 @@
1
1
  <script>
2
- import { onMount } from 'svelte'
3
- import { afterUpdate } from 'svelte'
4
- import { onDestroy } from 'svelte'
5
-
6
- import { getDefaultRegisteredComponents } from '../../constants/builder-registered-components.js';
7
- import { TARGET } from '../../constants/target.js';
8
- import BuilderContext from '../../context/builder.context';
9
- import { evaluate } from '../../functions/evaluate.js';
10
- import { convertSearchParamsToQueryObject, getBuilderSearchParams } from '../../functions/get-builder-search-params/index.js';
11
- import { getContent } from '../../functions/get-content/index.js';
12
- import { getFetch } from '../../functions/get-fetch.js';
13
- import { isBrowser } from '../../functions/is-browser.js';
14
- import { isEditing } from '../../functions/is-editing.js';
15
- import { isPreviewing } from '../../functions/is-previewing.js';
16
- import { previewingModelName } from '../../functions/previewing-model-name.js';
17
- import { components, createRegisterComponentMessage } from '../../functions/register-component.js';
18
- import { track } from '../../functions/track.js';
19
- import RenderBlocks from '../render-blocks.svelte';
20
- import RenderContentStyles from './components/render-styles.svelte';
21
-
22
-
23
-
24
- import { getContext, setContext } from "svelte";
25
-
26
-
27
- export let content;
28
- export let data;
29
- export let customComponents;
30
- export let model;
31
- export let apiKey;
32
-
33
- const processMessage = function processMessage(event) {
34
- const {
35
- data
36
- } = event;
37
-
38
- if (data) {
39
- switch (data.type) {
40
- case 'builder.contentUpdate':
41
- {
42
- const messageContent = data.data;
43
- const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
44
- const contentData = messageContent.data;
45
-
46
- if (key === model) {
47
- overrideContent = contentData;
2
+ import { onMount } from "svelte";
3
+ import { afterUpdate } from "svelte";
4
+ import { onDestroy } from "svelte";
5
+
6
+ import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components.js";
7
+ import { TARGET } from "../../constants/target.js";
8
+ import BuilderContext from "../../context/builder.context";
9
+ import { evaluate } from "../../functions/evaluate.js";
10
+ import {
11
+ convertSearchParamsToQueryObject,
12
+ getBuilderSearchParams,
13
+ } from "../../functions/get-builder-search-params/index.js";
14
+ import { getContent } from "../../functions/get-content/index.js";
15
+ import { getFetch } from "../../functions/get-fetch.js";
16
+ import { isBrowser } from "../../functions/is-browser.js";
17
+ import { isEditing } from "../../functions/is-editing.js";
18
+ import { isPreviewing } from "../../functions/is-previewing.js";
19
+ import { previewingModelName } from "../../functions/previewing-model-name.js";
20
+ import {
21
+ components,
22
+ createRegisterComponentMessage,
23
+ } from "../../functions/register-component.js";
24
+ import { track } from "../../functions/track.js";
25
+ import RenderBlocks from "../render-blocks.svelte";
26
+ import RenderContentStyles from "./components/render-styles.svelte";
27
+
28
+ import { getContext, setContext } from "svelte";
29
+
30
+ export let content;
31
+ export let data;
32
+ export let context;
33
+ export let customComponents;
34
+ export let model;
35
+ export let apiKey;
36
+
37
+ const processMessage = function processMessage(event) {
38
+ const { data } = event;
39
+
40
+ if (data) {
41
+ switch (data.type) {
42
+ case "builder.contentUpdate": {
43
+ const messageContent = data.data;
44
+ const key =
45
+ messageContent.key ||
46
+ messageContent.alias ||
47
+ messageContent.entry ||
48
+ messageContent.modelName;
49
+ const contentData = messageContent.data;
50
+
51
+ if (key === model) {
52
+ overrideContent = contentData;
53
+ }
54
+
55
+ break;
48
56
  }
49
57
 
50
- break;
58
+ case "builder.patchUpdates": {
59
+ // TODO
60
+ break;
61
+ }
51
62
  }
63
+ }
64
+ };
52
65
 
53
- case 'builder.patchUpdates':
54
- {
55
- // TODO
56
- break;
57
- }
58
- }
59
- }
60
- };
66
+ const evaluateJsCode = function evaluateJsCode() {
67
+ // run any dynamic JS code attached to content
68
+ const jsCode = useContent?.()?.data?.jsCode;
61
69
 
62
- const evaluateJsCode = function evaluateJsCode() {
63
- // run any dynamic JS code attached to content
64
- const jsCode = useContent?.()?.data?.jsCode;
70
+ if (jsCode) {
71
+ evaluate({
72
+ code: jsCode,
73
+ context: contextContext(),
74
+ state: contentState(),
75
+ });
76
+ }
77
+ };
65
78
 
66
- if (jsCode) {
67
- evaluate({
68
- code: jsCode,
69
- context: context(),
70
- state: contentState()
71
- });
72
- }
73
- };
79
+ const evalExpression = function evalExpression(expression) {
80
+ return expression.replace(/{{([^}]+)}}/g, (_match, group) =>
81
+ evaluate({
82
+ code: group,
83
+ context: contextContext(),
84
+ state: contentState(),
85
+ })
86
+ );
87
+ };
74
88
 
75
- const evalExpression = function evalExpression(expression) {
76
- return expression.replace(/{{([^}]+)}}/g, (_match, group) => evaluate({
77
- code: group,
78
- context: context(),
79
- state: contentState()
80
- }));
81
- };
89
+ const handleRequest = function handleRequest({ url, key }) {
90
+ const fetchAndSetState = async () => {
91
+ const fetch = await getFetch();
92
+ const response = await fetch(url);
93
+ const json = await response.json();
94
+ const newOverrideState = { ...overrideState, [key]: json };
95
+ overrideState = newOverrideState;
96
+ };
82
97
 
83
- const handleRequest = function handleRequest({
84
- url,
85
- key
86
- }) {
87
- const fetchAndSetState = async () => {
88
- const fetch = await getFetch();
89
- const response = await fetch(url);
90
- const json = await response.json();
91
- const newOverrideState = { ...overrideState,
92
- [key]: json
98
+ fetchAndSetState();
93
99
  };
94
- overrideState = newOverrideState;
95
- };
96
100
 
97
- fetchAndSetState();
98
- };
99
-
100
- const runHttpRequests = function runHttpRequests() {
101
- const requests = useContent?.()?.data?.httpRequests ?? {};
102
- Object.entries(requests).forEach(([key, url]) => {
103
- if (url && (!httpReqsData()[key] || isEditing())) {
104
- const evaluatedUrl = evalExpression(url);
105
- handleRequest({
106
- url: evaluatedUrl,
107
- key
101
+ const runHttpRequests = function runHttpRequests() {
102
+ const requests = useContent?.()?.data?.httpRequests ?? {};
103
+ Object.entries(requests).forEach(([key, url]) => {
104
+ if (url && (!httpReqsData()[key] || isEditing())) {
105
+ const evaluatedUrl = evalExpression(url);
106
+ handleRequest({
107
+ url: evaluatedUrl,
108
+ key,
109
+ });
110
+ }
108
111
  });
109
- }
110
- });
111
- };
112
+ };
112
113
 
113
- const emitStateUpdate = function emitStateUpdate() {
114
- if (isEditing()) {
115
- window.dispatchEvent(new CustomEvent('builder:component:stateChange', {
116
- detail: {
117
- state: contentState(),
118
- ref: {
119
- name: model
120
- }
114
+ const emitStateUpdate = function emitStateUpdate() {
115
+ if (isEditing()) {
116
+ window.dispatchEvent(
117
+ new CustomEvent("builder:component:stateChange", {
118
+ detail: {
119
+ state: contentState(),
120
+ ref: {
121
+ name: model,
122
+ },
123
+ },
124
+ })
125
+ );
121
126
  }
122
- }));
123
- }
124
- };
125
- $: useContent = () => {
126
- const mergedContent = { ...content,
127
- ...overrideContent,
128
- data: { ...content?.data,
129
- ...data,
130
- ...overrideContent?.data
131
- }
132
- };
133
- return mergedContent;
134
- };
135
-
136
- $: contentState = () => {
137
- return { ...content?.data?.state,
138
- ...data,
139
- ...overrideState
140
- };
141
- };
142
-
143
- $: context = () => {
144
- return {};
145
- };
127
+ };
128
+ $: useContent = () => {
129
+ const mergedContent = {
130
+ ...content,
131
+ ...overrideContent,
132
+ data: { ...content?.data, ...data, ...overrideContent?.data },
133
+ };
134
+ return mergedContent;
135
+ };
146
136
 
147
- $: allRegisteredComponents = () => {
148
- const allComponentsArray = [...getDefaultRegisteredComponents(), // While this `components` object is deprecated, we must maintain support for it.
149
- // Since users are able to override our default components, we need to make sure that we do not break such
150
- // existing usage.
151
- // This is why we spread `components` after the default Builder.io components, but before the `customComponents`,
152
- // which is the new standard way of providing custom components, and must therefore take precedence.
153
- ...components, ...(customComponents || [])];
154
- const allComponents = allComponentsArray.reduce((acc, curr) => ({ ...acc,
155
- [curr.name]: curr
156
- }), {});
157
- return allComponents;
158
- };
137
+ $: contentState = () => {
138
+ return { ...content?.data?.state, ...data, ...overrideState };
139
+ };
159
140
 
160
- $: httpReqsData = () => {
161
- return {};
162
- };
141
+ $: contextContext = () => {
142
+ return context || {};
143
+ };
163
144
 
164
-
165
- setContext(BuilderContext.key, { get content() {
166
- return useContent();
167
- }, get state() {
168
- return contentState();
169
- }, get context() {
170
- return context();
171
- }, get apiKey() {
172
- return apiKey;
173
- }, get registeredComponents() {
174
- return allRegisteredComponents();
175
- },});
176
- let overrideContent = null;
177
- let update = 0;
178
- let overrideState = {};
145
+ $: allRegisteredComponents = () => {
146
+ const allComponentsArray = [
147
+ ...getDefaultRegisteredComponents(), // While this `components` object is deprecated, we must maintain support for it.
148
+ // Since users are able to override our default components, we need to make sure that we do not break such
149
+ // existing usage.
150
+ // This is why we spread `components` after the default Builder.io components, but before the `customComponents`,
151
+ // which is the new standard way of providing custom components, and must therefore take precedence.
152
+ ...components,
153
+ ...(customComponents || []),
154
+ ];
155
+ const allComponents = allComponentsArray.reduce(
156
+ (acc, curr) => ({ ...acc, [curr.name]: curr }),
157
+ {}
158
+ );
159
+ return allComponents;
160
+ };
179
161
 
180
- onMount(() => { if (isBrowser()) {
181
- if (isEditing()) {
182
- Object.values(allRegisteredComponents()).forEach(registeredComponent => {
183
- const message = createRegisterComponentMessage(registeredComponent);
184
- window.parent?.postMessage(message, '*');
185
- });
186
- window.addEventListener('message', processMessage);
187
- window.addEventListener('builder:component:stateChangeListenerActivated', emitStateUpdate);
188
- }
162
+ $: httpReqsData = () => {
163
+ return {};
164
+ };
189
165
 
190
- if (useContent()) {
191
- track('impression', {
192
- contentId: useContent().id
166
+ setContext(BuilderContext.key, {
167
+ get content() {
168
+ return useContent();
169
+ },
170
+ get state() {
171
+ return contentState();
172
+ },
173
+ get context() {
174
+ return contextContext();
175
+ },
176
+ get apiKey() {
177
+ return apiKey;
178
+ },
179
+ get registeredComponents() {
180
+ return allRegisteredComponents();
181
+ },
193
182
  });
194
- } // override normal content in preview mode
195
-
196
-
197
- if (isPreviewing()) {
198
- if (model && previewingModelName() === model) {
199
- const currentUrl = new URL(location.href);
200
- const previewApiKey = currentUrl.searchParams.get('apiKey');
183
+ let overrideContent = null;
184
+ let update = 0;
185
+ let overrideState = {};
186
+
187
+ onMount(() => {
188
+ if (isBrowser()) {
189
+ if (isEditing()) {
190
+ Object.values(allRegisteredComponents()).forEach(
191
+ (registeredComponent) => {
192
+ const message = createRegisterComponentMessage(registeredComponent);
193
+ window.parent?.postMessage(message, "*");
194
+ }
195
+ );
196
+ window.addEventListener("message", processMessage);
197
+ window.addEventListener(
198
+ "builder:component:stateChangeListenerActivated",
199
+ emitStateUpdate
200
+ );
201
+ }
201
202
 
202
- if (previewApiKey) {
203
- getContent({
204
- model: model,
205
- apiKey: previewApiKey,
206
- options: getBuilderSearchParams(convertSearchParamsToQueryObject(currentUrl.searchParams))
207
- }).then(content => {
208
- if (content) {
209
- overrideContent = content;
203
+ if (useContent()) {
204
+ track("impression", {
205
+ contentId: useContent().id,
206
+ });
207
+ } // override normal content in preview mode
208
+
209
+ if (isPreviewing()) {
210
+ if (model && previewingModelName() === model) {
211
+ const currentUrl = new URL(location.href);
212
+ const previewApiKey = currentUrl.searchParams.get("apiKey");
213
+
214
+ if (previewApiKey) {
215
+ getContent({
216
+ model: model,
217
+ apiKey: previewApiKey,
218
+ options: getBuilderSearchParams(
219
+ convertSearchParamsToQueryObject(currentUrl.searchParams)
220
+ ),
221
+ }).then((content) => {
222
+ if (content) {
223
+ overrideContent = content;
224
+ }
225
+ });
226
+ }
210
227
  }
211
- });
212
- }
213
- }
214
- }
215
-
216
- evaluateJsCode();
217
- runHttpRequests();
218
- emitStateUpdate();
219
- } });
220
-
221
-
222
- function onUpdateFn_0() {
223
- evaluateJsCode();
224
- }
225
- $: onUpdateFn_0(...[useContent?.()?.data?.jsCode])
226
- ;
227
- function onUpdateFn_1() {
228
- runHttpRequests();
229
- }
230
- $: onUpdateFn_1(...[useContent?.()?.data?.httpRequests])
231
- ;
232
- function onUpdateFn_2() {
233
- emitStateUpdate();
234
- }
235
- $: onUpdateFn_2(...[contentState()])
236
-
237
-
238
- onDestroy(() => { if (isBrowser()) {
239
- window.removeEventListener('message', processMessage);
240
- window.removeEventListener('builder:component:stateChangeListenerActivated', emitStateUpdate);
241
- } });
242
- </script>
243
-
244
-
245
- {#if useContent() }
246
-
247
-
248
- <div on:click="{event => track('click', {
249
- contentId: useContent().id
250
- })}" data-builder-content-id={useContent?.()?.id} >
251
-
228
+ }
252
229
 
253
- {#if (useContent?.()?.data?.cssCode || useContent?.()?.data?.customFonts?.length) && TARGET !== 'reactNative' }
254
- <RenderContentStyles cssCode={useContent().data.cssCode} customFonts={useContent().data.customFonts} ></RenderContentStyles>
230
+ evaluateJsCode();
231
+ runHttpRequests();
232
+ emitStateUpdate();
233
+ }
234
+ });
255
235
 
236
+ function onUpdateFn_0() {
237
+ evaluateJsCode();
238
+ }
239
+ $: onUpdateFn_0(...[useContent?.()?.data?.jsCode]);
240
+ function onUpdateFn_1() {
241
+ runHttpRequests();
242
+ }
243
+ $: onUpdateFn_1(...[useContent?.()?.data?.httpRequests]);
244
+ function onUpdateFn_2() {
245
+ emitStateUpdate();
246
+ }
247
+ $: onUpdateFn_2(...[contentState()]);
248
+
249
+ onDestroy(() => {
250
+ if (isBrowser()) {
251
+ window.removeEventListener("message", processMessage);
252
+ window.removeEventListener(
253
+ "builder:component:stateChangeListenerActivated",
254
+ emitStateUpdate
255
+ );
256
+ }
257
+ });
258
+ </script>
256
259
 
260
+ {#if useContent()}
261
+ <div
262
+ on:click={(event) => {
263
+ track("click", {
264
+ contentId: useContent().id,
265
+ });
266
+ }}
267
+ data-builder-content-id={useContent?.()?.id}
268
+ >
269
+ {#if (useContent?.()?.data?.cssCode || useContent?.()?.data?.customFonts?.length) && TARGET !== "reactNative"}
270
+ <RenderContentStyles
271
+ cssCode={useContent().data.cssCode}
272
+ customFonts={useContent().data.customFonts}
273
+ />
274
+ {/if}
275
+
276
+ <RenderBlocks blocks={useContent?.()?.data?.blocks} />
277
+ </div>
257
278
  {/if}
258
-
259
-
260
- <RenderBlocks blocks={useContent?.()?.data?.blocks} ></RenderBlocks>
261
-
262
- </div>
263
-
264
-
265
-
266
-
267
- {/if}