Skip to content

Useful Code Information

MaryS94 edited this page Jul 25, 2016 · 8 revisions

Read any string

Blockly.Accessibility.Speech.Say("string to say");

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')

InputList Connection Types

This can be used to figure out what type of block is connected. 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.

Generating Comment Tree with Nary Search Tree

Blockly / Accessibility / prefixes.js has a function called generateTree() called when the 'C' key is pressed.
N-ary seach tree functions work by calling itself recursively for each childNode[i] and its childNode[i] until there are no more childNodes and then i is incremented and the process repeats itself. Here is an example of a basic N-ary search tree.

function narySearchTree(object){
//do something to object (like get comments)
for(var i = 0; i < object.childNodes; i++){
narySearchTree(object.childNodes[i]);
} }

Getting and Setting Comment Text

blockly.selected.comment.getText()
blockly.selected.comment.setText()
note: getText() will throw an error if the block does not have a comment, to work around this, simply setText(" ")