@builder.io/sdk-svelte 0.0.1-3 → 0.0.1-30

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 (77) hide show
  1. package/README.md +19 -0
  2. package/package.json +8 -1
  3. package/src/blocks/button/button.svelte +25 -47
  4. package/src/blocks/columns/columns.svelte +115 -102
  5. package/src/blocks/columns/component-info.js +25 -1
  6. package/src/blocks/custom-code/custom-code.svelte +59 -61
  7. package/src/blocks/embed/component-info.js +20 -1
  8. package/src/blocks/embed/embed.svelte +38 -56
  9. package/src/blocks/embed/helpers.js +9 -0
  10. package/src/blocks/form/form.svelte +264 -294
  11. package/src/blocks/fragment/fragment.svelte +1 -25
  12. package/src/blocks/image/component-info.js +47 -1
  13. package/src/blocks/image/image.helpers.js +48 -0
  14. package/src/blocks/image/image.svelte +126 -116
  15. package/src/blocks/img/img.svelte +25 -35
  16. package/src/blocks/input/input.svelte +17 -30
  17. package/src/blocks/raw-text/raw-text.svelte +4 -25
  18. package/src/blocks/section/section.svelte +20 -35
  19. package/src/blocks/select/select.svelte +16 -36
  20. package/src/blocks/submit-button/submit-button.svelte +4 -29
  21. package/src/blocks/symbol/component-info.js +1 -0
  22. package/src/blocks/symbol/symbol.svelte +67 -52
  23. package/src/blocks/text/text.svelte +1 -24
  24. package/src/blocks/textarea/textarea.svelte +5 -28
  25. package/src/blocks/video/video.svelte +60 -41
  26. package/src/components/render-block/block-styles.svelte +54 -50
  27. package/src/components/render-block/render-block.helpers.js +23 -0
  28. package/src/components/render-block/render-block.svelte +199 -159
  29. package/src/components/render-block/render-component.svelte +20 -0
  30. package/src/components/render-block/render-repeated-block.svelte +29 -0
  31. package/src/components/render-block/types.js +0 -0
  32. package/src/components/render-blocks.svelte +74 -79
  33. package/src/components/render-content/components/render-styles.svelte +43 -63
  34. package/src/components/render-content/index.js +4 -0
  35. package/src/components/render-content/render-content.svelte +266 -207
  36. package/src/components/render-inlined-styles.svelte +15 -43
  37. package/src/constants/builder-registered-components.js +48 -0
  38. package/src/constants/device-sizes.js +3 -21
  39. package/src/context/builder.context.js +7 -1
  40. package/src/functions/camel-to-kebab-case.js +4 -0
  41. package/src/functions/convert-style-object.js +6 -0
  42. package/src/functions/get-block-component-options.js +6 -1
  43. package/src/functions/get-block-styles.js +7 -4
  44. package/src/functions/get-builder-search-params/index.js +12 -1
  45. package/src/functions/get-content/ab-testing.js +38 -0
  46. package/src/functions/get-content/fn.test.js +1 -1
  47. package/src/functions/get-content/index.js +16 -58
  48. package/src/functions/get-content/types.js +0 -0
  49. package/src/functions/get-processed-block.js +15 -8
  50. package/src/functions/get-processed-block.test.js +2 -1
  51. package/src/functions/register-component.js +45 -26
  52. package/src/functions/sanitize-styles.js +5 -0
  53. package/src/helpers/css.js +12 -0
  54. package/src/helpers/flatten.js +34 -0
  55. package/src/index-helpers/blocks-exports.js +10 -8
  56. package/src/types/components.js +0 -0
  57. package/src/types/element.js +0 -0
  58. package/src/blocks/button/index.js +0 -7
  59. package/src/blocks/columns/index.js +0 -7
  60. package/src/blocks/custom-code/index.js +0 -7
  61. package/src/blocks/embed/index.js +0 -7
  62. package/src/blocks/form/index.js +0 -7
  63. package/src/blocks/fragment/index.js +0 -7
  64. package/src/blocks/image/index.js +0 -7
  65. package/src/blocks/img/index.js +0 -7
  66. package/src/blocks/input/index.js +0 -7
  67. package/src/blocks/raw-text/index.js +0 -7
  68. package/src/blocks/section/index.js +0 -7
  69. package/src/blocks/select/index.js +0 -7
  70. package/src/blocks/submit-button/index.js +0 -7
  71. package/src/blocks/symbol/index.js +0 -7
  72. package/src/blocks/text/index.js +0 -7
  73. package/src/blocks/textarea/index.js +0 -7
  74. package/src/blocks/video/index.js +0 -7
  75. package/src/components/error-boundary.svelte +0 -26
  76. package/src/functions/macro-eval.js +0 -5
  77. package/src/functions/previewing-model-name.js +0 -11
