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.

experimental: Paste from webflow (#3444)

Oleg Isonend56e857cb661
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts
index 7441fe01c2..6bdce8e166 100644
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts
@@ -1,9 +1,11 @@
import { describe, test, expect } from "@jest/globals";
-import { parse } from "./plugin-markdown";
+import { __testing__ } from "./plugin-markdown";
import { $breakpoints } from "../nano-states";
const options = { generateId: () => "123" };
+const { parse } = __testing__;
+
$breakpoints.set(new Map([["0", { id: "0", label: "base" }]]));
describe("Plugin Markdown", () => {
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
index 0444ac568f..a400780b75 100644
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
@@ -192,7 +192,7 @@ const toInstanceData = (
return children;
};
-export const parse = (clipboardData: string, options?: Options) => {
+const parse = (clipboardData: string, options?: Options) => {
const ast = fromMarkdown(clipboardData, micromarkOptions);
if (ast.children.length === 0) {
return;
@@ -248,3 +248,7 @@ export const onPaste = (clipboardData: string): boolean => {
insertTemplateData(data, dropTarget);
return true;
};
+
+export const __testing__ = {
+ parse,
+};
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow.test.ts b/apps/builder/app/shared/copy-paste/plugin-webflow.test.ts
new file mode 100644
index 0000000000..75ed138814
--- /dev/null
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow.test.ts
@@ -0,0 +1,192 @@
+import { test, expect } from "@jest/globals";
+import { __testing__ } from "./plugin-webflow";
+import { $breakpoints } from "../nano-states";
+
+const { toInstanceData } = __testing__;
+
+$breakpoints.set(new Map([["0", { id: "0", label: "base" }]]));
+
+test("Heading Node", () => {
+ const expected = toInstanceData({
+ type: "@webflow/XscpData",
+ payload: {
+ nodes: [
+ {
+ _id: "97d91be2-3bba-d340-0f13-a84e975b7497",
+ type: "Heading",
+ tag: "h1",
+ children: ["97d91be2-3bba-d340-0f13-a84e975b7498"],
+ classes: [],
+ },
+ {
+ _id: "97d91be2-3bba-d340-0f13-a84e975b7498",
+ v: "Turtle in the sea",
+ text: true,
+ },
+ ],
+ styles: [],
+ },
+ });
+ expect(expected.children).toEqual([
+ {
+ type: "id",
+ value: expect.not.stringMatching("instanceId"),
+ },
+ ]);
+ expect(expected.instances).toEqual([
+ {
+ children: [
+ {
+ type: "text",
+ value: "Turtle in the sea",
+ },
+ ],
+ component: "Heading",
+ id: expect.not.stringMatching("id"),
+ type: "instance",
+ },
+ ]);
+ expect(expected.props).toEqual([
+ {
+ id: expect.not.stringMatching("id"),
+ instanceId: expect.not.stringMatching("instanceId"),
+ name: "tag",
+ type: "string",
+ value: "h1",
+ },
+ ]);
+});
+
+test("List and ListItem", () => {
+ const expected = toInstanceData({
+ type: "@webflow/XscpData",
+ payload: {
+ nodes: [
+ {
+ _id: "7e11a800-c8e2-9b14-37cf-09a9e94754ad",
+ type: "List",
+ tag: "ul",
+ classes: [],
+ children: [
+ "7e11a800-c8e2-9b14-37cf-09a9e94754ae",
+ "7e11a800-c8e2-9b14-37cf-09a9e94754af",
+ "7e11a800-c8e2-9b14-37cf-09a9e94754b0",
+ ],
+ },
+ {
+ _id: "7e11a800-c8e2-9b14-37cf-09a9e94754ae",
+ type: "ListItem",
+ tag: "li",
+ classes: [],
+ children: [],
+ },
+ {
+ _id: "7e11a800-c8e2-9b14-37cf-09a9e94754af",
+ type: "ListItem",
+ tag: "li",
+ classes: [],
+ children: [],
+ },
+ {
+ _id: "7e11a800-c8e2-9b14-37cf-09a9e94754b0",
+ type: "ListItem",
+ tag: "li",
+ classes: [],
+ children: [],
+ },
+ ],
+ styles: [],
+ },
+ });
+ expect(expected.children).toEqual([
+ {
+ type: "id",
+ value: expect.not.stringMatching("instanceId"),
+ },
+ ]);
+
+ expect(expected.instances).toEqual([
+ {
+ id: expect.not.stringMatching("instanceId"),
+ type: "instance",
+ component: "ListItem",
+ children: [],
+ },
+ {
+ id: expect.not.stringMatching("instanceId"),
+ type: "instance",
+ component: "ListItem",
+ children: [],
+ },
+ {
+ id: expect.not.stringMatching("instanceId"),
+ type: "instance",
+ component: "ListItem",
+ children: [],
+ },
+ {
+ id: expect.not.stringMatching("instanceId"),
+ type: "instance",
+ component: "List",
+ children: [
+ { type: "id", value: expect.not.stringMatching("instanceId") },
+ { type: "id", value: expect.not.stringMatching("instanceId") },
+ { type: "id", value: expect.not.stringMatching("instanceId") },
+ ],
+ },
+ ]);
+});
+
+test("Heading with styles", () => {
+ const expected = toInstanceData({
+ type: "@webflow/XscpData",
+ payload: {
+ nodes: [
+ {
+ _id: "97d91be2-3bba-d340-0f13-a84e975b7497",
+ type: "Heading",
+ tag: "h1",
+ classes: [
+ "2891ad3d-89de-2434-bedd-51ef56dff4c4",
+ "a7bff598-b719-1edb-067b-a90a54d68605",
+ ],
+ children: ["97d91be2-3bba-d340-0f13-a84e975b7498"],
+ },
+ {
+ _id: "97d91be2-3bba-d340-0f13-a84e975b7498",
+ text: true,
+ v: "Turtle in the sea",
+ },
+ ],
+ styles: [
+ {
+ _id: "a7bff598-b719-1edb-067b-a90a54d68605",
+ type: "class",
+ name: "Heading",
+ styleLess: "color: hsla(0, 80.00%, 47.78%, 1.00);",
+ },
+ ],
+ },
+ });
+ expect(expected.styleSources).toEqual([
+ {
+ type: "token",
+ id: expect.not.stringMatching("styleSourceId"),
+ name: "Heading",
+ },
+ ]);
+ expect(expected.styleSourceSelections).toEqual([
+ {
+ instanceId: expect.not.stringMatching("instanceId"),
+ values: [expect.not.stringMatching("styleSourceId")],
+ },
+ ]);
+ expect(expected.styles).toEqual([
+ {
+ styleSourceId: expect.not.stringMatching("styleSourceId"),
+ breakpointId: "0",
+ property: "color",
+ value: { type: "rgb", alpha: 1, r: 219, g: 24, b: 24 },
+ },
+ ]);
+});
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow.ts b/apps/builder/app/shared/copy-paste/plugin-webflow.ts
new file mode 100644
index 0000000000..924502a8fa
--- /dev/null
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow.ts
@@ -0,0 +1,301 @@
+import { nanoid } from "nanoid";
+import type { Instance, WebstudioFragment } from "@webstudio-is/sdk";
+import {
+ computeInstancesConstraints,
+ findClosestDroppableTarget,
+ insertTemplateData,
+} from "../instance-utils";
+import {
+ $breakpoints,
+ $instances,
+ $registeredComponentMetas,
+ $selectedInstanceSelector,
+ $selectedPage,
+} from "../nano-states";
+import { z } from "zod";
+import { isBaseBreakpoint } from "../breakpoints";
+import { parseCss } from "@webstudio-is/css-data";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
+
+export const mimeType = "application/json";
+
+const wfToWsComponentMap = {
+ Block: "Box",
+};
+
+type WfComponent = keyof typeof wfToWsComponentMap;
+
+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()),
+ }),
+ z.object({
+ _id: z.string(),
+ v: z.string(),
+ text: z.boolean(),
+ }),
+]);
+type WfNode = z.infer<typeof WfNode>;
+
+const WfStyle = z.object({
+ _id: z.string(),
+ type: z.enum(["class"]),
+ name: z.string(),
+ styleLess: z.string(),
+ //comb: z.string(),
+ //namespace: z.string(),
+ //variants: z.object(),
+ //children: z.array(z.string()),
+ //createdBy: z.string(),
+ //origin: z.null(),
+ //selector: z.null(),
+});
+
+type WfStyle = z.infer<typeof WfStyle>;
+
+const WfData = z.object({
+ type: z.literal("@webflow/XscpData"),
+ payload: z.object({
+ nodes: z.array(WfNode),
+ styles: z.array(WfStyle),
+ }),
+});
+type WfData = z.infer<typeof WfData>;
+
+const addStyles = (
+ wfNodes: Map<WfNode["_id"], WfNode>,
+ wfStyles: Map<WfStyle["_id"], WfStyle>,
+ added: Map<WfNode["_id"], Instance["id"]>,
+ fragment: WebstudioFragment
+) => {
+ for (const wfNode of wfNodes.values()) {
+ if ("text" in wfNode) {
+ continue;
+ }
+ for (const classId of wfNode.classes) {
+ const style = wfStyles.get(classId);
+ if (style === undefined) {
+ continue;
+ }
+ const styleSourceId = nanoid();
+ fragment.styleSources.push({
+ type: "token",
+ id: styleSourceId,
+ name: style.name,
+ });
+ const instanceId = added.get(wfNode._id);
+ if (instanceId === undefined) {
+ console.error("No instance id found - should never happen");
+ continue;
+ }
+
+ fragment.styleSourceSelections.push({
+ instanceId,
+ values: [styleSourceId],
+ });
+
+ const breakpointId = Array.from($breakpoints.get().values()).find(
+ isBaseBreakpoint
+ )?.id;
+ if (breakpointId === undefined) {
+ console.error("No base breakpoint found - should never happen");
+ continue;
+ }
+ const parsed = parseCss(`.styles {${style.styleLess}}`);
+ for (const style of parsed.styles) {
+ fragment.styles.push({
+ styleSourceId,
+ breakpointId,
+ property: style.property,
+ value: style.value,
+ });
+ }
+ }
+ }
+};
+
+const addInstance = (
+ wfNode: WfNode,
+ added: Map<WfNode["_id"], Instance["id"]>,
+ wfNodes: Map<WfNode["_id"], WfNode>,
+ fragment: WebstudioFragment
+) => {
+ if (added.get(wfNode._id) || "text" in wfNode) {
+ return;
+ }
+
+ const children: Instance["children"] = [];
+ const instanceId = nanoid();
+
+ for (const wfChildId of wfNode.children) {
+ const wfChildNode = wfNodes.get(wfChildId);
+ if (wfChildNode === undefined) {
+ continue;
+ }
+ if ("text" in wfChildNode) {
+ children.push({
+ type: "text",
+ value: wfChildNode.v,
+ });
+ added.set(wfChildId, instanceId);
+ continue;
+ }
+
+ const childInstanceId = addInstance(wfChildNode, added, wfNodes, fragment);
+ if (childInstanceId !== undefined) {
+ children.push({
+ type: "id",
+ value: childInstanceId,
+ });
+ }
+ }
+
+ fragment.instances.push({
+ id: instanceId,
+ type: "instance",
+ component: wfToWsComponentMap[wfNode.type as WfComponent] ?? wfNode.type,
+ children,
+ });
+ added.set(wfNode._id, instanceId);
+
+ 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;
+};
+
+const addProperties = (
+ wfNodes: Map<WfNode["_id"], WfNode>,
+ added: Map<WfNode["_id"], Instance["id"]>,
+ fragment: WebstudioFragment
+) => {
+ for (const wfNode of wfNodes.values()) {
+ // 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(),
+ instanceId,
+ name: "tag",
+ value: wfNode.tag,
+ });
+ }
+ }
+};
+
+const toInstanceData = (wfData: WfData) => {
+ const fragment: WebstudioFragment = {
+ children: [],
+ instances: [],
+ props: [],
+ breakpoints: [],
+ styles: [],
+ styleSources: [],
+ styleSourceSelections: [],
+ dataSources: [],
+ resources: [],
+ assets: [],
+ };
+
+ const wfNodes = new Map<WfNode["_id"], WfNode>(
+ wfData.payload.nodes.map((node: WfNode) => [node._id, node])
+ );
+ const wfStyles = new Map<WfStyle["_id"], WfStyle>(
+ wfData.payload.styles.map((style: WfStyle) => [style._id, style])
+ );
+
+ const added = addInstances(wfNodes, fragment);
+ addStyles(wfNodes, wfStyles, added, fragment);
+ addProperties(wfNodes, added, fragment);
+ const rootWfNode = wfData.payload.nodes[0];
+ const rootInstanceId = added.get(rootWfNode._id);
+ if (rootInstanceId === undefined) {
+ console.error("No root instance id found - should never happen");
+ return fragment;
+ }
+ fragment.children = [
+ {
+ type: "id",
+ value: rootInstanceId,
+ },
+ ];
+ return fragment;
+};
+
+const parse = (clipboardData: string) => {
+ try {
+ const data = JSON.parse(clipboardData);
+ const result = WfData.safeParse(data);
+ if (result.success) {
+ return result.data;
+ }
+ throw result.error.message;
+ } catch (error) {
+ console.error(error);
+ }
+};
+
+export const onPaste = (clipboardData: string): boolean => {
+ if (isFeatureEnabled("pasteFromWebflow") === false) {
+ return false;
+ }
+ const WfData = parse(clipboardData);
+ if (WfData === undefined) {
+ return false;
+ }
+ const data = toInstanceData(WfData);
+ const selectedPage = $selectedPage.get();
+ if (data === undefined || selectedPage === undefined) {
+ return false;
+ }
+ const metas = $registeredComponentMetas.get();
+ const newInstances = new Map(
+ data.instances.map((instance) => [instance.id, instance])
+ );
+ const rootInstanceIds = data.children
+ .filter((child) => child.type === "id")
+ .map((child) => child.value);
+ // paste to the root if nothing is selected
+ const instanceSelector = $selectedInstanceSelector.get() ?? [
+ selectedPage.rootInstanceId,
+ ];
+ const dropTarget = findClosestDroppableTarget(
+ metas,
+ $instances.get(),
+ instanceSelector,
+ computeInstancesConstraints(metas, newInstances, rootInstanceIds)
+ );
+ if (dropTarget === undefined) {
+ return false;
+ }
+ insertTemplateData(data, dropTarget);
+ return true;
+};
+
+export const __testing__ = {
+ toInstanceData,
+};
diff --git a/apps/builder/app/shared/copy-paste/use-copy-paste.ts b/apps/builder/app/shared/copy-paste/use-copy-paste.ts
index 0cf93224ac..676a66f4de 100644
--- a/apps/builder/app/shared/copy-paste/use-copy-paste.ts
+++ b/apps/builder/app/shared/copy-paste/use-copy-paste.ts
@@ -3,9 +3,10 @@ import { initCopyPaste } from "./init-copy-paste";
import * as instance from "./plugin-instance";
import * as embedTemplate from "./plugin-embed-template";
import * as markdown from "./plugin-markdown";
+import * as webflow from "./plugin-webflow";
export const useCopyPaste = () => {
useEffect(() => {
- return initCopyPaste([instance, embedTemplate, markdown]);
+ return initCopyPaste([instance, embedTemplate, markdown, webflow]);
}, []);
};
diff --git a/packages/css-data/src/parse-css.ts b/packages/css-data/src/parse-css.ts
index e54c6a4e0f..17c16ccf68 100644
--- a/packages/css-data/src/parse-css.ts
+++ b/packages/css-data/src/parse-css.ts
@@ -7,6 +7,7 @@ import {
import { parseCssValue as parseCssValueLonghand } from "./parse-css-value";
import * as parsers from "./property-parsers/parsers";
import * as toLonghand from "./property-parsers/to-longhand";
+import { camelCase } from "change-case";
type Selector = string;
type Style = {
@@ -19,10 +20,10 @@ export type Styles = Record<Selector, Style[]>;
type Longhand = keyof typeof toLonghand;
-const parseCssValue = function parseCssValue(
+const parseCssValue = (
property: Longhand | StyleProperty,
value: string
-): S {
+): S => {
const unwrap = toLonghand[property as Longhand];
if (typeof unwrap === "function") {
@@ -70,13 +71,12 @@ const cssTreeTryParse = (input: string) => {
const ast = csstree.parse(input);
return ast;
} catch {
- return undefined;
+ return;
}
};
-export const parseCss = function cssToWS(css: string) {
+export const parseCss = (css: string) => {
const ast = cssTreeTryParse(css);
-
let selectors: Selector[] = [];
const styles: Styles = {};
@@ -107,6 +107,7 @@ export const parseCss = function cssToWS(css: string) {
([property, value]) => {
try {
StyleValue.parse(value);
+ property = camelCase(property) as StyleProperty;
selectors.forEach((selector) => {
const selectors = styles[selector];
if (Array.isArray(selectors)) {
diff --git a/packages/feature-flags/src/flags.ts b/packages/feature-flags/src/flags.ts
index 64c5105bad..03cd16f3b5 100644
--- a/packages/feature-flags/src/flags.ts
+++ b/packages/feature-flags/src/flags.ts
@@ -7,3 +7,4 @@ export const cms = false;
export const cssVars = false;
export const filters = false;
export const xmlElement = false;
+export const pasteFromWebflow = false;