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.

Paste markdown as normalized instances (#1195)

Bogdan Chadkin57c39c4a520b
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 813ad600c4..0d11be9978 100644
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.test.ts
@@ -1,25 +1,19 @@
import { describe, expect } from "@jest/globals";
-import { Instance, Prop } from "@webstudio-is/project-build";
+import { InstancesItem, Prop } from "@webstudio-is/project-build";
import { parse } from "./plugin-markdown";
-const parseInstanceData = (data?: {
- children: Instance["children"];
- props: Array<Prop>;
-}) => {
+const parseInstanceData = (data?: ReturnType<typeof parse>) => {
if (data === undefined) {
return;
}
- const { children, props } = data;
- for (const child of children) {
- if (child.type === "instance") {
- Instance.parse(child);
- parseInstanceData({ children: child.children, props });
- }
+ const { rootIds, instances, props } = data;
+ for (const instance of instances) {
+ InstancesItem.parse(instance);
}
for (const prop of props) {
Prop.parse(prop);
}
- return { children, props };
+ return { rootIds, instances, props };
};
const options = { generateId: () => "123" };
@@ -28,7 +22,7 @@ describe("Plugin Markdown", () => {
test("paragraph", () => {
expect(parseInstanceData(parse("xyz", options))).toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
@@ -42,6 +36,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -50,7 +47,7 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse("# heading", options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
@@ -72,6 +69,9 @@ describe("Plugin Markdown", () => {
"value": "h1",
},
],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -80,7 +80,7 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse("###### heading", options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
@@ -102,6 +102,9 @@ describe("Plugin Markdown", () => {
"value": "h6",
},
],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -110,19 +113,23 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse("__bold__", options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
+ {
+ "children": [
+ {
+ "type": "text",
+ "value": "bold",
+ },
+ ],
+ "component": "Bold",
+ "id": "123",
+ "type": "instance",
+ },
{
"children": [
{
- "children": [
- {
- "type": "text",
- "value": "bold",
- },
- ],
- "component": "Bold",
- "id": "123",
- "type": "instance",
+ "type": "id",
+ "value": "123",
},
],
"component": "Paragraph",
@@ -131,6 +138,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -139,19 +149,23 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse("**bold**", options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
- "children": [
- {
- "type": "text",
- "value": "bold",
- },
- ],
- "component": "Bold",
- "id": "123",
- "type": "instance",
+ "type": "text",
+ "value": "bold",
+ },
+ ],
+ "component": "Bold",
+ "id": "123",
+ "type": "instance",
+ },
+ {
+ "children": [
+ {
+ "type": "id",
+ "value": "123",
},
],
"component": "Paragraph",
@@ -160,6 +174,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -168,19 +185,23 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse("_italic_", options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
+ {
+ "children": [
+ {
+ "type": "text",
+ "value": "italic",
+ },
+ ],
+ "component": "Italic",
+ "id": "123",
+ "type": "instance",
+ },
{
"children": [
{
- "children": [
- {
- "type": "text",
- "value": "italic",
- },
- ],
- "component": "Italic",
- "id": "123",
- "type": "instance",
+ "type": "id",
+ "value": "123",
},
],
"component": "Paragraph",
@@ -189,6 +210,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -197,19 +221,23 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse("*italic*", options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
+ {
+ "children": [
+ {
+ "type": "text",
+ "value": "italic",
+ },
+ ],
+ "component": "Italic",
+ "id": "123",
+ "type": "instance",
+ },
{
"children": [
{
- "children": [
- {
- "type": "text",
- "value": "italic",
- },
- ],
- "component": "Italic",
- "id": "123",
- "type": "instance",
+ "type": "id",
+ "value": "123",
},
],
"component": "Paragraph",
@@ -218,6 +246,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -226,19 +257,23 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse('[link](/uri "Title")', options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
- "children": [
- {
- "type": "text",
- "value": "link",
- },
- ],
- "component": "RichTextLink",
- "id": "123",
- "type": "instance",
+ "type": "text",
+ "value": "link",
+ },
+ ],
+ "component": "RichTextLink",
+ "id": "123",
+ "type": "instance",
+ },
+ {
+ "children": [
+ {
+ "type": "id",
+ "value": "123",
},
],
"component": "Paragraph",
@@ -262,6 +297,9 @@ describe("Plugin Markdown", () => {
"value": "Title",
},
],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -270,14 +308,18 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse('![foo](/url "title")', options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
+ {
+ "children": [],
+ "component": "Image",
+ "id": "123",
+ "type": "instance",
+ },
{
"children": [
{
- "children": [],
- "component": "Image",
- "id": "123",
- "type": "instance",
+ "type": "id",
+ "value": "123",
},
],
"component": "Paragraph",
@@ -308,6 +350,9 @@ describe("Plugin Markdown", () => {
"value": "foo",
},
],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -323,7 +368,7 @@ describe("Plugin Markdown", () => {
)
).toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
@@ -341,6 +386,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -356,7 +404,7 @@ describe("Plugin Markdown", () => {
)
).toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
@@ -371,6 +419,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -378,19 +429,23 @@ describe("Plugin Markdown", () => {
test("blockquote", () => {
expect(parseInstanceData(parse("> bar", options))).toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
+ {
+ "children": [
+ {
+ "type": "text",
+ "value": "bar",
+ },
+ ],
+ "component": "Paragraph",
+ "id": "123",
+ "type": "instance",
+ },
{
"children": [
{
- "children": [
- {
- "type": "text",
- "value": "bar",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
+ "type": "id",
+ "value": "123",
},
],
"component": "Blockquote",
@@ -399,6 +454,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -406,19 +464,23 @@ describe("Plugin Markdown", () => {
test("inline code", () => {
expect(parseInstanceData(parse("`foo`", options))).toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
- "children": [
- {
- "type": "text",
- "value": "foo",
- },
- ],
- "component": "Code",
- "id": "123",
- "type": "instance",
+ "type": "text",
+ "value": "foo",
+ },
+ ],
+ "component": "Code",
+ "id": "123",
+ "type": "instance",
+ },
+ {
+ "children": [
+ {
+ "type": "id",
+ "value": "123",
},
],
"component": "Paragraph",
@@ -435,6 +497,9 @@ describe("Plugin Markdown", () => {
"value": true,
},
],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -443,7 +508,7 @@ describe("Plugin Markdown", () => {
expect(parseInstanceData(parse("```js meta\nfoo\n```", options)))
.toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
@@ -479,6 +544,9 @@ describe("Plugin Markdown", () => {
"value": "meta",
},
],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -486,26 +554,34 @@ describe("Plugin Markdown", () => {
test("list unordered", () => {
expect(parseInstanceData(parse("- one", options))).toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
+ {
+ "children": [
+ {
+ "type": "text",
+ "value": "one",
+ },
+ ],
+ "component": "Paragraph",
+ "id": "123",
+ "type": "instance",
+ },
+ {
+ "children": [
+ {
+ "type": "id",
+ "value": "123",
+ },
+ ],
+ "component": "ListItem",
+ "id": "123",
+ "type": "instance",
+ },
{
"children": [
{
- "children": [
- {
- "children": [
- {
- "type": "text",
- "value": "one",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "component": "ListItem",
- "id": "123",
- "type": "instance",
+ "type": "id",
+ "value": "123",
},
],
"component": "List",
@@ -522,6 +598,9 @@ describe("Plugin Markdown", () => {
"value": false,
},
],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -529,26 +608,34 @@ describe("Plugin Markdown", () => {
test("list ordered", () => {
expect(parseInstanceData(parse("3. one", options))).toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [
{
- "children": [
- {
- "children": [
- {
- "type": "text",
- "value": "one",
- },
- ],
- "component": "Paragraph",
- "id": "123",
- "type": "instance",
- },
- ],
- "component": "ListItem",
- "id": "123",
- "type": "instance",
+ "type": "text",
+ "value": "one",
+ },
+ ],
+ "component": "Paragraph",
+ "id": "123",
+ "type": "instance",
+ },
+ {
+ "children": [
+ {
+ "type": "id",
+ "value": "123",
+ },
+ ],
+ "component": "ListItem",
+ "id": "123",
+ "type": "instance",
+ },
+ {
+ "children": [
+ {
+ "type": "id",
+ "value": "123",
},
],
"component": "List",
@@ -572,6 +659,9 @@ describe("Plugin Markdown", () => {
"value": 3,
},
],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
@@ -579,7 +669,7 @@ describe("Plugin Markdown", () => {
test("thematic break | separator", () => {
expect(parseInstanceData(parse("---", options))).toMatchInlineSnapshot(`
{
- "children": [
+ "instances": [
{
"children": [],
"component": "Separator",
@@ -588,6 +678,9 @@ describe("Plugin Markdown", () => {
},
],
"props": [],
+ "rootIds": [
+ "123",
+ ],
}
`);
});
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
index 8d5aa33ba6..bd4c079604 100644
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
@@ -2,20 +2,20 @@ import store from "immerhin";
import { gfm } from "micromark-extension-gfm";
import type { Root } from "mdast";
import { fromMarkdown } from "mdast-util-from-markdown";
-import type { Instance, Prop } from "@webstudio-is/project-build";
-import { utils } from "@webstudio-is/project";
+import type {
+ Instance,
+ InstancesItem,
+ Prop,
+} from "@webstudio-is/project-build";
import { nanoid } from "nanoid";
import {
- createInstancesIndex,
findClosestDroppableTarget,
- insertInstanceMutable,
+ insertInstancesMutable,
} from "../tree-utils";
import {
instancesIndexStore,
instancesStore,
- patchInstancesMutable,
propsStore,
- rootInstanceContainer,
selectedInstanceIdStore,
} from "../nano-states";
@@ -44,164 +44,150 @@ const astTypeComponentMap: Record<string, Instance["component"]> = {
type Options = { generateId?: typeof nanoid };
-const createInstance = (
- {
- component,
- node,
- props,
- }: {
- component: Instance["component"];
- node: Root["children"][number];
- props: Array<Prop>;
- },
- options: Options
-) => {
- const { generateId = nanoid } = options;
- return utils.tree.createInstance({
- id: generateId(),
- component,
- children:
- "children" in node ? toInstanceData(node, options, props).children : [],
- });
-};
-
const toInstanceData = (
+ instances: InstancesItem[],
+ props: Prop[],
ast: { children: Root["children"] },
- options: Options = {},
- props: Array<Prop> = []
-): { children: Instance["children"]; props: Array<Prop> } => {
+ options: Options = {}
+): InstancesItem["children"] => {
const { generateId = nanoid } = options;
- const children: Instance["children"] = [];
+ const children: InstancesItem["children"] = [];
for (const child of ast.children) {
if (child.type === "text") {
- children.push({
- type: child.type,
- value: child.value,
- });
+ children.push({ type: "text", value: child.value });
continue;
}
const component = astTypeComponentMap[child.type];
- if (component) {
- const instance = createInstance(
- { component, node: child, props },
- options
- );
- children.push(instance);
- if (child.type === "heading") {
- props.push({
- id: generateId(),
- type: "string",
- name: "tag",
- instanceId: instance.id,
- value: `h${child.depth}`,
- });
- }
- if (child.type === "link") {
+ if (component === undefined) {
+ continue;
+ }
+ const instance: InstancesItem = {
+ type: "instance",
+ id: generateId(),
+ component,
+ children:
+ "children" in child
+ ? toInstanceData(instances, props, child, options)
+ : [],
+ };
+ instances.push(instance);
+ children.push({ type: "id", value: instance.id });
+
+ if (child.type === "heading") {
+ props.push({
+ id: generateId(),
+ type: "string",
+ name: "tag",
+ instanceId: instance.id,
+ value: `h${child.depth}`,
+ });
+ }
+ if (child.type === "link") {
+ props.push({
+ id: generateId(),
+ type: "string",
+ name: "href",
+ instanceId: instance.id,
+ value: child.url,
+ });
+ }
+ if (child.type === "image") {
+ props.push({
+ id: generateId(),
+ type: "string",
+ name: "src",
+ instanceId: instance.id,
+ value: child.url,
+ });
+ }
+ if (child.type === "inlineCode") {
+ instance.children.push({
+ type: "text",
+ value: child.value,
+ });
+ props.push({
+ id: generateId(),
+ type: "boolean",
+ name: "inline",
+ instanceId: instance.id,
+ value: true,
+ });
+ }
+ if (child.type === "code") {
+ instance.children.push({
+ type: "text",
+ value: child.value,
+ });
+ props.push({
+ id: generateId(),
+ type: "boolean",
+ name: "inline",
+ instanceId: instance.id,
+ value: false,
+ });
+ if (child.lang) {
props.push({
id: generateId(),
type: "string",
- name: "href",
+ name: "lang",
instanceId: instance.id,
- value: child.url,
+ value: child.lang,
});
}
- if (child.type === "image") {
+ if (child.meta) {
props.push({
id: generateId(),
type: "string",
- name: "src",
+ name: "meta",
instanceId: instance.id,
- value: child.url,
+ value: child.meta,
});
}
- if (child.type === "inlineCode") {
- instance.children.push({
- type: "text",
- value: child.value,
- });
+ }
+ if (child.type === "list") {
+ if (typeof child.ordered === "boolean") {
props.push({
id: generateId(),
type: "boolean",
- name: "inline",
+ name: "ordered",
instanceId: instance.id,
- value: true,
+ value: child.ordered,
});
}
- if (child.type === "code") {
- instance.children.push({
- type: "text",
- value: child.value,
- });
+ if (typeof child.start === "number") {
props.push({
id: generateId(),
- type: "boolean",
- name: "inline",
+ type: "number",
+ name: "start",
instanceId: instance.id,
- value: false,
+ value: child.start,
});
- if (child.lang) {
- props.push({
- id: generateId(),
- type: "string",
- name: "lang",
- instanceId: instance.id,
- value: child.lang,
- });
- }
- if (child.meta) {
- props.push({
- id: generateId(),
- type: "string",
- name: "meta",
- instanceId: instance.id,
- value: child.meta,
- });
- }
- }
- if (child.type === "list") {
- if (typeof child.ordered === "boolean") {
- props.push({
- id: generateId(),
- type: "boolean",
- name: "ordered",
- instanceId: instance.id,
- value: child.ordered,
- });
- }
- if (typeof child.start === "number") {
- props.push({
- id: generateId(),
- type: "number",
- name: "start",
- instanceId: instance.id,
- value: child.start,
- });
- }
}
+ }
- if ("title" in child && child.title) {
- props.push({
- id: generateId(),
- type: "string",
- name: "title",
- instanceId: instance.id,
- value: child.title,
- });
- }
- if ("alt" in child && child.alt) {
- props.push({
- id: generateId(),
- type: "string",
- name: "alt",
- instanceId: instance.id,
- value: child.alt,
- });
- }
+ if ("title" in child && child.title) {
+ props.push({
+ id: generateId(),
+ type: "string",
+ name: "title",
+ instanceId: instance.id,
+ value: child.title,
+ });
+ }
+ if ("alt" in child && child.alt) {
+ props.push({
+ id: generateId(),
+ type: "string",
+ name: "alt",
+ instanceId: instance.id,
+ value: child.alt,
+ });
}
}
- return { children, props };
+
+ return children;
};
export const parse = (clipboardData: string, options?: Options) => {
@@ -209,8 +195,14 @@ export const parse = (clipboardData: string, options?: Options) => {
if (ast.children.length === 0) {
return;
}
- const data = toInstanceData(ast, options);
- return data;
+ const instances: InstancesItem[] = [];
+ const props: Prop[] = [];
+ const children = toInstanceData(instances, props, ast, options);
+ // assume text is not top level
+ const rootIds = children.flatMap((child) =>
+ child.type === "id" ? [child.value] : []
+ );
+ return { props, instances, rootIds };
};
export const onPaste = (clipboardData: string) => {
@@ -223,19 +215,10 @@ export const onPaste = (clipboardData: string) => {
instancesIndexStore.get(),
selectedInstanceIdStore.get()
);
- const rootInstance = rootInstanceContainer.get();
store.createTransaction([instancesStore, propsStore], (instances, props) => {
- const instancesIndex = createInstancesIndex(rootInstance);
- // We are inserting backwards, so we need to reverse the order
- data.children.reverse();
- for (const child of data.children) {
- if (child.type === "instance") {
- insertInstanceMutable(instancesIndex, child, dropTarget);
- }
- }
+ insertInstancesMutable(instances, data.instances, data.rootIds, dropTarget);
for (const prop of data.props) {
props.set(prop.id, prop);
}
- patchInstancesMutable(rootInstance, instances);
});
};
diff --git a/apps/builder/app/shared/tree-utils.test.ts b/apps/builder/app/shared/tree-utils.test.ts
index 7fb8e8f344..7531148b8c 100644
--- a/apps/builder/app/shared/tree-utils.test.ts
+++ b/apps/builder/app/shared/tree-utils.test.ts
@@ -236,6 +236,7 @@ test("insert instances tree into target", () => {
]),
createInstancesItem("inserted2", "Box", []),
],
+ ["inserted1"],
{
parentId: "box1",
position: 1,
@@ -266,6 +267,7 @@ test("insert instances tree into target", () => {
]),
createInstancesItem("inserted4", "Box", []),
],
+ ["inserted3"],
{
parentId: "box1",
position: "end",
diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.ts
index ef281b273f..08b71742b0 100644
--- a/apps/builder/app/shared/tree-utils.ts
+++ b/apps/builder/app/shared/tree-utils.ts
@@ -282,6 +282,7 @@ export const findSubtreeLocalStyleSources = (
export const insertInstancesMutable = (
instances: Instances,
insertedInstances: InstancesItem[],
+ rootIds: Instance["id"][],
dropTarget: undefined | DroppableTarget
) => {
if (dropTarget === undefined) {
@@ -304,14 +305,16 @@ export const insertInstancesMutable = (
}
const { position } = dropTarget;
- const dropTargetChild: InstancesItem["children"][number] = {
- type: "id",
- value: treeRootInstanceId,
- };
+ const dropTargetChildren: InstancesItem["children"] = rootIds.map(
+ (instanceId) => ({
+ type: "id",
+ value: instanceId,
+ })
+ );
if (position === "end") {
- parentInstance.children.push(dropTargetChild);
+ parentInstance.children.push(...dropTargetChildren);
} else {
- parentInstance.children.splice(position, 0, dropTargetChild);
+ parentInstance.children.splice(position, 0, ...dropTargetChildren);
}
};
@@ -343,7 +346,13 @@ export const insertInstancesCopyMutable = (
});
}
- insertInstancesMutable(instances, copiedInstancesWithNewIds, dropTarget);
+ insertInstancesMutable(
+ instances,
+ copiedInstancesWithNewIds,
+ // consider the first instance as the root
+ [copiedInstancesWithNewIds[0].id],
+ dropTarget
+ );
return copiedInstanceIds;
};