@builder.io/sdk-solid 0.1.4 → 0.1.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-solid",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./solid-index.jsx",
@@ -48,10 +48,30 @@ function RenderBlock(props) {
48
48
  });
49
49
  }
50
50
 
51
+ function proxyState() {
52
+ if (typeof Proxy === "undefined") {
53
+ console.error(
54
+ "no Proxy available in this environment, cannot proxy state."
55
+ );
56
+ return props.context.state;
57
+ }
58
+ const useState = new Proxy(props.context.state, {
59
+ set: (obj, prop, value) => {
60
+ // set the value on the state object, so that the event handler instantly gets the update.
61
+ obj[prop] = value;
62
+
63
+ // set the value in the context, so that the rest of the app gets the update.
64
+ props.context.setState?.(obj);
65
+ return true;
66
+ },
67
+ });
68
+ return useState;
69
+ }
70
+
51
71
  function actions() {
52
72
  return getBlockActions({
53
73
  block: useBlock(),
54
- state: props.context.state,
74
+ state: proxyState(),
55
75
  context: props.context.context,
56
76
  });
57
77
  }
@@ -126,6 +146,7 @@ function RenderBlock(props) {
126
146
  state: props.context.state,
127
147
  content: props.context.content,
128
148
  context: props.context.context,
149
+ setState: props.context.setState,
129
150
  registeredComponents: props.context.registeredComponents,
130
151
  inheritedStyles: getInheritedTextStyles(),
131
152
  };
