Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Skip empty combo class when pasting from webflow #3755

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2597,7 +2597,7 @@ describe("Styles", () => {
type: "class",
name: "is-small",
comb: "&",
styleLess: "",
styleLess: "padding: 1rem;",
},
{
_id: "194e7d07-469d-6ffa-3925-1f51bdad7e46",
Expand Down Expand Up @@ -2648,7 +2648,92 @@ describe("Styles", () => {
}
button.is-small.is-secondary {
text-align: center;
background-color: transparent
background-color: transparent;
padding: 1rem
}
}"
`);
});

test("Skip empty combo class", async () => {
const fragment = await toWebstudioFragment({
type: "@webflow/XscpData",
payload: {
nodes: [
{
_id: "56ede23d-6dcc-a320-0af4-3a803d8efd88",
type: "Block",
tag: "div",
classes: ["08c3532a-4e88-1106-9fda-e4dcd66f93f0"],
children: [
"d06c01f8-da59-b31e-a910-55a6d33898f0",
"b67fb4c4-ce52-b441-1d89-55b165282c99",
],
},
{
_id: "d06c01f8-da59-b31e-a910-55a6d33898f0",
type: "Block",
tag: "div",
classes: ["6569b562-7de4-bd09-3d2d-59d7c11ec988"],
children: [],
},
{
_id: "b67fb4c4-ce52-b441-1d89-55b165282c99",
type: "Block",
tag: "div",
classes: [
"08c3532a-4e88-1106-9fda-e4dcd66f93f0",
"c4851546-799d-c6bf-fb17-0b5765f48d72",
],
children: [],
},
],
styles: [
{
_id: "08c3532a-4e88-1106-9fda-e4dcd66f93f0",
fake: false,
type: "class",
name: "d1",
namespace: "",
comb: "",
styleLess: "color: hsla(0, 70.45%, 48.11%, 1.00);",
variants: {},
children: ["c4851546-799d-c6bf-fb17-0b5765f48d72"],
},
{
_id: "6569b562-7de4-bd09-3d2d-59d7c11ec988",
fake: false,
type: "class",
name: "d2",
namespace: "",
comb: "",
styleLess: "font-size: 200px;",
variants: {},
children: [],
},
{
_id: "c4851546-799d-c6bf-fb17-0b5765f48d72",
fake: false,
type: "class",
name: "d2",
namespace: "",
comb: "&",
styleLess: "",
variants: {},
children: [],
},
],
assets: [],
},
});

expect(toCss(fragment)).toMatchInlineSnapshot(`
"@media all {
d1 {
color: rgba(209, 36, 36, 1)
}
d2 {
font-size: 200px
}
}"
`);
Expand Down
19 changes: 15 additions & 4 deletions apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,17 +441,28 @@ const mapComponentAndPresetStyles = (
// 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>();
const skip = new Set<string>();
let mergedStyle;
for (const wfStyle of wfStyles) {
const { name } = wfStyle;

classes.add(name);

if (mergedStyle === undefined) {
mergedStyle = { variants: {}, ...wfStyle, name };
continue;
}

const comboClass = mergedStyle.name + "." + name;

// We need to avoid creating combo classes when they have no additional styles.
if (wfStyle.comb === "&" && wfStyle.styleLess === "") {
skip.add(comboClass);
continue;
}

mergedStyle.styleLess += wfStyle.styleLess;
mergedStyle.name += "." + name;
mergedStyle.name = comboClass;
for (const key in wfStyle.variants) {
if (key in mergedStyle.variants === false) {
mergedStyle.variants[key] = { styleLess: "" };
Expand All @@ -464,9 +475,9 @@ const mergeComboStyles = (wfStyles: Array<WfStyle>) => {
// 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}`)
);
const comboClasses = classesArray
.flatMap((name1) => classesArray.map((name2) => `${name1}.${name2}`))
.filter((name) => skip.has(name) === false);

return {
mergedStyle,
Expand Down
Loading