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.

Fix props with assets loading (#869)

Bogdan Chadkin95e13f9bdf2a
diff --git a/packages/project/src/db/props.ts b/packages/project/src/db/props.ts
index 3cbfd49300..68c6d8cdf3 100644
--- a/packages/project/src/db/props.ts
+++ b/packages/project/src/db/props.ts
@@ -1,31 +1,23 @@
import warnOnce from "warn-once";
-import { type Tree, StoredProps } from "@webstudio-is/project-build";
+import { type Tree, StoredProps, Props } from "@webstudio-is/project-build";
import { type AllUserProps, type InstanceProps } from "@webstudio-is/react-sdk";
import { applyPatches, type Patch } from "immer";
import { prisma } from "@webstudio-is/prisma-client";
import { formatAsset } from "@webstudio-is/asset-uploader/server";
-export const loadByTreeId = async (treeId: Tree["id"]) => {
- const tree = await prisma.tree.findUnique({
- where: { id: treeId },
- });
-
- if (tree === null) {
- return [];
- }
+export const parseProps = async (propsString: string) => {
+ const storedProps = StoredProps.parse(JSON.parse(propsString));
- const props = StoredProps.parse(JSON.parse(tree.props));
-
- // Fin all assetId in all props
+ // find all asset ids in all props
const assetIds: string[] = [];
- for (const propsItem of props) {
- if (propsItem.type === "asset") {
- assetIds.push(propsItem.value);
+ for (const prop of storedProps) {
+ if (prop.type === "asset") {
+ assetIds.push(prop.value);
}
}
- // Load all assets
+ // load all assets
const assets = await prisma.asset.findMany({
where: {
id: {
@@ -38,31 +30,19 @@ export const loadByTreeId = async (treeId: Tree["id"]) => {
assets.map((asset) => [asset.id, formatAsset(asset)])
);
- const instancePropsMap = new Map<string, InstanceProps>();
-
- for (const propsItem of props) {
- let instanceProps = instancePropsMap.get(propsItem.instanceId);
- if (instanceProps === undefined) {
- instanceProps = {
- id: "",
- instanceId: propsItem.instanceId,
- treeId,
- props: [],
- };
- instancePropsMap.set(propsItem.instanceId, instanceProps);
- }
-
- if (propsItem.type === "asset") {
- const assetId = propsItem.value;
+ const props: Props = [];
+ for (const prop of storedProps) {
+ if (prop.type === "asset") {
+ const assetId = prop.value;
const asset = assetsMap.get(assetId);
if (asset) {
- instanceProps.props.push({
- id: propsItem.id,
- instanceId: propsItem.instanceId,
- name: propsItem.name,
- required: propsItem.required,
- type: propsItem.type,
+ props.push({
+ id: prop.id,
+ instanceId: prop.instanceId,
+ name: prop.name,
+ required: prop.required,
+ type: prop.type,
value: asset,
});
@@ -73,12 +53,60 @@ export const loadByTreeId = async (treeId: Tree["id"]) => {
continue;
}
+ props.push(prop);
+ }
+
+ return props;
+};
+
+export const loadByTreeId = async (treeId: Tree["id"]) => {
+ const tree = await prisma.tree.findUnique({
+ where: { id: treeId },
+ });
+
+ if (tree === null) {
+ return [];
+ }
+
+ const props = await parseProps(tree.props);
+
+ const instancePropsMap = new Map<string, InstanceProps>();
+ for (const propsItem of props) {
+ let instanceProps = instancePropsMap.get(propsItem.instanceId);
+ if (instanceProps === undefined) {
+ instanceProps = {
+ id: "",
+ instanceId: propsItem.instanceId,
+ treeId,
+ props: [],
+ };
+ instancePropsMap.set(propsItem.instanceId, instanceProps);
+ }
instanceProps.props.push(propsItem);
}
return Array.from(instancePropsMap.values());
};
+export const serializeProps = (props: Props) => {
+ const storedProps: StoredProps = [];
+ for (const prop of props) {
+ if (prop.type === "asset") {
+ storedProps.push({
+ id: prop.id,
+ instanceId: prop.instanceId,
+ name: prop.name,
+ required: prop.required,
+ type: prop.type,
+ value: prop.value.id,
+ });
+ continue;
+ }
+ storedProps.push(prop);
+ }
+ return JSON.stringify(storedProps);
+};
+
export const patch = async (
{ treeId }: { treeId: Tree["id"] },
patches: Array<Patch>
@@ -97,27 +125,11 @@ export const patch = async (
patches
);
- const props: StoredProps = [];
- for (const [instanceId, instanceProps] of Object.entries(nextProps)) {
- for (const propsItem of instanceProps) {
- if (propsItem.type === "asset") {
- props.push({
- id: propsItem.id,
- instanceId,
- name: propsItem.name,
- required: propsItem.required,
- type: propsItem.type,
- value: propsItem.value.id,
- });
- continue;
- }
- props.push(propsItem);
- }
- }
+ const props: Props = Object.values(nextProps).flat();
await prisma.tree.update({
data: {
- props: JSON.stringify(props),
+ props: serializeProps(props),
},
where: { id: treeId },
});
diff --git a/packages/project/src/db/tree.ts b/packages/project/src/db/tree.ts
index 85c8321bc1..637e506a76 100644
--- a/packages/project/src/db/tree.ts
+++ b/packages/project/src/db/tree.ts
@@ -4,9 +4,9 @@ import {
type Tree,
type InstancesItem,
Instance,
- Styles,
Instances,
- Props,
+ type Styles,
+ type Props,
} from "@webstudio-is/project-build";
import {
prisma,
@@ -15,6 +15,7 @@ import {
} from "@webstudio-is/prisma-client";
import { utils } from "../index";
import { parseStyles, serializeStyles } from "./styles";
+import { parseProps, serializeProps } from "./props";
type TreeData = Omit<Tree, "id">;
@@ -62,7 +63,7 @@ export const create = async (
data: {
root: "",
instances: JSON.stringify(instances),
- props: JSON.stringify(treeData.props),
+ props: serializeProps(treeData.props),
styles: serializeStyles(treeData.styles),
},
});
@@ -111,7 +112,7 @@ export const loadById = async (
const instances = Instances.parse(JSON.parse(tree.instances));
const root = Instance.parse(denormalizeTree(instances));
- const props = Props.parse(JSON.parse(tree.props));
+ const props = await parseProps(tree.props);
const styles = await parseStyles(tree.styles);
return {