1

I am trying to restore or generate a block with the XML code on a button click and set it to my workplace. But it does not work properly, somehow it ain't showing nothing. Help will be appreciated. I have tried the following links:

1.https://samadhipoornima.wordpress.com/2016/06/21/play-around-with-blockly-save-and-restore-the-workspace/

2.https://embed.plnkr.co/XIeR0L/

3.https://developers.google.com/blockly/guides/create-custom-blocks/blockly-developer-tools

4.https://developers.google.com/blockly/guides/get-started/web

My code to restore:

value = data[5];      
alert(value );
Blockly.Xml.appendDomToWorkspace(value ,demoWorkspace);

I have also tried in the following way:

var xml = Blockly.Xml.textToDom(data[5]);
Blockly.Xml.domToWorkspace(xml, demoWorkspace);

Block restore function:

function restore() {

        $.ajax({
            type: "GET",
            url: "/save_composition",
            content_type : "application/json",
            success: function(data) {
                value = data[5];
                {#console.log(value.replace(/['"]+/g, ''));#}
                var result = value.slice(1, -1);
                alert(result);
                var xml = Blockly.Xml.textToDom(data[5]);
                Blockly.Xml.domToWorkspace(xml, demoWorkspace);
            },
            error: function(xhr, textStatus) {
                   alert(textStatus);
            }});
    }

A single response of XML is:

<xml xmlns="http://www.w3.org/1999/xhtml">
<variables></variables>
<block type="controls_repeat_ext" id="J]k6G0~I).TfgP_;:j=S" x="223" y="65">
<value name="TIMES">
  <block type="math_number" id="j{Y2o]x%v(R#1#V*xm)0">
    <field name="NUM">10</field>
  </block>
</value>
<statement name="DO">
  <block type="print_content" id="}$v5G?]mBm(R^8)sri-L">
    <value name="to_print">
      <block type="text" id="esg1;Q4KV$g;^Ul?BF]p">
        <field name="TEXT">test</field>
      </block>
    </value>
  </block>
</statement>
</block>
</xml>
Sakhawat Hossain
  • 454
  • 8
  • 23
  • 2
    It seems like you've posted the links you've been using for reference, but I'm not seeing a lot about your own implementation. It seems like your code should be working if your XML is valid, so I suspect your XML may have issues... how is data populated? Can you log out the value of data and post that here? Are there any errors in your console? – Amber B. Oct 24 '19 at 13:09
  • I have updated the question description. Please have a look. – Sakhawat Hossain Oct 24 '19 at 14:02
  • 2
    Hrm, that XML does look correct... but wait, what's the purpose of the value.slice(1,-1) code? And is the value under "A single response of XML" the value in data[5]/value, or the value in result? (As an aside, you may have better luck posting your question over on the Blockly forums, which are a bit more conversational than SO: https://groups.google.com/forum/#!forum/blockly) – Amber B. Oct 24 '19 at 14:53
  • Ok, thanks for your co-operation. slice(1,-1) just removes the double quote from the XML code, nothing else. And I just used data[5], just to check a value. – Sakhawat Hossain Oct 24 '19 at 15:21
  • 1
    I'm not sure that including the wrapping quotes results in valid XML here... did you try running Blockly.Xml.textToDom on the sliced value without the double quotes? – Amber B. Oct 24 '19 at 17:27

0 Answers0