mo4islona / node-blockly

Blockly for Node.js and Browser via CommonJS module
133 stars 81 forks source link

Translate xml string to a javascript string in Browser #30

Closed kbruneel closed 5 years ago

kbruneel commented 5 years ago

Hi,

I'm trying to translate xml string to a javascript string in the browser but I don't have a visual workspace. I'm trying to use the code from the README except that I require the browser version on node-blockly. This is the essential part of my code:

var Blockly = require('node-blockly/browser');
var xml = Blockly.Xml.textToDom(xml_text);
var workspace = new Blockly.Workspace();
Blockly.Xml.domToWorkspace(xml, workspace);
var code = Blockly.JavaScript.workspaceToCode(workspace);

If I do this I get the following error:

Error: Trying to set block style to procedure_blocks before theme was defined via Blockly.setTheme().
    at module.exports.Blockly.Block.setStyle (blockly_compressed_browser.js:1460)
    at module.exports.Blockly.Block.init (blocks_compressed_browser.js:106)
    at new module.exports.Blockly.Block (blockly_compressed_browser.js:1440)
    at module.exports.Blockly.Workspace.newBlock (blockly_compressed_browser.js:1029)
    at Object.module.exports.Blockly.Xml.domToBlockHeadless_ (blockly_compressed_browser.js:993)
    at Object.module.exports.Blockly.Xml.domToBlock (blockly_compressed_browser.js:990)
    at Object.module.exports.Blockly.Xml.domToWorkspace (blockly_compressed_browser.js:986)

Which method would you recommend to go from an xml string to a javascript string in the browser?

Many thanks!

mo4islona commented 5 years ago

Hi! I`ve tried headless mode in a browser and it works fine for me. https://codesandbox.io/s/bri2g

Could you attach your xml string?

kbruneel commented 5 years ago

It indeed depends on the xml:

<xml xmlns="http://www.w3.org/1999/xhtml">
  <variables>
    <variable type="" id="j:|seQ!Cb:h%]r.Nv:%Y">left_sensor</variable>
    <variable type="" id="FJ]G9nydQGLnneo:ZbqE">right_sensor</variable>
    <variable type="" id="u?-rKl3vhHz+uDeW^7P|">speed</variable>
    <variable type="" id="G0Chi5P*a^O=e,BqIQOa">engineTorque</variable>
    <variable type="" id="_EjEfOO,oBya{dHE*8os">breakingTorque</variable>
    <variable type="" id="(J~4Dn0U.UWbd8EOTxb_">steeringAngle</variable>
  </variables>
  <block type="procedures_defreturn" id="Yl60#PO2|*uWo/.!BTri" x="22" y="17">
    <mutation>
      <arg name="left_sensor" varid="j:|seQ!Cb:h%]r.Nv:%Y"></arg>
      <arg name="right_sensor" varid="FJ]G9nydQGLnneo:ZbqE"></arg>
      <arg name="speed" varid="u?-rKl3vhHz+uDeW^7P|"></arg>
    </mutation>
    <field name="NAME">control</field>
    <comment pinned="false" h="80" w="160">Describe this function...</comment>
    <statement name="STACK">
      <block type="variables_set" id="O]OFir;j96=k,9,RK.s#">
        <field name="VAR" id="G0Chi5P*a^O=e,BqIQOa" variabletype="">engineTorque</field>
        <value name="VALUE">
          <block type="math_number" id="g-NlG|U!heP1m+5As`/4">
            <field name="NUM">0</field>
          </block>
        </value>
        <next>
          <block type="variables_set" id="=/@aA6R=aBsQf:piGb`6">
            <field name="VAR" id="_EjEfOO,oBya{dHE*8os" variabletype="">breakingTorque</field>
            <value name="VALUE">
              <block type="math_number" id="B}7$u)csO]L0VC?+HueC">
                <field name="NUM">0</field>
              </block>
            </value>
            <next>
              <block type="variables_set" id="[?y0jg4n7`)tOpm_yVa:">
                <field name="VAR" id="(J~4Dn0U.UWbd8EOTxb_" variabletype="">steeringAngle</field>
                <value name="VALUE">
                  <block type="math_number" id="^VH5^TrMjwGZ+y]@[/8Z">
                    <field name="NUM">0</field>
                  </block>
                </value>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
    <value name="RETURN">
      <block type="lists_create_with" id="027yG)#OYYGcKX0|@NI4">
        <mutation items="3"></mutation>
        <value name="ADD0">
          <block type="variables_get" id="iFM]Ho}p0py|QqEAi?/u">
            <field name="VAR" id="G0Chi5P*a^O=e,BqIQOa" variabletype="">engineTorque</field>
          </block>
        </value>
        <value name="ADD1">
          <block type="variables_get" id="=sJ.^7rxVo/acsD3KGu8">
            <field name="VAR" id="_EjEfOO,oBya{dHE*8os" variabletype="">breakingTorque</field>
          </block>
        </value>
        <value name="ADD2">
          <block type="variables_get" id=":![`-}#*aHZA{$IW+utl">
            <field name="VAR" id="(J~4Dn0U.UWbd8EOTxb_" variabletype="">steeringAngle</field>
          </block>
        </value>
      </block>
    </value>
  </block>
</xml>
kbruneel commented 5 years ago

I think this is related to: https://github.com/google/blockly/issues/2575

mo4islona commented 5 years ago

Please try latest 1.0.40 version

https://codesandbox.io/s/inspiring-jepsen-x9zgn - it works