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: Deduplicate styles on data paste from Webflow (#3622)

Ivan Starkov5e94a3093141
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
index 9852857bdd..de87f522a8 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
@@ -12,7 +12,11 @@ import {
import { $, renderJsx } from "@webstudio-is/sdk/testing";
import * as defaultMetas from "@webstudio-is/sdk-components-react-remix/metas";
import { __testing__ } from "./plugin-webflow";
-import { $breakpoints, $registeredComponentMetas } from "../../nano-states";
+import {
+ $breakpoints,
+ $project,
+ $registeredComponentMetas,
+} from "../../nano-states";
const { toWebstudioFragment } = __testing__;
@@ -83,6 +87,16 @@ const toCss = (fragment: WebstudioFragment) => {
beforeEach(() => {
const defaultMetasMap = new Map(Object.entries(defaultMetas));
$registeredComponentMetas.set(defaultMetasMap);
+ $project.set({
+ id: "test",
+ createdAt: "",
+ domain: "",
+ title: "",
+ userId: "",
+ isDeleted: false,
+ previewImageAsset: null,
+ marketplaceApprovalStatus: "PENDING",
+ });
$breakpoints.set(
new Map(
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts
index 8371538861..0558d6c74e 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts
@@ -12,12 +12,14 @@ import {
$registeredComponentMetas,
$selectedInstanceSelector,
$selectedPage,
+ $project,
} from "../../nano-states";
import { WfData, WfNode, WfStyle, wfNodeTypes } from "./schema";
import { addInstanceAndProperties } from "./instances-properties";
import { addStyles } from "./styles";
import { builderApi } from "~/shared/builder-api";
import { denormalizeSrcProps } from "../asset-upload";
+import { nanoHash } from "~/shared/nano-hash";
const { toast } = builderApi;
@@ -51,7 +53,27 @@ const toWebstudioFragment = async (wfData: WfData) => {
for (const wfNode of wfNodes.values()) {
addInstanceAndProperties(wfNode, doneNodes, wfNodes, fragment);
}
- await addStyles(wfNodes, wfStyles, doneNodes, fragment);
+
+ /**
+ * Generates deterministic style IDs based on sourceId or unique data.
+ * This simplifies merging and deduplicating styles from different sources.
+ */
+ const generateStyleSourceId = async (sourceData: string) => {
+ // We are using projectId here to avoid id collisions between different projects.
+ const projectId = $project.get()?.id;
+ if (projectId === undefined) {
+ throw new Error("Project id is not set");
+ }
+ return nanoHash(`${projectId}-${sourceData}`);
+ };
+
+ await addStyles(
+ wfNodes,
+ wfStyles,
+ doneNodes,
+ fragment,
+ generateStyleSourceId
+ );
// First node should be always the root node in theory, if not
// we need to find a node that is not a child of any other node.
const rootWfNode = wfData.payload.nodes[0];
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
index 54d917e332..3ac42a62e8 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
@@ -139,12 +139,14 @@ const addBreakpoints = (
};
const addNodeStyles = ({
+ styleSourceId,
name,
variants,
instanceId,
fragment,
breakpointsByName,
}: {
+ styleSourceId: string;
name: string;
variants: UnparsedVariants;
instanceId: Instance["id"];
@@ -153,7 +155,6 @@ const addNodeStyles = ({
}) => {
const parsedVariants = toParsedVariants(variants);
- const styleSourceId = nanoid();
fragment.styleSources.push({
type: "token",
id: styleSourceId,
@@ -277,7 +278,8 @@ export const addStyles = async (
wfNodes: Map<WfNode["_id"], WfNode>,
wfStyles: Map<WfStyle["_id"], WfStyle>,
doneNodes: Map<WfNode["_id"], Instance["id"] | false>,
- fragment: WebstudioFragment
+ fragment: WebstudioFragment,
+ generateStyleSourceId: (sourceData: string) => Promise<string>
) => {
const { styles: stylePresets } = await import(
"./__generated__/style-presets"
@@ -298,8 +300,9 @@ export const addStyles = async (
const breakpointsByName = addBreakpoints($breakpoints.get(), fragment);
- mapComponentAndPresetStyles(wfNode, stylePresets).forEach((name) => {
+ for (const name of mapComponentAndPresetStyles(wfNode, stylePresets)) {
addNodeStyles({
+ styleSourceId: await generateStyleSourceId(name),
name,
variants: new Map([
["base", stylePresets[name] as Array<EmbedTemplateStyleDecl>],
@@ -308,7 +311,7 @@ export const addStyles = async (
fragment,
breakpointsByName,
});
- });
+ }
const instance = fragment.instances.find(
(instance) => instance.id === instanceId
@@ -336,7 +339,9 @@ export const addStyles = async (
variants.set(breakpointName, variant.styleLess);
}
});
+
addNodeStyles({
+ styleSourceId: await generateStyleSourceId(classId),
name: style.name,
variants,
instanceId,