@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,172 +1,202 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
- import BuilderContext from '../../context/builder.context';
7
- import { getBlockActions } from '../../functions/get-block-actions.js';
8
- import { getBlockComponentOptions } from '../../functions/get-block-component-options.js';
9
- import { getBlockProperties } from '../../functions/get-block-properties.js';
10
- import { getBlockStyles } from '../../functions/get-block-styles.js';
11
- import { getBlockTag } from '../../functions/get-block-tag.js';
12
- import { getProcessedBlock } from '../../functions/get-processed-block.js';
13
- import BlockStyles from './block-styles.svelte';
14
- import { isEmptyHtmlElement } from './render-block.helpers.js';
15
- import RenderComponent from './render-component.svelte';
16
-
17
-
18
-
19
- import { getContext, setContext } from "svelte";
20
-
21
-
22
- export let block;
23
-
24
-
25
- $: component = () => {
26
- const componentName = useBlock().component?.name;
27
-
28
- if (!componentName) {
29
- return null;
30
- }
31
-
32
- const ref = builderContext.registeredComponents[componentName];
33
-
34
- if (!ref) {
35
- // TODO: Public doc page with more info about this message
36
- console.warn(`
2
+ import BuilderContext from "../../context/builder.context";
3
+ import { getBlockActions } from "../../functions/get-block-actions.js";
4
+ import { getBlockComponentOptions } from "../../functions/get-block-component-options.js";
5
+ import { getBlockProperties } from "../../functions/get-block-properties.js";
6
+ import { getBlockStyles } from "../../functions/get-block-styles.js";
7
+ import { getBlockTag } from "../../functions/get-block-tag.js";
8
+ import { getProcessedBlock } from "../../functions/get-processed-block.js";
9
+ import { evaluate } from "../../functions/evaluate.js";
10
+ import BlockStyles from "./block-styles.svelte";
11
+ import { isEmptyHtmlElement } from "./render-block.helpers.js";
12
+ import RenderComponent from "./render-component.svelte";
13
+ import RenderRepeatedBlock from "./render-repeated-block.svelte";
14
+
15
+ import { getContext, setContext } from "svelte";
16
+
17
+ export let block;
18
+
19
+ $: component = () => {
20
+ const componentName = getProcessedBlock({
21
+ block: block,
22
+ state: builderContext.state,
23
+ context: builderContext.context,
24
+ evaluateBindings: false,
25
+ }).component?.name;
26
+
27
+ if (!componentName) {
28
+ return null;
29
+ }
30
+
31
+ const ref = builderContext.registeredComponents[componentName];
32
+
33
+ if (!ref) {
34
+ // TODO: Public doc page with more info about this message
35
+ console.warn(`
37
36
  Could not find a registered component named "${componentName}".
38
37
  If you registered it, is the file that registered it imported by the file that needs to render it?`);
39
- return undefined;
40
- } else {
41
- return ref;
42
- }
43
- };
44
-
45
- $: componentInfo = () => {
46
- if (component()) {
47
- const {
48
- component: _,
49
- ...info
50
- } = component();
51
- return info;
52
- } else {
53
- return undefined;
54
- }
55
- };
56
-
57
- $: componentRef = () => {
58
- return component?.()?.component;
59
- };
60
-
61
- $: tagName = () => {
62
- return getBlockTag(useBlock());
63
- };
64
-
65
- $: useBlock = () => {
66
- return getProcessedBlock({
67
- block: block,
68
- state: builderContext.state,
69
- context: builderContext.context
70
- });
71
- };
72
-
73
- $: attributes = () => {
74
- return { ...getBlockProperties(useBlock()),
75
- ...getBlockActions({
76
- block: useBlock(),
77
- state: builderContext.state,
78
- context: builderContext.context
79
- }),
80
- style: getBlockStyles(useBlock())
81
- };
82
- };
83
-
84
- $: shouldWrap = () => {
85
- return !componentInfo?.()?.noWrap;
86
- };
87
-
88
- $: componentOptions = () => {
89
- return { ...getBlockComponentOptions(useBlock()),
90
-
91
- /**
92
- * These attributes are passed to the wrapper element when there is one. If `noWrap` is set to true, then
93
- * they are provided to the component itself directly.
94
- */
95
- ...(shouldWrap() ? {} : {
96
- attributes: attributes()
97
- })
98
- };
99
- };
100
-
101
- $: children = () => {
102
- // TO-DO: When should `canHaveChildren` dictate rendering?
103
- // This is currently commented out because some Builder components (e.g. Box) do not have `canHaveChildren: true`,
104
- // but still receive and need to render children.
105
- // return componentInfo?.()?.canHaveChildren ? useBlock().children : [];
106
- return useBlock().children ?? [];
107
- };
108
-
109
- $: noCompRefChildren = () => {
110
- /**
111
- * When there is no `componentRef`, there might still be children that need to be rendered. In this case,
112
- * we render them outside of `componentRef`
113
- */
114
- return componentRef() ? [] : children();
115
- };
116
-
117
- let builderContext = getContext(BuilderContext.key);
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
- </script>
127
-
128
-
129
- {#if shouldWrap() }
130
-
131
-
132
-
133
- {#if !isEmptyHtmlElement(tagName()) }
134
-
135
-
136
- <svelte:element {...attributes()} this={tagName()} >
137
-
138
- <RenderComponent blockChildren={children()} componentRef={componentRef()} componentOptions={componentOptions()} ></RenderComponent>
139
-
140
-
141
-
142
-
143
-
144
- {#each noCompRefChildren() as child, index }
145
- <svelte:self key={'render-block-' + child.id} block={child} ></svelte:self>
146
- {/each}
147
-
148
-
149
-
150
-
151
- {#each noCompRefChildren() as child, index }
152
- <BlockStyles key={'block-style-' + child.id} block={child} ></BlockStyles>
153
- {/each}
154
-
155
-
156
- </svelte:element>
157
-
158
-
159
-
160
-
38
+ return undefined;
39
+ } else {
40
+ return ref;
41
+ }
42
+ };
43
+
44
+ $: componentInfo = () => {
45
+ if (component()) {
46
+ const { component: _, ...info } = component();
47
+ return info;
48
+ } else {
49
+ return undefined;
50
+ }
51
+ };
52
+
53
+ $: componentRef = () => {
54
+ return component?.()?.component;
55
+ };
56
+
57
+ $: tagName = () => {
58
+ return getBlockTag(useBlock());
59
+ };
60
+
61
+ $: useBlock = () => {
62
+ return repeatItemData()
63
+ ? block
64
+ : getProcessedBlock({
65
+ block: block,
66
+ state: builderContext.state,
67
+ context: builderContext.context,
68
+ evaluateBindings: true,
69
+ });
70
+ };
71
+
72
+ $: attributes = () => {
73
+ return {
74
+ ...getBlockProperties(useBlock()),
75
+ ...getBlockActions({
76
+ block: useBlock(),
77
+ state: builderContext.state,
78
+ context: builderContext.context,
79
+ }),
80
+ style: getBlockStyles(useBlock()),
81
+ };
82
+ };
83
+
84
+ $: shouldWrap = () => {
85
+ return !componentInfo?.()?.noWrap;
86
+ };
87
+
88
+ $: componentOptions = () => {
89
+ return {
90
+ ...getBlockComponentOptions(useBlock()),
91
+
92
+ /**
93
+ * These attributes are passed to the wrapper element when there is one. If `noWrap` is set to true, then
94
+ * they are provided to the component itself directly.
95
+ */
96
+ ...(shouldWrap()
97
+ ? {}
98
+ : {
99
+ attributes: attributes(),
100
+ }),
101
+ };
102
+ };
103
+
104
+ $: renderComponentProps = () => {
105
+ return {
106
+ blockChildren: children(),
107
+ componentRef: componentRef(),
108
+ componentOptions: componentOptions(),
109
+ };
110
+ };
111
+
112
+ $: children = () => {
113
+ // TO-DO: When should `canHaveChildren` dictate rendering?
114
+ // This is currently commented out because some Builder components (e.g. Box) do not have `canHaveChildren: true`,
115
+ // but still receive and need to render children.
116
+ // return componentInfo?.()?.canHaveChildren ? useBlock().children : [];
117
+ return useBlock().children ?? [];
118
+ };
119
+
120
+ $: childrenWithoutParentComponent = () => {
121
+ /**
122
+ * When there is no `componentRef`, there might still be children that need to be rendered. In this case,
123
+ * we render them outside of `componentRef`.
124
+ * NOTE: We make sure not to render this if `repeatItemData` is non-null, because that means we are rendering an array of
125
+ * blocks, and the children will be repeated within those blocks.
126
+ */
127
+ const shouldRenderChildrenOutsideRef = !componentRef() && !repeatItemData();
128
+ return shouldRenderChildrenOutsideRef ? children() : [];
129
+ };
130
+
131
+ $: repeatItemData = () => {
132
+ /**
133
+ * we don't use `useBlock()` here because the processing done within its logic includes evaluating the block's bindings,
134
+ * which will not work if there is a repeat.
135
+ */
136
+ const { repeat, ...blockWithoutRepeat } = block;
137
+
138
+ if (!repeat?.collection) {
139
+ return undefined;
140
+ }
141
+
142
+ const { collection, itemName } = repeat;
143
+ const itemsArray = evaluate({
144
+ code: collection,
145
+ state: builderContext.state,
146
+ context: builderContext.context,
147
+ });
148
+
149
+ if (Array.isArray(itemsArray)) {
150
+ const collectionName = collection.split(".").pop();
151
+ const itemNameToUse =
152
+ itemName || (collectionName ? collectionName + "Item" : "item");
153
+ const repeatArray = itemsArray.map((item, index) => ({
154
+ context: {
155
+ ...builderContext,
156
+ state: {
157
+ ...builderContext.state,
158
+ $index: index,
159
+ $item: item,
160
+ [itemNameToUse]: item,
161
+ [`$${itemNameToUse}Index`]: index,
162
+ },
163
+ },
164
+ block: blockWithoutRepeat,
165
+ }));
166
+ return repeatArray;
167
+ } else {
168
+ return undefined;
169
+ }
170
+ };
171
+
172
+ let builderContext = getContext(BuilderContext.key);
173
+ </script>
174
+
175
+ {#if shouldWrap()}
176
+ {#if !isEmptyHtmlElement(tagName())}
177
+ <svelte:element this={tagName()} {...attributes()}>
178
+ {#if repeatItemData()}
179
+ {#each repeatItemData() as data, index (index)}
180
+ <RenderRepeatedBlock
181
+ repeatContext={data.context}
182
+ block={data.block}
183
+ />
184
+ {/each}
185
+ {:else}
186
+ <RenderComponent {...renderComponentProps()} />
187
+ {/if}
188
+
189
+ {#each childrenWithoutParentComponent() as child, undefined ("render-block-" + child.id)}
190
+ <svelte:self block={child} />
191
+ {/each}
192
+
193
+ {#each childrenWithoutParentComponent() as child, undefined ("block-style-" + child.id)}
194
+ <BlockStyles block={child} />
195
+ {/each}
196
+ </svelte:element>
197
+ {:else}
198
+ <svelte:element this={tagName()} {...attributes()} />
199
+ {/if}
161
200
  {:else}
162
- <svelte:element {...attributes()} this={tagName()} ></svelte:element>
163
-
201
+ <RenderComponent {...renderComponentProps()} />
164
202
  {/if}
165
-
166
-
167
-
168
-
169
- {:else}
170
- <RenderComponent blockChildren={children()} componentRef={componentRef()} componentOptions={componentOptions()} ></RenderComponent>
171
-
172
- {/if}
@@ -1,58 +1,20 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
- import BlockStyles from './block-styles.svelte';
7
- import RenderBlock from './render-block.svelte';
2
+ import BlockStyles from "./block-styles.svelte";
3
+ import RenderBlock from "./render-block.svelte";
8
4
 
9
-
10
-
11
-
12
-
13
-
14
5
  export let componentRef;
15
- export let componentOptions;
16
- export let blockChildren;
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
6
+ export let componentOptions;
7
+ export let blockChildren;
30
8
  </script>
31
9
 
10
+ {#if componentRef}
11
+ <svelte:component this={componentRef} {...componentOptions}>
12
+ {#each blockChildren as child, undefined ("render-block-" + child.id)}
13
+ <RenderBlock block={child} />
14
+ {/each}
32
15
 
33
- {#if componentRef }
34
-
35
-
36
- <svelte:component {...componentOptions} this={componentRef} >
37
-
38
-
39
-
40
-
41
- {#each blockChildren as child, index }
42
- <RenderBlock key={'render-block-' + child.id} block={child} ></RenderBlock>
43
- {/each}
44
-
45
-
46
-
47
-
48
- {#each blockChildren as child, index }
49
- <BlockStyles key={'block-style-' + child.id} block={child} ></BlockStyles>
50
- {/each}
51
-
52
-
16
+ {#each blockChildren as child, undefined ("block-style-" + child.id)}
17
+ <BlockStyles block={child} />
18
+ {/each}
53
19
  </svelte:component>
54
-
55
-
56
-
57
-
58
- {/if}
20
+ {/if}
@@ -0,0 +1,29 @@
1
+ <script>
2
+ import BuilderContext from "../../context/builder.context";
3
+ import RenderBlock from "./render-block.svelte";
4
+
5
+ import { getContext, setContext } from "svelte";
6
+
7
+ export let block;
8
+ export let repeatContext;
9
+
10
+ setContext(BuilderContext.key, {
11
+ get content() {
12
+ return repeatContext.content;
13
+ },
14
+ get state() {
15
+ return repeatContext.state;
16
+ },
17
+ get context() {
18
+ return repeatContext.context;
19
+ },
20
+ get apiKey() {
21
+ return repeatContext.apiKey;
22
+ },
23
+ get registeredComponents() {
24
+ return repeatContext.registeredComponents;
25
+ },
26
+ });
27
+ </script>
28
+
29
+ <RenderBlock {block} />
@@ -1,101 +1,77 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
- import { isEditing } from '../functions/is-editing.js';
7
- import BlockStyles from './render-block/block-styles.svelte';
8
- import RenderBlock from './render-block/render-block.svelte';
9
-
10
-
11
-
12
-
13
-
14
-
15
- export let blocks;
16
- export let parent;
17
- export let path;
18
-
19
- const onClick = function onClick() {
20
- if (isEditing() && !blocks?.length) {
21
- window.parent?.postMessage({
22
- type: 'builder.clickEmptyBlocks',
23
- data: {
24
- parentElementId: parent,
25
- dataPath: path
2
+ import { isEditing } from "../functions/is-editing.js";
3
+ import BlockStyles from "./render-block/block-styles.svelte";
4
+ import RenderBlock from "./render-block/render-block.svelte";
5
+
6
+ export let blocks;
7
+ export let parent;
8
+ export let path;
9
+
10
+ const onClick = function onClick() {
11
+ if (isEditing() && !blocks?.length) {
12
+ window.parent?.postMessage(
13
+ {
14
+ type: "builder.clickEmptyBlocks",
15
+ data: {
16
+ parentElementId: parent,
17
+ dataPath: path,
18
+ },
19
+ },
20
+ "*"
21
+ );
26
22
  }
27
- }, '*');
28
- }
29
- };
30
-
31
- const onMouseEnter = function onMouseEnter() {
32
- if (isEditing() && !blocks?.length) {
33
- window.parent?.postMessage({
34
- type: 'builder.hoverEmptyBlocks',
35
- data: {
36
- parentElementId: parent,
37
- dataPath: path
23
+ };
24
+
25
+ const onMouseEnter = function onMouseEnter() {
26
+ if (isEditing() && !blocks?.length) {
27
+ window.parent?.postMessage(
28
+ {
29
+ type: "builder.hoverEmptyBlocks",
30
+ data: {
31
+ parentElementId: parent,
32
+ dataPath: path,
33
+ },
34
+ },
35
+ "*"
36
+ );
38
37
  }
39
- }, '*');
40
- }
41
- };
42
- $: className = () => {
43
- return 'builder-blocks' + (!blocks?.length ? ' no-blocks' : '');
44
- };
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
- </script>
56
-
57
- <div class={className() + 'div'} builder-path={path} builder-parent-id={parent} dataSet={{
58
- class: className()
59
- }} on:click="{event => onClick()}" on:mouseenter="{event => onMouseEnter()}" >
60
-
61
-
62
-
63
-
64
- {#if blocks }
65
-
66
-
67
-
68
- {#each blocks as block, index }
69
- <RenderBlock key={'render-block-' + block.id} block={block} ></RenderBlock>
70
- {/each}
71
-
72
-
73
-
74
-
75
-
76
- {/if}
77
-
78
-
79
-
80
- {#if blocks }
81
-
82
-
83
-
84
- {#each blocks as block, index }
85
- <BlockStyles key={'block-style-' + block.id} block={block} ></BlockStyles>
86
- {/each}
87
-
88
-
89
-
90
-
91
-
92
- {/if}
93
-
94
- </div>
95
-
96
- <style>
97
- .div {
98
- display: flex;
99
- flex-direction: column;
100
- align-items: stretch; }
101
- </style>
38
+ };
39
+ $: className = () => {
40
+ return "builder-blocks" + (!blocks?.length ? " no-blocks" : "");
41
+ };
42
+ </script>
43
+
44
+ <div
45
+ class={className() + " div"}
46
+ builder-path={path}
47
+ builder-parent-id={parent}
48
+ dataSet={{
49
+ class: className(),
50
+ }}
51
+ on:click={(event) => {
52
+ onClick();
53
+ }}
54
+ on:mouseenter={(event) => {
55
+ onMouseEnter();
56
+ }}
57
+ >
58
+ {#if blocks}
59
+ {#each blocks as block, undefined ("render-block-" + block.id)}
60
+ <RenderBlock {block} />
61
+ {/each}
62
+ {/if}
63
+
64
+ {#if blocks}
65
+ {#each blocks as block, undefined ("block-style-" + block.id)}
66
+ <BlockStyles {block} />
67
+ {/each}
68
+ {/if}
69
+ </div>
70
+
71
+ <style>
72
+ .div {
73
+ display: flex;
74
+ flex-direction: column;
75
+ align-items: stretch;
76
+ }
77
+ </style>