Added component drop on ReactFlow

This commit is contained in:
Lucas Oliveira 2023-12-04 15:55:07 -03:00
commit 7e12316022
3 changed files with 29 additions and 10 deletions

View file

@ -8,7 +8,13 @@ import {
useRef, useRef,
useState, useState,
} from "react"; } from "react";
import { Edge, Node, ReactFlowJsonObject, addEdge } from "reactflow"; import {
Edge,
Node,
ReactFlowJsonObject,
XYPosition,
addEdge,
} from "reactflow";
import ShortUniqueId from "short-unique-id"; import ShortUniqueId from "short-unique-id";
import { import {
deleteFlowFromDatabase, deleteFlowFromDatabase,
@ -305,10 +311,12 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
newProject, newProject,
file, file,
isComponent = false, isComponent = false,
position = { x: 10, y: 10 },
}: { }: {
newProject: boolean; newProject: boolean;
file?: File; file?: File;
isComponent?: boolean; isComponent?: boolean;
position?: XYPosition;
}): Promise<String | never> { }): Promise<String | never> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let id; let id;
@ -316,19 +324,20 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
let text = await file.text(); let text = await file.text();
let fileData = JSON.parse(text); let fileData = JSON.parse(text);
if ( if (
(!fileData.is_component && isComponent === true) || newProject &&
(fileData.is_component !== undefined && ((!fileData.is_component && isComponent === true) ||
fileData.is_component !== isComponent) (fileData.is_component !== undefined &&
fileData.is_component !== isComponent))
) { ) {
reject("You cannot upload a component as a flow or vice versa"); reject("You cannot upload a component as a flow or vice versa");
} else { } else {
if (fileData.flows) { if (fileData.flows) {
fileData.flows.forEach((flow: FlowType) => { fileData.flows.forEach((flow: FlowType) => {
id = addFlow(newProject, flow); id = addFlow(newProject, flow, undefined, position);
}); });
resolve(""); resolve("");
} else { } else {
id = await addFlow(newProject, fileData); id = await addFlow(newProject, fileData, undefined, position);
resolve(id); resolve(id);
} }
} }
@ -353,7 +362,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
) { ) {
reject("You cannot upload a component as a flow or vice versa"); reject("You cannot upload a component as a flow or vice versa");
} else { } else {
id = await addFlow(newProject, fileData); id = await addFlow(newProject, fileData, undefined, position);
resolve(id); resolve(id);
} }
} }
@ -510,7 +519,8 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
const addFlow = async ( const addFlow = async (
newProject: Boolean, newProject: Boolean,
flow?: FlowType, flow?: FlowType,
override?: boolean override?: boolean,
position?: XYPosition
): Promise<String | undefined> => { ): Promise<String | undefined> => {
if (newProject) { if (newProject) {
let flowData = flow let flowData = flow
@ -550,7 +560,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
} else { } else {
paste( paste(
{ nodes: flow!.data!.nodes, edges: flow!.data!.edges }, { nodes: flow!.data!.nodes, edges: flow!.data!.edges },
{ x: 10, y: 10 } position ?? { x: 10, y: 10 }
); );
} }
}; };

View file

@ -340,10 +340,15 @@ export default function Page({
} else if (event.dataTransfer.types.some((types) => types === "Files")) { } else if (event.dataTransfer.types.some((types) => types === "Files")) {
takeSnapshot(); takeSnapshot();
if (event.dataTransfer.files.item(0)!.type === "application/json") { if (event.dataTransfer.files.item(0)!.type === "application/json") {
const position = {
x: event.clientX,
y: event.clientY,
};
uploadFlow({ uploadFlow({
newProject: false, newProject: false,
isComponent: false, isComponent: false,
file: event.dataTransfer.files.item(0)!, file: event.dataTransfer.files.item(0)!,
position: position,
}).catch((error) => { }).catch((error) => {
setErrorData({ setErrorData({
title: "Error uploading file", title: "Error uploading file",

View file

@ -1,3 +1,4 @@
import { XYPosition } from "reactflow";
import { tweakType } from "../components"; import { tweakType } from "../components";
import { FlowType, NodeDataType } from "../flow"; import { FlowType, NodeDataType } from "../flow";
@ -11,7 +12,8 @@ export type FlowsContextType = {
addFlow: ( addFlow: (
newProject: boolean, newProject: boolean,
flow?: FlowType, flow?: FlowType,
override?: boolean override?: boolean,
position?: XYPosition
) => Promise<String | undefined>; ) => Promise<String | undefined>;
updateFlow: (newFlow: FlowType) => void; updateFlow: (newFlow: FlowType) => void;
incrementNodeId: () => string; incrementNodeId: () => string;
@ -28,10 +30,12 @@ export type FlowsContextType = {
newProject, newProject,
file, file,
isComponent, isComponent,
position,
}: { }: {
newProject: boolean; newProject: boolean;
file?: File; file?: File;
isComponent?: boolean; isComponent?: boolean;
position?: XYPosition;
}) => Promise<String | never>; }) => Promise<String | never>;
hardReset: () => void; hardReset: () => void;
getNodeId: (nodeType: string) => string; getNodeId: (nodeType: string) => string;