Javascript to dom - Blockly

896 views Asked by At

I am building a simple editor using Blockly. User can save generated javascript to server and can download as 'txt' file. User can also import the notepad file to editor (notepad file contains generated javascript). How to build blocks from the javascript. There is a method for xml to dom ('domToWorkspace') conversation but the same is not available for javascript. Is there any function to convert or am I missing something?

1

There are 1 answers

0
Lukas Köhl On BEST ANSWER

workspaceToDom is correct in javascript and will give you the xml structure. This you need to parse into text with domToText.

       function saveWorkspace() {
            var xml = Blockly.Xml.workspaceToDom(workspace);
            var xmlString = Blockly.Xml.domToText(xml);

            save('workspace.txt', xmlString);
        }

save() puts the data in a txt file and downloads it.

On the other way around you need to use textToDom and have the xml structure. This you need to convert in domToWorkspace and everything is done.

       var xml = Blockly.Xml.textToDom(input);
       Blockly.Xml.domToWorkspace(xml,workspace);