mix
mix

Reputation: 178

mxCodec doesn't decode xml correctly

I integrated mxGraph(mxgraph npm package) in my react app, so when I trying to load graph using xml, I am getting an error in console

TypeError: geo.clone is not a function

The same I am doing in single html file and it's working.

I investigated and found that the mxCell in react app is different from the html one.

In case of HTML there is filled geometry prop instead of react(check screens below)

Сan someone help me to decode xml correctly?

Decoded mxCell from single HTML console: https://monosnap.com/file/yAHAi29zFGFpauqU2RtDcvmfPpZ0YJ

Decoded mxCell from React app console: https://monosnap.com/file/0XxPwyEracX7hMCnMHckAmI8Rl6OEh

Source code from React component:

const graph = new mx.mxGraph(this.automationRef.current)
new mx.mxRubberband(graph);

const xml = '<root>...</root>';
const doc = mx.mxUtils.parseXml(xml);
const codec = new mxCodec(doc);
let elt = doc.documentElement.firstChild;
const cells = [];
while (elt != null){
  const cell = codec.decodeCell(elt)
  cells.push(cell);
  graph.refresh();
  elt = elt.nextSibling;
}

graph.addCells(cells);

Upvotes: 1

Views: 534

Answers (1)

mix
mix

Reputation: 178

Found the issue. Here's the solution:

https://github.com/jgraph/mxgraph/issues/301#issuecomment-514284868

Quote:

you should add

window['mxGraphModel'] = mxGraphModel;
window['mxGeometry'] = mxGeometry;

before

let doc = mxUtils.parseXml(xml);
let codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());'

I found that the decode method resolve xml need windows param

End quote

Upvotes: 1

Related Questions