@builder.io/sdk-react-native 0.0.1-34 → 0.0.1-35
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 +1 -1
- package/src/blocks/button.js +35 -0
- package/src/blocks/button.lite.tsx +25 -0
- package/src/blocks/columns.js +43 -0
- package/src/blocks/columns.lite.tsx +41 -0
- package/src/blocks/custom-code.js +56 -0
- package/src/blocks/custom-code.lite.tsx +67 -0
- package/src/blocks/embed.js +56 -0
- package/src/blocks/embed.lite.tsx +65 -0
- package/src/blocks/form.js +226 -0
- package/src/blocks/form.lite.tsx +253 -0
- package/src/blocks/fragment.js +13 -0
- package/src/blocks/fragment.lite.tsx +11 -0
- package/src/blocks/image.js +121 -0
- package/src/blocks/image.lite.tsx +67 -0
- package/src/blocks/img.js +41 -0
- package/src/blocks/img.lite.tsx +19 -0
- package/src/blocks/input.js +41 -0
- package/src/blocks/input.lite.tsx +21 -0
- package/src/blocks/raw-text.js +17 -0
- package/src/blocks/raw-text.lite.tsx +12 -0
- package/src/blocks/section.js +36 -0
- package/src/blocks/section.lite.tsx +20 -0
- package/src/blocks/select.js +41 -0
- package/src/blocks/select.lite.tsx +24 -0
- package/src/blocks/submit-button.js +34 -0
- package/src/blocks/submit-button.lite.tsx +11 -0
- package/src/blocks/symbol.js +20 -0
- package/src/blocks/symbol.lite.tsx +20 -0
- package/src/blocks/text.js +85 -0
- package/src/blocks/text.lite.tsx +12 -0
- package/src/blocks/textarea.js +37 -0
- package/src/blocks/textarea.lite.tsx +15 -0
- package/src/blocks/video.js +88 -0
- package/src/blocks/video.lite.tsx +28 -0
- package/src/components/block-styles.js +7 -0
- package/src/components/block-styles.lite.tsx +7 -0
- package/src/components/error-boundary.js +25 -0
- package/src/components/error-boundary.lite.tsx +7 -0
- package/src/components/render-block.js +105 -0
- package/src/components/render-block.lite.tsx +125 -0
- package/src/components/render-blocks.js +47 -0
- package/src/components/render-blocks.lite.tsx +60 -0
- package/src/components/render-content.js +143 -0
- package/src/components/render-content.lite.tsx +207 -0
- package/src/constants/device-sizes.js +40 -0
- package/src/context/builder.context.js +5 -0
- package/src/functions/evaluate.js +20 -0
- package/src/functions/get-block-actions.js +23 -0
- package/src/functions/get-block-component-options.js +24 -0
- package/src/functions/get-block-properties.js +43 -0
- package/src/functions/get-block-styles.js +61 -0
- package/src/functions/get-block-tag.js +8 -0
- package/src/functions/get-content.js +128 -0
- package/src/functions/get-content.test.js +47 -0
- package/src/functions/get-fetch.js +13 -0
- package/src/functions/get-global-this.js +19 -0
- package/src/functions/get-processed-block.js +45 -0
- package/src/functions/get-processed-block.test.js +27 -0
- package/src/functions/get-target.js +7 -0
- package/src/functions/if-target.js +7 -0
- package/src/functions/is-browser.js +8 -0
- package/src/functions/is-editing.js +8 -0
- package/src/functions/is-iframe.js +8 -0
- package/src/functions/is-previewing.js +15 -0
- package/src/functions/is-react-native.js +7 -0
- package/src/functions/macro-eval.js +6 -0
- package/src/functions/on-change.js +28 -0
- package/src/functions/on-change.test.js +21 -0
- package/src/functions/previewing-model-name.js +12 -0
- package/src/functions/register-component.js +54 -0
- package/src/functions/register.js +30 -0
- package/src/functions/set-editor-settings.js +16 -0
- package/src/functions/set.js +12 -0
- package/src/functions/set.test.js +18 -0
- package/src/functions/track.js +19 -0
- package/src/index.js +25 -0
- package/src/package.json +18 -0
- package/src/scripts/init-editing.js +71 -0
- package/src/types/builder-block.js +1 -0
- package/src/types/builder-content.js +1 -0
- package/src/types/deep-partial.js +1 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { View, StyleSheet, Image, Text } from "react-native";
|
|
3
|
+
import { useState, useContext } from "react";
|
|
4
|
+
import { getBlockComponentOptions } from "../functions/get-block-component-options";
|
|
5
|
+
import { getBlockProperties } from "../functions/get-block-properties";
|
|
6
|
+
import { getBlockStyles } from "../functions/get-block-styles";
|
|
7
|
+
import { getBlockTag } from "../functions/get-block-tag";
|
|
8
|
+
import { components } from "../functions/register-component";
|
|
9
|
+
import BuilderContext from "../context/builder.context.lite";
|
|
10
|
+
import { getBlockActions } from "../functions/get-block-actions";
|
|
11
|
+
import { getProcessedBlock } from "../functions/get-processed-block";
|
|
12
|
+
import BlockStyles from "./block-styles.lite";
|
|
13
|
+
import RenderBlocks from "./render-blocks.lite";
|
|
14
|
+
|
|
15
|
+
export default function RenderBlock(props) {
|
|
16
|
+
function component() {
|
|
17
|
+
const componentName = useBlock().component?.name;
|
|
18
|
+
|
|
19
|
+
if (!componentName) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const ref = components[useBlock().component?.name];
|
|
24
|
+
|
|
25
|
+
if (componentName && !ref) {
|
|
26
|
+
// TODO: Public doc page with more info about this message
|
|
27
|
+
console.warn(`
|
|
28
|
+
Could not find a registered component named "${componentName}".
|
|
29
|
+
If you registered it, is the file that registered it imported by the file that needs to render it?`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return ref;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function componentInfo() {
|
|
36
|
+
return component?.()?.info;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function componentRef() {
|
|
40
|
+
return component?.()?.component;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function tagName() {
|
|
44
|
+
return getBlockTag(useBlock());
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function properties() {
|
|
48
|
+
return getBlockProperties(useBlock());
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function useBlock() {
|
|
52
|
+
return getProcessedBlock({
|
|
53
|
+
block: props.block,
|
|
54
|
+
state: builderContext.state,
|
|
55
|
+
context: builderContext.context,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function actions() {
|
|
60
|
+
return getBlockActions({
|
|
61
|
+
block: useBlock(),
|
|
62
|
+
state: builderContext.state,
|
|
63
|
+
context: builderContext.context,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function css() {
|
|
68
|
+
return getBlockStyles(useBlock());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function componentOptions() {
|
|
72
|
+
return getBlockComponentOptions(useBlock());
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const builderContext = useContext(BuilderContext);
|
|
76
|
+
|
|
77
|
+
const ComponentRefRef = componentRef();
|
|
78
|
+
const TagNameRef = tagName();
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<>
|
|
82
|
+
{!componentInfo?.()?.noWrap ? (
|
|
83
|
+
<>
|
|
84
|
+
<TagNameRef {...properties()} style={css()}>
|
|
85
|
+
<BlockStyles block={useBlock()} />
|
|
86
|
+
|
|
87
|
+
{componentRef() ? (
|
|
88
|
+
<ComponentRefRef
|
|
89
|
+
{...componentOptions()}
|
|
90
|
+
builderBlock={useBlock()}
|
|
91
|
+
>
|
|
92
|
+
{useBlock().children ? (
|
|
93
|
+
<>
|
|
94
|
+
<RenderBlocks
|
|
95
|
+
path="children"
|
|
96
|
+
blocks={useBlock().children}
|
|
97
|
+
/>
|
|
98
|
+
</>
|
|
99
|
+
) : null}
|
|
100
|
+
</ComponentRefRef>
|
|
101
|
+
) : null}
|
|
102
|
+
|
|
103
|
+
{!componentRef() &&
|
|
104
|
+
useBlock().children &&
|
|
105
|
+
useBlock().children.length ? (
|
|
106
|
+
<>
|
|
107
|
+
{useBlock().children?.map((child) => (
|
|
108
|
+
<RenderBlock block={child} />
|
|
109
|
+
))}
|
|
110
|
+
</>
|
|
111
|
+
) : null}
|
|
112
|
+
</TagNameRef>
|
|
113
|
+
</>
|
|
114
|
+
) : (
|
|
115
|
+
<ComponentRefRef
|
|
116
|
+
{...componentInfo?.()?.options}
|
|
117
|
+
attributes={properties()}
|
|
118
|
+
builderBlock={useBlock()}
|
|
119
|
+
style={css()}
|
|
120
|
+
children={useBlock().children}
|
|
121
|
+
/>
|
|
122
|
+
)}
|
|
123
|
+
</>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { View, StyleSheet } from "react-native";
|
|
3
|
+
import { isEditing } from "../functions/is-editing";
|
|
4
|
+
import RenderBlock from "./render-block";
|
|
5
|
+
function RenderBlocks(props) {
|
|
6
|
+
var _a, _b;
|
|
7
|
+
function onClick() {
|
|
8
|
+
var _a2, _b2;
|
|
9
|
+
if (isEditing() && !((_a2 = props.blocks) == null ? void 0 : _a2.length)) {
|
|
10
|
+
(_b2 = window.parent) == null ? void 0 : _b2.postMessage({
|
|
11
|
+
type: "builder.clickEmptyBlocks",
|
|
12
|
+
data: {
|
|
13
|
+
parentElementId: props.parent,
|
|
14
|
+
dataPath: props.path
|
|
15
|
+
}
|
|
16
|
+
}, "*");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function onMouseEnter() {
|
|
20
|
+
var _a2, _b2;
|
|
21
|
+
if (isEditing() && !((_a2 = props.blocks) == null ? void 0 : _a2.length)) {
|
|
22
|
+
(_b2 = window.parent) == null ? void 0 : _b2.postMessage({
|
|
23
|
+
type: "builder.hoverEmptyBlocks",
|
|
24
|
+
data: {
|
|
25
|
+
parentElementId: props.parent,
|
|
26
|
+
dataPath: props.path
|
|
27
|
+
}
|
|
28
|
+
}, "*");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return /* @__PURE__ */ React.createElement(View, {
|
|
32
|
+
"builder-path": props.path,
|
|
33
|
+
"builder-parent-id": props.parent,
|
|
34
|
+
onClick: (event) => onClick,
|
|
35
|
+
onMouseEnter: (event) => onMouseEnter,
|
|
36
|
+
className: "builder-blocks" + (!((_a = props.blocks) == null ? void 0 : _a.length) ? " no-blocks" : ""),
|
|
37
|
+
style: styles.view1
|
|
38
|
+
}, props.blocks ? /* @__PURE__ */ React.createElement(React.Fragment, null, (_b = props.blocks) == null ? void 0 : _b.map((block) => /* @__PURE__ */ React.createElement(RenderBlock, {
|
|
39
|
+
block
|
|
40
|
+
}))) : null);
|
|
41
|
+
}
|
|
42
|
+
const styles = StyleSheet.create({
|
|
43
|
+
view1: { display: "flex", flexDirection: "column", alignItems: "stretch" }
|
|
44
|
+
});
|
|
45
|
+
export {
|
|
46
|
+
RenderBlocks as default
|
|
47
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { View, StyleSheet, Image, Text } from "react-native";
|
|
3
|
+
import { useState, useContext } from "react";
|
|
4
|
+
import { isEditing } from "../functions/is-editing";
|
|
5
|
+
import RenderBlock from "./render-block.lite";
|
|
6
|
+
|
|
7
|
+
export default function RenderBlocks(props) {
|
|
8
|
+
function onClick() {
|
|
9
|
+
if (isEditing() && !props.blocks?.length) {
|
|
10
|
+
window.parent?.postMessage(
|
|
11
|
+
{
|
|
12
|
+
type: "builder.clickEmptyBlocks",
|
|
13
|
+
data: {
|
|
14
|
+
parentElementId: props.parent,
|
|
15
|
+
dataPath: props.path,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
"*"
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function onMouseEnter() {
|
|
24
|
+
if (isEditing() && !props.blocks?.length) {
|
|
25
|
+
window.parent?.postMessage(
|
|
26
|
+
{
|
|
27
|
+
type: "builder.hoverEmptyBlocks",
|
|
28
|
+
data: {
|
|
29
|
+
parentElementId: props.parent,
|
|
30
|
+
dataPath: props.path,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
"*"
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<View
|
|
40
|
+
builder-path={props.path}
|
|
41
|
+
builder-parent-id={props.parent}
|
|
42
|
+
onClick={(event) => onClick}
|
|
43
|
+
onMouseEnter={(event) => onMouseEnter}
|
|
44
|
+
className={"builder-blocks" + (!props.blocks?.length ? " no-blocks" : "")}
|
|
45
|
+
style={styles.view1}
|
|
46
|
+
>
|
|
47
|
+
{props.blocks ? (
|
|
48
|
+
<>
|
|
49
|
+
{props.blocks?.map((block) => (
|
|
50
|
+
<RenderBlock block={block} />
|
|
51
|
+
))}
|
|
52
|
+
</>
|
|
53
|
+
) : null}
|
|
54
|
+
</View>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const styles = StyleSheet.create({
|
|
59
|
+
view1: { display: "flex", flexDirection: "column", alignItems: "stretch" },
|
|
60
|
+
});
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { View, Text } from "react-native";
|
|
3
|
+
import { useState, useEffect } from "react";
|
|
4
|
+
import { isBrowser } from "../functions/is-browser";
|
|
5
|
+
import RenderBlock from "./render-block";
|
|
6
|
+
import BuilderContext from "../context/builder.context";
|
|
7
|
+
import { track } from "../functions/track";
|
|
8
|
+
import { isReactNative } from "../functions/is-react-native";
|
|
9
|
+
import { isEditing } from "../functions/is-editing";
|
|
10
|
+
import { isPreviewing } from "../functions/is-previewing";
|
|
11
|
+
import { previewingModelName } from "../functions/previewing-model-name";
|
|
12
|
+
import { getContent } from "../functions/get-content";
|
|
13
|
+
function RenderContent(props) {
|
|
14
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
15
|
+
function useContent() {
|
|
16
|
+
return overrideContent || props.content;
|
|
17
|
+
}
|
|
18
|
+
const [update, setUpdate] = useState(() => 0);
|
|
19
|
+
const [state, setState] = useState(() => ({}));
|
|
20
|
+
const [context, setContext] = useState(() => ({}));
|
|
21
|
+
const [overrideContent, setOverrideContent] = useState(() => null);
|
|
22
|
+
function getCssFromFont(font, data) {
|
|
23
|
+
const family = font.family + (font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
|
|
24
|
+
const name = family.split(",")[0];
|
|
25
|
+
const url = font.fileUrl ? font.fileUrl : font.files && font.files.regular;
|
|
26
|
+
let str = "";
|
|
27
|
+
if (url && family && name) {
|
|
28
|
+
str += `
|
|
29
|
+
@font-face {
|
|
30
|
+
font-family: "${family}";
|
|
31
|
+
src: local("${name}"), url('${url}') format('woff2');
|
|
32
|
+
font-display: fallback;
|
|
33
|
+
font-weight: 400;
|
|
34
|
+
}
|
|
35
|
+
`.trim();
|
|
36
|
+
}
|
|
37
|
+
if (font.files) {
|
|
38
|
+
for (const weight in font.files) {
|
|
39
|
+
const isNumber = String(Number(weight)) === weight;
|
|
40
|
+
if (!isNumber) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const weightUrl = font.files[weight];
|
|
44
|
+
if (weightUrl && weightUrl !== url) {
|
|
45
|
+
str += `
|
|
46
|
+
@font-face {
|
|
47
|
+
font-family: "${family}";
|
|
48
|
+
src: url('${weightUrl}') format('woff2');
|
|
49
|
+
font-display: fallback;
|
|
50
|
+
font-weight: ${weight};
|
|
51
|
+
}
|
|
52
|
+
`.trim();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return str;
|
|
57
|
+
}
|
|
58
|
+
function getFontCss(data) {
|
|
59
|
+
return (data == null ? void 0 : data.customFonts) && data.customFonts.length && data.customFonts.map((font) => this.getCssFromFont(font, data)).join(" ") || "";
|
|
60
|
+
}
|
|
61
|
+
function processMessage(event) {
|
|
62
|
+
const { data } = event;
|
|
63
|
+
if (data) {
|
|
64
|
+
switch (data.type) {
|
|
65
|
+
case "builder.contentUpdate": {
|
|
66
|
+
const key = data.data.key || data.data.alias || data.data.entry || data.data.modelName;
|
|
67
|
+
const contentData = data.data.data;
|
|
68
|
+
if (key === props.model) {
|
|
69
|
+
setOverrideContent(contentData);
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case "builder.patchUpdates": {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (isBrowser()) {
|
|
81
|
+
if (isEditing()) {
|
|
82
|
+
window.addEventListener("message", processMessage);
|
|
83
|
+
}
|
|
84
|
+
if (useContent() && !isEditing()) {
|
|
85
|
+
track("impression", {
|
|
86
|
+
contentId: useContent().id
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (isPreviewing()) {
|
|
90
|
+
if (props.model && previewingModelName() === props.model) {
|
|
91
|
+
const options = {};
|
|
92
|
+
const currentUrl = new URL(location.href);
|
|
93
|
+
const apiKey = currentUrl.searchParams.get("apiKey");
|
|
94
|
+
if (apiKey) {
|
|
95
|
+
const builderPrefix = "builder.";
|
|
96
|
+
currentUrl.searchParams.forEach((value, key) => {
|
|
97
|
+
if (key.startsWith(builderPrefix)) {
|
|
98
|
+
options[key.replace(builderPrefix, "")] = value;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
getContent({
|
|
102
|
+
model: props.model,
|
|
103
|
+
apiKey,
|
|
104
|
+
options
|
|
105
|
+
}).then((content) => {
|
|
106
|
+
if (content) {
|
|
107
|
+
setOverrideContent(content);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}, []);
|
|
115
|
+
return /* @__PURE__ */ React.createElement(BuilderContext.Provider, {
|
|
116
|
+
value: {
|
|
117
|
+
get content() {
|
|
118
|
+
return useContent();
|
|
119
|
+
},
|
|
120
|
+
get state() {
|
|
121
|
+
return state;
|
|
122
|
+
},
|
|
123
|
+
get context() {
|
|
124
|
+
return context;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}, useContent() ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(View, {
|
|
128
|
+
onClick: (event) => {
|
|
129
|
+
if (!isEditing()) {
|
|
130
|
+
track("click", {
|
|
131
|
+
contentId: useContent().id
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
"data-builder-content-id": (_a = useContent == null ? void 0 : useContent()) == null ? void 0 : _a.id
|
|
136
|
+
}, (((_c = (_b = useContent == null ? void 0 : useContent()) == null ? void 0 : _b.data) == null ? void 0 : _c.cssCode) || ((_e = (_d = useContent == null ? void 0 : useContent()) == null ? void 0 : _d.data) == null ? void 0 : _e.customFonts) && ((_g = (_f = useContent == null ? void 0 : useContent()) == null ? void 0 : _f.data) == null ? void 0 : _g.customFonts.length)) && !isReactNative() ? /* @__PURE__ */ React.createElement(View, null, /* @__PURE__ */ React.createElement(Text, null, useContent().data.cssCode), /* @__PURE__ */ React.createElement(Text, null, getFontCss(useContent().data))) : null, (_j = (_i = (_h = useContent == null ? void 0 : useContent()) == null ? void 0 : _h.data) == null ? void 0 : _i.blocks) == null ? void 0 : _j.map((block) => /* @__PURE__ */ React.createElement(RenderBlock, {
|
|
137
|
+
key: block.id,
|
|
138
|
+
block
|
|
139
|
+
})))) : null);
|
|
140
|
+
}
|
|
141
|
+
export {
|
|
142
|
+
RenderContent as default
|
|
143
|
+
};
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { View, StyleSheet, Image, Text } from "react-native";
|
|
3
|
+
import { useState, useContext, useEffect } from "react";
|
|
4
|
+
import { isBrowser } from "../functions/is-browser";
|
|
5
|
+
import RenderBlock from "./render-block.lite";
|
|
6
|
+
import BuilderContext from "../context/builder.context.lite";
|
|
7
|
+
import { track } from "../functions/track";
|
|
8
|
+
import { ifTarget } from "../functions/if-target";
|
|
9
|
+
import { onChange } from "../functions/on-change";
|
|
10
|
+
import { isReactNative } from "../functions/is-react-native";
|
|
11
|
+
import { isEditing } from "../functions/is-editing";
|
|
12
|
+
import { isPreviewing } from "../functions/is-previewing";
|
|
13
|
+
import { previewingModelName } from "../functions/previewing-model-name";
|
|
14
|
+
import { getContent } from "../functions/get-content";
|
|
15
|
+
|
|
16
|
+
export default function RenderContent(props) {
|
|
17
|
+
function useContent() {
|
|
18
|
+
return overrideContent || props.content;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const [update, setUpdate] = useState(() => 0);
|
|
22
|
+
|
|
23
|
+
const [state, setState] = useState(() => ({}));
|
|
24
|
+
|
|
25
|
+
const [context, setContext] = useState(() => ({}));
|
|
26
|
+
|
|
27
|
+
const [overrideContent, setOverrideContent] = useState(() => null);
|
|
28
|
+
|
|
29
|
+
function getCssFromFont(font, data) {
|
|
30
|
+
// TODO: compute what font sizes are used and only load those.......
|
|
31
|
+
const family =
|
|
32
|
+
font.family +
|
|
33
|
+
(font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
|
|
34
|
+
const name = family.split(",")[0];
|
|
35
|
+
const url = font.fileUrl ? font.fileUrl : font.files && font.files.regular;
|
|
36
|
+
let str = "";
|
|
37
|
+
|
|
38
|
+
if (url && family && name) {
|
|
39
|
+
str += `
|
|
40
|
+
@font-face {
|
|
41
|
+
font-family: "${family}";
|
|
42
|
+
src: local("${name}"), url('${url}') format('woff2');
|
|
43
|
+
font-display: fallback;
|
|
44
|
+
font-weight: 400;
|
|
45
|
+
}
|
|
46
|
+
`.trim();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (font.files) {
|
|
50
|
+
for (const weight in font.files) {
|
|
51
|
+
const isNumber = String(Number(weight)) === weight;
|
|
52
|
+
|
|
53
|
+
if (!isNumber) {
|
|
54
|
+
continue;
|
|
55
|
+
} // TODO: maybe limit number loaded
|
|
56
|
+
|
|
57
|
+
const weightUrl = font.files[weight];
|
|
58
|
+
|
|
59
|
+
if (weightUrl && weightUrl !== url) {
|
|
60
|
+
str += `
|
|
61
|
+
@font-face {
|
|
62
|
+
font-family: "${family}";
|
|
63
|
+
src: url('${weightUrl}') format('woff2');
|
|
64
|
+
font-display: fallback;
|
|
65
|
+
font-weight: ${weight};
|
|
66
|
+
}
|
|
67
|
+
`.trim();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return str;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function getFontCss(data) {
|
|
76
|
+
// TODO: flag for this
|
|
77
|
+
// if (!this.builder.allowCustomFonts) {
|
|
78
|
+
// return '';
|
|
79
|
+
// }
|
|
80
|
+
// TODO: separate internal data from external
|
|
81
|
+
return (
|
|
82
|
+
(data?.customFonts &&
|
|
83
|
+
data.customFonts.length &&
|
|
84
|
+
data.customFonts
|
|
85
|
+
.map((font) => this.getCssFromFont(font, data))
|
|
86
|
+
.join(" ")) ||
|
|
87
|
+
""
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function processMessage(event) {
|
|
92
|
+
const { data } = event;
|
|
93
|
+
|
|
94
|
+
if (data) {
|
|
95
|
+
switch (data.type) {
|
|
96
|
+
case "builder.contentUpdate": {
|
|
97
|
+
const key =
|
|
98
|
+
data.data.key ||
|
|
99
|
+
data.data.alias ||
|
|
100
|
+
data.data.entry ||
|
|
101
|
+
data.data.modelName;
|
|
102
|
+
const contentData = data.data.data; // oof
|
|
103
|
+
|
|
104
|
+
if (key === props.model) {
|
|
105
|
+
setOverrideContent(contentData);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
case "builder.patchUpdates": {
|
|
112
|
+
// TODO
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (isBrowser()) {
|
|
121
|
+
if (isEditing()) {
|
|
122
|
+
window.addEventListener("message", processMessage);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (useContent() && !isEditing()) {
|
|
126
|
+
track("impression", {
|
|
127
|
+
contentId: useContent().id,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (isPreviewing()) {
|
|
132
|
+
if (props.model && previewingModelName() === props.model) {
|
|
133
|
+
const options = {};
|
|
134
|
+
const currentUrl = new URL(location.href);
|
|
135
|
+
const apiKey = currentUrl.searchParams.get("apiKey");
|
|
136
|
+
|
|
137
|
+
if (apiKey) {
|
|
138
|
+
const builderPrefix = "builder.";
|
|
139
|
+
currentUrl.searchParams.forEach((value, key) => {
|
|
140
|
+
if (key.startsWith(builderPrefix)) {
|
|
141
|
+
options[key.replace(builderPrefix, "")] = value;
|
|
142
|
+
}
|
|
143
|
+
}); // TODO: need access to API key
|
|
144
|
+
|
|
145
|
+
getContent({
|
|
146
|
+
model: props.model,
|
|
147
|
+
apiKey,
|
|
148
|
+
options,
|
|
149
|
+
}).then((content) => {
|
|
150
|
+
if (content) {
|
|
151
|
+
setOverrideContent(content);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
} // TODO: fetch content and override. Forward all builder.* params
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}, []);
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<BuilderContext.Provider
|
|
162
|
+
value={{
|
|
163
|
+
get content() {
|
|
164
|
+
return useContent();
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
get state() {
|
|
168
|
+
return state;
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
get context() {
|
|
172
|
+
return context;
|
|
173
|
+
},
|
|
174
|
+
}}
|
|
175
|
+
>
|
|
176
|
+
{useContent() ? (
|
|
177
|
+
<>
|
|
178
|
+
<View
|
|
179
|
+
onClick={(event) => {
|
|
180
|
+
if (!isEditing()) {
|
|
181
|
+
track("click", {
|
|
182
|
+
contentId: useContent().id,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}}
|
|
186
|
+
data-builder-content-id={useContent?.()?.id}
|
|
187
|
+
>
|
|
188
|
+
{(useContent?.()?.data?.cssCode ||
|
|
189
|
+
(useContent?.()?.data?.customFonts &&
|
|
190
|
+
useContent?.()?.data?.customFonts.length)) &&
|
|
191
|
+
!isReactNative() ? (
|
|
192
|
+
<View>
|
|
193
|
+
<Text>{useContent().data.cssCode}</Text>
|
|
194
|
+
|
|
195
|
+
<Text>{getFontCss(useContent().data)}</Text>
|
|
196
|
+
</View>
|
|
197
|
+
) : null}
|
|
198
|
+
|
|
199
|
+
{useContent?.()?.data?.blocks?.map((block) => (
|
|
200
|
+
<RenderBlock key={block.id} block={block} />
|
|
201
|
+
))}
|
|
202
|
+
</View>
|
|
203
|
+
</>
|
|
204
|
+
) : null}
|
|
205
|
+
</BuilderContext.Provider>
|
|
206
|
+
);
|
|
207
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
const sizeNames = ["xsmall", "small", "medium", "large"];
|
|
3
|
+
const sizes = {
|
|
4
|
+
xsmall: {
|
|
5
|
+
min: 0,
|
|
6
|
+
default: 0,
|
|
7
|
+
max: 0
|
|
8
|
+
},
|
|
9
|
+
small: {
|
|
10
|
+
min: 320,
|
|
11
|
+
default: 321,
|
|
12
|
+
max: 640
|
|
13
|
+
},
|
|
14
|
+
medium: {
|
|
15
|
+
min: 641,
|
|
16
|
+
default: 642,
|
|
17
|
+
max: 991
|
|
18
|
+
},
|
|
19
|
+
large: {
|
|
20
|
+
min: 990,
|
|
21
|
+
default: 991,
|
|
22
|
+
max: 1200
|
|
23
|
+
},
|
|
24
|
+
getWidthForSize(size) {
|
|
25
|
+
return this[size].default;
|
|
26
|
+
},
|
|
27
|
+
getSizeForWidth(width) {
|
|
28
|
+
for (const size of sizeNames) {
|
|
29
|
+
const value = this[size];
|
|
30
|
+
if (width <= value.max) {
|
|
31
|
+
return size;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return "large";
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export {
|
|
38
|
+
sizeNames,
|
|
39
|
+
sizes
|
|
40
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { isBrowser } from "./is-browser";
|
|
3
|
+
import { isEditing } from "./is-editing";
|
|
4
|
+
function evaluate(options) {
|
|
5
|
+
const { code } = options;
|
|
6
|
+
const builder = {
|
|
7
|
+
isEditing: isEditing(),
|
|
8
|
+
isBrowser: isBrowser()
|
|
9
|
+
};
|
|
10
|
+
const useReturn = !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
|
|
11
|
+
const useCode = `${useReturn ? `return (${code});` : code}`;
|
|
12
|
+
try {
|
|
13
|
+
return new Function("builder", "Builder", "state", "context", "event", useCode)(builder, builder, options.state, options.context, options.event);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
console.warn("Builder custom code error", e);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
evaluate
|
|
20
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { evaluate } from "./evaluate";
|
|
3
|
+
function capitalizeFirstLetter(string) {
|
|
4
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
5
|
+
}
|
|
6
|
+
function getBlockActions(options) {
|
|
7
|
+
const obj = {};
|
|
8
|
+
if (options.block.actions) {
|
|
9
|
+
for (const key in options.block.actions) {
|
|
10
|
+
const value = options.block.actions[key];
|
|
11
|
+
obj["on" + capitalizeFirstLetter(key)] = (event) => evaluate({
|
|
12
|
+
code: value,
|
|
13
|
+
context: options.context,
|
|
14
|
+
state: options.state,
|
|
15
|
+
event
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return obj;
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
getBlockActions
|
|
23
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __spreadValues = (a, b) => {
|
|
8
|
+
for (var prop in b || (b = {}))
|
|
9
|
+
if (__hasOwnProp.call(b, prop))
|
|
10
|
+
__defNormalProp(a, prop, b[prop]);
|
|
11
|
+
if (__getOwnPropSymbols)
|
|
12
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
13
|
+
if (__propIsEnum.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
}
|
|
16
|
+
return a;
|
|
17
|
+
};
|
|
18
|
+
function getBlockComponentOptions(block) {
|
|
19
|
+
var _a;
|
|
20
|
+
return __spreadValues(__spreadValues({}, (_a = block.component) == null ? void 0 : _a.options), block.options);
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
getBlockComponentOptions
|
|
24
|
+
};
|