Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Use new data sources in jsx component generator (#2318)

Bogdan Chadkin706a590c2b84
diff --git a/packages/react-sdk/src/component-generator.test.ts b/packages/react-sdk/src/component-generator.test.ts
index 8d83545d91..e0cb4cb6a0 100644
--- a/packages/react-sdk/src/component-generator.test.ts
+++ b/packages/react-sdk/src/component-generator.test.ts
@@ -1,6 +1,11 @@
import { expect, test } from "@jest/globals";
import stripIndent from "strip-indent";
-import { createScope, type Instance, type Prop } from "@webstudio-is/sdk";
+import {
+ createScope,
+ type DataSource,
+ type Instance,
+ type Prop,
+} from "@webstudio-is/sdk";
import { showAttribute } from "./tree/webstudio-component";
import {
generateJsxChildren,
@@ -31,6 +36,12 @@ const createPropPair = (prop: Prop): [Prop["id"], Prop] => {
return [prop.id, prop];
};
+const createDataSourcePair = (
+ dataSource: DataSource
+): [DataSource["id"], DataSource] => {
+ return [dataSource.id, dataSource];
+};
+
test("generate jsx element with children and without them", () => {
expect(
generateJsxElement({
@@ -39,6 +50,7 @@ test("generate jsx element with children and without them", () => {
{ type: "id", value: "childId" },
]),
props: new Map(),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "Children\n",
})
@@ -56,6 +68,7 @@ test("generate jsx element with children and without them", () => {
scope: createScope(),
instance: createInstance("image", "Image", []),
props: new Map(),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "Children\n",
})
@@ -76,6 +89,7 @@ test("generate jsx element with namespaces components", () => {
{ type: "id", value: "childId" },
]),
props: new Map(),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "Children\n",
})
@@ -93,6 +107,7 @@ test("generate jsx element with namespaces components", () => {
scope: createScope(),
instance: createInstance("image", "@webstudio-is/library:Image", []),
props: new Map(),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "Children\n",
})
@@ -143,6 +158,7 @@ test("generate jsx element with literal props", () => {
{ type: "id", value: "image" },
]),
props,
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "Children\n",
})
@@ -162,6 +178,7 @@ test("generate jsx element with literal props", () => {
scope: createScope(),
instance: createInstance("image", "Image", []),
props,
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "",
})
@@ -197,6 +214,7 @@ test("ignore asset and page props", () => {
value: "assetId",
}),
]),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "",
})
@@ -247,6 +265,20 @@ test("generate jsx element with data sources and action", () => {
],
}),
]),
+ dataSources: new Map([
+ createDataSourcePair({
+ type: "variable",
+ id: "variableId",
+ name: "variableName",
+ value: { type: "number", value: 0 },
+ }),
+ createDataSourcePair({
+ type: "expression",
+ id: "expressionId",
+ name: "expressionName",
+ code: `$ws$dataSource$variableId + 1`,
+ }),
+ ]),
indexesWithinAncestors: new Map(),
children: "",
})
@@ -255,8 +287,8 @@ test("generate jsx element with data sources and action", () => {
<Box
data-ws-id="box"
data-ws-component="Box"
- variable={$ws$dataSource$variableId}
- expression={$ws$dataSource$expressionId}
+ variable={variableName}
+ expression={expressionName}
onClick={onClick}
onChange={onChange} />
`)
@@ -277,6 +309,7 @@ test("generate jsx element with condition based on show prop", () => {
value: true,
}),
]),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "",
})
@@ -300,6 +333,7 @@ test("generate jsx element with condition based on show prop", () => {
value: false,
}),
]),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
children: "",
})
@@ -314,7 +348,15 @@ test("generate jsx element with condition based on show prop", () => {
instanceId: "box",
type: "dataSource",
name: showAttribute,
- value: "condition",
+ value: "conditionId",
+ }),
+ ]),
+ dataSources: new Map([
+ createDataSourcePair({
+ type: "variable",
+ id: "conditionId",
+ name: "conditionName",
+ value: { type: "boolean", value: false },
}),
]),
indexesWithinAncestors: new Map(),
@@ -322,7 +364,7 @@ test("generate jsx element with condition based on show prop", () => {
})
).toEqual(
clear(`
- {$ws$dataSource$condition &&
+ {conditionName &&
<Box
data-ws-id="box"
data-ws-component="Box" />
@@ -337,6 +379,7 @@ test("generate jsx element with index prop", () => {
scope: createScope(),
instance: createInstance("box", "Box", []),
props: new Map(),
+ dataSources: new Map(),
indexesWithinAncestors: new Map([["box", 5]]),
children: "",
})
@@ -360,6 +403,7 @@ test("generate jsx children with text", () => {
],
instances: new Map(),
props: new Map(),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
})
).toEqual(
@@ -394,6 +438,7 @@ test("generate jsx children with nested instances", () => {
value: "value",
}),
]),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
})
).toEqual(
@@ -430,6 +475,7 @@ test("deduplicate base and namespaced components with same short name", () => {
),
]),
props: new Map(),
+ dataSources: new Map(),
indexesWithinAncestors: new Map(),
})
).toEqual(
@@ -444,7 +490,7 @@ test("deduplicate base and namespaced components with same short name", () => {
);
});
-test("generate page component", () => {
+test("generate page component with data sources", () => {
expect(
generatePageComponent({
scope: createScope(),
@@ -453,13 +499,21 @@ test("generate page component", () => {
createInstancePair("body", "Body", [{ type: "id", value: "input" }]),
createInstancePair("input", "Input", []),
]),
+ dataSources: new Map([
+ createDataSourcePair({
+ type: "variable",
+ id: "variableId",
+ name: "variableName",
+ value: { type: "string", value: "initial" },
+ }),
+ ]),
props: new Map([
createPropPair({
id: "1",
instanceId: "input",
name: "value",
type: "dataSource",
- value: "variable",
+ value: "variableId",
}),
createPropPair({
id: "2",
@@ -470,7 +524,7 @@ test("generate page component", () => {
{
type: "execute",
args: ["value"],
- code: `$ws$dataSource$variable = value`,
+ code: `$ws$dataSource$variableId = value`,
},
],
}),
@@ -480,17 +534,11 @@ test("generate page component", () => {
).toEqual(
clear(`
export const Page = (props: { scripts: ReactNode }) => {
- const { dataSourceValuesStore, setDataSourceValues, executeEffectfulExpression } = useContext(ReactSdkContext);
- const dataSourceValues = useStore(dataSourceValuesStore);
- const $ws$dataSource$variable = dataSourceValues.get("variable");
- const onChange = (value: unknown) => {
- const newValues = executeEffectfulExpression(
- value.code,
- new Map([["value", value]]),
- dataSourceValues
- );
- setDataSourceValues(newValues);
- };
+ let [variableName, set$variableName] = useState("initial")
+ let onChange = (value) => {
+ variableName = value
+ set$variableName(variableName)
+ }
return <Body
data-ws-id="body"
data-ws-component="Body">
@@ -498,81 +546,11 @@ test("generate page component", () => {
data-ws-id="input"
data-ws-component="Input"
data-ws-index="0"
- value={$ws$dataSource$variable}
- onChange={onChange} />
- {props.scripts}
- </Body>
- };
- `)
- );
-});
-
-test("deduplicate action names", () => {
- expect(
- generatePageComponent({
- scope: createScope(),
- rootInstanceId: "body",
- instances: new Map([
- createInstancePair("body", "Body", [
- { type: "id", value: "button1" },
- { type: "id", value: "button2" },
- ]),
- createInstancePair("button1", "Button", []),
- createInstancePair("button2", "Button", []),
- ]),
- props: new Map([
- createPropPair({
- id: "1",
- instanceId: "button1",
- name: "onChange",
- type: "action",
- value: [{ type: "execute", args: [], code: "" }],
- }),
- createPropPair({
- id: "2",
- instanceId: "button2",
- name: "onChange",
- type: "action",
- value: [{ type: "execute", args: [], code: "" }],
- }),
- ]),
- indexesWithinAncestors: new Map([["input", 0]]),
- })
- ).toEqual(
- clear(`
- export const Page = (props: { scripts: ReactNode }) => {
- const { dataSourceValuesStore, setDataSourceValues, executeEffectfulExpression } = useContext(ReactSdkContext);
- const dataSourceValues = useStore(dataSourceValuesStore);
- const onChange = () => {
- const newValues = executeEffectfulExpression(
- value.code,
- new Map([]),
- dataSourceValues
- );
- setDataSourceValues(newValues);
- };
- const onChange_1 = () => {
- const newValues = executeEffectfulExpression(
- value.code,
- new Map([]),
- dataSourceValues
- );
- setDataSourceValues(newValues);
- };
- return <Body
- data-ws-id="body"
- data-ws-component="Body">
- <Button
- data-ws-id="button1"
- data-ws-component="Button"
+ value={variableName}
onChange={onChange} />
- <Button
- data-ws-id="button2"
- data-ws-component="Button"
- onChange={onChange_1} />
{props.scripts}
</Body>
- };
+ }
`)
);
});
diff --git a/packages/react-sdk/src/component-generator.ts b/packages/react-sdk/src/component-generator.ts
index 179284c4ff..414bbdb29e 100644
--- a/packages/react-sdk/src/component-generator.ts
+++ b/packages/react-sdk/src/component-generator.ts
@@ -1,24 +1,32 @@
-import type { Instances, Instance, Props, Scope } from "@webstudio-is/sdk";
-import { findTreeInstanceIds, parseComponentName } from "@webstudio-is/sdk";
+import type {
+ Instances,
+ Instance,
+ Props,
+ Scope,
+ DataSources,
+} from "@webstudio-is/sdk";
+import { parseComponentName } from "@webstudio-is/sdk";
import {
componentAttribute,
idAttribute,
indexAttribute,
showAttribute,
} from "./tree/webstudio-component";
-import { encodeDataSourceVariable } from "./expression";
+import { generateDataSources } from "./expression";
import type { IndexesWithinAncestors } from "./instance-utils";
export const generateJsxElement = ({
scope,
instance,
props,
+ dataSources,
indexesWithinAncestors,
children,
}: {
scope: Scope;
instance: Instance;
props: Props;
+ dataSources: DataSources;
indexesWithinAncestors: IndexesWithinAncestors;
children: string;
}) => {
@@ -48,7 +56,11 @@ export const generateJsxElement = ({
}
if (prop.type === "dataSource") {
const dataSourceId = prop.value;
- conditionVariableName = encodeDataSourceVariable(dataSourceId);
+ const dataSource = dataSources.get(dataSourceId);
+ if (dataSource === undefined) {
+ continue;
+ }
+ conditionVariableName = scope.getName(dataSource.id, dataSource.name);
}
// ignore any other values
continue;
@@ -68,7 +80,11 @@ export const generateJsxElement = ({
}
if (prop.type === "dataSource") {
const dataSourceId = prop.value;
- const dataSourceVariable = encodeDataSourceVariable(dataSourceId);
+ const dataSource = dataSources.get(dataSourceId);
+ if (dataSource === undefined) {
+ continue;
+ }
+ const dataSourceVariable = scope.getName(dataSource.id, dataSource.name);
generatedProps += `\n${prop.name}={${dataSourceVariable}}`;
continue;
}
@@ -113,12 +129,14 @@ export const generateJsxChildren = ({
children,
instances,
props,
+ dataSources,
indexesWithinAncestors,
}: {
scope: Scope;
children: Instance["children"];
instances: Instances;
props: Props;
+ dataSources: DataSources;
indexesWithinAncestors: IndexesWithinAncestors;
}) => {
let generatedChildren = "";
@@ -142,12 +160,14 @@ export const generateJsxChildren = ({
scope,
instance,
props,
+ dataSources,
indexesWithinAncestors,
children: generateJsxChildren({
scope,
children: instance.children,
instances,
props,
+ dataSources,
indexesWithinAncestors,
}),
});
@@ -158,88 +178,42 @@ export const generateJsxChildren = ({
return generatedChildren;
};
-const generateDataSources = ({
- scope,
- rootInstanceId,
- instances,
- props,
-}: {
- scope: Scope;
- rootInstanceId: Instance["id"];
- instances: Instances;
- props: Props;
-}) => {
- let generatedDataSources = "";
- generatedDataSources += `const { dataSourceValuesStore, setDataSourceValues, executeEffectfulExpression } = useContext(ReactSdkContext);\n`;
- generatedDataSources +=
- "const dataSourceValues = useStore(dataSourceValuesStore);\n";
- const usedInstanceIds = findTreeInstanceIds(instances, rootInstanceId);
- for (const prop of props.values()) {
- if (prop.type === "dataSource" && usedInstanceIds.has(prop.instanceId)) {
- const dataSourceId = prop.value;
- const variableName = encodeDataSourceVariable(dataSourceId);
- const key = JSON.stringify(dataSourceId);
- generatedDataSources += `const ${variableName} = dataSourceValues.get(${key});\n`;
- }
- if (prop.type === "action") {
- const propVariable = scope.getName(prop.id, prop.name);
- let args = "";
- for (const value of prop.value) {
- const newArgs = value.args.map((arg) => `${arg}: unknown`).join(", ");
- // skip execution when arguments do not match
- if (args !== "" && newArgs !== args) {
- continue;
- }
- args = newArgs;
- }
- generatedDataSources += `const ${propVariable} = (${args}) => {\n`;
- for (const value of prop.value) {
- if (value.type === "execute") {
- generatedDataSources += `const newValues = executeEffectfulExpression(\n`;
- generatedDataSources += `value.code,\n`;
- generatedDataSources += `new Map([`;
- generatedDataSources += value.args
- .map((arg) => `[${JSON.stringify(arg)}, ${arg}]`)
- .join(", ");
- generatedDataSources += `]),\n`;
- generatedDataSources += `dataSourceValues\n`;
- generatedDataSources += `);\n`;
- generatedDataSources += `setDataSourceValues(newValues);\n`;
- }
- }
- generatedDataSources += `};\n`;
- }
- }
- return generatedDataSources;
-};
-
export const generatePageComponent = ({
scope,
rootInstanceId,
instances,
props,
+ dataSources,
indexesWithinAncestors,
}: {
scope: Scope;
rootInstanceId: Instance["id"];
instances: Instances;
props: Props;
+ dataSources: DataSources;
indexesWithinAncestors: IndexesWithinAncestors;
}) => {
const instance = instances.get(rootInstanceId);
if (instance === undefined) {
return "";
}
- const generatedDataSources = generateDataSources({
+ const { variables, body: dataSourcesBody } = generateDataSources({
scope,
- rootInstanceId,
- instances,
+ dataSources,
props,
});
+ let generatedDataSources = "";
+ for (const { valueName, setterName, initialValue } of variables.values()) {
+ const initialValueString = JSON.stringify(initialValue);
+ generatedDataSources += `let [${valueName}, ${setterName}] = useState(${initialValueString})\n`;
+ }
+ generatedDataSources += dataSourcesBody;
+
const generatedJsx = generateJsxElement({
scope,
instance,
props,
+ dataSources,
indexesWithinAncestors,
children:
generateJsxChildren({
@@ -247,6 +221,7 @@ export const generatePageComponent = ({
children: instance.children,
instances,
props,
+ dataSources,
indexesWithinAncestors,
}) + "{props.scripts}\n",
});
@@ -255,6 +230,6 @@ export const generatePageComponent = ({
generatedComponent += `export const Page = (props: { scripts: ReactNode }) => {\n`;
generatedComponent += `${generatedDataSources}`;
generatedComponent += `return ${generatedJsx}`;
- generatedComponent += `};\n`;
+ generatedComponent += `}\n`;
return generatedComponent;
};