Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot remove all nodes/ add multiple nodes #168

Open
adelghaenian opened this issue Aug 25, 2021 · 5 comments
Open

Cannot remove all nodes/ add multiple nodes #168

adelghaenian opened this issue Aug 25, 2021 · 5 comments

Comments

@adelghaenian
Copy link

It is functional only if we want to add/remove a single node from the diagram. However, when I want to add multiple nodes at once using a for loop, it adds some of them (not all of them). The same thing happens for removing nodes.

image

image

@SeanMengis
Copy link

I had the same problem, but instead of not having enough nodes i had too many. I did something like that:

console.log(schema.nodes.length);

addNode(node1);
addNode(node2);
addNode(node3);
addNode(node4);

console.log(schema.nodes.length);

And output would say something like:

0
7

Sadly I haven't found a real solution. What I'm doing right now is to make my own custom addFunctions where I copy the previous schema modify it and then replace the old one.

Really eager to hear any other solutions.

@adelghaenian
Copy link
Author

adelghaenian commented Aug 26, 2021

Thanks for the reply. But how do you change the schema? When I want to change the nodes like:
schema.nodes = [...schema.nodes, theNewNode]
the nodes of the schema will change but there is no change in the diagram.

@SeanMengis
Copy link

I have the schema in a React.useState const [schema, setSchema = useState(initialSchema)]

function addNewNode(schema: DiagramSchema<unknown>) { // I use Typescript
  const updatedNodes: Node<unknown>[] = [];
  const updatedLinks: Link[] = [];

  schema.nodes.forEach(node => {
    // Copy the node information and adjust whatever needs to be adjusted
  })

  // Do the same for the links

  // Add the new node
  updatedNodes.push({/*Node configuration*/})

  // Create a new schema
  const updatedSchema = createSchema({
     nodes: updatedNodes,
     links: updatedLinks
  })

  // Set state, I first set to null and "await" and then put the new schema. If you don't set it to null first your links don't get 
  // redrawn. It's really ugly and it's on my TODO list to find another solution for that. 
  setSchema(null); 
  setTimeout(() => {
     setSchema(updatedSchema);
   }, 1)
}

Also another problem I currently have with this library is, when you customize the render method of the nodes and pass the schema or other functions down with props, some references are not properly passed down. So I used useContext to save the state externally. Just in case you have the same problem.

I'm still working on this graph and havn't finished it yet. So if I find any solutions and better ways on how to solve them I'll post them here :)

@zihanxu3
Copy link

Same here - has anyone found a solution yet? Please kindly advise, thanks!

@SeanMengis
Copy link

Same here - has anyone found a solution yet? Please kindly advise, thanks!

I think the project is either temporarily paused or dead... The answer I posted works but is not nice. I think you'll either have to fix it yourself and make a PR (and send a message to the maintainer so he merges it) or look for another library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants