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

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