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

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.
@@ -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;
@@ -1,29 +0,0 @@
1
- import { Show } from "solid-js";
2
- import { Dynamic } from "solid-js/web";
3
- import { createMutable } from "solid-js/store";
4
-
5
- import { TARGET } from "../constants/target.js";
6
-
7
- function RenderInlinedStyles(props) {
8
- const state = createMutable({
9
- get injectedStyleScript() {
10
- return `<${state.tagName}>${props.styles}</${state.tagName}>`;
11
- },
12
- get tagName() {
13
- // NOTE: we have to obfusctate the name of the tag due to a limitation in the svelte-preprocessor plugin.
14
- // https://github.com/sveltejs/vite-plugin-svelte/issues/315#issuecomment-1109000027
15
- return "sty" + "le";
16
- },
17
- });
18
-
19
- return (
20
- <Show
21
- fallback={<Dynamic component={state.tagName}>{props.styles}</Dynamic>}
22
- when={TARGET === "svelte"}
23
- >
24
- <div innerHTML={state.injectedStyleScript}></div>
25
- </Show>
26
- );
27
- }
28
-
29
- export default RenderInlinedStyles;