Fixed nodes not deleting all at once

This commit is contained in:
Lucas Oliveira 2023-09-27 22:13:33 -03:00
commit a7a52755ec
3 changed files with 14 additions and 7 deletions

View file

@ -92,14 +92,22 @@ export function TypesProvider({ children }: { children: ReactNode }) {
} }
} }
function deleteNode(idx: string) { function deleteNode(idx: string | Array<string>) {
reactFlowInstance!.setNodes( reactFlowInstance!.setNodes(
reactFlowInstance!.getNodes().filter((node: Node) => node.id !== idx) reactFlowInstance!
.getNodes()
.filter((node: Node) =>
typeof idx === "string" ? node.id !== idx : !idx.includes(node.id)
)
); );
reactFlowInstance!.setEdges( reactFlowInstance!.setEdges(
reactFlowInstance! reactFlowInstance!
.getEdges() .getEdges()
.filter((edge) => edge.source !== idx && edge.target !== idx) .filter((edge) =>
typeof idx === "string"
? edge.source !== idx && edge.target !== idx
: !idx.includes(edge.source) && !idx.includes(edge.target)
)
); );
} }
return ( return (

View file

@ -120,9 +120,8 @@ export default function Page({
lastSelection lastSelection
) { ) {
event.preventDefault(); event.preventDefault();
lastSelection.nodes.forEach((k) => { console.log(lastSelection);
deleteNode(k.id); deleteNode(lastSelection.nodes.map((node) => node.id));
});
} }
} }
}; };

View file

@ -9,7 +9,7 @@ const data: { [char: string]: string } = {};
export type typesContextType = { export type typesContextType = {
reactFlowInstance: ReactFlowInstance | null; reactFlowInstance: ReactFlowInstance | null;
setReactFlowInstance: (newState: ReactFlowInstance) => void; setReactFlowInstance: (newState: ReactFlowInstance) => void;
deleteNode: (idx: string) => void; deleteNode: (idx: string | Array<string>) => void;
types: typeof types; types: typeof types;
setTypes: (newState: {}) => void; setTypes: (newState: {}) => void;
templates: typeof template; templates: typeof template;