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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
experimental: Paste from webflow 2 (#3455)
Oleg Isonen4d156d894b50
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow.test.ts b/apps/builder/app/shared/copy-paste/plugin-webflow.test.tsindex 75ed138814..25dd30c3ad 100644--- a/apps/builder/app/shared/copy-paste/plugin-webflow.test.ts+++ b/apps/builder/app/shared/copy-paste/plugin-webflow.test.ts@@ -2,12 +2,12 @@ import { test, expect } from "@jest/globals";import { __testing__ } from "./plugin-webflow";import { $breakpoints } from "../nano-states";-const { toInstanceData } = __testing__;+const { toWebstudioFragment } = __testing__;$breakpoints.set(new Map([["0", { id: "0", label: "base" }]]));-test("Heading Node", () => {- const expected = toInstanceData({+test("Heading", () => {+ const fragment = toWebstudioFragment({type: "@webflow/XscpData",payload: {nodes: [@@ -27,13 +27,13 @@ test("Heading Node", () => {styles: [],},});- expect(expected.children).toEqual([+ expect(fragment.children).toEqual([{type: "id",value: expect.not.stringMatching("instanceId"),},]);- expect(expected.instances).toEqual([+ expect(fragment.instances).toEqual([{children: [{@@ -46,7 +46,7 @@ test("Heading Node", () => {type: "instance",},]);- expect(expected.props).toEqual([+ expect(fragment.props).toEqual([{id: expect.not.stringMatching("id"),instanceId: expect.not.stringMatching("instanceId"),@@ -57,8 +57,71 @@ test("Heading Node", () => {]);});+test("Link", () => {+ const fragment = toWebstudioFragment({+ type: "@webflow/XscpData",+ payload: {+ nodes: [+ {+ _id: "97539676-c2ca-2e8f-55f3-6c4a3104a5c0",+ type: "Link",+ tag: "a",+ classes: [],+ children: [],+ data: {+ link: {+ url: "https://webstudio.is",+ target: "_blank",+ },+ },+ },+ ],+ styles: [],+ },+ });+ expect(fragment.children).toEqual([+ {+ type: "id",+ value: expect.not.stringMatching("instanceId"),+ },+ ]);++ expect(fragment.instances).toEqual([+ {+ id: expect.not.stringMatching("instanceId"),+ type: "instance",+ component: "Link",+ children: [],+ },+ ]);++ expect(fragment.props).toEqual([+ {+ type: "string",+ id: expect.not.stringMatching("id"),+ instanceId: expect.not.stringMatching("instanceId"),+ name: "tag",+ value: "a",+ },+ {+ type: "string",+ id: expect.not.stringMatching("id"),+ instanceId: expect.not.stringMatching("instanceId"),+ name: "href",+ value: "https://webstudio.is",+ },+ {+ type: "string",+ id: expect.not.stringMatching("id"),+ instanceId: expect.not.stringMatching("instanceId"),+ name: "target",+ value: "_blank",+ },+ ]);+});+test("List and ListItem", () => {- const expected = toInstanceData({+ const fragment = toWebstudioFragment({type: "@webflow/XscpData",payload: {nodes: [@@ -98,14 +161,14 @@ test("List and ListItem", () => {styles: [],},});- expect(expected.children).toEqual([+ expect(fragment.children).toEqual([{type: "id",value: expect.not.stringMatching("instanceId"),},]);- expect(expected.instances).toEqual([+ expect(fragment.instances).toEqual([{id: expect.not.stringMatching("instanceId"),type: "instance",@@ -137,8 +200,149 @@ test("List and ListItem", () => {]);});-test("Heading with styles", () => {- const expected = toInstanceData({+test("Paragraph", () => {+ const fragment = toWebstudioFragment({+ type: "@webflow/XscpData",+ payload: {+ nodes: [+ {+ _id: "dfab64ae-6624-b6db-a909-b85588aa3f8d",+ type: "Paragraph",+ tag: "p",+ classes: [],+ children: ["dfab64ae-6624-b6db-a909-b85588aa3f8e"],+ },+ {+ _id: "dfab64ae-6624-b6db-a909-b85588aa3f8e",+ text: true,+ v: "Text in a paragraph",+ },+ ],+ styles: [],+ },+ });++ expect(fragment.instances).toEqual([+ {+ id: expect.not.stringMatching("instanceId"),+ type: "instance",+ component: "Paragraph",+ children: [+ {+ type: "text",+ value: "Text in a paragraph",+ },+ ],+ },+ ]);+ expect(fragment.props).toEqual([+ {+ id: expect.not.stringMatching("id"),+ instanceId: expect.not.stringMatching("instanceId"),+ name: "tag",+ type: "string",+ value: "p",+ },+ ]);+});++test("Text", () => {+ const fragment = toWebstudioFragment({+ type: "@webflow/XscpData",+ payload: {+ nodes: [+ {+ _id: "adea2109-96eb-63e0-c27f-632a7f40bce8",+ type: "Block",+ tag: "div",+ classes: [],+ children: ["adea2109-96eb-63e0-c27f-632a7f40bce9"],+ data: {+ text: true,+ },+ },+ {+ _id: "adea2109-96eb-63e0-c27f-632a7f40bce9",+ text: true,+ v: "This is some text inside of a div block.",+ },+ ],+ styles: [],+ },+ });++ expect(fragment.instances).toEqual([+ {+ id: expect.not.stringMatching("instanceId"),+ type: "instance",+ component: "Text",+ children: [+ {+ type: "text",+ value: "This is some text inside of a div block.",+ },+ ],+ },+ ]);+ expect(fragment.props).toEqual([+ {+ id: expect.not.stringMatching("id"),+ instanceId: expect.not.stringMatching("instanceId"),+ name: "tag",+ type: "string",+ value: "div",+ },+ ]);+});++test("Blockquote", () => {+ const fragment = toWebstudioFragment({+ type: "@webflow/XscpData",+ payload: {+ nodes: [+ {+ _id: "25ffefdf-c015-5edd-7673-933b41a25328",+ type: "Blockquote",+ tag: "blockquote",+ classes: [],+ children: ["25ffefdf-c015-5edd-7673-933b41a25329"],+ },+ {+ _id: "25ffefdf-c015-5edd-7673-933b41a25329",+ text: true,+ v: "Block Quote",+ },+ ],+ styles: [],+ },+ });++ expect(fragment.instances).toEqual([+ {+ id: expect.not.stringMatching("instanceId"),+ type: "instance",+ component: "Blockquote",+ children: [+ {+ type: "text",+ value: "Block Quote",+ },+ ],+ },+ ]);+ expect(fragment.props).toEqual([+ {+ id: expect.not.stringMatching("id"),+ instanceId: expect.not.stringMatching("instanceId"),+ name: "tag",+ type: "string",+ value: "blockquote",+ },+ ]);+});++test("Basic styles with a class", () => {+ const fragment = toWebstudioFragment({type: "@webflow/XscpData",payload: {nodes: [@@ -168,20 +372,20 @@ test("Heading with styles", () => {],},});- expect(expected.styleSources).toEqual([+ expect(fragment.styleSources).toEqual([{type: "token",id: expect.not.stringMatching("styleSourceId"),name: "Heading",},]);- expect(expected.styleSourceSelections).toEqual([+ expect(fragment.styleSourceSelections).toEqual([{instanceId: expect.not.stringMatching("instanceId"),values: [expect.not.stringMatching("styleSourceId")],},]);- expect(expected.styles).toEqual([+ expect(fragment.styles).toEqual([{styleSourceId: expect.not.stringMatching("styleSourceId"),breakpointId: "0",diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow.ts b/apps/builder/app/shared/copy-paste/plugin-webflow.tsindex 924502a8fa..01bf8580ca 100644--- a/apps/builder/app/shared/copy-paste/plugin-webflow.ts+++ b/apps/builder/app/shared/copy-paste/plugin-webflow.ts@@ -19,30 +19,50 @@ import { isFeatureEnabled } from "@webstudio-is/feature-flags";export const mimeType = "application/json";-const wfToWsComponentMap = {- Block: "Box",+// A list of Webflow component names that need to be mapped.+const componentMappers = {+ Block(wfNode: WfNode) {+ if ("type" in wfNode === false || wfNode.type !== "Block") {+ return;+ }+ return wfNode.data.text ? "Text" : "Box";+ },};-type WfComponent = keyof typeof wfToWsComponentMap;+const WfBaseNode = z.object({+ _id: z.string(),+ tag: z.string(),+ children: z.array(z.string()),+ classes: z.array(z.string()),+ data: z.object({}).optional(),+});++const WfTextNode = z.object({+ _id: z.string(),+ v: z.string(),+ text: z.boolean(),+});const WfNode = z.union([- z.object({- _id: z.string(),- type: z.enum([- "Heading",- "List",- "ListItem",- ...(Object.keys(wfToWsComponentMap) as Array<WfComponent>),- ]),- tag: z.string(),- children: z.array(z.string()),- classes: z.array(z.string()),+ WfBaseNode.extend({ type: z.enum(["Heading"]) }),+ WfBaseNode.extend({+ type: z.enum(["Block"]),+ data: z.object({ text: z.boolean().optional() }),}),- z.object({- _id: z.string(),- v: z.string(),- text: z.boolean(),+ WfBaseNode.extend({ type: z.enum(["List"]) }),+ WfBaseNode.extend({ type: z.enum(["ListItem"]) }),+ WfBaseNode.extend({+ type: z.enum(["Link"]),+ data: z.object({+ link: z.object({+ url: z.string(),+ target: z.string().optional(),+ }),+ }),}),+ WfBaseNode.extend({ type: z.enum(["Paragraph"]) }),+ WfBaseNode.extend({ type: z.enum(["Blockquote"]) }),+ WfTextNode,]);type WfNode = z.infer<typeof WfNode>;@@ -110,8 +130,8 @@ const addStyles = (console.error("No base breakpoint found - should never happen");continue;}- const parsed = parseCss(`.styles {${style.styleLess}}`);- for (const style of parsed.styles) {+ const styles = parseCss(`.styles {${style.styleLess}}`).styles ?? [];+ for (const style of styles) {fragment.styles.push({styleSourceId,breakpointId,@@ -159,10 +179,17 @@ const addInstance = (}}+ const component =+ wfNode.type in componentMappers+ ? componentMappers[wfNode.type as keyof typeof componentMappers](+ wfNode+ ) ?? wfNode.type+ : wfNode.type;+fragment.instances.push({id: instanceId,type: "instance",- component: wfToWsComponentMap[wfNode.type as WfComponent] ?? wfNode.type,+ component,children,});added.set(wfNode._id, instanceId);@@ -170,15 +197,35 @@ const addInstance = (return instanceId;};-const addInstances = (- wfNodes: Map<WfNode["_id"], WfNode>,- fragment: WebstudioFragment-) => {- const added = new Map<WfNode["_id"], Instance["id"]>();- for (const wfNode of wfNodes.values()) {- addInstance(wfNode, added, wfNodes, fragment);- }- return added;+// Converting Webflow attributes and data to Webstudio props.+const propertyMappers = {+ Link(wfNode: WfNode, instanceId: Instance["id"]) {+ if ("type" in wfNode === false || wfNode.type !== "Link") {+ return [];+ }+ const data = wfNode.data;+ const props: WebstudioFragment["props"] = [];++ if (data.link.url) {+ props.push({+ type: "string",+ id: nanoid(),+ instanceId,+ name: "href",+ value: data.link.url,+ });+ }+ if (data.link.target) {+ props.push({+ type: "string",+ id: nanoid(),+ instanceId,+ name: "target",+ value: data.link.target,+ });+ }+ return props;+ },};const addProperties = (@@ -187,15 +234,18 @@ const addProperties = (fragment: WebstudioFragment) => {for (const wfNode of wfNodes.values()) {+ if ("text" in wfNode) {+ continue;+ }+ const instanceId = added.get(wfNode._id);+ if (instanceId === undefined) {+ console.error("No instance id found - should never happen");+ continue;+ }// Webflow nodes always come with a tag.// We support tag only for instances like Heading, not all of them.// @todo decide what to do about other instances.if ("tag" in wfNode) {- const instanceId = added.get(wfNode._id);- if (instanceId === undefined) {- console.error("No instance id found - should never happen");- continue;- }fragment.props.push({type: "string",id: nanoid(),@@ -204,10 +254,16 @@ const addProperties = (value: wfNode.tag,});}+ if (wfNode.type in propertyMappers) {+ const props = propertyMappers[+ wfNode.type as keyof typeof propertyMappers+ ](wfNode, instanceId);+ fragment.props.push(...props);+ }}};-const toInstanceData = (wfData: WfData) => {+const toWebstudioFragment = (wfData: WfData) => {const fragment: WebstudioFragment = {children: [],instances: [],@@ -227,10 +283,14 @@ const toInstanceData = (wfData: WfData) => {const wfStyles = new Map<WfStyle["_id"], WfStyle>(wfData.payload.styles.map((style: WfStyle) => [style._id, style]));-- const added = addInstances(wfNodes, fragment);+ const added = new Map<WfNode["_id"], Instance["id"]>();+ for (const wfNode of wfNodes.values()) {+ addInstance(wfNode, added, wfNodes, fragment);+ }addStyles(wfNodes, wfStyles, added, fragment);addProperties(wfNodes, added, fragment);+ // First node should be always the root node in theory, if not+ // we need to find a node that is not a child of any other node.const rootWfNode = wfData.payload.nodes[0];const rootInstanceId = added.get(rootWfNode._id);if (rootInstanceId === undefined) {@@ -263,11 +323,11 @@ export const onPaste = (clipboardData: string): boolean => {if (isFeatureEnabled("pasteFromWebflow") === false) {return false;}- const WfData = parse(clipboardData);- if (WfData === undefined) {+ const wfData = parse(clipboardData);+ if (wfData === undefined) {return false;}- const data = toInstanceData(WfData);+ const data = toWebstudioFragment(wfData);const selectedPage = $selectedPage.get();if (data === undefined || selectedPage === undefined) {return false;@@ -297,5 +357,5 @@ export const onPaste = (clipboardData: string): boolean => {};export const __testing__ = {- toInstanceData,+ toWebstudioFragment,};