@builder.io/sdk-svelte 0.0.1-4 → 0.0.1-7

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 (34) hide show
  1. package/README.md +15 -0
  2. package/package.json +2 -1
  3. package/src/blocks/columns/columns.svelte +1 -1
  4. package/src/blocks/image/image.svelte +1 -1
  5. package/src/components/render-block/render-block.helpers.js +21 -0
  6. package/src/components/render-block/render-block.svelte +22 -11
  7. package/src/components/render-content/index.js +4 -0
  8. package/src/components/render-content/render-content.svelte +32 -8
  9. package/src/components/render-inlined-styles.svelte +2 -0
  10. package/src/constants/builder-registered-components.js +29 -0
  11. package/src/context/builder.context.js +1 -1
  12. package/src/functions/fast-clone.js +4 -0
  13. package/src/functions/get-content/fn.test.js +1 -1
  14. package/src/functions/register-component.js +13 -13
  15. package/src/index-helpers/blocks-exports.js +8 -8
  16. package/src/types/components.js +0 -0
  17. package/src/types/element.js +0 -0
  18. package/src/blocks/button/index.js +0 -7
  19. package/src/blocks/columns/index.js +0 -7
  20. package/src/blocks/custom-code/index.js +0 -7
  21. package/src/blocks/embed/index.js +0 -7
  22. package/src/blocks/form/index.js +0 -7
  23. package/src/blocks/fragment/index.js +0 -7
  24. package/src/blocks/image/index.js +0 -7
  25. package/src/blocks/img/index.js +0 -7
  26. package/src/blocks/input/index.js +0 -7
  27. package/src/blocks/raw-text/index.js +0 -7
  28. package/src/blocks/section/index.js +0 -7
  29. package/src/blocks/select/index.js +0 -7
  30. package/src/blocks/submit-button/index.js +0 -7
  31. package/src/blocks/symbol/index.js +0 -7
  32. package/src/blocks/text/index.js +0 -7
  33. package/src/blocks/textarea/index.js +0 -7
  34. package/src/blocks/video/index.js +0 -7
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Builder.io Svelte SDK (BETA)
2
+
3
+ This is the Svelte SDK. It is currently in beta.
4
+
5
+ ## Feature Support
6
+
7
+ To check the status of the SDK, look at [these tables](../../README.md#feature-implementation).
8
+
9
+ ## Getting Started
10
+
11
+ ```
12
+ npm install @builder.io/sdk-svelte@dev
13
+ ```
14
+
15
+ Take a look at [our example repo](/examples/svelte/sveltekit) for how to use this SDK.
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-svelte",
3
3
  "description": "Builder.io SDK for Svelte",
4
- "version": "0.0.1-4",
4
+ "version": "0.0.1-7",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "src"
8
8
  ],
9
9
  "main": "./src/index.js",
10
10
  "module": "./src/index.js",
11
+ "svelte": "./src/index.js",
11
12
  "exports": {
12
13
  ".": {
13
14
  "import": "./src/index.js",
@@ -78,7 +78,7 @@ return {
78
78
  width: getColumnCssWidth(index),
79
79
  marginLeft: `${index === 0 ? 0 : getGutterSize()}px`,
80
80
  ...columnCssVars()
81
- }} >
81
+ }} key={index} >
82
82
 
83
83
  <RenderBlocks blocks={column.blocks} ></RenderBlocks>
84
84
 
@@ -38,7 +38,7 @@ export let builderBlock;
38
38
 
39
39
  <picture >
40
40
 
