popUp logic implemneted

This commit is contained in:
anovazzi1 2023-02-10 11:26:01 -03:00
commit 7d0f700ef7
5 changed files with 47 additions and 14 deletions

View file

@ -1,10 +1,13 @@
import Flow from "./flow"; import Flow from "./flow";
import "./App.css"; import "./App.css";
import PopUpProvider from "./context/popUpContext";
function App() { function App() {
return ( return (
<PopUpProvider>
<div className="w-screen h-screen"> <div className="w-screen h-screen">
<Flow></Flow> <Flow></Flow>
</div> </div>
</PopUpProvider>
); );
} }

View file

@ -1,10 +1,13 @@
import { Handle, Position } from "reactflow"; import { Handle, Position } from "reactflow";
import { useContext } from "react";
import { PopUpContext } from "../../context/popUpContext";
export default function PromptNode({ data }) { export default function PromptNode({ data }) {
console.log(data) const {openPopUp} = useContext(PopUpContext)
return ( return (
<div <div
onClick={()=>data.onClick()} onClick={()=>openPopUp(<div className="absolute top-1/2 left-1/2">teste</div>)}
className="prompt-Node relative bg-white h-16 w-40 border rounded-sm solid border-black flex flex-col justify-center" className="prompt-Node relative bg-white h-16 w-40 border rounded-sm solid border-black flex flex-col justify-center"
> >
<Handle type="target" position={Position.Left}></Handle> <Handle type="target" position={Position.Left}></Handle>

View file

@ -9,7 +9,7 @@ export function Sidebar(){
} }
return( return(
<div className="h-full w-48 bg-slate-200 absolute z-10 flex flex-col"> <div className="h-full w-48 bg-slate-200 flex flex-col">
<div className="w-full text-center border border-black cursor-grab" onDragStart={(event)=>onDragStart(event,'promptNode')}> prompt Node</div> <div className="w-full text-center border border-black cursor-grab" onDragStart={(event)=>onDragStart(event,'promptNode')}> prompt Node</div>
</div> </div>
) )

View file

@ -0,0 +1,32 @@
import { createContext } from "react";
import React, { useState } from 'react';
export const PopUpContext = createContext({
openPopUp: (popUpElement: JSX.Element) => {},
closePopUp: () => {}
});
interface PopUpProviderProps {
children: React.ReactNode;
}
const PopUpProvider = ({ children }: PopUpProviderProps) => {
const [popUpElement, setPopUpElement] = useState<JSX.Element | null>(null);
const openPopUp = (element: JSX.Element) => {
setPopUpElement(element);
};
const closePopUp = () => {
setPopUpElement(null);
};
return (
<PopUpContext.Provider value={{ openPopUp, closePopUp }}>
{children}
{popUpElement}
</PopUpContext.Provider>
);
};
export default PopUpProvider;

View file

@ -21,9 +21,6 @@ export default function Flow(){
const reactFlowWrapper = useRef(null) const reactFlowWrapper = useRef(null)
const [nodes,setNodes,onNodesChange] = useNodesState([]) const [nodes,setNodes,onNodesChange] = useNodesState([])
const [edges,setEdges, onEdgesChange] = useEdgesState([]) const [edges,setEdges, onEdgesChange] = useEdgesState([])
//allow iteractive with the react flow
// const onNodesChange = useCallback( (changes) => setNodes((nds) => applyNodeChanges(changes, nds)),[] );
// const onEdgesChange = useCallback( (changes) => setEdges((eds) => applyEdgeChanges(changes, eds)),[] );
const [reactFlowInstance,setReactFlowInstance] = useState(null) const [reactFlowInstance,setReactFlowInstance] = useState(null)
const onConnect = useCallback((params)=>setEdges((eds)=>addEdge(params,eds)),[]) const onConnect = useCallback((params)=>setEdges((eds)=>addEdge(params,eds)),[])
const onDragOver = useCallback((event) => { const onDragOver = useCallback((event) => {
@ -36,9 +33,7 @@ export default function Flow(){
const reactflowBounds = reactFlowWrapper.current.getBoundingClientRect(); const reactflowBounds = reactFlowWrapper.current.getBoundingClientRect();
const type = event.dataTransfer.getData('application/reactflow'); const type = event.dataTransfer.getData('application/reactflow');
let json = JSON.parse(event.dataTransfer.getData('json')) let data = JSON.parse(event.dataTransfer.getData('json'))
const data = {...json}
data.onClick = console.log("clicked")
// check if the dropped element is valid // check if the dropped element is valid
if (typeof type === 'undefined' || !type) { if (typeof type === 'undefined' || !type) {
return; return;
@ -59,10 +54,10 @@ export default function Flow(){
return ( return (
//need parent component with width and height //need parent component with width and height
<div className='w-full h-full'> <div className='w-full h-full flex flex-row'>
<ReactFlowProvider> <ReactFlowProvider>
<Sidebar/> <Sidebar/>
<div className='w-full h-full' ref={reactFlowWrapper}> <div className='w-screen h-full' ref={reactFlowWrapper}>
<ReactFlow <ReactFlow
nodes={nodes} nodes={nodes}
edges={edges} edges={edges}