@builder.io/sdk-svelte 0.0.1-2 → 0.0.1-22

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 (82) hide show
  1. package/README.md +19 -0
  2. package/package.json +22 -4
  3. package/src/blocks/button/button.svelte +17 -47
  4. package/src/blocks/columns/columns.svelte +110 -102
  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 -25
  11. package/src/blocks/image/image.svelte +85 -116
  12. package/src/blocks/img/img.svelte +25 -35
  13. package/src/blocks/input/input.svelte +17 -30
  14. package/src/blocks/raw-text/raw-text.svelte +4 -25
  15. package/src/blocks/section/section.svelte +20 -35
  16. package/src/blocks/select/select.svelte +16 -36
  17. package/src/blocks/submit-button/submit-button.svelte +4 -29
  18. package/src/blocks/symbol/component-info.js +1 -0
  19. package/src/blocks/symbol/symbol.svelte +57 -52
  20. package/src/blocks/text/text.svelte +1 -24
  21. package/src/blocks/textarea/textarea.svelte +5 -28
  22. package/src/blocks/video/video.svelte +35 -42
  23. package/src/components/render-block/block-styles.svelte +54 -50
  24. package/src/components/render-block/render-block.helpers.js +23 -0
  25. package/src/components/render-block/render-block.svelte +198 -159
  26. package/src/components/render-block/render-component.svelte +20 -0
  27. package/src/components/render-block/render-repeated-block.svelte +29 -0
  28. package/src/components/render-blocks.svelte +74 -79
  29. package/src/components/render-content/components/render-styles.svelte +43 -63
  30. package/src/components/render-content/index.js +4 -0
  31. package/src/components/render-content/render-content.svelte +258 -208
  32. package/src/components/render-inlined-styles.svelte +15 -43
  33. package/src/constants/builder-registered-components.js +48 -0
  34. package/src/constants/device-sizes.js +3 -21
  35. package/src/context/builder.context.js +7 -1
  36. package/src/functions/camel-to-kebab-case.js +4 -0
  37. package/src/functions/convert-style-object.js +6 -0
  38. package/src/functions/evaluate.js +2 -2
  39. package/src/functions/get-block-actions.js +2 -2
  40. package/src/functions/get-block-component-options.js +6 -1
  41. package/src/functions/get-block-styles.js +7 -4
  42. package/src/functions/get-content/index.js +1 -1
  43. package/src/functions/get-fetch.js +1 -1
  44. package/src/functions/get-processed-block.js +18 -11
  45. package/src/functions/get-processed-block.test.js +3 -2
  46. package/src/functions/if-target.js +1 -1
  47. package/src/functions/is-editing.js +1 -1
  48. package/src/functions/is-iframe.js +1 -1
  49. package/src/functions/is-previewing.js +2 -2
  50. package/src/functions/on-change.test.js +1 -1
  51. package/src/functions/previewing-model-name.js +1 -1
  52. package/src/functions/register-component.js +35 -26
  53. package/src/functions/register.js +1 -1
  54. package/src/functions/sanitize-styles.js +5 -0
  55. package/src/functions/set-editor-settings.js +1 -1
  56. package/src/functions/set.test.js +1 -1
  57. package/src/functions/track.js +3 -3
  58. package/src/helpers/css.js +12 -0
  59. package/src/index-helpers/blocks-exports.js +10 -8
  60. package/src/index.js +10 -10
  61. package/src/scripts/init-editing.js +4 -4
  62. package/src/types/components.js +0 -0
  63. package/src/types/element.js +0 -0
  64. package/src/blocks/button/index.js +0 -7
  65. package/src/blocks/columns/index.js +0 -7
  66. package/src/blocks/custom-code/index.js +0 -7
  67. package/src/blocks/embed/index.js +0 -7
  68. package/src/blocks/form/index.js +0 -7
  69. package/src/blocks/fragment/index.js +0 -7
  70. package/src/blocks/image/index.js +0 -7
  71. package/src/blocks/img/index.js +0 -7
  72. package/src/blocks/input/index.js +0 -7
  73. package/src/blocks/raw-text/index.js +0 -7
  74. package/src/blocks/section/index.js +0 -7
  75. package/src/blocks/select/index.js +0 -7
  76. package/src/blocks/submit-button/index.js +0 -7
  77. package/src/blocks/symbol/index.js +0 -7
  78. package/src/blocks/text/index.js +0 -7
  79. package/src/blocks/textarea/index.js +0 -7
  80. package/src/blocks/video/index.js +0 -7
  81. package/src/components/error-boundary.svelte +0 -26
  82. package/src/functions/macro-eval.js +0 -5
@@ -1,32 +1,9 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
2
  export let attributes;
13
- export let placeholder;
14
- export let name;
15
- export let value;
16
- export let defaultValue;
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
3
+ export let placeholder;
4
+ export let name;
5
+ export let value;
6
+ export let defaultValue;
30
7
  </script>
31
8
 
32
- <textarea {...attributes} placeholder={placeholder} name={name} value={value} defaultValue={defaultValue} ></textarea>
9
+ <textarea {...attributes} {placeholder} {name} {value} {defaultValue} />
@@ -1,45 +1,38 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
-
7
-
2
+ export let attributes;
3
+ export let fit;
4
+ export let position;
5
+ export let video;
6
+ export let posterImage;
7
+ export let autoPlay;
8
+ export let muted;
9
+ export let controls;
10
+ export let loop;
8
11
 
9
-
12
+ function mitosis_styling(node, vars) {
13
+ Object.entries(vars).forEach(([p, v]) => {
14
+ node.style[p] = v;
15
+ });
16
+ }
17
+ </script>
10
18
 