41
- <img loading="lazy" alt={altText} aria-role={altText ? 'presentation' : undefined} style={{
41
+ <img loading="lazy" alt={altText} role={altText ? 'presentation' : undefined} style={{
42
42
  objectPosition: backgroundSize || 'center',
43
43
  objectFit: backgroundSize || 'cover'
44
44
  }} class={'builder-image' + (className ? ' ' + className : '')} src={image} srcset={srcset} sizes={sizes} />
@@ -0,0 +1,21 @@
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) => EMPTY_HTML_ELEMENTS.includes(tagName.toLowerCase());
19
+ export {
20
+ isEmptyHtmlElement
21
+ };
@@ -11,8 +11,8 @@ import { getBlockProperties } from '../../functions/get-block-properties.js'
11
11
  import { getBlockStyles } from '../../functions/get-block-styles.js';
12
12
  import { getBlockTag } from '../../functions/get-block-tag.js';
13
13
  import { getProcessedBlock } from '../../functions/get-processed-block.js';
14
- import { components } from '../../functions/register-component.js';
15
14
  import BlockStyles, { } from './block-styles.svelte';
15
+ import { isEmptyHtmlElement } from './render-block.helpers.js';
16
16
 
17
17
 
18
18
 
@@ -29,16 +29,17 @@ if (!componentName) {
29
29
  return null;
30
30
  }
31
31
 
32
- const ref = components[componentName];
32
+ const ref = builderContext.registeredComponents[componentName];
33
33
 
34
- if (componentName && !ref) {
34
+ if (!ref) {
35
35
  // TODO: Public doc page with more info about this message
36
36
  console.warn(`
37
37
  Could not find a registered component named "${componentName}".
38
38
  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;
39
42
  }
40
-
41
- return ref;
42
43
  };
43
44
 
44
45
  $: componentInfo = () => {
@@ -103,9 +104,11 @@ return componentRef() ? [] : children();
103
104
  </script>
104
105
 
105
106
 
107
+ {#if !componentInfo?.()?.noWrap }
108
+
106
109
 
107
110
 
108
- {#if !componentInfo?.()?.noWrap }
111
+ {#if !isEmptyHtmlElement(tagName()) }
109
112
 
110
113
 
111
114
  <svelte:element {...propertiesAndActions()} style={css()} this={tagName()} >
@@ -128,7 +131,7 @@ return componentRef() ? [] : children();
128
131
 
129
132
 
130
133
  {#each children() as child, index }
131
- <svelte:self block={child} ></svelte:self>
134
+ <svelte:self key={child.id} block={child} ></svelte:self>
132
135
  {/each}
133
136
 
134
137
 
@@ -140,7 +143,7 @@ return componentRef() ? [] : children();
140
143
 
141
144
 
142
145
  {#each noCompRefChildren() as child, index }
143
- <svelte:self block={child} ></svelte:self>
146
+ <svelte:self key={child.id} block={child} ></svelte:self>
144
147
  {/each}
145
148
 
146
149
 
@@ -149,15 +152,23 @@ return componentRef() ? [] : children();
149
152
 
150
153
 
151
154
 
155
+ {:else}
156
+ <svelte:element {...propertiesAndActions()} style={css()} this={tagName()} ></svelte:element>
157
+
158
+ {/if}
159
+
160
+
161
+
162
+
152
163
  {:else}
153
164
  <svelte:component {...componentOptions()} attributes={propertiesAndActions()} builderBlock={useBlock()} style={css()} this={componentRef()} >
154
-
165
+
155
166
 
156
167
  {#each children() as child, index }
157
- <svelte:self block={child} ></svelte:self>
168
+ <svelte:self key={child.id} block={child} ></svelte:self>
158
169
  {/each}
159
170
 
160
171
 
161
- </svelte:component>
172
+ </svelte:component>
162
173
 
163
174
  {/if}
@@ -0,0 +1,4 @@
1
+ import { default as default2 } from "./render-content.svelte";
2
+ export {
3
+ default2 as default
4
+ };
@@ -3,7 +3,8 @@
3
3
  import { afterUpdate } from 'svelte'
4
4
  import { onDestroy } from 'svelte'
5
5
 
6
- import { TARGET } from '../../constants/target.js';
6
+ import { getDefaultRegisteredComponents } from '../../constants/builder-registered-components.js';
7
+ import { TARGET } from '../../constants/target.js';
7
8
  import BuilderContext, { } from '../../context/builder.context';
8
9
  import { evaluate } from '../../functions/evaluate.js';
9
10
  import { convertSearchParamsToQueryObject , getBuilderSearchParams } from '../../functions/get-builder-search-params/index.js';
@@ -13,6 +14,7 @@ import { isBrowser } from '../../functions/is-browser.js';
13
14
  import { isEditing } from '../../functions/is-editing.js';
14
15
  import { isPreviewing } from '../../functions/is-previewing.js';
15
16
  import { previewingModelName } from '../../functions/previewing-model-name.js';
17
+ import { components , createRegisterComponentMessage } from '../../functions/register-component.js';
16
18
  import { track } from '../../functions/track.js';
17
19
  import RenderBlocks, { } from '../render-blocks.svelte';
18
20
  import RenderContentStyles, { } from './components/render-styles.svelte';
@@ -24,6 +26,7 @@ import RenderContentStyles, { } from './components/render-styles.svelte';
24
26
 
25
27
  export let content;
26
28
  export let data;
29
+ export let customComponents;
27
30
  export let model;
28
31
  export let apiKey;
29
32
 
@@ -108,14 +111,16 @@ Object.entries(requests).forEach(([key, url]) => {
108
111
  }
109
112
 
110
113
  function emitStateUpdate() {
111
- window.dispatchEvent(new CustomEvent('builder:component:stateChange', {
112
- detail: {
113
- state: contentState(),
114
- ref: {
115
- name: model
114
+ if (isEditing()) {
115
+ window.dispatchEvent(new CustomEvent('builder:component:stateChange', {
116
+ detail: {
117
+ state: contentState(),
118
+ ref: {
119
+ name: model
120
+ }
116
121
  }
117
- }
118
- }));
122
+ }));
123
+ }
119
124
  }
120
125
  $: useContent = () => {
121
126
  const mergedContent = { ...content,
@@ -139,6 +144,19 @@ $: context = () => {
139
144
  return {};
140
145
  };
141
146
 
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.info.name]: curr
156
+ }), {});
157
+ return allComponents;
158
+ };
159
+
142
160
  $: httpReqsData = () => {
143
161
  return {};
144
162
  };
@@ -152,6 +170,8 @@ return contentState();
152
170
  return context();
153
171
  }, get apiKey() {
154
172
  return apiKey;
173
+ }, get registeredComponents() {
174
+ return allRegisteredComponents();
155
175
  },});
156
176
  let overrideContent = null;
157
177
  let update = 0;
@@ -159,6 +179,10 @@ let overrideState = {};
159
179
 
160
180
  onMount(() => { if (isBrowser()) {
161
181
  if (isEditing()) {
182
+ Object.values(allRegisteredComponents()).forEach(registeredComponent => {
183
+ const message = createRegisterComponentMessage(registeredComponent);
184
+ window.parent?.postMessage(message, '*');
185
+ });
162
186
  window.addEventListener('message', processMessage);
163
187
  window.addEventListener('builder:component:stateChangeListenerActivated', emitStateUpdate);
164
188
  }
@@ -38,6 +38,8 @@ return 'sty' + 'le';
38
38
  {#if TARGET === 'svelte' }
39
39
 
40
40
 
41
+
42
+
41
43
  {@html injectedStyleScript()}
42
44
 
43
45
 
@@ -0,0 +1,29 @@
1
+ import { default as Button } from "../blocks/button/button.svelte";
2
+ import { componentInfo as buttonComponentInfo } from "../blocks/button/component-info";
3
+ import { default as Columns } from "../blocks/columns/columns.svelte";
4
+ import { componentInfo as columnsComponentInfo } from "../blocks/columns/component-info";
5
+ import { componentInfo as fragmentComponentInfo } from "../blocks/fragment/component-info";
6
+ import { default as Fragment } from "../blocks/fragment/fragment.svelte";
7
+ import { componentInfo as imageComponentInfo } from "../blocks/image/component-info";
8
+ import { default as Image } from "../blocks/image/image.svelte";
9
+ import { componentInfo as sectionComponentInfo } from "../blocks/section/component-info";
10
+ import { default as Section } from "../blocks/section/section.svelte";
11
+ import { componentInfo as symbolComponentInfo } from "../blocks/symbol/component-info";
12
+ import { default as Symbol } from "../blocks/symbol/symbol.svelte";
13
+ import { componentInfo as textComponentInfo } from "../blocks/text/component-info";
14
+ import { default as Text } from "../blocks/text/text.svelte";
15
+ import { componentInfo as videoComponentInfo } from "../blocks/video/component-info";
16
+ import { default as Video } from "../blocks/video/video.svelte";
17
+ const getDefaultRegisteredComponents = () => [
18
+ { component: Columns, info: columnsComponentInfo },
19
+ { component: Image, info: imageComponentInfo },
20
+ { component: Text, info: textComponentInfo },
21
+ { component: Video, info: videoComponentInfo },
22
+ { component: Symbol, info: symbolComponentInfo },
23
+ { component: Button, info: buttonComponentInfo },
24
+ { component: Section, info: sectionComponentInfo },
25
+ { component: Fragment, info: fragmentComponentInfo }
26
+ ];
27
+ export {
28
+ getDefaultRegisteredComponents
29
+ };
@@ -1,6 +1,6 @@
1
1
  const key = Symbol();
2
2
  var stdin_default = {
3
- Builder: { content: null, context: {}, state: {}, apiKey: null },
3
+ Builder: { content: null, context: {}, state: {}, apiKey: null, registeredComponents: {} },
4
4
  key
5
5
  };
6
6
  export {
@@ -0,0 +1,4 @@
1
+ const fastClone = (obj) => JSON.parse(JSON.stringify(obj));
2
+ export {
3
+ fastClone
4
+ };
@@ -1,4 +1,4 @@
1
- import { generateContentUrl } from "..js";
1
+ import { generateContentUrl } from ".";
2
2
  const testKey = "YJIGb4i01jvw0SRdL5Bt";
3
3
  const testModel = "page";
4
4
  const testId = "c1b81bab59704599b997574eb0736def";
@@ -17,27 +17,26 @@ var __spreadValues = (a, b) => {
17
17
  return a;
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- import { isBrowser } from "./is-browser.js";
21
- const components = {};
20
+ import { fastClone } from "./fast-clone.js";
21
+ const components = [];
22
22
  function registerComponent(component, info) {
23
- var _a;
24
- components[info.name] = { component, info };
25
- if (isBrowser()) {
26
- const sendInfo = prepareComponentInfoToSend(info);
27
- (_a = window.parent) == null ? void 0 : _a.postMessage({
28
- type: "builder.registerComponent",
29
- data: sendInfo
30
- }, "*");
31
- }
23
+ components.push({ component, info });
24
+ console.warn("registerComponent is deprecated. Use the `customComponents` prop in RenderContent instead to provide your custom components to the builder SDK.");
32
25
  return component;
33
26
  }
27
+ const createRegisterComponentMessage = ({
28
+ info
29
+ }) => ({
30
+ type: "builder.registerComponent",
31
+ data: prepareComponentInfoToSend(fastClone(info))
32
+ });
34
33
  function prepareComponentInfoToSend(info) {
35
34
  return __spreadValues(__spreadValues({}, info), info.inputs && {
36
35
  inputs: info.inputs.map((input) => {
37
36
  const keysToConvertFnToString = ["onChange", "showIf"];
38
37
  for (const key of keysToConvertFnToString) {
39
- if (input[key] && typeof input[key] === "function") {
40
- const fn = input[key];
38
+ const fn = input[key];
39
+ if (fn && typeof fn === "function") {
41
40
  input = __spreadProps(__spreadValues({}, input), {
42
41
  [key]: `return (${fn.toString()}).apply(this, arguments)`
43
42
  });
@@ -49,5 +48,6 @@ function prepareComponentInfoToSend(info) {
49
48
  }
50
49
  export {
51
50
  components,
51
+ createRegisterComponentMessage,
52
52
  registerComponent
53
53
  };
@@ -1,11 +1,11 @@
1
- import { default as default2 } from "../blocks/columns/index.js";
2
- import { default as default3 } from "../blocks/image/index.js";
3
- import { default as default4 } from "../blocks/text/index.js";
4
- import { default as default5 } from "../blocks/video/index.js";
5
- import { default as default6 } from "../blocks/symbol/index.js";
6
- import { default as default7 } from "../blocks/button/index.js";
7
- import { default as default8 } from "../blocks/section/index.js";
8
- import { default as default9 } from "../blocks/fragment/index.js";
1
+ import { default as default2 } from "../blocks/columns/columns.svelte";
2
+ import { default as default3 } from "../blocks/image/image.svelte";
3
+ import { default as default4 } from "../blocks/text/text.svelte";
4
+ import { default as default5 } from "../blocks/video/video.svelte";
5
+ import { default as default6 } from "../blocks/symbol/symbol.svelte";
6
+ import { default as default7 } from "../blocks/button/button.svelte";
7
+ import { default as default8 } from "../blocks/section/section.svelte";
8
+ import { default as default9 } from "../blocks/fragment/fragment.svelte";
9
9
  import { default as default10 } from "../components/render-content/render-content.svelte";
10
10
  export {
11
11
  default7 as Button,
File without changes
File without changes
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import component from "./button.svelte";
3
- import { componentInfo } from "./component-info.js";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import component from "./columns.svelte";
3
- import { componentInfo } from "./component-info.js";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./custom-code.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./embed.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./form.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./fragment.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./image.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./img.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./input.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./raw-text.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./section.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./select.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./submit-button.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./symbol.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./text.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./textarea.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };
@@ -1,7 +0,0 @@
1
- import { registerComponent } from "../../functions/register-component.js";
2
- import { componentInfo } from "./component-info.js";
3
- import component from "./video.svelte";
4
- var stdin_default = registerComponent(component, componentInfo);
5
- export {
6
- stdin_default as default
7
- };