0

I'm trying to clone an Vertex object in mxgraph, to edit the coordinates without changing the original Vertex, but I get the Error "Uncaught TypeError: terminal.insertEdge is not a function" in the console and none of the vertices, from that point on, render.

I tried different Cloning Methods like:

vertexClone = structuredClone(vertex); vertexClone = JSON.parse(JSON.stringify(vertex)); vertexClone = Object.assign({}, vertex); vertexClone = { ...vertex}; 

But when further using the VertexClone object, I get the TypeError in my console.

6
  • A structuredClone does not duplicate prototypes! Neither does Object.assign, or any of the methods you provided. JSON cannot serialise methods (and therefor prototypes), assign only does it for own properties and destructuring does the same thing. In order to this this you will have to reconstruct it or manually make a cloning method. Commented May 23, 2024 at 12:27
  • @somethinghere When I try cloning the prototype and copying the properties into my new object, then my edges are gone and my vertices aren't connected to anything at all anymore. Commented May 23, 2024 at 12:58
  • I'm not familiar with the framework you are using, but if I had to make a guess is that it's not just the object that makes things happen - your vertex is probably in other structures, and creating a clone does not change anything about that. These kind of systems (I'm more of a threeJS user) have lot of things that depend on each other knowing about it, and you cant just snap a piece out, clone it and expect the system to know how it fits in. Commented May 23, 2024 at 13:11
  • 1
    By also cloning all edges and readjusting the graph after it works for me now, thanks for the help Commented May 23, 2024 at 13:43
  • Feel free to add an answer with all you learned and how you figured it out (if you are okay with AI eating it and spitting it back out. Cant blame you if you wouldn’t) Commented May 24, 2024 at 4:05

1 Answer 1

0

In the end the function I made looked like this:

function clonePrototype(vertex, graph) { let clone = Object.create(Object.getPrototypeOf(vertex)); Object.getOwnPropertyNames(vertex).forEach(prop => { if (prop !== "edges") { clone[prop] = vertex[prop]; } }); graph.addCell(clone); let edges = graph.getAllEdges([vertex]); for (let edge of edges) { graph.addEdge(edge, edge.parent, edge.source, clone, 0) } return clone; }

And it just made sure the old edges get connected to the new clone

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.