javascriptmxgraphcloning

Why does using structuredClone on an Object in mxGraph result in "Uncaught TypeError: terminal.insertEdge is not a function"


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.


Solution

  • 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