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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Generate human readable data sources and actions (#2312)
Bogdan Chadkin8f00d5af2e28
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsxindex dfcbb0b653..592fc37217 100644--- a/apps/builder/app/canvas/canvas.tsx+++ b/apps/builder/app/canvas/canvas.tsx@@ -2,14 +2,11 @@ import { useMemo, useEffect } from "react";import { useStore } from "@nanostores/react";import { computed } from "nanostores";import { Scripts, ScrollRestoration } from "@remix-run/react";-import type { DataSource, Instances, Page } from "@webstudio-is/sdk";+import type { Instances, Page } from "@webstudio-is/sdk";import {type Params,type Components,createElementsTree,- executeEffectfulExpression,- encodeVariablesMap,- decodeVariablesMap,getIndexesWithinAncestors,} from "@webstudio-is/react-sdk";import * as baseComponents from "@webstudio-is/sdk-components-react";@@ -43,11 +40,10 @@ import {useIsPreviewMode,selectedPageStore,registerComponentLibrary,- dataSourceValuesStore,- dataSourceVariablesStore,registeredComponentsStore,registeredComponentMetasStore,subscribeComponentHooks,+ dataSourcesLogicStore,} from "~/shared/nano-states";import { useDragAndDrop } from "./shared/use-drag-drop";import { useCopyPaste } from "~/shared/copy-paste";@@ -68,21 +64,6 @@ const propsByInstanceIdStore = computed((propsIndex) => propsIndex.propsByInstanceId);-const executeEffectfulExpressionWithDecodedVariables: typeof executeEffectfulExpression =- (code, args, values) => {- return decodeVariablesMap(- executeEffectfulExpression(code, args, encodeVariablesMap(values))- );- };--const onDataSourceUpdate = (newValues: Map<DataSource["id"], unknown>) => {- const dataSourceVariables = new Map(dataSourceVariablesStore.get());- for (const [dataSourceId, value] of newValues) {- dataSourceVariables.set(dataSourceId, value);- }- dataSourceVariablesStore.set(dataSourceVariables);-};-const useElementsTree = (components: Components,instances: Instances,@@ -137,10 +118,7 @@ const useElementsTree = (propsByInstanceIdStore,assetsStore,pagesStore: pagesMapStore,- dataSourceValuesStore,- executeEffectfulExpression:- executeEffectfulExpressionWithDecodedVariables,- onDataSourceUpdate,+ dataSourcesLogicStore,Component: isPreviewMode? WebstudioComponentPreview: WebstudioComponentCanvas,diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.tsindex a09400ed97..c9cea36af0 100644--- a/apps/builder/app/shared/nano-states/nano-states.ts+++ b/apps/builder/app/shared/nano-states/nano-states.ts@@ -4,27 +4,29 @@ import { useStore } from "@nanostores/react";import { nanoid } from "nanoid";import type { AuthPermit } from "@webstudio-is/trpc-interface/index.server";import type { ItemDropTarget, Placement } from "@webstudio-is/design-system";-import type {- Asset,- Assets,- Breakpoint,- DataSource,- DataSources,- Instance,- Prop,- Props,- StyleDecl,- StyleDeclKey,- Styles,- StyleSource,- StyleSources,- StyleSourceSelection,- StyleSourceSelections,+import {+ createScope,+ type Asset,+ type Assets,+ type Breakpoint,+ type DataSource,+ type DataSources,+ type Instance,+ type Prop,+ type Props,+ type StyleDecl,+ type StyleDeclKey,+ type Styles,+ type StyleSource,+ type StyleSources,+ type StyleSourceSelection,+ type StyleSourceSelections,} from "@webstudio-is/sdk";import {executeComputingExpressions,encodeDataSourceVariable,decodeDataSourceVariable,+ generateDataSources,} from "@webstudio-is/react-sdk";import type { Style } from "@webstudio-is/css-engine";import type { DragStartPayload } from "~/canvas/shared/use-drag-drop";@@ -137,6 +139,53 @@ export const useSetProps = (props: [Prop["id"], Prop][]) => {});};+// result of executing generated code+// includes variables, computed expressions and action callbacks+export const dataSourcesLogicStore = computed(+ [dataSourcesStore, dataSourceVariablesStore, propsStore],+ (dataSources, dataSourceVariables, props) => {+ const { variables, body, output } = generateDataSources({+ scope: createScope(["_getVariable", "_setVariable", "_output"]),+ dataSources,+ props,+ });+ let generatedCode = "";+ for (const [dataSourceId, variable] of variables) {+ const { valueName, setterName } = variable;+ const initialValue = JSON.stringify(variable.initialValue);+ generatedCode += `let ${valueName} = _getVariable("${dataSourceId}") ?? ${initialValue};\n`;+ generatedCode += `let ${setterName} = (value) => _setVariable("${dataSourceId}", value);\n`;+ }+ generatedCode += body;+ generatedCode += `let _output = new Map();\n`;+ for (const [dataSourceId, variableName] of output) {+ generatedCode += `_output.set('${dataSourceId}', ${variableName})\n`;+ }+ generatedCode += `return _output\n`;++ try {+ const executeFn = new Function(+ "_getVariable",+ "_setVariable",+ generatedCode+ );+ const getVariable = (id: string) => {+ return dataSourceVariables.get(id);+ };+ const setVariable = (id: string, value: unknown) => {+ const dataSourceVariables = new Map(dataSourceVariablesStore.get());+ dataSourceVariables.set(id, value);+ dataSourceVariablesStore.set(dataSourceVariables);+ };+ return executeFn(getVariable, setVariable);+ } catch (error) {+ // eslint-disable-next-line no-console+ console.error(error);+ }+ return new Map();+ }+);+export const stylesStore = atom<Styles>(new Map());export const useSetStyles = (styles: [StyleDeclKey, StyleDecl][]) => {diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsxindex af671d291d..ccc8d32e1f 100644--- a/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx@@ -1,7 +1,6 @@/* eslint-disable *//* This is a auto generated file for building the project */-import * as sdk from "@webstudio-is/react-sdk";import type { PageData } from "~/routes/_index";import type { Components } from "@webstudio-is/react-sdk";import type { Asset } from "@webstudio-is/sdk";@@ -119,52 +118,17 @@ export const projectId = "cddc1d44-af37-4cb6-a430-d300cf6f932d";const indexesWithinAncestors = new Map<string, number>([]);-const rawExecuteComputingExpressions = (- _variables: Map<string, unknown>-): Map<string, unknown> => {- return new Map([]);-};-const executeComputingExpressions = (variables: Map<string, unknown>) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteComputingExpressions(encodedvariables);- return sdk.decodeVariablesMap(encodedResult);-};--const generatedEffectfulExpressions = new Map<- string,- (args: Map<string, any>, variables: Map<string, any>) => Map<string, unknown>->([]);--const rawExecuteEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>-): Map<string, unknown> => {- if (generatedEffectfulExpressions.has(code)) {- return generatedEffectfulExpressions.get(code)!(args, variables);- }- console.error("Effectful expression not found", code);- throw new Error("Effectful expression not found");-};--const executeEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>+const getDataSourcesLogic = (+ _getVariable: (id: string) => unknown,+ _setVariable: (id: string, value: unknown) => void) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteEffectfulExpression(- code,- args,- encodedvariables- );- return sdk.decodeVariablesMap(encodedResult);+ let _output = new Map();+ return _output;};export const utils = {indexesWithinAncestors,- executeComputingExpressions,- executeEffectfulExpression,+ getDataSourcesLogic,};/* eslint-enable */diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsxindex 0edf9561a0..dc87d2fe8c 100644--- a/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx@@ -1,7 +1,6 @@/* eslint-disable *//* This is a auto generated file for building the project */-import * as sdk from "@webstudio-is/react-sdk";import type { PageData } from "~/routes/_index";import type { Components } from "@webstudio-is/react-sdk";import type { Asset } from "@webstudio-is/sdk";@@ -453,67 +452,26 @@ const indexesWithinAncestors = new Map<string, number>([["65djoTmSBGemZ2L5izQ5M", 2],]);-const rawExecuteComputingExpressions = (- _variables: Map<string, unknown>-): Map<string, unknown> => {- return new Map([]);-};-const executeComputingExpressions = (variables: Map<string, unknown>) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteComputingExpressions(encodedvariables);- return sdk.decodeVariablesMap(encodedResult);-};--const generatedEffectfulExpressions = new Map<- string,- (args: Map<string, any>, variables: Map<string, any>) => Map<string, unknown>->([- [- "$ws$dataSource$RR_FthRebEUcAKUJIXl0j = value",- (_args: Map<string, any>, _variables: Map<string, any>) => {- let value = _args.get("value");- let $ws$dataSource$RR_FthRebEUcAKUJIXl0j;- $ws$dataSource$RR_FthRebEUcAKUJIXl0j = value;- return new Map([- [- "$ws$dataSource$RR_FthRebEUcAKUJIXl0j",- $ws$dataSource$RR_FthRebEUcAKUJIXl0j,- ],- ]);- },- ],-]);--const rawExecuteEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>-): Map<string, unknown> => {- if (generatedEffectfulExpressions.has(code)) {- return generatedEffectfulExpressions.get(code)!(args, variables);- }- console.error("Effectful expression not found", code);- throw new Error("Effectful expression not found");-};--const executeEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>+const getDataSourcesLogic = (+ _getVariable: (id: string) => unknown,+ _setVariable: (id: string, value: unknown) => void) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteEffectfulExpression(- code,- args,- encodedvariables- );- return sdk.decodeVariablesMap(encodedResult);+ let accordionValue = _getVariable("RR_FthRebEUcAKUJIXl0j") ?? "0";+ let set$accordionValue = (value: unknown) =>+ _setVariable("RR_FthRebEUcAKUJIXl0j", value);+ let onValueChange = (value: any) => {+ accordionValue = value;+ set$accordionValue(accordionValue);+ };+ let _output = new Map();+ _output.set("RR_FthRebEUcAKUJIXl0j", accordionValue);+ _output.set("UZg-1PcrNODgUo94IvnGC", onValueChange);+ return _output;};export const utils = {indexesWithinAncestors,- executeComputingExpressions,- executeEffectfulExpression,+ getDataSourcesLogic,};/* eslint-enable */diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsxindex 3894de3da2..a3fc4e1f96 100644--- a/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx+++ b/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx@@ -1,7 +1,6 @@/* eslint-disable *//* This is a auto generated file for building the project */-import * as sdk from "@webstudio-is/react-sdk";import type { PageData } from "~/routes/_index";import type { Components } from "@webstudio-is/react-sdk";import type { Asset } from "@webstudio-is/sdk";@@ -255,52 +254,17 @@ export const projectId = "cddc1d44-af37-4cb6-a430-d300cf6f932d";const indexesWithinAncestors = new Map<string, number>([]);-const rawExecuteComputingExpressions = (- _variables: Map<string, unknown>-): Map<string, unknown> => {- return new Map([]);-};-const executeComputingExpressions = (variables: Map<string, unknown>) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteComputingExpressions(encodedvariables);- return sdk.decodeVariablesMap(encodedResult);-};--const generatedEffectfulExpressions = new Map<- string,- (args: Map<string, any>, variables: Map<string, any>) => Map<string, unknown>->([]);--const rawExecuteEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>-): Map<string, unknown> => {- if (generatedEffectfulExpressions.has(code)) {- return generatedEffectfulExpressions.get(code)!(args, variables);- }- console.error("Effectful expression not found", code);- throw new Error("Effectful expression not found");-};--const executeEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>+const getDataSourcesLogic = (+ _getVariable: (id: string) => unknown,+ _setVariable: (id: string, value: unknown) => void) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteEffectfulExpression(- code,- args,- encodedvariables- );- return sdk.decodeVariablesMap(encodedResult);+ let _output = new Map();+ return _output;};export const utils = {indexesWithinAncestors,- executeComputingExpressions,- executeEffectfulExpression,+ getDataSourcesLogic,};/* eslint-enable */diff --git a/packages/cli/__generated__/index.ts b/packages/cli/__generated__/index.tsindex 3a58139072..18f1fd45d1 100644--- a/packages/cli/__generated__/index.ts+++ b/packages/cli/__generated__/index.ts@@ -35,22 +35,11 @@ export const projectId = "project-id";const indexesWithinAncestors = new Map<string, number>([]);-const executeComputingExpressions = (- variables: Map<string, unknown>-): Map<string, unknown> => {- return new Map();-};--const executeEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>-): Map<string, unknown> => {+const getDataSourcesLogic = () => {return new Map();};export const utils = {indexesWithinAncestors,- executeComputingExpressions,- executeEffectfulExpression,+ getDataSourcesLogic,};diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.tsindex ab93488309..c5b640970d 100644--- a/packages/cli/src/prebuild.ts+++ b/packages/cli/src/prebuild.ts@@ -348,7 +348,6 @@ export const prebuild = async (options: {for (const [pathName, pageComponents] of Object.entries(componentsByPage)) {const scope = createScope([// manually maintained list of occupied identifiers- "sdk","PageData","Components","Asset",@@ -427,7 +426,6 @@ export const prebuild = async (options: {const pageExports = `/* eslint-disable *//* This is a auto generated file for building the project */ \n-import * as sdk from "@webstudio-is/react-sdk";import type { PageData } from "~/routes/_index";import type { Components } from "@webstudio-is/react-sdk";import type { Asset } from "@webstudio-is/sdk";diff --git a/packages/react-sdk/src/component-renderer.tsx b/packages/react-sdk/src/component-renderer.tsxindex 7863855fa5..2360809dae 100644--- a/packages/react-sdk/src/component-renderer.tsx+++ b/packages/react-sdk/src/component-renderer.tsx@@ -1,6 +1,6 @@import type { ExoticComponent } from "react";import type { Instance } from "@webstudio-is/sdk";-import { getStyleDeclKey } from "@webstudio-is/sdk";+import { createScope, getStyleDeclKey } from "@webstudio-is/sdk";import type { WsComponentMeta } from "./components/component-meta";import {WsEmbedTemplate,@@ -8,13 +8,7 @@ import {} from "./embed-template";import { generateCssText } from "./css";import { InstanceRoot, WebstudioComponent } from "./tree";-import {- decodeVariablesMap,- encodeDataSourceVariable,- encodeVariablesMap,- executeComputingExpressions,- executeEffectfulExpression,-} from "./expression";+import { generateDataSources } from "./expression";import { getIndexesWithinAncestors } from "./instance-utils";import type { ImageLoader } from "@webstudio-is/image";@@ -120,25 +114,43 @@ export const renderComponentTemplate = ({new Map(instances),["root"]),- executeComputingExpressions: (values) => {- const expressions = new Map<string, string>();- for (const dataSource of data.dataSources) {- const name = encodeDataSourceVariable(dataSource.id);- if (dataSource.type === "expression") {- expressions.set(name, dataSource.code);- }+ getDataSourcesLogic(getVariable, setVariable) {+ const { variables, body, output } = generateDataSources({+ scope: createScope(["_getVariable", "_setVariable", "_output"]),+ props: new Map(data.props.map((prop) => [prop.id, prop])),+ dataSources: new Map(+ data.dataSources.map((dataSource) => [+ dataSource.id,+ dataSource,+ ])+ ),+ });+ let generatedCode = "";+ for (const [dataSourceId, variable] of variables) {+ const { valueName, setterName } = variable;+ const initialValue = JSON.stringify(variable.initialValue);+ generatedCode += `let ${valueName} = _getVariable("${dataSourceId}") ?? ${initialValue};\n`;+ generatedCode += `let ${setterName} = (value) => _setVariable("${dataSourceId}", value);\n`;}- return decodeVariablesMap(- executeComputingExpressions(- expressions,- encodeVariablesMap(values)- )- );- },- executeEffectfulExpression: (code, args, values) => {- return decodeVariablesMap(- executeEffectfulExpression(code, args, encodeVariablesMap(values))- );+ generatedCode += body;+ generatedCode += `let _output = new Map();\n`;+ for (const [dataSourceId, variableName] of output) {+ generatedCode += `_output.set('${dataSourceId}', ${variableName})\n`;+ }+ generatedCode += `return _output\n`;++ try {+ const executeFn = new Function(+ "_getVariable",+ "_setVariable",+ generatedCode+ );+ return executeFn(getVariable, setVariable);+ } catch (error) {+ // eslint-disable-next-line no-console+ console.error(error);+ }+ return new Map();},}}Component={WebstudioComponent}diff --git a/packages/react-sdk/src/context.tsx b/packages/react-sdk/src/context.tsxindex 9bd5a6290b..2b0cf21c79 100644--- a/packages/react-sdk/src/context.tsx+++ b/packages/react-sdk/src/context.tsx@@ -1,6 +1,6 @@import { type ReadableAtom, atom } from "nanostores";import { createContext } from "react";-import type { DataSource, Assets } from "@webstudio-is/sdk";+import type { Assets } from "@webstudio-is/sdk";import type { Pages, PropsByInstanceId } from "./props";import type { IndexesWithinAncestors } from "./instance-utils";import type { ImageLoader } from "@webstudio-is/image";@@ -32,21 +32,13 @@ export type Params = {assetBaseUrl: string;};-export type DataSourceValues = Map<DataSource["id"], unknown>;-export const ReactSdkContext = createContext<Params & {imageLoader: ImageLoader;propsByInstanceIdStore: ReadableAtom<PropsByInstanceId>;assetsStore: ReadableAtom<Assets>;pagesStore: ReadableAtom<Pages>;- dataSourceValuesStore: ReadableAtom<DataSourceValues>;- executeEffectfulExpression: (- expression: string,- args: DataSourceValues,- values: DataSourceValues- ) => DataSourceValues;- setDataSourceValues: (newValues: DataSourceValues) => void;+ dataSourcesLogicStore: ReadableAtom<Map<string, unknown>>;indexesWithinAncestors: IndexesWithinAncestors;}>({@@ -56,12 +48,6 @@ export const ReactSdkContext = createContext<propsByInstanceIdStore: atom(new Map()),assetsStore: atom(new Map()),pagesStore: atom(new Map()),- dataSourceValuesStore: atom(new Map()),- executeEffectfulExpression: () => {- throw Error("React SDK executeEffectfulExpression is not implemented");- },- setDataSourceValues: () => {- throw Error("React SDK setDataSourceValues is not implemented");- },+ dataSourcesLogicStore: atom(new Map()),indexesWithinAncestors: new Map(),});diff --git a/packages/react-sdk/src/expression.test.ts b/packages/react-sdk/src/expression.test.tsindex 12525ee446..7fd9f128bf 100644--- a/packages/react-sdk/src/expression.test.ts+++ b/packages/react-sdk/src/expression.test.ts@@ -8,7 +8,9 @@ import {generateComputingExpressions,generateEffectfulExpression,validateExpression,+ generateDataSources,} from "./expression";+import { createScope } from "@webstudio-is/sdk";test("allow literals and array expressions", () => {expect(@@ -307,3 +309,173 @@ test("handle cyclic dependencies", () => {]));});++test("generate variables with actions", () => {+ const generated = generateDataSources({+ scope: createScope(),+ dataSources: new Map([+ [+ "dataSource1",+ {+ id: "dataSource1",+ scopeInstanceId: "instance1",+ type: "variable",+ name: "myVar",+ value: { type: "string", value: "initial" },+ },+ ],+ [+ "dataSource2",+ {+ id: "dataSource2",+ scopeInstanceId: "instance1",+ type: "variable",+ name: "myVar",+ value: { type: "string", value: "initial" },+ },+ ],+ ]),+ props: new Map([+ [+ "prop1",+ {+ id: "prop1",+ instanceId: "instance1",+ type: "action",+ name: "onChange",+ value: [+ {+ type: "execute",+ args: ["value"],+ code: `$ws$dataSource$dataSource1 = value`,+ },+ {+ type: "execute",+ args: ["value"],+ code: `$ws$dataSource$dataSource1 = 'success'`,+ },+ ],+ },+ ],+ [+ "prop2",+ {+ id: "prop2",+ instanceId: "instance1",+ type: "action",+ name: "onSelect",+ value: [+ {+ type: "execute",+ args: [],+ code: `$ws$dataSource$dataSource2 = 'error'`,+ },+ ],+ },+ ],+ ]),+ });+ expect(generated.body).toMatchInlineSnapshot(`+ "let onChange = (value) => {+ myVar = value+ myVar = 'success'+ set$myVar(myVar)+ }+ let onSelect = () => {+ myVar_1 = 'error'+ set$myVar_1(myVar_1)+ }+ "+ `);+ expect(generated.variables).toEqual(+ new Map([+ [+ "dataSource1",+ {+ initialValue: "initial",+ setterName: "set$myVar",+ valueName: "myVar",+ },+ ],+ [+ "dataSource2",+ {+ initialValue: "initial",+ setterName: "set$myVar_1",+ valueName: "myVar_1",+ },+ ],+ ])+ );+ expect(generated.output).toEqual(+ new Map([+ ["dataSource1", "myVar"],+ ["dataSource2", "myVar_1"],+ ["prop1", "onChange"],+ ["prop2", "onSelect"],+ ])+ );+});++test("generate variables with sorted expressions", () => {+ const generated = generateDataSources({+ scope: createScope(),+ dataSources: new Map([+ [+ "dataSource1",+ {+ id: "dataSource1",+ scopeInstanceId: "instance1",+ type: "variable",+ name: "myVar",+ value: { type: "string", value: "initial" },+ },+ ],+ [+ "dataSource2",+ {+ id: "dataSource2",+ scopeInstanceId: "instance1",+ type: "expression",+ name: "myExp",+ code: `$ws$dataSource$dataSource3 + "Name"`,+ },+ ],+ [+ "dataSource3",+ {+ id: "dataSource3",+ scopeInstanceId: "instance1",+ type: "expression",+ name: "myExp",+ code: `$ws$dataSource$dataSource1 + "Value"`,+ },+ ],+ ]),+ props: new Map(),+ });+ expect(generated.body).toMatchInlineSnapshot(`+"let myExp = (myVar + "Value");+let myExp_1 = (myExp + "Name");+"+`);+ expect(generated.variables).toEqual(+ new Map([+ [+ "dataSource1",+ {+ initialValue: "initial",+ setterName: "set$myVar",+ valueName: "myVar",+ },+ ],+ ])+ );+ expect(generated.output).toEqual(+ new Map([+ ["dataSource1", "myVar"],+ ["dataSource2", "myExp_1"],+ ["dataSource3", "myExp"],+ ])+ );+});diff --git a/packages/react-sdk/src/expression.ts b/packages/react-sdk/src/expression.tsindex 869c0a8524..092d6a75c8 100644--- a/packages/react-sdk/src/expression.ts+++ b/packages/react-sdk/src/expression.ts@@ -4,6 +4,7 @@ import type {UpdateExpression,AssignmentExpression,} from "@jsep-plugin/assignment";+import type { DataSources, Props, Scope } from "@webstudio-is/sdk";jsep.plugins.register(jsepAssignment);@@ -399,3 +400,161 @@ export const decodeVariablesMap = (values: Values) => {}return decodedValues;};++/*++// header++let formState_1 = _getVariable('#formState_1')+let set$formState = value => _setVariable('#formStateId', value)++// body++let formInitial_1 = formState_1 === 'initial'++let onStateChange_1 = (state) => {+ formState_1 = state;++ set$formState(formState_1)+}++// footer++let _output = new Map();+_output.set('#', formState_1)+_output.set('#', formInitial_1)+_output.set('#', onStateChange_1)+return _output++*/++type DataSourceId = string;+type DataSourceOrPropId = string;+type VariableName = string;++export const generateDataSources = ({+ scope,+ typed = false,+ dataSources,+ props,+}: {+ scope: Scope;+ typed?: boolean;+ dataSources: DataSources;+ props: Props;+}) => {+ const variables = new Map<+ DataSourceId,+ { valueName: VariableName; setterName: VariableName; initialValue: unknown }+ >();+ let body = "";+ const output = new Map<DataSourceOrPropId, VariableName>();++ // collect each data source depndencies to sort topologically+ // and replace data sources with scoped names+ const depsById = new Map<DataSourceId, Set<DataSourceId>>();+ const codeById = new Map<DataSourceId, string>();+ for (const dataSource of dataSources.values()) {+ if (dataSource.type === "expression") {+ const deps = new Set<string>();+ const newCode = validateExpression(dataSource.code, {+ transformIdentifier: (identifier) => {+ const depId = decodeDataSourceVariable(identifier);+ const dep = depId ? dataSources.get(depId) : undefined;+ if (dep) {+ deps.add(dep.id);+ return scope.getName(dep.id, dep.name);+ }+ // eslint-disable-next-line no-console+ console.error(`Unknown dependency "${identifier}"`);+ return identifier;+ },+ });+ depsById.set(dataSource.id, deps);+ codeById.set(dataSource.id, newCode);+ }+ }++ // sort expressions starting with used ones as entry points+ const sortedDataSources = sortTopologically(+ new Set(dataSources.keys()),+ depsById+ );+ for (const dataSourceId of sortedDataSources) {+ const dataSource = dataSources.get(dataSourceId);+ if (dataSource?.type === "variable") {+ // save variables to generate header and footer depending on environment+ const valueName = scope.getName(dataSource.id, dataSource.name);+ const setterName = scope.getName(+ `set$${dataSource.id}`,+ `set$${dataSource.name}`+ );+ const initialValue = dataSource.value.value;+ output.set(dataSource.id, valueName);+ variables.set(dataSource.id, { valueName, setterName, initialValue });+ }+ if (dataSource?.type === "expression") {+ const name = scope.getName(dataSource.id, dataSource.name);+ const code = codeById.get(dataSourceId);+ output.set(dataSource.id, name);+ body += `let ${name} = (${code});\n`;+ }+ }++ // generate actions assigning variables and invoking their setters+ for (const prop of props.values()) {+ if (prop.type !== "action") {+ continue;+ }+ const name = scope.getName(prop.id, prop.name);+ output.set(prop.id, name);+ const setters = new Set<DataSourceId>();+ let args: undefined | string[] = undefined;+ let newCode = "";+ for (const value of prop.value) {+ args = value.args;+ newCode += validateExpression(value.code, {+ effectful: true,+ transformIdentifier: (identifier, assignee) => {+ if (args?.includes(identifier)) {+ return identifier;+ }+ const depId = decodeDataSourceVariable(identifier);+ const dep = depId ? dataSources.get(depId) : undefined;+ if (dep) {+ const name = scope.getName(dep.id, dep.name);+ if (assignee) {+ setters.add(dep.id);+ }+ return name;+ }+ // eslint-disable-next-line no-console+ console.error(`Unknown dependency "${identifier}"`);+ return identifier;+ },+ });+ newCode += `\n`;+ }+ if (args === undefined) {+ continue;+ }+ if (typed) {+ args = args.map((arg) => `${arg}: any`);+ }+ body += `let ${name} = (${args.join(", ")}) => {\n`;+ body += newCode;+ for (const dataSourceId of setters.values()) {+ const variable = variables.get(dataSourceId);+ if (variable) {+ body += `${variable.setterName}(${variable.valueName})\n`;+ }+ }+ body += `}\n`;+ }++ return {+ variables,+ body,+ output,+ };+};diff --git a/packages/react-sdk/src/generator.test.ts b/packages/react-sdk/src/generator.test.tsindex d50ce6a3df..d8a0a7a2fd 100644--- a/packages/react-sdk/src/generator.test.ts+++ b/packages/react-sdk/src/generator.test.ts@@ -60,28 +60,28 @@ test("generates utils", () => {]),props: new Map([[- "open",+ "_open",{type: "dataSource",- id: "open",+ id: "_open",instanceId: "tabs",name: "open",- value: "tabsOpen",+ value: "_tabsOpen",},],[- "onOpenChange",+ "_onOpenChange",{type: "action",- id: "onOpenChange",+ id: "_onOpenChange",instanceId: "tabs",name: "onOpenChange",value: [{type: "execute",args: ["open"],- code: `$ws$dataSource$tabsOpen = open`,+ code: `$ws$dataSource$_tabsOpen = open`,},],},@@ -89,9 +89,9 @@ test("generates utils", () => {]),dataSources: new Map([[- "tabsOpen",+ "_tabsOpen",{- id: "tabsOpen",+ id: "_tabsOpen",name: "tabsOpen",scopeInstanceId: "tabs",type: "variable",@@ -101,69 +101,38 @@ test("generates utils", () => {]),})).toMatchInlineSnapshot(`-"- /* eslint-disable */-- const indexesWithinAncestors = new Map<string, number>([- ["content1", 0],-["content2", 1],-- ]);-- const rawExecuteComputingExpressions = (- _variables: Map<string, unknown>- ): Map<string, unknown> => {- return new Map([-]);- };- const executeComputingExpressions = (variables: Map<string, unknown>) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteComputingExpressions(encodedvariables);- return sdk.decodeVariablesMap(encodedResult);- };-- const generatedEffectfulExpressions = new Map<- string,- (args: Map<string, any>, variables: Map<string, any>) => Map<string, unknown>- >([- ["$ws$dataSource$tabsOpen = open", (_args: Map<string, any>, _variables: Map<string, any>) => { let open = _args.get('open');-let $ws$dataSource$tabsOpen;-$ws$dataSource$tabsOpen = open;-return new Map([- ['$ws$dataSource$tabsOpen', $ws$dataSource$tabsOpen],-]); }],+ "+ /* eslint-disable */- ]);+ const indexesWithinAncestors = new Map<string, number>([+ ["content1", 0],+ ["content2", 1],- const rawExecuteEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>- ): Map<string, unknown> => {- if(generatedEffectfulExpressions.has(code)) {- return generatedEffectfulExpressions.get(code)!(args, variables);- }- console.error("Effectful expression not found", code);- throw new Error("Effectful expression not found");- };+ ]);- const executeEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>+ const getDataSourcesLogic = (+ _getVariable: (id: string) => unknown,+ _setVariable: (id: string, value: unknown) => void) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteEffectfulExpression(code, args, encodedvariables);- return sdk.decodeVariablesMap(encodedResult);- };+ let tabsOpen = _getVariable("_tabsOpen") ?? "0";+ let set$tabsOpen = (value: unknown) => _setVariable("_tabsOpen", value);+ let onOpenChange = (open: any) => {+ tabsOpen = open+ set$tabsOpen(tabsOpen)+ }+ let _output = new Map();+ _output.set('_tabsOpen', tabsOpen)+ _output.set('_onOpenChange', onOpenChange)+ return _output+ }- export const utils = {- indexesWithinAncestors,- executeComputingExpressions,- executeEffectfulExpression,- };- /* eslint-enable */- "-`);+ export const utils = {+ indexesWithinAncestors,+ getDataSourcesLogic,+ };++ /* eslint-enable */+ "+ `);});diff --git a/packages/react-sdk/src/generator.ts b/packages/react-sdk/src/generator.tsindex 9e8dcdfc00..65b594a8a5 100644--- a/packages/react-sdk/src/generator.ts+++ b/packages/react-sdk/src/generator.ts@@ -1,21 +1,17 @@-import type {- DataSources,- Instance,- Instances,- Page,- Props,+import {+ createScope,+ type DataSources,+ type Instance,+ type Instances,+ type Page,+ type Props,} from "@webstudio-is/sdk";import type { WsComponentMeta } from "./components/component-meta";import {getIndexesWithinAncestors,type IndexesWithinAncestors,} from "./instance-utils";-import {- encodeDataSourceVariable,- generateComputingExpressions,- generateEffectfulExpression,-} from "./expression";-import type { DataSourceValues } from "./context";+import { generateDataSources } from "./expression";type PageData = {page: Page;@@ -27,12 +23,10 @@ type PageData = {export type GeneratedUtils = {indexesWithinAncestors: IndexesWithinAncestors;- executeComputingExpressions: (values: DataSourceValues) => DataSourceValues;- executeEffectfulExpression: (- expression: string,- args: DataSourceValues,- values: DataSourceValues- ) => DataSourceValues;+ getDataSourcesLogic: (+ getVariable: (id: string) => unknown,+ setVariable: (id: string, value: unknown) => void+ ) => Map<string, unknown>;};/**@@ -58,88 +52,41 @@ export const generateUtilsExport = (siteData: PageData) => {]);`;- const variables = new Set<string>();- const expressions = new Map<string, string>();- for (const dataSource of siteData.dataSources.values()) {- if (dataSource.type === "variable") {- variables.add(encodeDataSourceVariable(dataSource.id));- }- if (dataSource.type === "expression") {- expressions.set(encodeDataSourceVariable(dataSource.id), dataSource.code);- }+ const { variables, body, output } = generateDataSources({+ scope: createScope(["_getVariable", "_setVariable", "_output"]),+ typed: true,+ dataSources: siteData.dataSources,+ props: siteData.props,+ });+ let generatedDataSources = "";+ generatedDataSources += `const getDataSourcesLogic = (\n`;+ generatedDataSources += ` _getVariable: (id: string) => unknown,\n`;+ generatedDataSources += ` _setVariable: (id: string, value: unknown) => void\n`;+ generatedDataSources += `) => {\n`;+ for (const [dataSourceId, variable] of variables) {+ const { valueName, setterName } = variable;+ const initialValue = JSON.stringify(variable.initialValue);+ generatedDataSources += `let ${valueName} = _getVariable("${dataSourceId}") ?? ${initialValue};\n`;+ generatedDataSources += `let ${setterName} = (value: unknown) => _setVariable("${dataSourceId}", value);\n`;}- const generatedExecuteComputingExpressions = `- const rawExecuteComputingExpressions = (- _variables: Map<string, unknown>- ): Map<string, unknown> => {- ${generateComputingExpressions(expressions, variables)}- };- const executeComputingExpressions = (variables: Map<string, unknown>) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteComputingExpressions(encodedvariables);- return sdk.decodeVariablesMap(encodedResult);- };- `;-- let effectfulExpressionsEntries = "";- for (const prop of siteData.props.values()) {- if (prop.type === "action") {- for (const executableValue of prop.value) {- const codeString = JSON.stringify(executableValue.code);- const generatedCode = generateEffectfulExpression(- executableValue.code,- new Set(executableValue.args),- variables- );- const generatedFunction = `(_args: Map<string, any>, _variables: Map<string, any>) => { ${generatedCode} }`;-- effectfulExpressionsEntries += `[${codeString}, ${generatedFunction}],\n`;- }- }+ generatedDataSources += body;+ generatedDataSources += `let _output = new Map();\n`;+ for (const [dataSourceId, variableName] of output) {+ generatedDataSources += `_output.set('${dataSourceId}', ${variableName})\n`;}- const generatedExecuteEffectfulExpression = `const generatedEffectfulExpressions = new Map<- string,- (args: Map<string, any>, variables: Map<string, any>) => Map<string, unknown>- >([- ${effectfulExpressionsEntries}- ]);-- const rawExecuteEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>- ): Map<string, unknown> => {- if(generatedEffectfulExpressions.has(code)) {- return generatedEffectfulExpressions.get(code)!(args, variables);- }- console.error("Effectful expression not found", code);- throw new Error("Effectful expression not found");- };-- const executeEffectfulExpression = (- code: string,- args: Map<string, unknown>,- variables: Map<string, unknown>- ) => {- const encodedvariables = sdk.encodeVariablesMap(variables);- const encodedResult = rawExecuteEffectfulExpression(code, args, encodedvariables);- return sdk.decodeVariablesMap(encodedResult);- };- `;+ generatedDataSources += `return _output\n`;+ generatedDataSources += `}\n`;return `/* eslint-disable */${generatedIndexesWithinAncestors.trim()}- ${generatedExecuteComputingExpressions.trim()}-- ${generatedExecuteEffectfulExpression.trim()}+ ${generatedDataSources}export const utils = {indexesWithinAncestors,- executeComputingExpressions,- executeEffectfulExpression,+ getDataSourcesLogic,};/* eslint-enable */diff --git a/packages/react-sdk/src/index.ts b/packages/react-sdk/src/index.tsindex 0be90d4982..1779470b9c 100644--- a/packages/react-sdk/src/index.ts+++ b/packages/react-sdk/src/index.ts@@ -32,6 +32,7 @@ export {encodeVariablesMap,decodeDataSourceVariable,decodeVariablesMap,+ generateDataSources,} from "./expression";export { renderComponentTemplate } from "./component-renderer";export { getIndexesWithinAncestors } from "./instance-utils";diff --git a/packages/react-sdk/src/props.ts b/packages/react-sdk/src/props.tsindex edf2940e86..f71547a6f4 100644--- a/packages/react-sdk/src/props.ts+++ b/packages/react-sdk/src/props.ts@@ -34,16 +34,14 @@ export const getPropsByInstanceId = (props: Props) => {export const useInstanceProps = (instanceId: Instance["id"]) => {const {propsByInstanceIdStore,- dataSourceValuesStore,- executeEffectfulExpression,- setDataSourceValues,+ dataSourcesLogicStore,indexesWithinAncestors,} = useContext(ReactSdkContext);const index = indexesWithinAncestors.get(instanceId);const instancePropsObjectStore = useMemo(() => {return computed(- [propsByInstanceIdStore, dataSourceValuesStore],- (propsByInstanceId, dataSourceValues) => {+ [propsByInstanceIdStore, dataSourcesLogicStore],+ (propsByInstanceId, dataSourcesLogic) => {const instancePropsObject: Record<Prop["name"], unknown> = {};if (index !== undefined) {instancePropsObject[indexAttribute] = index.toString();@@ -58,29 +56,17 @@ export const useInstanceProps = (instanceId: Instance["id"]) => {}if (prop.type === "dataSource") {const dataSourceId = prop.value;- const value = dataSourceValues.get(dataSourceId);+ const value = dataSourcesLogic.get(dataSourceId);if (value !== undefined) {instancePropsObject[prop.name] = value;}continue;}if (prop.type === "action") {- instancePropsObject[prop.name] = (...args: unknown[]) => {- for (const value of prop.value) {- if (value.type === "execute") {- const argsMap = new Map<string, unknown>();- for (const [i, name] of value.args.entries()) {- argsMap.set(name, args[i]);- }- const newValues = executeEffectfulExpression(- value.code,- argsMap,- dataSourceValues- );- setDataSourceValues(newValues);- }- }- };+ const action = dataSourcesLogic.get(prop.id);+ if (typeof action === "function") {+ instancePropsObject[prop.name] = action;+ }continue;}instancePropsObject[prop.name] = prop.value;@@ -88,14 +74,7 @@ export const useInstanceProps = (instanceId: Instance["id"]) => {return instancePropsObject;});- }, [- propsByInstanceIdStore,- dataSourceValuesStore,- instanceId,- executeEffectfulExpression,- setDataSourceValues,- index,- ]);+ }, [propsByInstanceIdStore, dataSourcesLogicStore, instanceId, index]);const instancePropsObject = useStore(instancePropsObjectStore);return instancePropsObject;};diff --git a/packages/react-sdk/src/tree/create-elements-tree.tsx b/packages/react-sdk/src/tree/create-elements-tree.tsxindex 8d5b0ca9b7..aefb09c0f7 100644--- a/packages/react-sdk/src/tree/create-elements-tree.tsx+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx@@ -7,11 +7,7 @@ import {import type { ReadableAtom } from "nanostores";import type { Instance, Instances, Assets } from "@webstudio-is/sdk";import type { Components } from "../components/components-utils";-import {- type Params,- type DataSourceValues,- ReactSdkContext,-} from "../context";+import { type Params, ReactSdkContext } from "../context";import type { Pages, PropsByInstanceId } from "../props";import type { WebstudioComponentProps } from "./webstudio-component";import type { IndexesWithinAncestors } from "../instance-utils";@@ -29,9 +25,7 @@ export const createElementsTree = ({propsByInstanceIdStore,assetsStore,pagesStore,- dataSourceValuesStore,- executeEffectfulExpression,- onDataSourceUpdate,+ dataSourcesLogicStore,indexesWithinAncestors,Component,components,@@ -43,13 +37,7 @@ export const createElementsTree = ({propsByInstanceIdStore: ReadableAtom<PropsByInstanceId>;assetsStore: ReadableAtom<Assets>;pagesStore: ReadableAtom<Pages>;- executeEffectfulExpression: (- expression: string,- args: DataSourceValues,- values: DataSourceValues- ) => DataSourceValues;- dataSourceValuesStore: ReadableAtom<DataSourceValues>;- onDataSourceUpdate: (newValues: DataSourceValues) => void;+ dataSourcesLogicStore: ReadableAtom<Map<string, unknown>>;indexesWithinAncestors: IndexesWithinAncestors;Component: ForwardRefExoticComponent<@@ -89,14 +77,12 @@ export const createElementsTree = ({propsByInstanceIdStore,assetsStore,pagesStore,- dataSourceValuesStore,+ dataSourcesLogicStore,renderer,imageLoader,assetBaseUrl,imageBaseUrl,indexesWithinAncestors,- executeEffectfulExpression,- setDataSourceValues: onDataSourceUpdate,}}>{root}diff --git a/packages/react-sdk/src/tree/root.ts b/packages/react-sdk/src/tree/root.tsindex e7706dfa3c..b248da091e 100644--- a/packages/react-sdk/src/tree/root.ts+++ b/packages/react-sdk/src/tree/root.ts@@ -1,6 +1,5 @@import {useRef,- useCallback,type ForwardRefExoticComponent,type RefAttributes,type ReactNode,@@ -25,10 +24,12 @@ import {} from "./webstudio-component";import { getPropsByInstanceId } from "../props";import type { Components } from "../components/components-utils";-import type { Params, DataSourceValues } from "../context";+import type { Params } from "../context";import type { GeneratedUtils } from "../generator";import type { ImageLoader } from "@webstudio-is/image";+type DataSourceValues = Map<DataSource["id"], unknown>;+export type RootPropsData = {params?: Params;page: Page;@@ -60,11 +61,7 @@ export const InstanceRoot = ({scripts,imageLoader,}: RootProps): JSX.Element | null => {- const {- indexesWithinAncestors,- executeComputingExpressions,- executeEffectfulExpression,- } = utils;+ const { indexesWithinAncestors, getDataSourcesLogic } = utils;const dataSourceVariablesStoreRef = useRef<undefined | WritableAtom<DataSourceValues>>(undefined);@@ -73,50 +70,33 @@ export const InstanceRoot = ({}const dataSourceVariablesStore = dataSourceVariablesStoreRef.current;- const dataSourceValuesStoreRef = useRef<+ const dataSourcesLogicStoreRef = useRef<undefined | ReadableAtom<DataSourceValues>>(undefined);- if (dataSourceValuesStoreRef.current === undefined) {- dataSourceValuesStoreRef.current = computed(+ if (dataSourcesLogicStoreRef.current === undefined) {+ dataSourcesLogicStoreRef.current = computed(dataSourceVariablesStore,(dataSourceVariables) => {- // set vriables with defaults- const dataSourceValues: DataSourceValues = new Map();- for (const [dataSourceId, dataSource] of data.build.dataSources) {- if (dataSource.type === "variable") {- const value =- dataSourceVariables.get(dataSourceId) ?? dataSource.value.value;- dataSourceValues.set(dataSourceId, value);- }- }-- // set expression valuestry {- const result = executeComputingExpressions(dataSourceValues);- for (const [id, value] of result) {- dataSourceValues.set(id, value);- }+ const getVariable = (id: string) => {+ return dataSourceVariables.get(id);+ };+ const setVariable = (id: string, value: unknown) => {+ const dataSourceVariables = new Map(dataSourceVariablesStore.get());+ dataSourceVariables.set(id, value);+ dataSourceVariablesStore.set(dataSourceVariables);+ };+ return getDataSourcesLogic(getVariable, setVariable);} catch (error) {// eslint-disable-next-line no-consoleconsole.error(error);}- return dataSourceValues;+ return new Map();});}- const dataSourceValuesStore = dataSourceValuesStoreRef.current;-- const onDataSourceUpdate = useCallback(- (newValues: DataSourceValues) => {- const dataSourceVariables = new Map(dataSourceVariablesStore.get());- for (const [dataSourceId, value] of newValues) {- dataSourceVariables.set(dataSourceId, value);- }- dataSourceVariablesStore.set(dataSourceVariables);- },- [dataSourceVariablesStore]- );+ const dataSourcesLogicStore = dataSourcesLogicStoreRef.current;return createElementsTree({imageLoader,@@ -130,9 +110,7 @@ export const InstanceRoot = ({assetsStore: atom(new Map(data.assets.map((asset) => [asset.id, asset]))),pagesStore: atom(new Map(data.pages.map((page) => [page.id, page]))),indexesWithinAncestors,- executeEffectfulExpression,- dataSourceValuesStore,- onDataSourceUpdate,+ dataSourcesLogicStore,Component: Component ?? WebstudioComponent,components,scripts,