I am using blockly in webview android and calling a method I have defined - setCatagoryByData(arrayViewType, arrayViewID)
with javascript native interface.
I have defined various methods which are working great like clearing workspace, resetting existing workspace with blocks data, etc.
Following is the code where I think I am missing something-
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Blockly My Workspace</title>
<script src="../blockly_compressed.js"></script>
<script src="../blocks_compressed.js"></script>
<script src="../javascript_compressed.js"></script>
<script src="../msg/js/en.js"></script>
<div id="blocklyDiv" style="height: 700px; width: 1000px;"></div>
<xml xmlns="https://developers.google.com/blockly/xml" id="toolbox" style="display: none">
<!-- <category name="LN" >-->
<!-- <block type="onclick_block"></block>-->
<!-- </category>-->
<!-- <category name="ED" >-->
<!-- <block type="controls_if"></block>-->
<!-- </category>-->
</xml>
<script>
var demoWorkspace = Blockly.inject('blocklyDiv',
{
media: '../media/',
toolbox: document.getElementById('toolbox')
});
Blockly.Xml.domToWorkspace(document.getElementById('startBlocks'),
demoWorkspace);
// Some other methods here
function setCatagoryByData(arrayViewType, arrayViewID) {
var value = JSON.parse(arrayViewType);
<!-- var category = toolbox.getToolboxItems()[0];-->
<!-- category.hide();-->
<!-- if (value.includes("LN")) {-->
<!-- category.show();-->
<!-- }else{-->
<!-- category.hide();-->
<!-- }-->
<!-- var category2 = toolbox.getToolboxItems()[1];-->
<!-- category2.hide();-->
<!-- if (value.includes("ED")) {-->
<!-- category2.show();-->
<!-- }else{-->
<!-- category2.hide();-->
<!-- }-->
var toolboxText = '<xml>';
if (value.includes("LN")) {
toolboxText += ' <category name="LN">' +
' <block type="controls_if"></block>' +
' </category>';
}
if (value.includes("ED")) {
toolboxText += ' <category name="ED">' +
' <block type="controls_if"></block>' +
' </category>';
}
toolboxText += '</xml>';
var toolboxXml = Blockly.Xml.textToDom(toolboxText);
demoWorkspace.updateToolbox(toolboxXml);
}
</script>
</body>
</html>
Please ignore the comments, for now. When I call the method I am trying to change the categories here based on the arrays passed as the method arguments. When I run the method I see an error in Android Studios Logcat
Uncaught Error: Existing toolbox has no categories. Can't change mode.
I tried another way to hide and show predefined categories in XML for example uncomment the categories in the above code.
Comment the uncomment code and uncomment the comments inside the method.
Now It again shows an error -
toolbox.getToolboxItems is not a function
Maybe, the toolbox is not recognizable here.
Thanks,