@@ -1,163 +1,203 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
- import { TARGET } from '../../constants/target.js';
7
- import BuilderContext, { } from '../../context/builder.context';
8
- import { getBlockActions } from '../../functions/get-block-actions.js';
9
- import { getBlockComponentOptions } from '../../functions/get-block-component-options.js';
10
- import { getBlockProperties } from '../../functions/get-block-properties.js';
11
- import { getBlockStyles } from '../../functions/get-block-styles.js';
12
- import { getBlockTag } from '../../functions/get-block-tag.js';
13
- import { getProcessedBlock } from '../../functions/get-processed-block.js';
14
- import { components } from '../../functions/register-component.js';
15
- import BlockStyles, { } from './block-styles.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 = components[componentName];
33
-
34
- if (componentName && !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
- }
40
-
41
- return ref;
42
- };
43
-
44
- $: componentInfo = () => {
45
- return component?.()?.info;
46
- };
47
-
48
- $: componentRef = () => {
49
- return component?.()?.component;
50
- };
51
-
52
- $: tagName = () => {
53
- return getBlockTag(useBlock());
54
- };
55
-
56
- $: useBlock = () => {
57
- return getProcessedBlock({
58
- block: block,
59
- state: builderContext.state,
60
- context: builderContext.context
61
- });
62
- };
63
-
64
- $: propertiesAndActions = () => {
65
- return { ...getBlockProperties(useBlock()),
66
- ...getBlockActions({
67
- block: useBlock(),
68
- state: builderContext.state,
69
- context: builderContext.context
70
- })
71
- };
72
- };
73
-
74
- $: css = () => {
75
- return getBlockStyles(useBlock());
76
- };
77
-
78
- $: componentOptions = () => {
79
- return getBlockComponentOptions(useBlock());
80
- };
81
-
82
- $: children = () => {
83
- // TO-DO: When should `canHaveChildren` dictate rendering?
84
- // This is currently commented out because some Builder components (e.g. Box) do not have `canHaveChildren: true`,
85
- // but still receive and need to render children.
86
- // return componentInfo?.()?.canHaveChildren ? useBlock().children : [];
87
- return useBlock().children ?? [];
88
- };
89
-
90
- $: noCompRefChildren = () => {
91
- return componentRef() ? [] : children();
92
- };
93
-
94
- let builderContext = getContext(BuilderContext.key);
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
- </script>
104
-
105
-
106
-
107
-
108
- {#if !componentInfo?.()?.noWrap }
109
-
110
-
111
- <svelte:element {...propertiesAndActions()} style={css()} this={tagName()} >
112
-
113
-
114
- {#if TARGET === 'vue' || TARGET === 'svelte' }
115
-
116
-
117
- <BlockStyles block={useBlock()} ></BlockStyles>
118
-
119
-
120
-
121
-
122
- {/if}
123
-
124
-
125
-
126
- {#if componentRef() }
127
- <svelte:component {...componentOptions()} builderBlock={useBlock()} this={componentRef()} >
128
-
129
-
130
- {#each children() as child, index }
131
- <svelte:self block={child} ></svelte:self>
132
- {/each}
133
-
134
-
135
- </svelte:component>
136
-
137
-
138
- {/if}
139
-
140
-
141
-
142
- {#each noCompRefChildren() as child, index }
143
- <svelte:self block={child} ></svelte:self>
144
- {/each}
145
-
146
-
147
- </svelte:element>
148
-
149
-
150
-
151
-
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 itemsArray = evaluate({
143
+ code: repeat.collection,
144
+ state: builderContext.state,
145
+ context: builderContext.context,
146
+ });
147
+
148
+ if (!Array.isArray(itemsArray)) {
149
+ return undefined;
150
+ }
151
+
152
+ const collectionName = repeat.collection.split(".").pop();
153
+ const itemNameToUse =
154
+ repeat.itemName || (collectionName ? collectionName + "Item" : "item");
155
+ const repeatArray = itemsArray.map((item, index) => ({
156
+ context: {
157
+ ...builderContext,
158
+ state: {
159
+ ...builderContext.state,
160
+ $index: index,
161
+ $item: item,
162
+ [itemNameToUse]: item,
163
+ [`$${itemNameToUse}Index`]: index,
164
+ },
165
+ },
166
+ block: blockWithoutRepeat,
167
+ }));
168
+ return repeatArray;
169
+ };
170
+
171
+ let builderContext = getContext(BuilderContext.key);
172
+ </script>
173
+
174
+ {#if shouldWrap()}
175
+ {#if !isEmptyHtmlElement(tagName())}
176
+ <svelte:element this={tagName()} {...attributes()}>
177
+ {#if repeatItemData()}
178
+ {#each repeatItemData() as data, index (index)}
179
+ <RenderRepeatedBlock
180
+ repeatContext={data.context}
181
+ block={data.block}
182
+ />
183
+ {/each}
184
+ {/if}
185
+
186
+ {#if !repeatItemData()}
187
+ <RenderComponent {...renderComponentProps()} />
188
+ {/if}
189
+
190
+ {#each childrenWithoutParentComponent() as child, undefined ("render-block-" + child.id)}
191
+ <svelte:self block={child} />
192
+ {/each}
193
+
194
+ {#each childrenWithoutParentComponent() as child, undefined ("block-style-" + child.id)}
195
+ <BlockStyles block={child} />
196
+ {/each}
197
+ </svelte:element>
198
+ {:else}
199
+ <svelte:element this={tagName()} {...attributes()} />
200
+ {/if}
152
201
  {:else}
153
- <svelte:component {...componentOptions()} attributes={propertiesAndActions()} builderBlock={useBlock()} style={css()} this={componentRef()} >
154
-
155
-
156
- {#each children() as child, index }
157
- <svelte:self block={child} ></svelte:self>
158
- {/each}
159
-
160
-
161
- </svelte:component>
162
-
163
- {/if}
202
+ <RenderComponent {...renderComponentProps()} />
203
+ {/if}
@@ -0,0 +1,20 @@
1
+ <script>
2
+ import BlockStyles from "./block-styles.svelte";
3
+ import RenderBlock from "./render-block.svelte";
4
+
5
+ export let componentRef;
6
+ export let componentOptions;
7
+ export let blockChildren;
8
+ </script>
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}
15
+
16
+ {#each blockChildren as child, undefined ("block-style-" + child.id)}
17
+ <BlockStyles block={child} />
18
+ {/each}
19
+ </svelte:component>
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} />
File without changes
@@ -1,82 +1,77 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
- import { isEditing } from '../functions/is-editing.js';
7
- import RenderBlock, { } from './render-block/render-block.svelte';
8
-
9
-
10
-
11
-
12
-
13
-
14
- export let blocks;
15
- export let parent;
16
- export let path;
17
-
18
- function onClick() {
19
- if (isEditing() && !blocks?.length) {
20
- window.parent?.postMessage({
21
- type: 'builder.clickEmptyBlocks',
22
- data: {
23
- parentElementId: parent,
24
- 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
+ );
25
22
  }
26
- }, '*');
27
- }
28
- }
29
-
30
- function onMouseEnter() {
31
- if (isEditing() && !blocks?.length) {
32
- window.parent?.postMessage({
33
- type: 'builder.hoverEmptyBlocks',
34
- data: {
35
- parentElementId: parent,
36
- 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
+ );
37
37
  }
38
- }, '*');
39
- }
40
- }
41
- $: className = () => {
42
- return 'builder-blocks' + (!blocks?.length ? ' no-blocks' : '');
43
- };
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
- </script>
55
-
56
- <div builder-path={path} builder-parent-id={parent} dataSet={{
57
- class: className()
58
- }} on:click="{event => onClick()}" on:mouseenter="{event => onMouseEnter()}" class={className()} >
59
-
60
-
61
- {#if blocks }
62
-
63
-
64
-
65
- {#each blocks as block, index }
66
- <RenderBlock key={block.id} block={block} ></RenderBlock>
67
- {/each}
68
-
69
-
70
-
71
-
72
-
73
- {/if}
74
-
75
- </div>
76
-
77
- <style>
78
- .div {
79
- display: flex;
80
- flex-direction: column;
81
- align-items: stretch; }
82
- </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>