11
-
12
- export let attributes;
13
- export let fit;
14
- export let position;
15
- export let video;
16
- export let posterImage;
17
- export let autoPlay;
18
- export let muted;
19
- export let controls;
20
- export let loop;
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
- </script>
35
-
36
- <video {...attributes} preload="none" style={{
37
- width: '100%',
38
- height: '100%',
39
- ...attributes?.style,
40
- objectFit: fit,
41
- objectPosition: position,
42
- // Hack to get object fit to work as expected and
43
- // not have the video overflow
44
- borderRadius: 1
45
- }} key={video || 'no-src'} poster={posterImage} autoPlay={autoPlay} muted={muted} controls={controls} loop={loop} ></video>
19
+ <video
20
+ {...attributes}
21
+ use:mitosis_styling={{
22
+ width: "100%",
23
+ height: "100%",
24
+ ...attributes?.style,
25
+ objectFit: fit,
26
+ objectPosition: position,
27
+ // Hack to get object fit to work as expected and
28
+ // not have the video overflow
29
+ borderRadius: 1,
30
+ }}
31
+ preload="none"
32
+ src={video || "no-src"}
33
+ poster={posterImage}
34
+ {autoPlay}
35
+ {muted}
36
+ {controls}
37
+ {loop}
38
+ />
@@ -1,51 +1,55 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
- import RenderInlinedStyles, { } from '../render-inlined-styles.svelte';
7
-
8
-
9
-
10
-
11
-
12
-
13
- export let block;
14
-
15
- function camelToKebabCase(string) {
16
- return string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase();
17
- }
18
- $: css = () => {
19
- // TODO: media queries
20
- const styleObject = block.responsiveStyles?.large;
21
-
22
- if (!styleObject) {
23
- return '';
24
- }
25
-
26
- let str = `.${block.id} {`;
27
-
28
- for (const key in styleObject) {
29
- const value = styleObject[key];
30
-
31
- if (typeof value === 'string') {
32
- str += `${camelToKebabCase(key)}: ${value};`;
33
- }
34
- }
35
-
36
- str += '}';
37
- return str;
38
- };
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
- </script>
50
-
51
- <RenderInlinedStyles styles={css()} ></RenderInlinedStyles>
2
+ import { TARGET } from "../../constants/target.js";
3
+ import BuilderContext from "../../context/builder.context";
4
+ import { getProcessedBlock } from "../../functions/get-processed-block.js";
5
+ import RenderInlinedStyles from "../render-inlined-styles.svelte";
6
+ import { convertStyleMaptoCSS } from "../../helpers/css.js";
7
+ import { getMaxWidthQueryForSize } from "../../constants/device-sizes.js";
8
+
9
+ import { getContext, setContext } from "svelte";
10
+
11
+ export let block;
12
+
13
+ $: useBlock = () => {
14
+ return getProcessedBlock({
15
+ block: block,
16
+ state: builderContext.state,
17
+ context: builderContext.context,
18
+ evaluateBindings: true,
19
+ });
20
+ };
21
+
22
+ $: css = () => {
23
+ const styles = useBlock().responsiveStyles;
24
+ const largeStyles = styles?.large;
25
+ const mediumStyles = styles?.medium;
26
+ const smallStyles = styles?.small;
27
+ return `
28
+ ${
29
+ largeStyles
30
+ ? `.${useBlock().id} {${convertStyleMaptoCSS(largeStyles)}}`
31
+ : ""
32
+ }
33
+ ${
34
+ mediumStyles
35
+ ? `${getMaxWidthQueryForSize("medium")} {
36
+ .${useBlock().id} {${convertStyleMaptoCSS(mediumStyles)}}
37
+ }`
38
+ : ""
39
+ }
40
+ ${
41
+ smallStyles
42
+ ? `${getMaxWidthQueryForSize("small")} {
43
+ .${useBlock().id} {${convertStyleMaptoCSS(smallStyles)}}
44
+ }`
45
+ : ""
46
+ }
47
+ }`;
48
+ };
49
+
50
+ let builderContext = getContext(BuilderContext.key);
51
+ </script>
52
+
53
+ {#if TARGET === "vue2" || TARGET === "vue3" || TARGET === "svelte"}
54
+ <RenderInlinedStyles styles={css()} />
55
+ {/if}
@@ -0,0 +1,23 @@
1
+ const EMPTY_HTML_ELEMENTS = [
2
+ "area",
3
+ "base",
4
+ "br",
5
+ "col",
6
+ "embed",
7
+ "hr",
8
+ "img",
9
+ "input",
10
+ "keygen",
11
+ "link",
12
+ "meta",
13
+ "param",
14
+ "source",
15
+ "track",
16
+ "wbr"
17
+ ];
18
+ const isEmptyHtmlElement = (tagName) => {
19
+ return typeof tagName === "string" && EMPTY_HTML_ELEMENTS.includes(tagName.toLowerCase());
20
+ };
21
+ export {
22
+ isEmptyHtmlElement
23
+ };
@@ -1,163 +1,202 @@
1
1
  <script>
2
-
3
-
4
-
5
-
6
- import { TARGET } from '../../constants/target';
7
- import BuilderContext, { } from '../../context/builder.context';
8
- import { getBlockActions } from '../../functions/get-block-actions';
9
- import { getBlockComponentOptions } from '../../functions/get-block-component-options';
10
- import { getBlockProperties } from '../../functions/get-block-properties';
11
- import { getBlockStyles } from '../../functions/get-block-styles';
12
- import { getBlockTag } from '../../functions/get-block-tag';
13
- import { getProcessedBlock } from '../../functions/get-processed-block';
14
- import { components } from '../../functions/register-component';
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 { 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}
152
200
  {: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}
201
+ <RenderComponent {...renderComponentProps()} />
202
+ {/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} />