Skip to content

Commit

Permalink
fix: duplicate local style sources (#4235)
Browse files Browse the repository at this point in the history
Fixes #4233

When added support for merging root styles when import from marketplace
forgot to check only :root styles should be merged and others
duplicated.
  • Loading branch information
TrySound authored Oct 6, 2024
1 parent 54a97c1 commit 02c073c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
37 changes: 36 additions & 1 deletion apps/builder/app/shared/instance-utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3175,7 +3175,7 @@ describe("insert webstudio fragment copy", () => {
});
});

describe("support copy paste of :root styles", () => {
describe("copy paste", () => {
const css = (strings: TemplateStringsArray, ...values: string[]) => {
let cssString = "";
strings.forEach((string, index) => {
Expand Down Expand Up @@ -3268,4 +3268,39 @@ describe("support copy paste of :root styles", () => {
}
`);
});

test("should copy local styles of duplicated instance", () => {
const project = getWebstudioDataStub({
...renderJsx(
<$.Body ws:id="bodyId">
<$.Box ws:id="boxId"></$.Box>
</$.Body>
),
...css`
boxId__boxlocalid {
color: red;
}
`,
});
const fragment = extractWebstudioFragment(project, "boxId");
insertWebstudioFragmentCopy({
data: project,
fragment,
availableDataSources: new Set(),
});
const [_bodyId, _boxId, newBoxId] = project.instances.keys();
const [_boxLocalId, newBoxLocalId] = project.styleSources.keys();
expect({
styleSourceSelections: project.styleSourceSelections,
styleSources: project.styleSources,
styles: project.styles,
}).toEqual(css`
boxId__boxlocalid {
color: red;
}
${newBoxId}__${newBoxLocalId} {
color: red;
}
`);
});
});
3 changes: 2 additions & 1 deletion apps/builder/app/shared/instance-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,8 @@ export const insertWebstudioFragmentCopy = ({
for (let styleSourceId of values) {
const newLocalStyleSource = newLocalStyleSources.get(styleSourceId);
if (newLocalStyleSource) {
if (existingLocalStyleSource) {
// merge only :root styles and duplicate others
if (instanceId === ROOT_INSTANCE_ID && existingLocalStyleSource) {
// write local styles into existing local style source
styleSourceId = existingLocalStyleSource.id;
} else {
Expand Down

0 comments on commit 02c073c

Please sign in to comment.