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.

+

Roman Pominov4320b9c774b9
diff --git a/apps/designer/app/dnd-prototype/main.tsx b/apps/designer/app/dnd-prototype/main.tsx
index cbb42c2655..cd20000b86 100644
--- a/apps/designer/app/dnd-prototype/main.tsx
+++ b/apps/designer/app/dnd-prototype/main.tsx
@@ -1,4 +1,4 @@
-import { CSSProperties, useRef } from "react";
+import { CSSProperties, useRef, useState } from "react";
type componentType = "div" | "p" | "ul" | "li" | "span";
@@ -53,6 +53,18 @@ const CanvasNode = ({ item }: { item: Item }) => {
}
};
+const canInsert = (item: Item, destanation: Item) => {
+ if (destanation.type === "ul") {
+ return item.type === "li";
+ }
+
+ if (destanation.type === "p") {
+ return false;
+ }
+
+ return typeof destanation.content !== "string";
+};
+
const findId = (
domNode: EventTarget,
root: HTMLElement | null
@@ -74,14 +86,116 @@ const findId = (
return findId(paretn, root);
};
+const findElement = (
+ root: HTMLElement | null,
+ id: string
+): HTMLElement | null => {
+ if (root === null) {
+ return null;
+ }
+
+ if (root.dataset.id === id) {
+ return root;
+ }
+
+ const children = [...root.children];
+
+ for (const child of children) {
+ const result = findElement(child as HTMLElement, id);
+
+ if (result != null) {
+ return result;
+ }
+ }
+
+ return null;
+};
+
+type InsertLocation =
+ | { type: "vertical"; minX: number; maxX: number; y: number }
+ | { type: "horizontal"; minY: number; maxY: number; x: number };
+
export const Canvas = ({ data }: CanvasProps) => {
let rootRef = useRef(null);
+
+ let [moveState, setMoveState] = useState<{
+ initialPointerPosition: { x: number; y: number };
+ initialRect: DOMRect;
+ id: string;
+ } | null>(null);
+
+ let insertLocation: InsertLocation | null = {
+ type: "vertical",
+ minX: 0,
+ maxX: 100,
+ y: 0,
+ } as InsertLocation;
+
return (
<div
ref={rootRef}
- onPointerDown={(ev) => console.log(findId(ev.target, rootRef.current))}
+ style={{ position: "relative" }}
+ onPointerDown={(ev) => {
+ const id = findId(ev.target, rootRef.current);
+
+ if (id == null) {
+ return;
+ }
+
+ const element = findElement(rootRef.current, id);
+
+ if (element == null) {
+ return;
+ }
+
+ setMoveState({
+ id,
+ initialRect: element.getBoundingClientRect(),
+ initialPointerPosition: { x: ev.clientX, y: ev.clientY },
+ });
+ }}
+ onPointerMove={(ev) => {
+ if (moveState != null) {
+ console.log(findId(ev.target, rootRef.current));
+ }
+ }}
+ onPointerUp={(ev) => setMoveState(null)}
>
<CanvasNode item={data} />
+ {insertLocation !== null && (
+ <div
+ style={{
+ position: "absolute",
+ left: "0",
+ top: "0",
+ width: "100%",
+ height: "100%",
+ pointerEvents: "none",
+ }}
+ >
+ <div
+ style={
+ insertLocation.type === "vertical"
+ ? {
+ position: "absolute",
+ top: insertLocation.y,
+ left: insertLocation.minX,
+ width: insertLocation.maxX - insertLocation.minX,
+ height: "1px",
+ backgroundColor: "lightblue",
+ }
+ : {
+ position: "absolute",
+ left: insertLocation.x,
+ top: insertLocation.minY,
+ height: insertLocation.maxY - insertLocation.minY,
+ width: "1px",
+ backgroundColor: "lightblue",
+ }
+ }
+ />
+ </div>
+ )}
</div>
);
};