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.

experimental: Combo classes for wf copy paste (#3713)

Oleg Isonen4c60e957ff4e
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 645b8f9a1f..812b692415 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
@@ -2525,6 +2525,14 @@ describe("Styles", () => {
name: "button",
},
],
+ [
+ "uuORexg4BOrRXBJZgB80_",
+ {
+ id: "uuORexg4BOrRXBJZgB80_",
+ type: "token",
+ name: "button.is-small",
+ },
+ ],
])
);
$styles.set(
@@ -2538,6 +2546,15 @@ describe("Styles", () => {
value: { type: "keyword", value: "green" },
},
],
+ [
+ "uuORexg4BOrRXBJZgB80_0",
+ {
+ styleSourceId: "uuORexg4BOrRXBJZgB80_",
+ breakpointId: "base",
+ property: "paddingTop",
+ value: { type: "unit", unit: "rem", value: 1 },
+ },
+ ],
])
);
@@ -2551,6 +2568,7 @@ describe("Styles", () => {
tag: "a",
classes: [
"194e7d07-469d-6ffa-3925-1f51bdad7e44",
+ "194e7d07-469d-6ffa-3925-1f51bdad7e47",
"194e7d07-469d-6ffa-3925-1f51bdad7e46",
],
children: [],
@@ -2567,7 +2585,18 @@ describe("Styles", () => {
type: "class",
name: "button",
styleLess: "text-align: center;",
- children: ["194e7d07-469d-6ffa-3925-1f51bdad7e46"],
+ children: [
+ "194e7d07-469d-6ffa-3925-1f51bdad7e47",
+ "194e7d07-469d-6ffa-3925-1f51bdad7e46",
+ ],
+ },
+ {
+ _id: "194e7d07-469d-6ffa-3925-1f51bdad7e47",
+ type: "class",
+ name: "is-small",
+ comb: "&",
+ styleLess: "",
+ createdBy: "6075409192d886a671499223",
},
{
_id: "194e7d07-469d-6ffa-3925-1f51bdad7e46",
@@ -2591,17 +2620,17 @@ describe("Styles", () => {
{
type: "token",
id: expect.any(String),
- name: "button.is-secondary",
+ name: "button.is-small.is-secondary",
},
]);
-
expect(fragment.styleSourceSelections).toEqual([
{
instanceId: expect.any(String),
values: [
"uu1p3Xdvlq_AZOxnzDvAv",
"uuBw1PRC_uE8RhTmwxaH8",
- "uuFc5tqBSacRR9VpjbFi1",
+ "uuORexg4BOrRXBJZgB80_",
+ "uumXb7vHOnzTr-4SIW-wJ",
],
},
]);
@@ -2617,7 +2646,7 @@ describe("Styles", () => {
a:hover {
outline: 0 none currentColor
}
- button.is-secondary {
+ button.is-small.is-secondary {
text-align: center;
background-color: transparent
}
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 b898e07823..fa6012f628 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
@@ -411,33 +411,26 @@ const mapComponentAndPresetStyles = (
return presetStyles;
};
+// Merges wf styles that are combo classes into a single style.
const mergeComboStyles = (styles: Array<WfStyle>) => {
- const mergedStyles: Array<WfStyle> = [];
const mergedClasses = new Set<string>();
+ let mergedStyle;
for (const style of styles) {
- if (style.comb) {
- // Child style can be skipped, as it will be already merged into parent
+ mergedClasses.add(style.name);
+ if (mergedStyle === undefined) {
+ mergedStyle = { variants: {}, ...style };
continue;
}
- const mergedStyle = { variants: {}, ...style };
- mergedStyles.push(mergedStyle);
- for (const childId of style.children ?? []) {
- const childStyle = styles.find((style) => style._id === childId);
- if (childStyle) {
- mergedClasses.add(style.name);
- mergedClasses.add(childStyle.name);
- mergedStyle.styleLess += childStyle.styleLess;
- for (const key in childStyle.variants) {
- if (key in mergedStyle.variants === false) {
- mergedStyle.variants[key] = { styleLess: "" };
- }
- mergedStyle.variants[key].styleLess += childStyle.variants[key];
- }
- mergedStyle.name += "." + childStyle.name;
+ mergedStyle.styleLess += style.styleLess;
+ mergedStyle.name += "." + style.name;
+ for (const key in style.variants) {
+ if (key in style.variants === false) {
+ mergedStyle.variants[key] = { styleLess: "" };
}
+ mergedStyle.variants[key].styleLess += style.variants[key];
}
}
- return { mergedStyles, mergedClasses };
+ return { mergedStyle, mergedClasses: Array.from(mergedClasses) };
};
export const addStyles = async ({
@@ -509,48 +502,57 @@ export const addStyles = async ({
continue;
}
- let wfNodeStyles = wfNode.classes
+ const wfNodeStyles = wfNode.classes
.map((classId) => wfStyles.get(classId))
.filter(<T>(value: T): value is NonNullable<T> => value !== undefined);
const comboStylesMerge = mergeComboStyles(wfNodeStyles);
- wfNodeStyles = comboStylesMerge.mergedStyles;
+ const wfStyle = comboStylesMerge.mergedStyle;
+ if (wfStyle === undefined) {
+ continue;
+ }
+ if (instance && instance.label === undefined) {
+ instance.label = wfStyle.name;
+ }
- for (const wfStyle of wfNodeStyles) {
- if (instance && instance.label === undefined) {
- instance.label = wfStyle.name;
+ const variants = new Map();
+ variants.set(
+ "base",
+ replaceAtImages(replaceAtVariables(wfStyle.styleLess), wfAssets)
+ );
+ const wfVariants = wfStyle.variants;
+ Object.keys(wfVariants).forEach((breakpointName) => {
+ const variant = wfVariants[breakpointName as keyof typeof wfVariants];
+ if (variant && "styleLess" in variant) {
+ variants.set(
+ breakpointName,
+ replaceAtImages(replaceAtVariables(variant.styleLess), wfAssets)
+ );
}
- const variants = new Map();
- variants.set(
- "base",
- replaceAtImages(replaceAtVariables(wfStyle.styleLess), wfAssets)
- );
- const wfVariants = wfStyle.variants ?? {};
- Object.keys(wfVariants).forEach((breakpointName) => {
- const variant = wfVariants[breakpointName as keyof typeof wfVariants];
- if (variant && "styleLess" in variant) {
- variants.set(
- breakpointName,
- replaceAtImages(replaceAtVariables(variant.styleLess), wfAssets)
- );
- }
- });
+ });
- // We need to see if the individual classes have already existed in the system
- // and if so, add them to the selection, because webflow relies on combo class logic and we merge the combo into one.
- for (const childClass of comboStylesMerge.mergedClasses) {
- const wsStyleSourceId = await generateStyleSourceId(childClass);
- if ($styleSources.get().has(wsStyleSourceId)) {
- addStyleSource(wsStyleSourceId, instanceId, fragment);
- }
+ // Produce all possible combinatios of combo classes so we can check later if they alredy exist.
+ // This is needed to achieve the same end-result as with combo-classes in webflow.
+ const allComboClasses = comboStylesMerge.mergedClasses.flatMap((name1) =>
+ comboStylesMerge.mergedClasses.map((name2) => `${name1}.${name2}`)
+ );
+ const allClasses = [...comboStylesMerge.mergedClasses, ...allComboClasses];
+
+ // We need to see if the individual classes have already existed in the system
+ // and if so, add them to the selection, because webflow relies on combo class logic and we merge the combo into one.
+ for (const className of allClasses) {
+ const wsStyleSourceId = await generateStyleSourceId(className);
+ if ($styleSources.get().has(wsStyleSourceId)) {
+ addStyleSource(wsStyleSourceId, instanceId, fragment);
}
- addNodeTokenStyles({
- styleSourceId: await generateStyleSourceId(wfStyle.name),
- name: wfStyle.name,
- variants,
- instanceId,
- fragment,
- });
}
+
+ addNodeTokenStyles({
+ styleSourceId: await generateStyleSourceId(wfStyle.name),
+ name: wfStyle.name,
+ variants,
+ instanceId,
+ fragment,
+ });
}
};