How to clean Google Blockly?

0

I am using in my Google Blockly project and need that when I click a button, empty the mounted structure that is in memory. How can I do it?

link

My example: I need to insert a button to delete everything in the workspace

	<!DOCTYPE html>
	<html>
	<head>
	  <meta charset="utf-8">
	  <title>Blockly Demo: Fixed Blockly</title>
	  <script>
		(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
		(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
		m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
		})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
		ga('create', 'UA-102948379-1', 'auto');
		ga('send', 'pageview');
	  </script>
	  <script src="https://blockly-demo.appspot.com/static/blockly_compressed.js"></script><scriptsrc="https://blockly-demo.appspot.com/static/blocks_compressed.js"></script>
	  <script src="https://blockly-demo.appspot.com/static/msg/js/en.js"></script><style>body{background-color:#fff;font-family:sans-serif;}h1{font-weight:normal;font-size:140%;}</style></head><body><h1><ahref="https://developers.google.com/blockly/">Blockly</a> &gt;

	  <p>This is a simple demo of injecting Blockly into a fixed-sized 'div' element.</p>

	  <p>&rarr; More info on <a href="https://developers.google.com/blockly/guides/configure-blockly/web/fixed-size">injecting fixed-sized Blockly</a>&hellip;</p>

	  <div id="blocklyDiv" style="height: 480px; width: 600px;"></div>

	  <xml id="toolbox" style="display: none">
		<block type="controls_if"></block>
		<block type="logic_compare"></block>
		<block type="controls_repeat_ext"></block>
		<block type="math_number"></block>
		<block type="math_arithmetic"></block>
		<block type="text"></block>
		<block type="text_print"></block>
	  </xml>

	  <script>
		var workspace = Blockly.inject('blocklyDiv',
			{media: '../../media/',
			 toolbox: document.getElementById('toolbox')});
	  </script>

	</body>
	</html>

Step 2

Is it possible to assign the ID to each block of separate code?

Thank you

    
asked by anonymous 18.09.2017 / 13:48

1 answer

0

As I've commented, just use Worspace.clear . The workspace object you already have in JavaScript, just call the clear method when you press the button. See:

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

var button = document.getElementById("clear");

button.addEventListener("click", event => {
   workspace.clear();
});
<script src="https://blockly-demo.appspot.com/static/blockly_compressed.js"></script><scriptsrc="https://blockly-demo.appspot.com/static/blocks_compressed.js"></script>
<script src="https://blockly-demo.appspot.com/static/msg/js/en.js"></script><buttonid="clear">Limpar</button>

<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>

<xml id="toolbox" style="display: none">
  <block type="controls_if"></block>
  <block type="logic_compare"></block>
  <block type="controls_repeat_ext"></block>
  <block type="math_number"></block>
  <block type="math_arithmetic"></block>
  <block type="text"></block>
  <block type="text_print"></block>
</xml>
    
18.09.2017 / 15:56