Skip to content

Useful Code Information

MaryS94 edited this page Jun 1, 2016 · 8 revisions

Get block and workspace information

To return the block SVG which displays all of the information about the block:
console.log(Blockly.selected);

To access specific information to alter or reference:
var test = Blockly.selected.type;

To get information about the workspace:
console.log(Blockly.mainWorkspace);
Note that Blockly.mainWorkspace can be used to call useful functions found in ../core/workspace.js and ../core/workspace_svg.js

Get all blocks in the workspace

//Check if the workspace is empty
if (!xmlDoc || !xmlDoc.getElementsByTagName('BLOCK')) {
  console.log("nothings here");
  return null;
}
//Add all xml blocks to blockArr 
var blockArr = xmlDoc.getElementsByTagName('BLOCK');

Get tags based on other tagnames

var commentArr = xmlDoc.getElementsByTagName('COMMENT');

Get the child tags of a block such as value, field, statement, and next

return blockArr[index].childNodes;  ex return:[field, value, value, value, statement, next]

Get attributes of your block such as the ID

return blockArr[index].getAttribute('id');  ex return: 17  

Check for a blocks children depending on the direction

//The string comparison needs to be in all caps to check properly
if(blockArr[index].childNodes[index2].nodeName == 'STATEMENT')

Connection Types

Input Type = 1    Connecting a block to the right of the current block
Output Type = 2   Connecting a block to the left of the current block
Next Type = 3     Connecting a block to the one below it below the current block 
Previous Type = 4 Connecting to a block above the current block

Accessing Connections

selectedNode = highlighted menu block
Type 1 = selectedNode.outputConnection
Type 2 = selectedNode.inputList[0].connection
Type 3 = var newBlock = Blockly.Accessibility.menu_nav.flyoutToWorkspace();
         this.safeConnect(newBlock.previousConnection);
Type 4 = var newBlock = Blockly.Accessibility.menu_nav.flyoutToWorkspace();
         this.safeConnect(newBlock.nextConnection);

To check the compatibility of blocks use their connection then use .check_[0]

if(toolboxChoices[i].outputConnection.check_[0] == this.storedConnection.check_[0])
ex. Boolean == Boolean

Change Field Values

Blockly / Accessibility / in_block.js has functions to show the editor of various text fields. This code can be modified to get and set the values using:
this.selectionList[this.connectionsIndex]. -> showEditor_(); setValue(value); getValue();

  • Some fields may have different syntax for these methods so it may be helpful to check the .js file of the relevant field first.
  • Get and Set methods for accessing dropdown menus have already been created.