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: Pasting from webflow 15 (#3733)

Oleg Isonen531dc6a4e817
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 ea62e6d7cf..c8b19eed5c 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
@@ -2596,7 +2596,6 @@ describe("Styles", () => {
name: "is-small",
comb: "&",
styleLess: "",
- createdBy: "6075409192d886a671499223",
},
{
_id: "194e7d07-469d-6ffa-3925-1f51bdad7e46",
@@ -2604,7 +2603,6 @@ describe("Styles", () => {
name: "is-secondary",
comb: "&",
styleLess: "background-color: transparent;",
- createdBy: "6075409192d886a671499223",
},
],
assets: [],
@@ -2614,12 +2612,12 @@ describe("Styles", () => {
expect(fragment.styleSources).toEqual([
{
type: "token",
- id: expect.any(String),
+ id: "uu1p3Xdvlq_AZOxnzDvAv",
name: "a",
},
{
type: "token",
- id: expect.any(String),
+ id: "uumXb7vHOnzTr-4SIW-wJ",
name: "button.is-small.is-secondary",
},
]);
@@ -2628,8 +2626,8 @@ describe("Styles", () => {
instanceId: expect.any(String),
values: [
"uu1p3Xdvlq_AZOxnzDvAv",
- "uumXb7vHOnzTr-4SIW-wJ",
"uuBw1PRC_uE8RhTmwxaH8",
+ "uumXb7vHOnzTr-4SIW-wJ",
"uuORexg4BOrRXBJZgB80_",
],
},
@@ -2913,9 +2911,6 @@ describe("Styles", () => {
"height: 400px; background-image: linear-gradient(180deg, hsla(0, 0.00%, 0.00%, 0.11), white), @img_667d0b7769e0cc3754b584f6, @img_667d0fe180995eadc1534a26, @img_example_bg; background-position: 0px 0px, 550px 0px, 0px 0px,0px 0px; background-size: auto, contain, auto, auto; background-repeat: repeat, no-repeat, repeat,repeat; background-attachment: scroll, fixed, scroll, fixed;",
variants: {},
children: [],
- createdBy: "5b7c48038bdf56493c54eae4",
- origin: null,
- selector: null,
},
],
assets: [
@@ -3071,9 +3066,6 @@ describe("Styles", () => {
styleLess: "color: @raw<|black|>; background-color: @raw<|red|>;",
variants: {},
children: [],
- createdBy: "635e72dd77408d16b581b4bc",
- origin: null,
- selector: null,
},
],
assets: [],
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/schema.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/schema.ts
index a87960cb8e..49fc5a57a2 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/schema.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/schema.ts
@@ -341,9 +341,6 @@ const WfStyle = z.object({
.record(z.string(), z.object({ styleLess: z.string() }))
.optional(),
children: z.array(z.string()).optional(),
- createdBy: z.string().optional(),
- origin: z.null().optional(),
- selector: z.null().optional(),
});
export type WfStyle = z.infer<typeof WfStyle>;
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 a0e4cd6dbb..7028f88e72 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
@@ -412,25 +412,41 @@ const mapComponentAndPresetStyles = (
};
// Merges wf styles that are combo classes into a single style.
-const mergeComboStyles = (styles: Array<WfStyle>) => {
- const mergedClasses = new Set<string>();
+// Checks if a style source with that name already exists and the new one has new styles and is not empty - if so, adds a number to a name.
+const mergeComboStyles = (wfStyles: Array<WfStyle>) => {
+ const classes = new Set<string>();
let mergedStyle;
- for (const style of styles) {
- mergedClasses.add(style.name);
+ for (const wfStyle of wfStyles) {
+ const { name } = wfStyle;
+
+ classes.add(name);
if (mergedStyle === undefined) {
- mergedStyle = { variants: {}, ...style };
+ mergedStyle = { variants: {}, ...wfStyle, name };
continue;
}
- mergedStyle.styleLess += style.styleLess;
- mergedStyle.name += "." + style.name;
- for (const key in style.variants) {
+ mergedStyle.styleLess += wfStyle.styleLess;
+ mergedStyle.name += "." + name;
+ for (const key in wfStyle.variants) {
if (key in mergedStyle.variants === false) {
mergedStyle.variants[key] = { styleLess: "" };
}
- mergedStyle.variants[key].styleLess += style.variants[key];
+ mergedStyle.variants[key].styleLess += wfStyle.variants[key];
}
}
- return { mergedStyle, mergedClasses: Array.from(mergedClasses) };
+
+ const classesArray = Array.from(classes);
+ // Produce all possible combinations 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.
+ // Example .a.b.c -> .a, .b, .c, .a.b, .a.c, .b.c, .a.b.c
+ const comboClasses = classesArray.flatMap((name1) =>
+ classesArray.map((name2) => `${name1}.${name2}`)
+ );
+
+ return {
+ mergedStyle,
+ classes: classesArray,
+ comboClasses,
+ };
};
export const addStyles = async ({
@@ -506,8 +522,8 @@ export const addStyles = async ({
.map((classId) => wfStyles.get(classId))
.filter(<T>(value: T): value is NonNullable<T> => value !== undefined);
- const comboStylesMerge = mergeComboStyles(wfNodeStyles);
- const wfStyle = comboStylesMerge.mergedStyle;
+ const stylesMerge = mergeComboStyles(wfNodeStyles);
+ const wfStyle = stylesMerge.mergedStyle;
if (wfStyle === undefined) {
continue;
}
@@ -531,6 +547,21 @@ export const addStyles = async ({
}
});
+ // 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.
+ const addExistingStyleSources = async (classes: Array<string>) => {
+ for (const className of classes) {
+ const styleSourceId = await generateStyleSourceId(className);
+ if ($styleSources.get().has(styleSourceId)) {
+ addStyleSource(styleSourceId, instanceId, fragment);
+ }
+ }
+ };
+
+ // First go individual classes: .a, .b, .c
+ await addExistingStyleSources(stylesMerge.classes);
+
+ // Second goes merged combo class .a.b.c
addNodeTokenStyles({
styleSourceId: await generateStyleSourceId(wfStyle.name),
name: wfStyle.name,
@@ -539,20 +570,10 @@ export const addStyles = async ({
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);
- }
- }
+ // Third goes .a.b, .b.c
+ // This worder is arbitrary and seems to be most likely to match webflow,
+ // but it can be wrong sometimes.
+ // Source order specificity in Webflow is a mess.
+ await addExistingStyleSources(stylesMerge.comboClasses);
}
};