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.

prototype template

Roman Pominovc0b68840da79
diff --git a/apps/designer/app/dnd-prototype/main.stories.tsx b/apps/designer/app/dnd-prototype/main.stories.tsx
new file mode 100644
index 0000000000..6e2e91e16f
--- /dev/null
+++ b/apps/designer/app/dnd-prototype/main.stories.tsx
@@ -0,0 +1,108 @@
+import { ComponentStory, ComponentMeta } from "@storybook/react";
+import { Canvas, Item } from "./main";
+
+export default {
+ component: Canvas,
+} as ComponentMeta<typeof Canvas>;
+
+const tree = {
+ id: "root",
+ type: "div",
+ style: {},
+ content: [
+ {
+ id: "1",
+ type: "ul",
+ style: {},
+ content: [
+ { id: "1.1", type: "li", content: "Foo" },
+ { id: "1.2", type: "li", content: "Bar" },
+ {
+ id: "1.3",
+ type: "li",
+ content: [
+ {
+ id: "1.3.1",
+ type: "div",
+ style: { background: "red" },
+ content: "Baz",
+ },
+ ],
+ },
+ ],
+ },
+ {
+ id: "2",
+ type: "div",
+ style: { display: "flex" },
+ content: [
+ {
+ id: "2.1",
+ type: "div",
+ style: { width: "100px", height: "100px", background: "red" },
+ content: [],
+ },
+ {
+ id: "2.2",
+ type: "div",
+ style: { width: "100px", height: "100px", background: "green" },
+ content: [],
+ },
+ {
+ id: "2.3",
+ type: "div",
+ style: { width: "100px", height: "100px", background: "blue" },
+ content: [],
+ },
+ ],
+ },
+ {
+ id: "3",
+ type: "div",
+ style: {},
+ content: [
+ {
+ id: "3.1",
+ type: "div",
+ style: { height: "100px", background: "red" },
+ content: [],
+ },
+ {
+ id: "3.2",
+ type: "div",
+ style: { display: "flex", padding: "20px", background: "green" },
+ content: [
+ {
+ id: "3.2.1",
+ type: "div",
+ style: { width: "100px", height: "100px", background: "red" },
+ content: [],
+ },
+ {
+ id: "3.2.2",
+ type: "div",
+ style: { width: "100px", height: "100px", background: "yellow" },
+ content: [],
+ },
+ {
+ id: "3.2.3",
+ type: "div",
+ style: { width: "100px", height: "100px", background: "blue" },
+ content: [],
+ },
+ ],
+ },
+ {
+ id: "3.3",
+ type: "div",
+ style: { height: "100px", background: "blue" },
+ content: [],
+ },
+ ],
+ },
+ ],
+} as Item;
+
+export const Main: ComponentStory<typeof Canvas> = () => {
+ return <Canvas data={tree} />;
+};
diff --git a/apps/designer/app/dnd-prototype/main.tsx b/apps/designer/app/dnd-prototype/main.tsx
new file mode 100644
index 0000000000..cbb42c2655
--- /dev/null
+++ b/apps/designer/app/dnd-prototype/main.tsx
@@ -0,0 +1,87 @@
+import { CSSProperties, useRef } from "react";
+
+type componentType = "div" | "p" | "ul" | "li" | "span";
+
+export type Item = {
+ id: string;
+ type: componentType;
+ style: CSSProperties;
+ content: Item[] | string;
+};
+
+type CanvasProps = {
+ data: Item;
+};
+
+const CanvasNode = ({ item }: { item: Item }) => {
+ const children =
+ typeof item.content === "string"
+ ? item.content
+ : item.content.map((child) => <CanvasNode item={child} key={child.id} />);
+
+ switch (item.type) {
+ case "div":
+ return (
+ <div style={item.style} data-id={item.id}>
+ {children}
+ </div>
+ );
+ case "p":
+ return (
+ <p style={item.style} data-id={item.id}>
+ {children}
+ </p>
+ );
+ case "ul":
+ return (
+ <ul style={item.style} data-id={item.id}>
+ {children}
+ </ul>
+ );
+ case "li":
+ return (
+ <li style={item.style} data-id={item.id}>
+ <div>{children}</div>
+ </li>
+ );
+ case "span":
+ return (
+ <span style={item.style} data-id={item.id}>
+ {children}
+ </span>
+ );
+ }
+};
+
+const findId = (
+ domNode: EventTarget,
+ root: HTMLElement | null
+): string | null => {
+ if (!(domNode instanceof HTMLElement)) {
+ return null;
+ }
+
+ if (domNode.dataset.id != null) {
+ return domNode.dataset.id;
+ }
+
+ let paretn = domNode.parentElement;
+
+ if (paretn === root || paretn === null) {
+ return null;
+ }
+
+ return findId(paretn, root);
+};
+
+export const Canvas = ({ data }: CanvasProps) => {
+ let rootRef = useRef(null);
+ return (
+ <div
+ ref={rootRef}
+ onPointerDown={(ev) => console.log(findId(ev.target, rootRef.current))}
+ >
+ <CanvasNode item={data} />
+ </div>
+ );
+};