@builder.io/sdk-solid 0.0.8-12 → 0.0.8-15

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/image/image.jsx +1 -3
  3. package/src/components/render-block/block-styles.jsx +18 -25
  4. package/src/components/render-block/render-block.jsx +81 -10
  5. package/src/components/render-block/render-repeated-block.jsx +32 -0
  6. package/src/components/render-content/render-content.jsx +5 -5
  7. package/src/constants/device-sizes.js +3 -21
  8. package/src/functions/camel-to-kebab-case.js +4 -0
  9. package/src/functions/convert-style-object.js +14 -0
  10. package/src/functions/get-block-styles.js +7 -15
  11. package/src/functions/get-processed-block.js +15 -8
  12. package/src/functions/get-processed-block.test.js +2 -1
  13. package/src/functions/sanitize-styles.js +5 -0
  14. package/src/helpers/css.js +12 -0
  15. package/src/blocks/button/button.lite.tsx +0 -23
  16. package/src/blocks/columns/columns.lite.tsx +0 -109
  17. package/src/blocks/custom-code/custom-code.lite.tsx +0 -68
  18. package/src/blocks/embed/embed.lite.tsx +0 -60
  19. package/src/blocks/form/form.lite.tsx +0 -296
  20. package/src/blocks/fragment/fragment.lite.tsx +0 -5
  21. package/src/blocks/image/image.lite.tsx +0 -86
  22. package/src/blocks/img/img.lite.tsx +0 -18
  23. package/src/blocks/input/input.lite.tsx +0 -20
  24. package/src/blocks/raw-text/raw-text.lite.tsx +0 -10
  25. package/src/blocks/section/section.lite.tsx +0 -18
  26. package/src/blocks/select/select.lite.tsx +0 -28
  27. package/src/blocks/submit-button/submit-button.lite.tsx +0 -9
  28. package/src/blocks/symbol/symbol.lite.tsx +0 -41
  29. package/src/blocks/text/text.lite.tsx +0 -5
  30. package/src/blocks/textarea/textarea.lite.tsx +0 -13
  31. package/src/blocks/video/video.lite.tsx +0 -26
  32. package/src/components/error-boundary.jsx +0 -5
  33. package/src/components/error-boundary.lite.tsx +0 -5
  34. package/src/components/render-block/block-styles.lite.tsx +0 -56
  35. package/src/components/render-block/render-block.lite.tsx +0 -156
  36. package/src/components/render-block/render-component.lite.tsx +0 -38
  37. package/src/components/render-blocks.lite.tsx +0 -94
  38. package/src/components/render-content/components/render-styles.lite.tsx +0 -76
  39. package/src/components/render-content/render-content.lite.tsx +0 -262
  40. package/src/components/render-inlined-styles.lite.tsx +0 -29
