Editor doesn't created when mxGraph is used as npm module
I use latest mxGraph version (3.7.0.0) with Webpack2
when trying to create editor
const mx = require('imports?mxBasePath=>"PATH/mxGraph"!mxgraph-js');
...
const node = mx.mxUtils.load(config).getDocumentElement();
const editor = new mx.mxEditor(node);
nothing happens - no graph displayed and no errors in console.
After spending some time founded a problem in function
mxCodec.prototype.decode = function (node, into) {
..
try {
ctor = window[node.nodeName];
}
..
};
mxGraph rely on global scope, but this does not working with modules. Used workaround for this
window['mxEditor'] = mx.mxEditor;
window['mxGeometry'] = mx.mxGeometry;
window['mxDefaultKeyHandler'] = mx.mxDefaultKeyHandler;
window['mxDefaultPopupMenu'] = mx.mxDefaultPopupMenu;
window['mxGraph'] = mx.mxGraph;
window['mxStylesheet'] = mx.mxStylesheet;
window['mxDefaultToolbar'] = mx.mxDefaultToolbar;
window['mxGraphModel'] = mx.mxGraphModel;
const node = mx.mxUtils.load(config).getDocumentElement();
const editor = new mx.mxEditor(node);
but I think it should be fixed in library as well
Hi,
I faced the same issue when trying to integrate mxGraph in an Electron application. I used the same workaround as @nbotalov.
A quick and dirty modification would be to replace: ctor = window[node.nodeName];
by: ctor = eval(node.nodeName);
A safer one would be to keep a reference to these functions in an object:
mxCodecFunctions = {
mxEditor: mxEditor,
mxGeometry: mxGeometry
...
}
And then call: ctor = mxCodecFunctions[node.nodeName]
Is this resolved now that we publish an NPM version?
I have updated to the latest npm version and I still need this workaround.
I think we should fix this problem in the root - #112