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: duplicate local style sources (#4235)

Bogdan Chadkin02c073c10bed
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsx
index 301f94ced6..cb46111732 100644
--- a/apps/builder/app/shared/instance-utils.test.tsx
+++ b/apps/builder/app/shared/instance-utils.test.tsx
@@ -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) => {
@@ -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;
+ }
+ `);
+ });
});
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 76d78c1e5e..f1061d3c27 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -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 {