@@ -1,56 +0,0 @@
1
- import { useContext, Show } from "solid-js";
2
-
3
- import { createMutable } from "solid-js/store";
4
-
5
- import { TARGET } from "../../constants/target.js";
6
- import BuilderContext from "../../context/builder.context";
7
- import { getProcessedBlock } from "../../functions/get-processed-block.js";
8
- import RenderInlinedStyles from "../render-inlined-styles.jsx";
9
-
10
- function BlockStyles(props) {
11
- const state = createMutable({
12
- get useBlock() {
13
- return getProcessedBlock({
14
- block: props.block,
15
- state: builderContext.state,
16
- context: builderContext.context,
17
- });
18
- },
19
- camelToKebabCase(string: string) {
20
- return string
21
- .replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2")
22
- .toLowerCase();
23
- },
24
- get css() {
25
- // TODO: media queries
26
- const styleObject = state.useBlock.responsiveStyles?.large;
27
-
28
- if (!styleObject) {
29
- return "";
30
- }
31
-
32
- let str = `.${state.useBlock.id} {`;
33
-
34
- for (const key in styleObject) {
35
- const value = styleObject[key];
36
-
37
- if (typeof value === "string") {
38
- str += `${state.camelToKebabCase(key)}: ${value};`;
39
- }
40
- }
41
-
42
- str += "}";
43
- return str;
44
- },
45
- });
46
-
47
- const builderContext = useContext(BuilderContext);
48
-
49
- return (
50
- <Show when={TARGET === "vue" || TARGET === "svelte"}>
51
- <RenderInlinedStyles styles={state.css}></RenderInlinedStyles>
52
- </Show>
53
- );
54
- }
55
-
56
- export default BlockStyles;
@@ -1,156 +0,0 @@
1
- import { useContext, Show, For } from "solid-js";
2
- import { Dynamic } from "solid-js/web";
3
- import { createMutable } from "solid-js/store";
4
-
5
- import BuilderContext from "../../context/builder.context";
6
- import { getBlockActions } from "../../functions/get-block-actions.js";
7
- import { getBlockComponentOptions } from "../../functions/get-block-component-options.js";
8
- import { getBlockProperties } from "../../functions/get-block-properties.js";
9
- import { getBlockStyles } from "../../functions/get-block-styles.js";
10
- import { getBlockTag } from "../../functions/get-block-tag.js";
11
- import { getProcessedBlock } from "../../functions/get-processed-block.js";
12
- import BlockStyles from "./block-styles.jsx";
13
- import { isEmptyHtmlElement } from "./render-block.helpers.js";
14
- import RenderComponent from "./render-component.jsx";
15
-
16
- function RenderBlock(props) {
17
- const state = createMutable({
18
- get component() {
19
- const componentName = state.useBlock.component?.name;
20
-
21
- if (!componentName) {
22
- return null;
23
- }
24
-
25
- const ref = builderContext.registeredComponents[componentName];
26
-
27
- if (!ref) {
28
- // TODO: Public doc page with more info about this message
29
- console.warn(`
30
- Could not find a registered component named "${componentName}".
31
- If you registered it, is the file that registered it imported by the file that needs to render it?`);
32
- return undefined;
33
- } else {
34
- return ref;
35
- }
36
- },
37
- get componentInfo() {
38
- if (state.component) {
39
- const { component: _, ...info } = state.component;
40
- return info;
41
- } else {
42
- return undefined;
43
- }
44
- },
45
- get componentRef() {
46
- return state.component?.component;
47
- },
48
- get tagName() {
49
- return getBlockTag(state.useBlock);
50
- },
51
- get useBlock() {
52
- return getProcessedBlock({
53
- block: props.block,
54
- state: builderContext.state,
55
- context: builderContext.context,
56
- });
57
- },
58
- get attributes() {
59
- return {
60
- ...getBlockProperties(state.useBlock),
61
- ...getBlockActions({
62
- block: state.useBlock,
63
- state: builderContext.state,
64
- context: builderContext.context,
65
- }),
66
- style: getBlockStyles(state.useBlock),
67
- };
68
- },
69
- get shouldWrap() {
70
- return !state.componentInfo?.noWrap;
71
- },
72
- get componentOptions() {
73
- return {
74
- ...getBlockComponentOptions(state.useBlock),
75
-
76
- /**
77
- * These attributes are passed to the wrapper element when there is one. If `noWrap` is set to true, then
78
- * they are provided to the component itself directly.
79
- */
80
- ...(state.shouldWrap
81
- ? {}
82
- : {
83
- attributes: state.attributes,
84
- }),
85
- };
86
- },
87
- get children() {
88
- // TO-DO: When should `canHaveChildren` dictate rendering?
89
- // This is currently commented out because some Builder components (e.g. Box) do not have `canHaveChildren: true`,
90
- // but still receive and need to render children.
91
- // return state.componentInfo?.canHaveChildren ? state.useBlock.children : [];
92
- return state.useBlock.children ?? [];
93
- },
94
- get noCompRefChildren() {
95
- /**
96
- * When there is no `componentRef`, there might still be children that need to be rendered. In this case,
97
- * we render them outside of `componentRef`
98
- */
99
- return state.componentRef ? [] : state.children;
100
- },
101
- });
102
-
103
- const builderContext = useContext(BuilderContext);
104
-
105
- return (
106
- <Show
107
- fallback={
108
- <RenderComponent
109
- blockChildren={state.children}
110
- componentRef={state.componentRef}
111
- componentOptions={state.componentOptions}
112
- ></RenderComponent>
113
- }
114
- when={state.shouldWrap}
115
- >
116
- <Show
117
- fallback={
118
- <Dynamic {...state.attributes} component={state.tagName}></Dynamic>
119
- }
120
- when={!isEmptyHtmlElement(state.tagName)}
121
- >
122
- <Dynamic {...state.attributes} component={state.tagName}>
123
- <RenderComponent
124
- blockChildren={state.children}
125
- componentRef={state.componentRef}
126
- componentOptions={state.componentOptions}
127
- ></RenderComponent>
128
- <For each={state.noCompRefChildren}>
129
- {(child, _index) => {
130
- const index = _index();
131
- return (
132
- <RenderBlock
133
- key={"render-block-" + child.id}
134
- block={child}
135
- ></RenderBlock>
136
- );
137
- }}
138
- </For>
139
- <For each={state.noCompRefChildren}>
140
- {(child, _index) => {
141
- const index = _index();
142
- return (
143
- <BlockStyles
144
- key={"block-style-" + child.id}
145
- block={child}
146
- ></BlockStyles>
147
- );
148
- }}
149
- </For>
150
- </Dynamic>
151
- </Show>
152
- </Show>
153
- );
154
- }
155
-
156
- export default RenderBlock;
@@ -1,38 +0,0 @@
1
- import { Show, For } from "solid-js";
2
- import { Dynamic } from "solid-js/web";
3
-
4
- import BlockStyles from "./block-styles.jsx";
5
- import RenderBlock from "./render-block.jsx";
6
-
7
- function RenderComponent(props) {
8
- return (
9
- <Show when={props.componentRef}>
10
- <Dynamic {...props.componentOptions} component={props.componentRef}>
11
- <For each={props.blockChildren}>
12
- {(child, _index) => {
13
- const index = _index();
14
- return (
15
- <RenderBlock
16
- key={"render-block-" + child.id}
17
- block={child}
18
- ></RenderBlock>
19
- );
20
- }}
21
- </For>
22
- <For each={props.blockChildren}>
23
- {(child, _index) => {
24
- const index = _index();
25
- return (
26
- <BlockStyles
27
- key={"block-style-" + child.id}
28
- block={child}
29
- ></BlockStyles>
30
- );
31
- }}
32
- </For>
33
- </Dynamic>
34
- </Show>
35
- );
36
- }
37
-
38
- export default RenderComponent;
@@ -1,94 +0,0 @@
1
- import { Show, For } from "solid-js";
2
-
3
- import { createMutable } from "solid-js/store";
4
- import { css } from "solid-styled-components";
5
-
6
- import { isEditing } from "../functions/is-editing.js";
7
- import BlockStyles from "./render-block/block-styles.jsx";
8
- import RenderBlock from "./render-block/render-block.jsx";
9
-
10
- function RenderBlocks(props) {
11
- const state = createMutable({
12
- get className() {
13
- return "builder-blocks" + (!props.blocks?.length ? " no-blocks" : "");
14
- },
15
- onClick() {
16
- if (isEditing() && !props.blocks?.length) {
17
- window.parent?.postMessage(
18
- {
19
- type: "builder.clickEmptyBlocks",
20
- data: {
21
- parentElementId: props.parent,
22
- dataPath: props.path,
23
- },
24
- },
25
- "*"
26
- );
27
- }
28
- },
29
- onMouseEnter() {
30
- if (isEditing() && !props.blocks?.length) {
31
- window.parent?.postMessage(
32
- {
33
- type: "builder.hoverEmptyBlocks",
34
- data: {
35
- parentElementId: props.parent,
36
- dataPath: props.path,
37
- },
38
- },
39
- "*"
40
- );
41
- }
42
- },
43
- });
44
-
45
- return (
46
- <div
47
- class={
48
- state.className +
49
- " " +
50
- css({
51
- display: "flex",
52
- flexDirection: "column",
53
- alignItems: "stretch",
54
- })
55
- }
56
- builder-path={props.path}
57
- builder-parent-id={props.parent}
58
- dataSet={{
59
- class: state.className,
60
- }}
61
- onClick={(event) => state.onClick()}
62
- onMouseEnter={(event) => state.onMouseEnter()}
63
- >
64
- <Show when={props.blocks}>
65
- <For each={props.blocks}>
66
- {(block, _index) => {
67
- const index = _index();
68
- return (
69
- <RenderBlock
70
- key={"render-block-" + block.id}
71
- block={block}
72
- ></RenderBlock>
73
- );
74
- }}
75
- </For>
76
- </Show>
77
- <Show when={props.blocks}>
78
- <For each={props.blocks}>
79
- {(block, _index) => {
80
- const index = _index();
81
- return (
82
- <BlockStyles
83
- key={"block-style-" + block.id}
84
- block={block}
85
- ></BlockStyles>
86
- );
87
- }}
88
- </For>
89
- </Show>
90
- </div>
91
- );
92
- }
93
-
94
- export default RenderBlocks;
@@ -1,76 +0,0 @@
1
- import { createMutable } from "solid-js/store";
2
-
3
- import RenderInlinedStyles from "../../render-inlined-styles.jsx";
4
-
5
- function RenderContentStyles(props) {
6
- const state = createMutable({
7
- getCssFromFont(font: CustomFont) {
8
- // TODO: compute what font sizes are used and only load those.......
9
- const family =
10
- font.family +
11
- (font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
12
- const name = family.split(",")[0];
13
- const url = font.fileUrl ?? font?.files?.regular;
14
- let str = "";
15
-
16
- if (url && family && name) {
17
- str += `
18
- @font-face {
19
- font-family: "${family}";
20
- src: local("${name}"), url('${url}') format('woff2');
21
- font-display: fallback;
22
- font-weight: 400;
23
- }
24
- `.trim();
25
- }
26
-
27
- if (font.files) {
28
- for (const weight in font.files) {
29
- const isNumber = String(Number(weight)) === weight;
30
-
31
- if (!isNumber) {
32
- continue;
33
- } // TODO: maybe limit number loaded
34
-
35
- const weightUrl = font.files[weight];
36
-
37
- if (weightUrl && weightUrl !== url) {
38
- str += `
39
- @font-face {
40
- font-family: "${family}";
41
- src: url('${weightUrl}') format('woff2');
42
- font-display: fallback;
43
- font-weight: ${weight};
44
- }
45
- `.trim();
46
- }
47
- }
48
- }
49
-
50
- return str;
51
- },
52
- getFontCss({ customFonts }: { customFonts?: CustomFont[] }) {
53
- // TODO: flag for this
54
- // if (!this.builder.allowCustomFonts) {
55
- // return '';
56
- // }
57
- // TODO: separate internal data from external
58
- return (
59
- customFonts?.map((font) => this.getCssFromFont(font))?.join(" ") || ""
60
- );
61
- },
62
- get injectedStyles() {
63
- return `
64
- ${props.cssCode || ""}
65
- ${state.getFontCss({
66
- customFonts: props.customFonts,
67
- })}`;
68
- },
69
- });
70
-
71
- return (
72
- <RenderInlinedStyles styles={state.injectedStyles}></RenderInlinedStyles>
73
- );
74
- }
75
-
76
- export default RenderContentStyles;
@@ -1,262 +0,0 @@
1
- import { useContext, Show, onMount } from "solid-js";
2
- import { Dynamic } from "solid-js/web";
3
- import { createMutable } from "solid-js/store";
4
-
5
- import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components.js";
6
- import { TARGET } from "../../constants/target.js";
7
- import BuilderContext from "../../context/builder.context";
8
- import { evaluate } from "../../functions/evaluate.js";
9
- import {
10
- convertSearchParamsToQueryObject,
11
- getBuilderSearchParams,
12
- } from "../../functions/get-builder-search-params/index.js";
13
- import { getContent } from "../../functions/get-content/index.js";
14
- import { getFetch } from "../../functions/get-fetch.js";
15
- import { isBrowser } from "../../functions/is-browser.js";
16
- import { isEditing } from "../../functions/is-editing.js";
17
- import { isPreviewing } from "../../functions/is-previewing.js";
18
- import { previewingModelName } from "../../functions/previewing-model-name.js";
19
- import {
20
- components,
21
- createRegisterComponentMessage,
22
- } from "../../functions/register-component.js";
23
- import { track } from "../../functions/track.js";
24
- import RenderBlocks from "../render-blocks.jsx";
25
- import RenderContentStyles from "./components/render-styles.jsx";
26
-
27
- function RenderContent(props) {
28
- const state = createMutable({
29
- get useContent() {
30
- const mergedContent: BuilderContent = {
31
- ...props.content,
32
- ...state.overrideContent,
33
- data: {
34
- ...props.content?.data,
35
- ...props.data,
36
- ...state.overrideContent?.data,
37
- },
38
- };
39
- return mergedContent;
40
- },
41
- overrideContent: null,
42
- update: 0,
43
- overrideState: {},
44
- get contentState() {
45
- return {
46
- ...props.content?.data?.state,
47
- ...props.data,
48
- ...state.overrideState,
49
- };
50
- },
51
- get context() {
52
- return {} as Dictionary<any>;
53
- },
54
- get allRegisteredComponents() {
55
- const allComponentsArray = [
56
- ...getDefaultRegisteredComponents(), // While this `components` object is deprecated, we must maintain support for it.
57
- // Since users are able to override our default components, we need to make sure that we do not break such
58
- // existing usage.
59
- // This is why we spread `components` after the default Builder.io components, but before the `props.customComponents`,
60
- // which is the new standard way of providing custom components, and must therefore take precedence.
61
- ...components,
62
- ...(props.customComponents || []),
63
- ];
64
- const allComponents = allComponentsArray.reduce(
65
- (acc, curr) => ({ ...acc, [curr.name]: curr }),
66
- {} as RegisteredComponents
67
- );
68
- return allComponents;
69
- },
70
- processMessage(event: MessageEvent) {
71
- const { data } = event;
72
-
73
- if (data) {
74
- switch (data.type) {
75
- case "builder.contentUpdate": {
76
- const messageContent = data.data;
77
- const key =
78
- messageContent.key ||
79
- messageContent.alias ||
80
- messageContent.entry ||
81
- messageContent.modelName;
82
- const contentData = messageContent.data;
83
-
84
- if (key === props.model) {
85
- state.overrideContent = contentData;
86
- }
87
-
88
- break;
89
- }
90
-
91
- case "builder.patchUpdates": {
92
- // TODO
93
- break;
94
- }
95
- }
96
- }
97
- },
98
- evaluateJsCode() {
99
- // run any dynamic JS code attached to content
100
- const jsCode = state.useContent?.data?.jsCode;
101
-
102
- if (jsCode) {
103
- evaluate({
104
- code: jsCode,
105
- context: state.context,
106
- state: state.contentState,
107
- });
108
- }
109
- },
110
- get httpReqsData() {
111
- return {};
112
- },
113
- evalExpression(expression: string) {
114
- return expression.replace(/{{([^}]+)}}/g, (_match, group) =>
115
- evaluate({
116
- code: group,
117
- context: state.context,
118
- state: state.contentState,
119
- })
120
- );
121
- },
122
- handleRequest({ url, key }: { key: string; url: string }) {
123
- const fetchAndSetState = async () => {
124
- const fetch = await getFetch();
125
- const response = await fetch(url);
126
- const json = await response.json();
127
- const newOverrideState = { ...state.overrideState, [key]: json };
128
- state.overrideState = newOverrideState;
129
- };
130
-
131
- fetchAndSetState();
132
- },
133
- runHttpRequests() {
134
- const requests = state.useContent?.data?.httpRequests ?? {};
135
- Object.entries(requests).forEach(([key, url]) => {
136
- if (url && (!state.httpReqsData[key] || isEditing())) {
137
- const evaluatedUrl = state.evalExpression(url);
138
- state.handleRequest({
139
- url: evaluatedUrl,
140
- key,
141
- });
142
- }
143
- });
144
- },
145
- emitStateUpdate() {
146
- if (isEditing()) {
147
- window.dispatchEvent(
148
- new CustomEvent<BuilderComponentStateChange>(
149
- "builder:component:stateChange",
150
- {
151
- detail: {
152
- state: state.contentState,
153
- ref: {
154
- name: props.model,
155
- },
156
- },
157
- }
158
- )
159
- );
160
- }
161
- },
162
- });
163
-
164
- onMount(() => {
165
- if (isBrowser()) {
166
- if (isEditing()) {
167
- Object.values(state.allRegisteredComponents).forEach(
168
- (registeredComponent) => {
169
- const message = createRegisterComponentMessage(registeredComponent);
170
- window.parent?.postMessage(message, "*");
171
- }
172
- );
173
- window.addEventListener("message", state.processMessage);
174
- window.addEventListener(
175
- "builder:component:stateChangeListenerActivated",
176
- state.emitStateUpdate
177
- );
178
- }
179
-
180
- if (state.useContent) {
181
- track("impression", {
182
- contentId: state.useContent.id,
183
- });
184
- } // override normal content in preview mode
185
-
186
- if (isPreviewing()) {
187
- if (props.model && previewingModelName() === props.model) {
188
- const currentUrl = new URL(location.href);
189
- const previewApiKey = currentUrl.searchParams.get("apiKey");
190
-
191
- if (previewApiKey) {
192
- getContent({
193
- model: props.model,
194
- apiKey: previewApiKey,
195
- options: getBuilderSearchParams(
196
- convertSearchParamsToQueryObject(currentUrl.searchParams)
197
- ),
198
- }).then((content) => {
199
- if (content) {
200
- state.overrideContent = content;
201
- }
202
- });
203
- }
204
- }
205
- }
206
-
207
- state.evaluateJsCode();
208
- state.runHttpRequests();
209
- state.emitStateUpdate();
210
- }
211
- });
212
-
213
- return (
214
- <Dynamic
215
- value={{
216
- get content() {
217
- return state.useContent;
218
- },
219
- get state() {
220
- return state.contentState;
221
- },
222
- get context() {
223
- return state.context;
224
- },
225
- get apiKey() {
226
- return props.apiKey;
227
- },
228
- get registeredComponents() {
229
- return state.allRegisteredComponents;
230
- },
231
- }}
232
- component={BuilderContext.Provider}
233
- >
234
- <Show when={state.useContent}>
235
- <div
236
- onClick={(event) =>
237
- track("click", {
238
- contentId: state.useContent.id,
239
- })
240
- }
241
- data-builder-content-id={state.useContent?.id}
242
- >
243
- <Show
244
- when={
245
- (state.useContent?.data?.cssCode ||
246
- state.useContent?.data?.customFonts?.length) &&
247
- TARGET !== "reactNative"
248
- }
249
- >
250
- <RenderContentStyles
251
- cssCode={state.useContent.data.cssCode}
252
- customFonts={state.useContent.data.customFonts}
253
- ></RenderContentStyles>
254
- </Show>
255
- <RenderBlocks blocks={state.useContent?.data?.blocks}></RenderBlocks>
256
- </div>
257
- </Show>
258
- </Dynamic>
259
- );
260
- }
261
-
262
- export default RenderContent;