@@ -7,6 +7,7 @@ function RenderRepeatedBlock(props) {
7
7
  value={{
8
8
  content: props.repeatContext.content,
9
9
  state: props.repeatContext.state,
10
+ setState: props.repeatContext.setState,
10
11
  context: props.repeatContext.context,
11
12
  apiKey: props.repeatContext.apiKey,
12
13
  registeredComponents: props.repeatContext.registeredComponents,
@@ -0,0 +1,57 @@
1
+ const getCssFromFont = (font) => {
2
+ var _a, _b;
3
+ const family = font.family + (font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
4
+ const name = family.split(",")[0];
5
+ const url = (_b = font.fileUrl) != null ? _b : (_a = font == null ? void 0 : font.files) == null ? void 0 : _a.regular;
6
+ let str = "";
7
+ if (url && family && name) {
8
+ str += `
9
+ @font-face {
10
+ font-family: "${family}";
11
+ src: local("${name}"), url('${url}') format('woff2');
12
+ font-display: fallback;
13
+ font-weight: 400;
14
+ }
15
+ `.trim();
16
+ }
17
+ if (font.files) {
18
+ for (const weight in font.files) {
19
+ const isNumber = String(Number(weight)) === weight;
20
+ if (!isNumber) {
21
+ continue;
22
+ }
23
+ const weightUrl = font.files[weight];
24
+ if (weightUrl && weightUrl !== url) {
25
+ str += `
26
+ @font-face {
27
+ font-family: "${family}";
28
+ src: url('${weightUrl}') format('woff2');
29
+ font-display: fallback;
30
+ font-weight: ${weight};
31
+ }
32
+ `.trim();
33
+ }
34
+ }
35
+ }
36
+ return str;
37
+ };
38
+ const getFontCss = ({ customFonts }) => {
39
+ var _a;
40
+ return ((_a = customFonts == null ? void 0 : customFonts.map((font) => getCssFromFont(font))) == null ? void 0 : _a.join(" ")) || "";
41
+ };
42
+ const getCss = ({
43
+ cssCode,
44
+ contentId
45
+ }) => {
46
+ if (!cssCode) {
47
+ return "";
48
+ }
49
+ if (!contentId) {
50
+ return cssCode;
51
+ }
52
+ return (cssCode == null ? void 0 : cssCode.replace(/&/g, `div[builder-content-id="${contentId}"]`)) || "";
53
+ };
54
+ export {
55
+ getCss,
56
+ getFontCss
57
+ };
@@ -1,67 +1,19 @@
1
1
  import { createSignal } from "solid-js";
2
2
 
3
3
  import RenderInlinedStyles from "../../render-inlined-styles.jsx";
4
+ import { getCss } from "./render-styles.helpers";
5
+ import { getFontCss } from "./render-styles.helpers";
4
6
 
5
7
  function RenderContentStyles(props) {
6
- function getCssFromFont(font) {
7
- // TODO: compute what font sizes are used and only load those.......
8
- const family =
9
- font.family +
10
- (font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
11
- const name = family.split(",")[0];
12
- const url = font.fileUrl ?? font?.files?.regular;
13
- let str = "";
14
- if (url && family && name) {
15
- str += `
16
- @font-face {
17
- font-family: "${family}";
18
- src: local("${name}"), url('${url}') format('woff2');
19
- font-display: fallback;
20
- font-weight: 400;
21
- }
22
- `.trim();
23
- }
24
- if (font.files) {
25
- for (const weight in font.files) {
26
- const isNumber = String(Number(weight)) === weight;
27
- if (!isNumber) {
28
- continue;
29
- }
30
- // TODO: maybe limit number loaded
31
- const weightUrl = font.files[weight];
32
- if (weightUrl && weightUrl !== url) {
33
- str += `
34
- @font-face {
35
- font-family: "${family}";
36
- src: url('${weightUrl}') format('woff2');
37
- font-display: fallback;
38
- font-weight: ${weight};
39
- }
40
- `.trim();
41
- }
42
- }
43
- }
44
- return str;
45
- }
46
-
47
- function getFontCss({ customFonts }) {
48
- // TODO: flag for this
49
- // if (!this.builder.allowCustomFonts) {
50
- // return '';
51
- // }
52
- // TODO: separate internal data from external
53
- return (
54
- customFonts?.map((font) => this.getCssFromFont(font))?.join(" ") || ""
55
- );
56
- }
57
-
58
- function injectedStyles() {
59
- return `
60
- ${props.cssCode || ""}
8
+ const [injectedStyles, setInjectedStyles] = createSignal(`
9
+ ${getCss({
10
+ cssCode: props.cssCode,
11
+ contentId: props.contentId,
12
+ })}
61
13
  ${getFontCss({
62
14
  customFonts: props.customFonts,
63
- })}`;
64
- }
15
+ })}
16
+ `);
65
17
 
66
18
  return <RenderInlinedStyles styles={injectedStyles()}></RenderInlinedStyles>;
67
19
  }
@@ -0,0 +1,48 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ const getContextStateInitialValue = ({
21
+ content,
22
+ data,
23
+ locale
24
+ }) => {
25
+ var _a, _b, _c;
26
+ const defaultValues = {};
27
+ (_b = (_a = content == null ? void 0 : content.data) == null ? void 0 : _a.inputs) == null ? void 0 : _b.forEach((input) => {
28
+ var _a2;
29
+ if (input.name && input.defaultValue !== void 0 && ((_a2 = content == null ? void 0 : content.data) == null ? void 0 : _a2.state) && content.data.state[input.name] === void 0) {
30
+ defaultValues[input.name] = input.defaultValue;
31
+ }
32
+ });
33
+ const stateToUse = __spreadValues(__spreadValues(__spreadValues({}, (_c = content == null ? void 0 : content.data) == null ? void 0 : _c.state), data), locale ? { locale } : {});
34
+ return __spreadValues(__spreadValues({}, defaultValues), stateToUse);
35
+ };
36
+ const getContentInitialValue = ({
37
+ content,
38
+ data
39
+ }) => {
40
+ return !content ? void 0 : __spreadProps(__spreadValues({}, content), {
41
+ data: __spreadValues(__spreadValues({}, content == null ? void 0 : content.data), data),
42
+ meta: content == null ? void 0 : content.meta
43
+ });
44
+ };
45
+ export {
46
+ getContentInitialValue,
47
+ getContextStateInitialValue
48
+ };
@@ -24,6 +24,10 @@ import {
24
24
  } from "../../scripts/init-editing.js";
25
25
  import { checkIsDefined } from "../../helpers/nullable.js";
26
26
  import { getInteractionPropertiesForEvent } from "../../functions/track/interaction.js";
27
+ import {
28
+ getContentInitialValue,
29
+ getContextStateInitialValue,
30
+ } from "./render-content.helpers.js";
27
31
 
28
32
  function RenderContent(props) {
29
33
  const [forceReRenderCount, setForceReRenderCount] = createSignal(0);
@@ -32,15 +36,17 @@ function RenderContent(props) {
32
36
 
33
37
  const [update, setUpdate] = createSignal(0);
34
38
 
35
- const [useBreakpoints, setUseBreakpoints] = createSignal(null);
36
-
37
39
  const [canTrackToUse, setCanTrackToUse] = createSignal(
38
40
  checkIsDefined(props.canTrack) ? props.canTrack : true
39
41
  );
40
42
 
41
- const [overrideState, setOverrideState] = createSignal({});
42
-
43
- const [contextContext, setContextContext] = createSignal(props.context || {});
43
+ const [contentState, setContentState] = createSignal(
44
+ getContextStateInitialValue({
45
+ content: props.content,
46
+ data: props.data,
47
+ locale: props.locale,
48
+ })
49
+ );
44
50
 
45
51
  const [allRegisteredComponents, setAllRegisteredComponents] = createSignal(
46
52
  [
@@ -65,17 +71,35 @@ function RenderContent(props) {
65
71
 
66
72
  const [clicked, setClicked] = createSignal(false);
67
73
 
68
- function contentState() {
69
- return {
70
- ...props.content?.data?.state,
71
- ...props.data,
72
- ...(props.locale
73
- ? {
74
- locale: props.locale,
75
- }
76
- : {}),
77
- ...overrideState(),
78
- };
74
+ function mergeNewContent(newContent) {
75
+ setUseContent({
76
+ ...useContent,
77
+ ...newContent,
78
+ data: {
79
+ ...useContent?.data,
80
+ ...newContent?.data,
81
+ },
82
+ meta: {
83
+ ...useContent?.meta,
84
+ ...newContent?.meta,
85
+ breakpoints:
86
+ newContent?.meta?.breakpoints || useContent?.meta?.breakpoints,
87
+ },
88
+ });
89
+ }
90
+
91
+ function setBreakpoints(breakpoints) {
92
+ setUseContent({
93
+ ...useContent,
94
+ meta: {
95
+ ...useContent?.meta,
96
+ breakpoints,
97
+ },
98
+ });
99
+ }
100
+
101
+ function setContextState(newState) {
102
+ setContentState(newState);
79
103
  }
80
104
 
81
105
  function processMessage(event) {
@@ -88,7 +112,9 @@ function RenderContent(props) {
88
112
  if (!contentId || contentId !== useContent?.id) {
89
113
  return;
90
114
  }
91
- setUseBreakpoints(breakpoints);
115
+ if (breakpoints) {
116
+ setBreakpoints(breakpoints);
117
+ }
92
118
  setForceReRenderCount(forceReRenderCount() + 1); // This is a hack to force Qwik to re-render.
93
119
  break;
94
120
  }
@@ -101,7 +127,7 @@ function RenderContent(props) {
101
127
  messageContent.modelName;
102
128
  const contentData = messageContent.data;
103
129
  if (key === props.model) {
104
- setOverrideContent(contentData);
130
+ mergeNewContent(contentData);
105
131
  setForceReRenderCount(forceReRenderCount() + 1); // This is a hack to force Qwik to re-render.
106
132
  }
107
133
 
@@ -121,7 +147,7 @@ function RenderContent(props) {
121
147
  if (jsCode) {
122
148
  evaluate({
123
149
  code: jsCode,
124
- context: contextContext(),
150
+ context: props.context || {},
125
151
  state: contentState(),
126
152
  });
127
153
  }
@@ -150,7 +176,7 @@ function RenderContent(props) {
150
176
  return expression.replace(/{{([^}]+)}}/g, (_match, group) =>
151
177
  evaluate({
152
178
  code: group,
153
- context: contextContext(),
179
+ context: props.context || {},
154
180
  state: contentState(),
155
181
  })
156
182
  );
@@ -160,11 +186,11 @@ function RenderContent(props) {
160
186
  fetch(url)
161
187
  .then((response) => response.json())
162
188
  .then((json) => {
163
- const newOverrideState = {
164
- ...overrideState(),
189
+ const newState = {
190
+ ...contentState(),
165
191
  [key]: json,
166
192
  };
167
- setOverrideState(newOverrideState);
193
+ setContextState(newState);
168
194
  })
169
195
  .catch((err) => {
170
196
  console.log("error fetching dynamic data", url, err);
@@ -205,35 +231,11 @@ function RenderContent(props) {
205
231
  TARGET !== "reactNative"
206
232
  );
207
233
  }
208
-
209
- const updateUseContent = function useContent() {
210
- if (!props.content && !overrideContent()) {
211
- return undefined;
212
- }
213
- return {
214
- ...props.content,
215
- ...overrideContent(),
216
- data: {
217
- ...props.content?.data,
218
- ...props.data,
219
- ...overrideContent()?.data,
220
- },
221
- meta: {
222
- ...props.content?.meta,
223
- ...overrideContent()?.meta,
224
- breakpoints:
225
- useBreakpoints() ||
226
- overrideContent()?.meta?.breakpoints ||
227
- props.content?.meta?.breakpoints,
228
- },
229
- };
230
- };
231
- const [useContent, setUseContent] = createStore(updateUseContent());
232
- createEffect(
233
- on(
234
- () => [overrideContent(), useBreakpoints(), props.content, props.data],
235
- () => setUseContent(reconcile(updateUseContent()))
236
- )
234
+ const [useContent, setUseContent] = createStore(
235
+ getContentInitialValue({
236
+ content: props.content,
237
+ data: props.data,
238
+ })
237
239
  );
238
240
 
239
241
  let elementRef;
@@ -311,7 +313,7 @@ function RenderContent(props) {
311
313
  apiKey: props.apiKey,
312
314
  }).then((content) => {
313
315
  if (content) {
314
- setOverrideContent(content);
316
+ mergeNewContent(content);
315
317
  }
316
318
  });
317
319
  }
@@ -344,7 +346,8 @@ function RenderContent(props) {
344
346
  value={{
345
347
  content: useContent,
346
348
  state: contentState(),
347
- context: contextContext(),
349
+ setState: setContextState,
350
+ context: props.context || {},
348
351
  apiKey: props.apiKey,
349
352
  registeredComponents: allRegisteredComponents(),
350
353
  }}
@@ -358,6 +361,7 @@ function RenderContent(props) {
358
361
  >
359
362
  <Show when={shouldRenderContentStyles()}>
360
363
  <RenderContentStyles
364
+ contentId={useContent?.id}
361
365
  cssCode={useContent?.data?.cssCode}
362
366
  customFonts={useContent?.data?.customFonts}
363
367
  ></RenderContentStyles>
@@ -37,17 +37,17 @@ import { componentInfo as imgComponentInfo } from "../blocks/img/component-info.
37
37
  import { default as customCode } from "../blocks/custom-code/custom-code";
38
38
  import { componentInfo as customCodeInfo } from "../blocks/custom-code/component-info.js";
39
39
  const getDefaultRegisteredComponents = () => [
40
+ __spreadValues({ component: Button }, buttonComponentInfo),
40
41
  __spreadValues({ component: Columns }, columnsComponentInfo),
42
+ __spreadValues({ component: customCode }, customCodeInfo),
43
+ __spreadValues({ component: embed }, embedComponentInfo),
44
+ __spreadValues({ component: Fragment }, fragmentComponentInfo),
41
45
  __spreadValues({ component: Image }, imageComponentInfo),
42
46
  __spreadValues({ component: Img }, imgComponentInfo),
43
- __spreadValues({ component: Text }, textComponentInfo),
44
- __spreadValues({ component: Video }, videoComponentInfo),
45
- __spreadValues({ component: Symbol }, symbolComponentInfo),
46
- __spreadValues({ component: Button }, buttonComponentInfo),
47
47
  __spreadValues({ component: Section }, sectionComponentInfo),
48
- __spreadValues({ component: Fragment }, fragmentComponentInfo),
49
- __spreadValues({ component: embed }, embedComponentInfo),
50
- __spreadValues({ component: customCode }, customCodeInfo)
48
+ __spreadValues({ component: Symbol }, symbolComponentInfo),
49
+ __spreadValues({ component: Text }, textComponentInfo),
50
+ __spreadValues({ component: Video }, videoComponentInfo)
51
51
  ];
52
52
  export {
53
53
  getDefaultRegisteredComponents
@@ -3,6 +3,8 @@ var stdin_default = createContext({
3
3
  content: null,
4
4
  context: {},
5
5
  state: {},
6
+ setState() {
7
+ },
6
8
  apiKey: null,
7
9
  registeredComponents: {},
8
10
  inheritedStyles: {}
@@ -1,22 +1,22 @@
1
- import { default as default2 } from "../blocks/columns/columns";
2
- import { default as default3 } from "../blocks/image/image";
3
- import { default as default4 } from "../blocks/text/text";
4
- import { default as default5 } from "../blocks/video/video";
5
- import { default as default6 } from "../blocks/symbol/symbol";
6
- import { default as default7 } from "../blocks/button/button";
1
+ import { default as default2 } from "../blocks/button/button";
2
+ import { default as default3 } from "../blocks/columns/columns";
3
+ import { default as default4 } from "../blocks/fragment/fragment";
4
+ import { default as default5 } from "../blocks/image/image";
5
+ import { default as default6 } from "../components/render-blocks";
6
+ import { default as default7 } from "../components/render-content/render-content";
7
7
  import { default as default8 } from "../blocks/section/section";
8
- import { default as default9 } from "../blocks/fragment/fragment";
9
- import { default as default10 } from "../components/render-content/render-content";
10
- import { default as default11 } from "../components/render-blocks";
8
+ import { default as default9 } from "../blocks/symbol/symbol";
9
+ import { default as default10 } from "../blocks/text/text";
10
+ import { default as default11 } from "../blocks/video/video";
11
11
  export {
12
- default7 as Button,
13
- default2 as Columns,
14
- default9 as Fragment,
15
- default3 as Image,
16
- default11 as RenderBlocks,
17
- default10 as RenderContent,
12
+ default2 as Button,
13
+ default3 as Columns,
14
+ default4 as Fragment,
15
+ default5 as Image,
16
+ default6 as RenderBlocks,
17
+ default7 as RenderContent,
18
18
  default8 as Section,
19
- default6 as Symbol,
20
- default4 as Text,
21
- default5 as Video
19
+ default9 as Symbol,
20
+ default10 as Text,
21
+ default11 as Video
22
22
  };
File without changes