domToWorkspace not working

694 views
Skip to first unread message

Manuel Bonatti

unread,
Jan 17, 2016, 4:12:25 AM1/17/16
to Blockly
Hello,

I developed a mobile application that loads a local html page in order to present to the user a list of blocks that can be concatenated in order to create a program.
In the case in which the user created a block sequence, I'm able to export it as an XML, read the content and store all the data in an internal database.
I was able to recreate, from the stored data, the original XML, but when I try use function domToWorkspace (as described in the original Blockly guidelines) it is not working. 

For that reason i tried to use the html file directly on a pc with Chrome and it worked without problems.

Since I had some problems with the standard WebView in Android I downloaded the XWalkWebView instead.
Everything else is working except the function domToWorkspace.

Hope you are able to help me.

Kind regards,
Manuel

Avi

unread,
Jan 17, 2016, 12:16:11 PM1/17/16
to Blockly
Manuel,

Could you post a snippet of your code, where you are using domToWorkspace. That way, we can have a better idea of what could be going wrong. Any errors etc. if available in the log will help as well.

Regards
Avi


Manuel Bonatti

unread,
Jan 19, 2016, 10:35:10 AM1/19/16
to Blockly
I tried to do a simple test, passing a static xml string. At the end of my html page, I have the following code:

var workspace = Blockly.inject('blocklyDiv', {toolbox: document.getElementById('toolbox')});

function loadBlocksFromCode() {
    var xml = Blockly.Xml.textToDom('<xml xmlns="http://www.w3.org/1999/xhtml"><block type="when_flag_pressed" id="11" x="191" y="48"></block></xml>');
    Blockly.Xml.domToWorkspace(workspace, xml);
}
		  
loadBlocksFromCode();  

I tried also to past the function on top with the other javascript functions and to call it when the page surely finished loading, but without positive results.
I'm also sure that the function is called, since i tried to put an alert('test') inside it and it was successfully called.

Hope this helps a bit to understand better my problem.

Best Regards,
Manuel

Blake

unread,
Jan 19, 2016, 11:48:41 AM1/19/16
to Blockly
I wonder if the loadBlocksFromCode(); call is happening before blockly is ready. Maybe try removing the call to loadBlocksFromCode(); and running it from the developer console in your browser.

Blake

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manuel Bonatti

unread,
Jan 19, 2016, 12:58:55 PM1/19/16
to Blockly
I tried already to run this after the page is fully loaded.
I have to problem to run the function that gets the code from the workspace in form of XML and it works without problems.

I tried also to run the same html page in Chrome on my PC instead of the Web View inside the Android application and it works fine.

Carlos Pereira

unread,
Jan 19, 2016, 5:09:23 PM1/19/16
to Blockly
If it doesn't work only on the Android webview I'd recommend to install chrome canary, connect to your Android app, and debug it from there, see where it fails.

Manuel Bonatti

unread,
Jan 20, 2016, 8:58:58 AM1/20/16
to Blockly
I'm no using the standard Android Web View, since I had already the problem that, if a block contains a field and you tap on it to change the value, a dialog should appear, which does not happen.
For that reason I'm using the XWalkWebView that allowed me to resolve such a problem.

Manuel Bonatti

unread,
Jan 23, 2016, 3:19:06 AM1/23/16
to Blockly
I tried to put a javascript alert after the function domToWorkspace and found out that the it does never reach such an alert, which means that something happens and the function is not executed completely...

Carlos Pereira

unread,
Jan 24, 2016, 5:53:32 PM1/24/16
to Blockly
If this is only failing only on your specific difficult-to-debug browser, and it is not replicable anywhere else, I am not quite sure there is much help you can get from here.

Have a look at the documentation of your specific web iew to see if there is a way to access the debug or console. On the meantime, you can try to retrieve the error thrown by doing something like this:

try{
 domToWorkspace...
} catch(e) {
  console.error(e);
  alert('Error:\n' + e);
}

speed...@gmail.com

unread,
Apr 2, 2016, 12:11:31 PM4/2/16
to Blockly
having a similar problem:

code sniplet:

  <button onclick="save_xml()">SAVE XML</button>
  <button onclick="load_xml()">LOAD XML</button>
  <textarea id="myTextarea"> </textarea>

  <script>

    function save_xml() {
      var xml = Blockly.Xml.workspaceToDom(workspace);
      var xml_text = Blockly.Xml.domToPrettyText(xml);
      document.getElementById("myTextarea").value = xml_text;
      alert(xml_text);
    }

    function load_xml() {
   
      try {
            var xml_text = document.getElementById("myTextarea").value;
            var xml = Blockly.Xml.textToDom(xml_text);
            Blockly.mainWorkspace.clear()
            Blockly.Xml.domToWorkspace(Blockly.mainworkspace, xml);

          } catch(e) {
            console.error(e);
            alert('Error:\n' + e);
          }
      }

the error is definitely thrown during the call to Blockly.Xml.domToWorkspace(Blockly.mainworkspace, xml);

we are seeing the following text in the alert box:

Error:
TypeError: a is undefined

any ideas ?

Carlos P.A.

unread,
Apr 2, 2016, 12:19:32 PM4/2/16
to Blockly

Are you sure the xml is correct? Could you provide it as well?


--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/KXAQx7lr0Ww/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.

speed...@gmail.com

unread,
Apr 2, 2016, 12:35:41 PM4/2/16
to Blockly
this is happening for all xml's that i try to save and reload ... example:

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="controls_if" id="%QtcL~c_bOWGM`f%j;wl" x="226" y="106">
    <value name="IF0">
      <block type="logic_boolean" id="X-Ly-?},?f]ePz=SO-Hh">
        <field name="BOOL">TRUE</field>
      </block>
    </value>
    <statement name="DO0">
      <block type="variables_set" id="l2.Kp/A-j!v-vIHhsp%}">
        <field name="VAR">item</field>
        <value name="VALUE">
          <block type="math_number" id="cDA2:(!|9%xS^zRk%+QV">
            <field name="NUM">0</field>
          </block>
        </value>
      </block>
    </statement>
  </block>
</xml>

Neil Fraser

unread,
Apr 2, 2016, 12:39:20 PM4/2/16
to blo...@googlegroups.com
That XML is valid.

Can you remove your try-catch and get a stack trace (from the JS console) of where the actual error is?  Also, which browser and OS are you using?

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

speed...@gmail.com

unread,
Apr 2, 2016, 1:00:42 PM4/2/16
to Blockly, ro...@neil.fraser.name
sure :-)

on firefox 39 the following is happening:

TypeError: a is undefined
.../blockly_compressed.js (line 995, col 47)


....Xml.domToWorkspace=function(a,b){var c;a.RTL&&(c=a.getWidth());Blockly.Field.st...

on ie 11 we see the following error:
TypeError: Unable to get property 'RTL' of undefined or null reference

speed...@gmail.com

unread,
Apr 2, 2016, 1:07:21 PM4/2/16
to Blockly, ro...@neil.fraser.name
mea culpa:

setting
Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);

instead of
Blockly.Xml.domToWorkspace(Blockly.mainworkspace, xml);

solved the issue :-)

thanks for your incredibly fast and competent help & support

keep up with this unique cool and marellous project !!!

Reply all
Reply to author
Forward
0 new messages