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!.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!
.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 (

View file

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

View file

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