mxgraph icon indicating copy to clipboard operation
mxgraph copied to clipboard

Editor doesn't created when mxGraph is used as npm module

Open nbotalov opened this issue 9 years ago • 4 comments

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

nbotalov avatar Feb 08 '17 14:02 nbotalov

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]

tanguy2m avatar Feb 20 '17 16:02 tanguy2m

Is this resolved now that we publish an NPM version?

davidjgraph avatar May 12 '17 16:05 davidjgraph

I have updated to the latest npm version and I still need this workaround.

NicCOConnor avatar May 17 '17 12:05 NicCOConnor

I think we should fix this problem in the root - #112

stalkerg avatar Sep 26 '17 03:09 stalkerg