diff --git a/src/components/diagram/index.jsx b/src/components/diagram/index.jsx index 3e44fcb..c6fec29 100644 --- a/src/components/diagram/index.jsx +++ b/src/components/diagram/index.jsx @@ -1,39 +1,48 @@ import React, { useEffect, useRef } from "react"; import { dia, shapes } from "jointjs"; +import { useDrop } from "react-dnd"; function Diagram(props) { const canvas = useRef(null); - useEffect(() => { + const [{ isOver }, drop] = useDrop(() => ({ + accept: "CARD", + drop: (item, monitor) => { + const offset = monitor.getClientOffset(); + const canvasRect = canvas.current.getBoundingClientRect(); + const x = offset.x - canvasRect.left - item.size.width * 0.5; + const y = offset.y - canvasRect.top - item.size.height * 0.5; + if (item.type === "rect") { + const rect = new shapes.standard.Rectangle(); + rect.position(x, y); + rect.resize(item.size.width, item.size.height); + rect.attr(item.attrs); + rect.addTo(props.graph); + props.setCode((prevCode) => `create table hi\n\n${prevCode}`); + } + }, + collect: (monitor) => ({ + isOver: !!monitor.isOver(), + }), + })); + useEffect(() => { new dia.Paper({ el: document.getElementById("canvas"), background: { color: "#aec3b0", }, model: props.graph, - height: "100%", width: "100%", gridSize: 1, interactive: true, }); - - const rect = new shapes.standard.Rectangle(); - rect.position(100, 100); - rect.resize(100, 40); - rect.attr({ - body: { - fill: "#7039FF", - }, - label: { - text: "hi", - fill: "white", - }, - }); - rect.addTo(props.graph); }, [props.graph]); - return
; + return ( +
+
+
+ ); } - export default Diagram; diff --git a/src/editor/index.jsx b/src/editor/index.jsx index 2a237fb..b39cf57 100644 --- a/src/editor/index.jsx +++ b/src/editor/index.jsx @@ -1,5 +1,5 @@ -import { React, useState, useRef, useEffect, useMemo } from "react"; -// import Diagram from "../components/diagram"; +import { React, useState, useEffect, useMemo } from "react"; +import Diagram from "../components/diagram"; import Header from "../components/header"; import Sidebar from "../components/sidebar"; import { ResizableBox } from "react-resizable"; @@ -26,156 +26,117 @@ const myTheme = createTheme({ { tag: t.tagName, color: "#008a02" }, ], }); - const Shape = () => { + const rectData = { + type: "rect", + position: { x: 100, y: 100 }, + size: { width: 100, height: 40 }, + attrs: { + body: { + fill: "#7039FF", + }, + label: { + text: "hi", + fill: "white", + }, + }, + }; + const [{ isDragging }, drag] = useDrag(() => ({ type: "CARD", + item: rectData, collect: (monitor) => ({ isDragging: !!monitor.isDragging(), }), })); - // const canvas = useRef(null); - - // useEffect(() => { - // const graph = new dia.Graph(); - // new dia.Paper({ - // el: document.getElementById("canvas"), - // background: { - // color: "#fec3b0", - // }, - // model: graph, - // height: "100%", - // width: "100%", - // gridSize: 1, - // interactive: true, - // }); - - // const rect = new shapes.standard.Rectangle(); - // rect.position(100, 100); - // rect.resize(100, 40); - // rect.attr({ - // body: { - // fill: "#9039FF", - // }, - // label: { - // text: "hi", - // fill: "white", - // }, - // }); - // rect.addTo(graph); - // }); - return ( - <> -
- - hi -
- {/*
*/} -
- +
+ rect +
); }; -function Diagram(props) { - const canvas = useRef(null); +const Window = (props) => { + const [editor, setEditor] = useState(true); - useEffect(() => { - new dia.Paper({ - el: document.getElementById("canvas"), - background: { - color: "#aec3b0", - }, - model: props.graph, - height: "100%", - width: "100%", - gridSize: 1, - interactive: true, - }); - }); - - return
; -} + return ( + +
+ +
+ + {editor ? ( + {}} + /> + ) : ( + + )} +
+
+ ); +}; export default function Editor(props) { const graph = useMemo(() => new dia.Graph(), []); - const [editor, setEditor] = useState(true); const [code, setCode] = useState(""); useEffect(() => {}, [graph]); + return ( <>
- -
- -
- - {editor ? ( - { - console.log("value:", value); - }} - /> - ) : ( -
- - - -
- )} -
-
-
- -
+ + + +