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: Preserve tokens when pasting Slot content (#5934)

Oleg Isonenf08cc01c9f38
diff --git a/packages/project-build/src/runtime/fragment.test.tsx b/packages/project-build/src/runtime/fragment.test.tsx
index bbf244b255..cfb12f3d16 100644
--- a/packages/project-build/src/runtime/fragment.test.tsx
+++ b/packages/project-build/src/runtime/fragment.test.tsx
@@ -33,6 +33,7 @@ import {
ActionValue,
renderTemplate,
Parameter,
+ token,
} from "@webstudio-is/template";
import { findAvailableVariables } from "./data";
@@ -338,6 +339,41 @@ test("insert instances with slots", () => {
]);
});
+test("remap token selections in inserted slot content", () => {
+ const data = renderData(<$.Body ws:id="bodyId"></$.Body>);
+ const fragment = renderTemplate(
+ <$.Slot ws:id="slotId">
+ <$.Fragment ws:id="fragmentId">
+ <$.HtmlEmbed
+ ws:id="iconId"
+ ws:tokens={[
+ token(
+ "Accordion Icon",
+ css`
+ color: red;
+ `
+ ),
+ ]}
+ />
+ </$.Fragment>
+ </$.Slot>
+ );
+
+ insertWebstudioFragmentCopy({
+ data,
+ fragment,
+ availableVariables: [],
+ projectId: "",
+ });
+
+ const [insertedTokenId] =
+ data.styleSourceSelections.get("iconId")?.values ?? [];
+ expect(data.styleSources.get(insertedTokenId)).toMatchObject({
+ type: "token",
+ name: "Accordion Icon",
+ });
+});
+
test("insert instances with multiple roots", () => {
const data = renderData(<$.Body ws:id="bodyId"></$.Body>);
const fragment = renderTemplate(
diff --git a/packages/project-build/src/runtime/fragment.ts b/packages/project-build/src/runtime/fragment.ts
index 2d4b022cf2..0a76c69428 100644
--- a/packages/project-build/src/runtime/fragment.ts
+++ b/packages/project-build/src/runtime/fragment.ts
@@ -702,6 +702,7 @@ export const insertWebstudioFragmentCopy = ({
styleSources,
styleSourceSelections,
styles,
+ styleSourceIdMap,
mergedBreakpointIds,
});
}
diff --git a/packages/project-build/src/runtime/style-copy.test.ts b/packages/project-build/src/runtime/style-copy.test.ts
index a6956409d8..e6dd29c5a4 100644
--- a/packages/project-build/src/runtime/style-copy.test.ts
+++ b/packages/project-build/src/runtime/style-copy.test.ts
@@ -191,13 +191,14 @@ describe("style source insertion", () => {
styleSources,
styleSourceSelections,
styles,
+ styleSourceIdMap: new Map([["token", "mapped-token"]]),
mergedBreakpointIds: new Map([["old-base", "base"]]),
});
expect(styleSources.get("local")).toEqual(local("local"));
expect(styleSourceSelections.get("portal")).toEqual({
instanceId: "portal",
- values: ["local", "token"],
+ values: ["local", "mapped-token"],
});
expect(Array.from(styles.values())).toEqual([
style("local", "base", "color", red),
diff --git a/packages/project-build/src/runtime/style-copy.ts b/packages/project-build/src/runtime/style-copy.ts
index ef2252494e..47c218fc98 100644
--- a/packages/project-build/src/runtime/style-copy.ts
+++ b/packages/project-build/src/runtime/style-copy.ts
@@ -392,6 +392,7 @@ export const insertPortalLocalStyleSources = ({
styleSources,
styleSourceSelections,
styles,
+ styleSourceIdMap,
mergedBreakpointIds,
}: {
fragmentStyleSources: StyleSource[];
@@ -401,6 +402,7 @@ export const insertPortalLocalStyleSources = ({
styleSources: StyleSources;
styleSourceSelections: StyleSourceSelections;
styles: Styles;
+ styleSourceIdMap: Map<StyleSource["id"], StyleSource["id"]>;
mergedBreakpointIds: Map<Breakpoint["id"], Breakpoint["id"]>;
}): void => {
const instanceStyleSourceIds = new Set<StyleSource["id"]>();
@@ -409,8 +411,11 @@ export const insertPortalLocalStyleSources = ({
if (instanceIds.has(instanceId) === false) {
continue;
}
- styleSourceSelections.set(instanceId, styleSourceSelection);
- for (const styleSourceId of styleSourceSelection.values) {
+ const values = styleSourceSelection.values.map(
+ (styleSourceId) => styleSourceIdMap.get(styleSourceId) ?? styleSourceId
+ );
+ styleSourceSelections.set(instanceId, { instanceId, values });
+ for (const styleSourceId of values) {
instanceStyleSourceIds.add(styleSourceId);
}
}