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.

Convert text children to nodes in tree (#654)

Bogdan Chadkin41c533271ad4
diff --git a/apps/designer/app/canvas/features/text-editor/interop.ts b/apps/designer/app/canvas/features/text-editor/interop.ts
index 3e54ee2f01..fd94c71a9a 100644
--- a/apps/designer/app/canvas/features/text-editor/interop.ts
+++ b/apps/designer/app/canvas/features/text-editor/interop.ts
@@ -11,7 +11,7 @@ import {
$isLineBreakNode,
} from "lexical";
import { $createLinkNode, $isLinkNode } from "@lexical/link";
-import type { ChildrenUpdates, Instance } from "@webstudio-is/react-sdk";
+import type { ChildrenUpdates, Instance, Text } from "@webstudio-is/react-sdk";
import { utils } from "@webstudio-is/project";
import { $isSpanNode, $setNodeSpan } from "./toolbar-connector";
@@ -36,13 +36,14 @@ const $writeUpdates = (
$writeUpdates(child, updates, refs);
}
if ($isLineBreakNode(child)) {
- updates.push("\n");
+ updates.push({ type: "text", value: "\n" });
}
if ($isLinkNode(child)) {
const id = refs.get(child.getKey());
const childrenUpdates: ChildrenUpdates = [];
$writeUpdates(child, childrenUpdates, refs);
updates.push({
+ type: "instance",
id,
component: "RichTextLink",
children: childrenUpdates,
@@ -62,6 +63,7 @@ const $writeUpdates = (
refs.set(`${child.getKey()}:span`, id);
}
const update: ChildrenUpdates[number] = {
+ type: "instance",
id,
component: "Span",
children: [],
@@ -74,6 +76,7 @@ const $writeUpdates = (
if (child.hasFormat(format)) {
const id = refs.get(`${child.getKey()}:${format}`);
const update: ChildrenUpdates[number] = {
+ type: "instance",
id,
component,
children: [],
@@ -82,7 +85,7 @@ const $writeUpdates = (
parentUpdates = update.children;
}
}
- parentUpdates.push(text);
+ parentUpdates.push({ type: "text", value: text });
}
}
};
@@ -94,7 +97,7 @@ export const $convertToUpdates = (refs: Refs) => {
return updates;
};
-type InstanceChild = string | Instance;
+type InstanceChild = Text | Instance;
const $writeLexical = (
parent: ElementNode | TextNode,
@@ -102,17 +105,17 @@ const $writeLexical = (
refs: Refs
) => {
for (const child of children) {
- // convert text
- if (child === "\n" && $isElementNode(parent)) {
- const lineBreakNode = $createLineBreakNode();
- parent.append(lineBreakNode);
- continue;
- }
- if (typeof child === "string") {
+ if (child.type === "text") {
+ // convert text
+ if (child.value === "\n" && $isElementNode(parent)) {
+ const lineBreakNode = $createLineBreakNode();
+ parent.append(lineBreakNode);
+ continue;
+ }
if ($isTextNode(parent)) {
- parent.setTextContent(child);
+ parent.setTextContent(child.value);
} else {
- const textNode = $createTextNode(child);
+ const textNode = $createTextNode(child.value);
parent.append(textNode);
}
continue;
diff --git a/apps/designer/app/canvas/features/text-editor/text-editor.stories.tsx b/apps/designer/app/canvas/features/text-editor/text-editor.stories.tsx
index fa68281f95..c45303201e 100644
--- a/apps/designer/app/canvas/features/text-editor/text-editor.stories.tsx
+++ b/apps/designer/app/canvas/features/text-editor/text-editor.stories.tsx
@@ -99,23 +99,23 @@ export const Basic: ComponentStory<typeof TextEditor> = ({ onChange }) => {
instance={utils.tree.createInstance({
component: "TextBlock",
children: [
- "Pragraph you can edit ",
+ { type: "text", value: "Paragraph you can edit " },
utils.tree.createInstance({
component: "Bold",
- children: ["very bold text "],
+ children: [{ type: "text", value: "very bold text " }],
}),
utils.tree.createInstance({
component: "Bold",
children: [
utils.tree.createInstance({
component: "Italic",
- children: ["with small italic"],
+ children: [{ type: "text", value: "with small italic" }],
}),
],
}),
utils.tree.createInstance({
component: "Bold",
- children: [" subtext"],
+ children: [{ type: "text", value: " subtext" }],
}),
],
})}
diff --git a/apps/designer/app/canvas/shared/instance.test.ts b/apps/designer/app/canvas/shared/instance.test.ts
index de9e7df62b..a826f355ba 100644
--- a/apps/designer/app/canvas/shared/instance.test.ts
+++ b/apps/designer/app/canvas/shared/instance.test.ts
@@ -3,19 +3,45 @@ import { type Instance } from "@webstudio-is/react-sdk";
import { findInsertLocation } from "./instance";
const tree: Instance = {
+ type: "instance",
component: "Body",
id: "root",
cssRules: [],
children: [
- { component: "Heading", id: "heading1", cssRules: [], children: [] },
{
+ type: "instance",
+ component: "Heading",
+ id: "heading1",
+ cssRules: [],
+ children: [],
+ },
+ {
+ type: "instance",
component: "Box",
id: "box1",
cssRules: [],
children: [
- { component: "Box", id: "box2", cssRules: [], children: [] },
- { component: "Heading", id: "heading2", cssRules: [], children: [] },
- { component: "Heading", id: "heading3", cssRules: [], children: [] },
+ {
+ type: "instance",
+ component: "Box",
+ id: "box2",
+ cssRules: [],
+ children: [],
+ },
+ {
+ type: "instance",
+ component: "Heading",
+ id: "heading2",
+ cssRules: [],
+ children: [],
+ },
+ {
+ type: "instance",
+ component: "Heading",
+ id: "heading3",
+ cssRules: [],
+ children: [],
+ },
],
},
],
diff --git a/apps/designer/app/designer/features/style-panel/shared/get-inherited-style.ts b/apps/designer/app/designer/features/style-panel/shared/get-inherited-style.ts
index f6aa47e36e..5d6e18dd09 100644
--- a/apps/designer/app/designer/features/style-panel/shared/get-inherited-style.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/get-inherited-style.ts
@@ -32,7 +32,7 @@ const findParents = (
const parents: Array<Instance> = [];
for (const child of instance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
// I am your father
diff --git a/apps/designer/app/designer/shared/tree/index.tsx b/apps/designer/app/designer/shared/tree/index.tsx
index 3dcbc73989..22f3f9d70d 100644
--- a/apps/designer/app/designer/shared/tree/index.tsx
+++ b/apps/designer/app/designer/shared/tree/index.tsx
@@ -37,8 +37,8 @@ const instanceRelatedProps = {
}
return item.children.filter(
- (child) => typeof child !== "string"
- ) as Instance[];
+ (child): child is Instance => child.type === "instance"
+ );
},
renderItem(props: TreeItemRenderProps<Instance>) {
const { Icon, label } = getComponentMeta(props.itemData.component);
diff --git a/apps/designer/app/shared/css-utils/__snapshots__/delete-css-rules-by-breakpoint.test.ts.snap b/apps/designer/app/shared/css-utils/__snapshots__/delete-css-rules-by-breakpoint.test.ts.snap
index b9455c4349..ef46dcf247 100644
--- a/apps/designer/app/shared/css-utils/__snapshots__/delete-css-rules-by-breakpoint.test.ts.snap
+++ b/apps/designer/app/shared/css-utils/__snapshots__/delete-css-rules-by-breakpoint.test.ts.snap
@@ -10,6 +10,7 @@ exports[`Delete breakpoint basic 1`] = `
"component": "Box",
"cssRules": [],
"id": "3",
+ "type": "instance",
},
],
"component": "Box",
@@ -20,10 +21,12 @@ exports[`Delete breakpoint basic 1`] = `
},
],
"id": "2",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
diff --git a/apps/designer/app/shared/css-utils/delete-css-rules-by-breakpoint.test.ts b/apps/designer/app/shared/css-utils/delete-css-rules-by-breakpoint.test.ts
index 860777aa85..608f26dc68 100644
--- a/apps/designer/app/shared/css-utils/delete-css-rules-by-breakpoint.test.ts
+++ b/apps/designer/app/shared/css-utils/delete-css-rules-by-breakpoint.test.ts
@@ -5,16 +5,19 @@ import { deleteCssRulesByBreakpoint } from "./delete-css-rules-by-breakpoint";
describe("Delete breakpoint", () => {
test("basic", () => {
const instance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [{ breakpoint: "delete this", style: {} }],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [{ breakpoint: "do not delete", style: {} }],
children: [
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [{ breakpoint: "delete this", style: {} }],
diff --git a/apps/designer/app/shared/css-utils/delete-css-rules-by-breakpoint.ts b/apps/designer/app/shared/css-utils/delete-css-rules-by-breakpoint.ts
index be6c29b861..259ed28ed3 100644
--- a/apps/designer/app/shared/css-utils/delete-css-rules-by-breakpoint.ts
+++ b/apps/designer/app/shared/css-utils/delete-css-rules-by-breakpoint.ts
@@ -13,7 +13,7 @@ export const deleteCssRulesByBreakpoint = (
});
for (const child of instance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
deleteCssRulesByBreakpoint(child, breakpointId);
diff --git a/packages/prisma-client/prisma/migrations/20221218211129_tree_text/migration.ts b/packages/prisma-client/prisma/migrations/20221218211129_tree_text/migration.ts
new file mode 100644
index 0000000000..94578fdce8
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20221218211129_tree_text/migration.ts
@@ -0,0 +1,70 @@
+import { PrismaClient } from "./client";
+
+type PrevInstance = {
+ id: string;
+ component: any;
+ cssRules: Array<any>;
+ children: Array<PrevInstance | string>;
+};
+
+type Text = {
+ type: "text";
+ value: string;
+};
+
+type Instance = {
+ type: "instance";
+ id: string;
+ component: any;
+ cssRules: Array<any>;
+ children: Array<Instance | Text>;
+};
+
+const convertTree = (prevInstance: PrevInstance): Instance => {
+ const instance: Instance = {
+ type: "instance",
+ id: prevInstance.id,
+ component: prevInstance.component,
+ cssRules: prevInstance.cssRules,
+ children: [],
+ };
+
+ for (const child of prevInstance.children) {
+ if (typeof child === "string") {
+ instance.children.push({ type: "text", value: child });
+ } else {
+ instance.children.push(convertTree(child));
+ }
+ }
+ return instance;
+};
+
+export default () => {
+ const client = new PrismaClient({
+ // Uncomment to see the queries in console as the migration runs
+ // log: ["query", "info", "warn", "error"],
+ });
+ return client.$transaction(
+ async (prisma) => {
+ const previousTrees = await prisma.tree.findMany();
+ const update = previousTrees.map((tree) => {
+ let newRoot = tree.root;
+ try {
+ newRoot = JSON.stringify(convertTree(JSON.parse(tree.root)));
+ } catch {
+ console.info(`Tree ${tree.id} cannot be converted`);
+ }
+ return {
+ id: tree.id,
+ root: newRoot,
+ };
+ });
+ await Promise.all(
+ update.map(({ id, ...data }) =>
+ prisma.tree.update({ where: { id }, data })
+ )
+ );
+ },
+ { timeout: 1000 * 60 * 5 }
+ );
+};
diff --git a/packages/prisma-client/prisma/migrations/20221218211129_tree_text/schema.prisma b/packages/prisma-client/prisma/migrations/20221218211129_tree_text/schema.prisma
new file mode 100644
index 0000000000..c0b38bfec4
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20221218211129_tree_text/schema.prisma
@@ -0,0 +1,100 @@
+// DO NOT EDIT THIS FILE!
+// This is a copy of your schema.prisma that corresponds to the state of the database
+// when all migrations up until this one are applied.
+// It's used to generate a Prisma Client for the migration.
+
+// This is your Prisma schema file,
+// learn more about it in the docs: https://pris.ly/d/prisma-schema
+
+generator client {
+ provider = "prisma-client-js"
+ previewFeatures = ["interactiveTransactions"]
+ output = "client"
+}
+
+datasource db {
+ provider = "postgres"
+ url = env("DATABASE_URL")
+}
+
+model Team {
+ id String @id @default(uuid())
+ users User[]
+}
+
+enum Location {
+ FS
+ REMOTE
+}
+
+model Asset {
+ id String @id @default(uuid())
+ projectId String
+ format String
+ size Int
+ name String
+ description String?
+ location Location
+ createdAt DateTime @default(now())
+ meta String @default("{}")
+}
+
+model User {
+ id String @id @default(uuid())
+ email String? @unique
+ provider String?
+ image String?
+ username String?
+ createdAt DateTime @default(now())
+ team Team? @relation(fields: [teamId], references: [id])
+ teamId String?
+ projects Project[]
+}
+
+model Project {
+ id String @id @default(uuid())
+ title String
+ domain String @unique
+ user User? @relation(fields: [userId], references: [id])
+ userId String?
+ build Build[]
+}
+
+model Build {
+ id String @id @default(uuid())
+ createdAt DateTime @default(now())
+ pages String
+
+ project Project @relation(fields: [projectId], references: [id])
+ projectId String
+
+ isDev Boolean // exctly one is true per project
+ isProd Boolean // at most one is true per project (none if not published)
+
+ breakpoints Breakpoints?
+ designTokens DesignTokens?
+}
+
+model Tree {
+ id String @id @default(uuid())
+ root String
+}
+
+model InstanceProps {
+ id String @id @default(uuid())
+ instanceId String
+ treeId String
+ props String @default("[]")
+}
+
+model Breakpoints {
+ build Build @relation(fields: [buildId], references: [id])
+ buildId String @id
+ values String @default("[]")
+}
+
+model DesignTokens {
+ build Build @relation(fields: [buildId], references: [id])
+ buildId String @id
+ value String @default("[]")
+}
diff --git a/packages/project/src/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap b/packages/project/src/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap
index 568eb8b8d3..b03c60384a 100644
--- a/packages/project/src/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap
+++ b/packages/project/src/shared/tree-utils/__snapshots__/delete-instance.test.ts.snap
@@ -8,11 +8,13 @@ exports[`Delete instance div 1 > div 2 > [div 3] 1`] = `
"component": "Box",
"cssRules": [],
"id": "2",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
@@ -24,10 +26,12 @@ exports[`Delete instance div 1 > div 2, [div 3] 1`] = `
"component": "Box",
"cssRules": [],
"id": "2",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
diff --git a/packages/project/src/shared/tree-utils/__snapshots__/insert-instance.test.ts.snap b/packages/project/src/shared/tree-utils/__snapshots__/insert-instance.test.ts.snap
index 7b486a5225..4ed18691f8 100644
--- a/packages/project/src/shared/tree-utils/__snapshots__/insert-instance.test.ts.snap
+++ b/packages/project/src/shared/tree-utils/__snapshots__/insert-instance.test.ts.snap
@@ -8,17 +8,20 @@ exports[`Insert instance after a nested div using "end": div 1 > div 2, [div 3]
"component": "Box",
"cssRules": [],
"id": "2",
+ "type": "instance",
},
{
"children": [],
"component": "Box",
"cssRules": [],
"id": "3",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
@@ -30,17 +33,20 @@ exports[`Insert instance after a nested div using index: div 1 > div 2, [div 3]
"component": "Box",
"cssRules": [],
"id": "2",
+ "type": "instance",
},
{
"children": [],
"component": "Box",
"cssRules": [],
"id": "3",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
@@ -52,17 +58,20 @@ exports[`Insert instance before a nested div using index: div 1 > [div 3], div 2
"component": "Box",
"cssRules": [],
"id": "3",
+ "type": "instance",
},
{
"children": [],
"component": "Box",
"cssRules": [],
"id": "2",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
@@ -76,15 +85,18 @@ exports[`Insert instance inside a nested div using "end": div 1 > div 2 > [div 3
"component": "Box",
"cssRules": [],
"id": "3",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "2",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
diff --git a/packages/project/src/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap b/packages/project/src/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap
index b879c8e77d..7f8414de21 100644
--- a/packages/project/src/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap
+++ b/packages/project/src/shared/tree-utils/__snapshots__/reparent-instance.test.ts.snap
@@ -10,22 +10,26 @@ exports[`Reparent instance new parent 1`] = `
"component": "Box",
"cssRules": [],
"id": "4",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "2",
+ "type": "instance",
},
{
"children": [],
"component": "Box",
"cssRules": [],
"id": "3",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
@@ -37,22 +41,26 @@ exports[`Reparent instance same parent different position 1`] = `
"component": "Box",
"cssRules": [],
"id": "3",
+ "type": "instance",
},
{
"children": [],
"component": "Box",
"cssRules": [],
"id": "2",
+ "type": "instance",
},
{
"children": [],
"component": "Box",
"cssRules": [],
"id": "4",
+ "type": "instance",
},
],
"component": "Box",
"cssRules": [],
"id": "1",
+ "type": "instance",
}
`;
diff --git a/packages/project/src/shared/tree-utils/clone-instance.test.ts b/packages/project/src/shared/tree-utils/clone-instance.test.ts
index e15fd68256..b6b74daccc 100644
--- a/packages/project/src/shared/tree-utils/clone-instance.test.ts
+++ b/packages/project/src/shared/tree-utils/clone-instance.test.ts
@@ -5,7 +5,7 @@ import { cloneInstance } from "./clone-instance";
const getIds = (instance: Instance, ids: Array<Instance["id"]> = []) => {
ids.push(instance.id);
for (const child of instance.children) {
- if (typeof child !== "string") {
+ if (child.type === "instance") {
getIds(child);
}
}
@@ -15,16 +15,19 @@ const getIds = (instance: Instance, ids: Array<Instance["id"]> = []) => {
describe("Clone instance", () => {
test("ensure new ids", () => {
const instance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
diff --git a/packages/project/src/shared/tree-utils/clone-instance.ts b/packages/project/src/shared/tree-utils/clone-instance.ts
index b393d1c770..f479f8c86f 100644
--- a/packages/project/src/shared/tree-utils/clone-instance.ts
+++ b/packages/project/src/shared/tree-utils/clone-instance.ts
@@ -5,7 +5,7 @@ import { type Instance } from "@webstudio-is/react-sdk";
const updateIds = (instance: Instance) => {
instance.id = ObjectId().toString();
for (const child of instance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
updateIds(child);
diff --git a/packages/project/src/shared/tree-utils/create-instance.test.ts b/packages/project/src/shared/tree-utils/create-instance.test.ts
index 69db50080f..01b3290988 100644
--- a/packages/project/src/shared/tree-utils/create-instance.test.ts
+++ b/packages/project/src/shared/tree-utils/create-instance.test.ts
@@ -29,6 +29,7 @@ describe("create instance", () => {
component: "Box",
children: [
{
+ type: "instance",
id: "1",
component: "Box",
cssRules: [],
@@ -39,6 +40,7 @@ describe("create instance", () => {
});
expect(createdInstance.children).toEqual([
{
+ type: "instance",
id: "1",
component: "Box",
cssRules: [],
diff --git a/packages/project/src/shared/tree-utils/create-instance.ts b/packages/project/src/shared/tree-utils/create-instance.ts
index 8d50b9bc38..a2a28dbe3e 100644
--- a/packages/project/src/shared/tree-utils/create-instance.ts
+++ b/packages/project/src/shared/tree-utils/create-instance.ts
@@ -19,9 +19,14 @@ export const createInstance = ({
}): Instance => {
const componentMeta = getComponentMeta(component);
return {
+ type: "instance",
component,
id: id === undefined ? createInstanceId() : id,
cssRules: cssRules ?? [],
- children: children === undefined ? componentMeta.children ?? [] : children,
+ children:
+ children === undefined
+ ? componentMeta.children?.map((value) => ({ type: "text", value })) ??
+ []
+ : children,
};
};
diff --git a/packages/project/src/shared/tree-utils/delete-instance.test.ts b/packages/project/src/shared/tree-utils/delete-instance.test.ts
index 1f8a49bb63..5d85b77fea 100644
--- a/packages/project/src/shared/tree-utils/delete-instance.test.ts
+++ b/packages/project/src/shared/tree-utils/delete-instance.test.ts
@@ -5,16 +5,19 @@ import { deleteInstanceMutable } from "./delete-instance";
describe("Delete instance", () => {
test("div 1 > div 2 > [div 3]", () => {
const instance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
@@ -31,17 +34,20 @@ describe("Delete instance", () => {
test("div 1 > div 2, [div 3]", () => {
const instance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
children: [],
},
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
diff --git a/packages/project/src/shared/tree-utils/delete-instance.ts b/packages/project/src/shared/tree-utils/delete-instance.ts
index a696a2247a..b2f0e43e12 100644
--- a/packages/project/src/shared/tree-utils/delete-instance.ts
+++ b/packages/project/src/shared/tree-utils/delete-instance.ts
@@ -9,7 +9,7 @@ export const deleteInstanceMutable = (
}
for (const child of instance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
const shouldDelete = deleteInstanceMutable(child, instanceId);
diff --git a/packages/project/src/shared/tree-utils/find-closest-sibling-instance.test.ts b/packages/project/src/shared/tree-utils/find-closest-sibling-instance.test.ts
index 4a1249dc6b..6dac370ead 100644
--- a/packages/project/src/shared/tree-utils/find-closest-sibling-instance.test.ts
+++ b/packages/project/src/shared/tree-utils/find-closest-sibling-instance.test.ts
@@ -5,25 +5,29 @@ import { findClosestSiblingInstance } from "./find-closest-sibling-instance";
describe("Find closest sibling instance", () => {
test("find", () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
children: [],
},
- "a",
+ { type: "text", value: "a" },
{
+ type: "instance",
component: "Box",
id: "4",
cssRules: [],
children: [],
},
- "b",
+ { type: "text", value: "b" },
{
+ type: "instance",
component: "Box",
id: "5",
cssRules: [],
diff --git a/packages/project/src/shared/tree-utils/find-closest-sibling-instance.ts b/packages/project/src/shared/tree-utils/find-closest-sibling-instance.ts
index af7d072ecb..4e92dee947 100644
--- a/packages/project/src/shared/tree-utils/find-closest-sibling-instance.ts
+++ b/packages/project/src/shared/tree-utils/find-closest-sibling-instance.ts
@@ -8,8 +8,8 @@ export const findClosestSiblingInstance = (
return;
}
const children = instance.children.filter(
- (instance) => typeof instance === "object"
- ) as Array<Instance>;
+ (child): child is Instance => child.type === "instance"
+ );
const index = children.findIndex((instance) => instance.id === instanceId);
if (index === -1) {
return;
diff --git a/packages/project/src/shared/tree-utils/find-instance.ts b/packages/project/src/shared/tree-utils/find-instance.ts
index e8b470c31c..86759ac5ce 100644
--- a/packages/project/src/shared/tree-utils/find-instance.ts
+++ b/packages/project/src/shared/tree-utils/find-instance.ts
@@ -11,7 +11,7 @@ export const findInstanceById = (
if (instance.children !== undefined) {
for (const child of instance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
const foundInstance = findInstanceById(child, id);
diff --git a/packages/project/src/shared/tree-utils/find-parent-instance.test.ts b/packages/project/src/shared/tree-utils/find-parent-instance.test.ts
index 5be56cbdbe..9782c04b8c 100644
--- a/packages/project/src/shared/tree-utils/find-parent-instance.test.ts
+++ b/packages/project/src/shared/tree-utils/find-parent-instance.test.ts
@@ -5,16 +5,19 @@ import { findParentInstance } from "./find-parent-instance";
describe("Find parent instance", () => {
test("find", () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
diff --git a/packages/project/src/shared/tree-utils/find-parent-instance.ts b/packages/project/src/shared/tree-utils/find-parent-instance.ts
index 6f164a87ab..f5e4303403 100644
--- a/packages/project/src/shared/tree-utils/find-parent-instance.ts
+++ b/packages/project/src/shared/tree-utils/find-parent-instance.ts
@@ -12,7 +12,7 @@ export const findParentInstance = (
return parentInstance;
}
for (const child of childInstance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
const foundInstance = find(child, childInstance);
diff --git a/packages/project/src/shared/tree-utils/get-css-rules.test.ts b/packages/project/src/shared/tree-utils/get-css-rules.test.ts
index 561cb6f5c1..fde45dbd5a 100644
--- a/packages/project/src/shared/tree-utils/get-css-rules.test.ts
+++ b/packages/project/src/shared/tree-utils/get-css-rules.test.ts
@@ -5,6 +5,7 @@ import { getCssRules } from "./get-css-rules";
describe("Get all cssRules from an instance including children", () => {
test("getCssRules", () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [
@@ -15,6 +16,7 @@ describe("Get all cssRules from an instance including children", () => {
],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [
@@ -25,6 +27,7 @@ describe("Get all cssRules from an instance including children", () => {
],
children: [
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [
diff --git a/packages/project/src/shared/tree-utils/get-css-rules.ts b/packages/project/src/shared/tree-utils/get-css-rules.ts
index ece0f398f9..1671ed2b3a 100644
--- a/packages/project/src/shared/tree-utils/get-css-rules.ts
+++ b/packages/project/src/shared/tree-utils/get-css-rules.ts
@@ -9,7 +9,7 @@ const aggregateCssRules = (
result.push([instance.id, cssRule]);
}
for (const child of instance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
aggregateCssRules(child, result);
diff --git a/packages/project/src/shared/tree-utils/get-instance-path.test.ts b/packages/project/src/shared/tree-utils/get-instance-path.test.ts
index d0f505b7ff..9f6dccd616 100644
--- a/packages/project/src/shared/tree-utils/get-instance-path.test.ts
+++ b/packages/project/src/shared/tree-utils/get-instance-path.test.ts
@@ -10,16 +10,19 @@ const getIds = (array: Array<{ id: string }>) => array.map((item) => item.id);
describe("Get instance path", () => {
test("single possible path", () => {
const instance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
@@ -35,16 +38,19 @@ describe("Get instance path", () => {
test("two possible paths", () => {
const instance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2a",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3a",
cssRules: [],
@@ -53,11 +59,13 @@ describe("Get instance path", () => {
],
},
{
+ type: "instance",
component: "Box",
id: "2b",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3b",
cssRules: [],
@@ -81,16 +89,19 @@ const getIdAndPosition = (
describe("Get instance path with positions", () => {
test("single possible path", () => {
const instance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
@@ -110,16 +121,19 @@ describe("Get instance path with positions", () => {
test("two possible paths", () => {
const instance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2a",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3a",
cssRules: [],
@@ -128,11 +142,13 @@ describe("Get instance path with positions", () => {
],
},
{
+ type: "instance",
component: "Box",
id: "2b",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "3b",
cssRules: [],
diff --git a/packages/project/src/shared/tree-utils/get-instance-path.ts b/packages/project/src/shared/tree-utils/get-instance-path.ts
index 065688cb16..9ba6542fcf 100644
--- a/packages/project/src/shared/tree-utils/get-instance-path.ts
+++ b/packages/project/src/shared/tree-utils/get-instance-path.ts
@@ -11,7 +11,7 @@ export const getInstancePath = (
return true;
}
for (const child of instance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
const found = find(child);
@@ -41,7 +41,7 @@ export const getInstancePathWithPositions = (
}
for (let index = 0; index < instance.children.length; index++) {
const child = instance.children[index];
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
const found = find(child);
diff --git a/packages/project/src/shared/tree-utils/insert-instance.test.ts b/packages/project/src/shared/tree-utils/insert-instance.test.ts
index 1d52e7bc4b..28f0f7f8b2 100644
--- a/packages/project/src/shared/tree-utils/insert-instance.test.ts
+++ b/packages/project/src/shared/tree-utils/insert-instance.test.ts
@@ -8,11 +8,13 @@ const instance = createInstance({ id: "3", component: "Box" });
describe("Insert instance", () => {
test(`inside a nested div using "end": div 1 > div 2 > [div 3]`, () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
@@ -31,11 +33,13 @@ describe("Insert instance", () => {
test("after a nested div using index: div 1 > div 2, [div 3]", () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
@@ -54,11 +58,13 @@ describe("Insert instance", () => {
test("before a nested div using index: div 1 > [div 3], div 2", () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
@@ -76,11 +82,13 @@ describe("Insert instance", () => {
test(`after a nested div using "end": div 1 > div 2, [div 3]`, () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
diff --git a/packages/project/src/shared/tree-utils/insert-instance.ts b/packages/project/src/shared/tree-utils/insert-instance.ts
index 1b88126223..5f02867c99 100644
--- a/packages/project/src/shared/tree-utils/insert-instance.ts
+++ b/packages/project/src/shared/tree-utils/insert-instance.ts
@@ -12,7 +12,7 @@ export const insertInstanceMutable = (
): boolean => {
if (spec.parentId !== rootInstance.id) {
for (const child of rootInstance.children) {
- if (typeof child === "string") {
+ if (child.type === "text") {
continue;
}
const hasInserted = insertInstanceMutable(child, instance, spec);
diff --git a/packages/project/src/shared/tree-utils/reparent-instance.test.ts b/packages/project/src/shared/tree-utils/reparent-instance.test.ts
index dd54115b7f..e016d4848d 100644
--- a/packages/project/src/shared/tree-utils/reparent-instance.test.ts
+++ b/packages/project/src/shared/tree-utils/reparent-instance.test.ts
@@ -5,22 +5,26 @@ import { reparentInstanceMutable } from "./reparent-instance";
describe("Reparent instance", () => {
test(`new parent`, () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
children: [],
},
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "4",
cssRules: [],
@@ -37,23 +41,27 @@ describe("Reparent instance", () => {
test(`same parent different position`, () => {
const rootInstance: Instance = {
+ type: "instance",
component: "Box",
id: "1",
cssRules: [],
children: [
{
+ type: "instance",
component: "Box",
id: "2",
cssRules: [],
children: [],
},
{
+ type: "instance",
component: "Box",
id: "3",
cssRules: [],
children: [],
},
{
+ type: "instance",
component: "Box",
id: "4",
cssRules: [],
diff --git a/packages/project/src/shared/tree-utils/reparent-instance.ts b/packages/project/src/shared/tree-utils/reparent-instance.ts
index 44345f8aba..6a08ec0e06 100644
--- a/packages/project/src/shared/tree-utils/reparent-instance.ts
+++ b/packages/project/src/shared/tree-utils/reparent-instance.ts
@@ -30,7 +30,7 @@ export const reparentInstanceMutable = (
const currentParent = findParentInstance(rootInstance, instance.id);
if (currentParent !== undefined && currentParent.id === newParentId) {
const currentPosition = currentParent.children.findIndex(
- (child) => typeof child !== "string" && child.id === instance.id
+ (child) => child.type === "instance" && child.id === instance.id
);
if (currentPosition < newPosition) {
newPositionAdjusted--;
diff --git a/packages/project/src/shared/tree-utils/set-instance-children.ts b/packages/project/src/shared/tree-utils/set-instance-children.ts
index 8cedd02a96..cb7a1fa34d 100644
--- a/packages/project/src/shared/tree-utils/set-instance-children.ts
+++ b/packages/project/src/shared/tree-utils/set-instance-children.ts
@@ -1,10 +1,10 @@
import type { Breakpoint } from "@webstudio-is/css-data";
-import type { ChildrenUpdates, Instance } from "@webstudio-is/react-sdk";
+import type { ChildrenUpdates, Instance, Text } from "@webstudio-is/react-sdk";
import { createInstance, createInstanceId } from "./create-instance";
import { findInstanceById } from "./find-instance";
import { populateInstance } from "./populate";
-type InstanceChild = Instance | string;
+type InstanceChild = Instance | Text;
const hydrateTree = (
parent: Instance,
@@ -14,7 +14,7 @@ const hydrateTree = (
const children: InstanceChild[] = [];
for (const update of updates) {
// Set a string as a child
- if (typeof update === "string") {
+ if (update.type === "text") {
children.push(update);
continue;
}
diff --git a/packages/react-sdk/src/db/instance.ts b/packages/react-sdk/src/db/instance.ts
index f901149477..ccbf989b83 100644
--- a/packages/react-sdk/src/db/instance.ts
+++ b/packages/react-sdk/src/db/instance.ts
@@ -10,8 +10,14 @@ export type BaseInstance = {
cssRules: Array<CssRule>;
};
+export type Text = {
+ type: "text";
+ value: string;
+};
+
export type Instance = BaseInstance & {
- children: Array<Instance | string>;
+ type: "instance";
+ children: Array<Instance | Text>;
};
export const toBaseInstance = (instance: Instance): BaseInstance => {
@@ -22,12 +28,20 @@ export const toBaseInstance = (instance: Instance): BaseInstance => {
};
};
+export const Text = z.lazy(() =>
+ z.object({
+ type: z.literal("text"),
+ value: z.string(),
+ })
+);
+
export const Instance = z.lazy(
() =>
z.object({
+ type: z.literal("instance"),
id: z.string(),
component: z.string(),
- children: z.array(z.union([Instance, z.string()])),
+ children: z.array(z.union([Instance, Text])),
cssRules: z.array(CssRule),
})
// @todo can't figure out how to make component to be z.enum(Object.keys(components))
diff --git a/packages/react-sdk/src/tree/create-elements-tree.tsx b/packages/react-sdk/src/tree/create-elements-tree.tsx
index e5b655dcc4..8eead836e0 100644
--- a/packages/react-sdk/src/tree/create-elements-tree.tsx
+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx
@@ -5,8 +5,12 @@ import { Scripts, ScrollRestoration } from "@remix-run/react";
import { SessionStoragePolyfill } from "./session-storage-polyfill";
export type ChildrenUpdates = Array<
- | string
| {
+ type: "text";
+ value: string;
+ }
+ | {
+ type: "instance";
id: undefined | Instance["id"];
component: Instance["component"];
children: ChildrenUpdates;
@@ -60,8 +64,8 @@ const createInstanceChildrenElements = ({
}) => {
const elements = [];
for (const child of children) {
- if (typeof child === "string") {
- elements.push(child);
+ if (child.type === "text") {
+ elements.push(child.value);
continue;
}
const children = createInstanceChildrenElements({