Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

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

Closed
nbotalov opened this issue Feb 8, 2017 · 4 comments
Closed

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

nbotalov opened this issue Feb 8, 2017 · 4 comments

Comments

@nbotalov
Copy link

nbotalov commented Feb 8, 2017

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

@tanguy2m
Copy link

tanguy2m commented Feb 20, 2017

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]

@davidjgraph
Copy link
Contributor

Is this resolved now that we publish an NPM version?

@NicCOConnor
Copy link
Contributor

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

@stalkerg
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants