partridgejiang / Kekule.js

A Javascript cheminformatics toolkit.
http://partridgejiang.github.io/Kekule.js
MIT License
250 stars 61 forks source link

How to get embed Json format ? #72

Closed mpomet closed 6 years ago

mpomet commented 6 years ago

Hello, I'm looking for a converter to correctly load data from json. Here's an example.

// Init
var chemEditor = new Kekule.Editor.ChemSpaceEditor(doc, null, Kekule.Render.RendererType.R2D);
var chemComposer = new Kekule.Editor.Composer(elem, chemEditor);
...
// Retreive
var molObj = chemComposer.exportObj(Kekule.StructureFragment);
var molJson = // How to get the Json object correctly ?

// Display
var emmbedMolView = '<span style="display:block;border:1px solid black;width:300px;height:300px" data-widget="Kekule.ChemWidget.Viewer2D" data-chem-obj="' + molJson + '" data-predefined-setting="basic" data-caption="Molecule-X"></span>';
...

There must be a method for this but didn't manage to find it. I tried with JSON.stringify but it leads to circular error. Same thing with chemComposer.saveObj(molObj, 'json'); I don't want to use script tag nor file as ressources.

Thanks,

EDIT: Finally found it. var molJson = Kekule.IO.saveFormatData(molObj, 'Kekule-JSON'); This line really should be in http://partridgejiang.github.io/Kekule.js/documents/tutorial/content/molIO.html#saving-molecule