4

I would like to run blockly on Node.js and make the code being executed directly on the server (block by block execution included), without needing to save the XML first and then running it in background; I have tried some modules present on npm but none of them does what I want or it just run a pre-generated XML.

Any idea?

skarux
  • 131
  • 2
  • 7

2 Answers2

4

Here are the steps to do on the server:

1) Install xmldom module of node.js:

$ npm install xmldom

2) Clone google's Closure library (github). Be sure the cloned directory is named "closure-library" and is located at the same directory level as your blockly project, like this: (image link)

enter image description here

3) Add a javascript generate.js in your blockly project folder. Note that here I use blockly xml to generate python code. You need to adjust line 7-10 based on the type of generator you use.

global.DOMParser = require('xmldom').DOMParser;

global.Blockly = require('./blockly_uncompressed.js');
require('./blocks/math.js');
require('./blocks/text.js');
require('./blocks/lists.js');
require('./generators/python.js');
require('./generators/python/math.js');
require('./generators/python/text.js');
require('./generators/python/lists.js');
require('./msg/messages.js');

var fs = require('fs');

var xmlText = process.argv[2];
try {
    var xml = Blockly.Xml.textToDom(xmlText);
    // Create a headless workspace.
    var workspace = new Blockly.Workspace();
    Blockly.Xml.domToWorkspace(workspace, xml);
    var code = Blockly.Python.workspaceToCode(workspace);
    console.log(code);
} catch (e) {
    console.log(e);
}

4) Finally, run node.js on generate.js with your pre-generated XML string:

$ node generate.js '<xml>...</xml>'

Reference:

1) Headless Blockly

2) Blockly code generation on the server side

3) Building Blockly

Ida
  • 2,919
  • 3
  • 32
  • 40
  • If you're going to post code, which is great, please **post it as plain-text**. This image means that code may as well be locked behind glass. – tadman Sep 09 '16 at 02:18
  • I get this error ''' Deprecated call to Blockly.Xml.domToWorkspace, swap the arguments. ReferenceError: document is not defined''' ; swapping the arguments that error occurrs anyway – skarux Sep 09 '16 at 07:02
  • by the way, I can't look at the image because imgur is blocked by the proxy – skarux Sep 09 '16 at 07:04
  • @skarux I couldn't reproduce your error. I added some reference though, hopefully they'll be helpful for you. As for the image, you can click the image link to see it. – Ida Sep 10 '16 at 03:50
  • Currently, doing the same steps on my workstation it will give me this error: global.Blockly = require('./blockly_uncompressed.js'); Error: Cannot find module './../../../../blockly/core/blocks.js' – Symon Jul 02 '18 at 19:57
0
  1. To fix the error

    Deprecated call to Blockly.Xml.domToWorkspace, swap the arguments.

Change the line Blockly.Xml.domToWorkspace(workspace, xml); to Blockly.Xml.domToWorkspace(xml, workspace);

  1. To fix the error

    ReferenceError: document is not defined

    Add the line Blockly.Events.disable(); before calling Blockly.Xml.domToWorkspace

Refer to the link

eli-k
  • 10,898
  • 11
  • 40
  • 44