Toolbox search function

524 views
Skip to first unread message

Quentin Noirhomme

unread,
Jul 11, 2017, 10:44:07 AM7/11/17
to Blockly
Hi,

I designed a short toolbox search function to search for block with a given string in the 'type' or 'tooltip' attribute.
The toolbox tree is recursively searched and a 'node' array is return.

The search works fine for my toolbox (classic toolbox  plus a few hundred own blocks).

The function has to be included in toolbox.js
The code is:

/**
* A search block function
* @param {string} a search string
* @return {Array<block>} Blocks (in 'node' format) with the searchstring in type or tooltip
*/

Blockly.Toolbox.prototype.searchBlock =  function(searchstring) {

   
var results = [];
   
var searchworkspace = new Blockly.Workspace(); // a headless workspace for searching block attributes
    recursiveSearch
(this.tree_);

   
function recursiveSearch(child) { //search the tree recursively
       
if (child.children_) { // check if children_ is not null
           
for (var i=0; i<child.children_.length; i++) { //for each children
                recursiveSearch
(child.children_[i]);
           
}    
       
} else { // we are at a tree leaf, we can search for block
           
if ("blocks" in child) {        
               
// search the block array and the blocks attribute.
               
for (var i = 0; i<child.blocks.length; i++) {
                   
if (typeof(child.blocks[i]) === 'object'){  
                       
if ("type" in child.blocks[i].attributes) { // if it is a block, it has a type attribute, not if it's a label
                           
//search block type
                           
if (child.blocks[i].attributes.type.value.search(searchstring)>-1) {
                                results
.push(child.blocks[i]);
                           
} else { // search block tooltip
                               
try{
                                   
var searchblock = searchworkspace.newBlock(child.blocks[i].attributes.type.value);
                                   
if (typeof(searchblock.tooltip) === 'string' && searchblock.tooltip.search(searchstring)>-1) {
                                        results
.push(child.blocks[i]);
                                   
}
                                    searchworkspace
.clear();
                               
} catch(err) {
                                   
//alert(err + ' ' + child.blocks[i].attributes.type.value);
                               
}
                           
}
                       
}
                   
}
               
}
           
}
       
}  
   
}
    searchworkspace
.dispose();
   
return results;
};

I only tested it on my toolbox which contains categories. I tested it  on firefox, chrome and edge
I return 'block/nodes' in a format I can easily add in the toolbox in the first category which is the search results.

Any advice to improve the search on the search tree, other attribute I could look in, what should be returned or the function in general would be much appreciate.

Best regards,

Quentin

Ivan Vidović

unread,
May 22, 2018, 3:47:14 PM5/22/18
to Blockly
Dear Quentin,

what are the necessary steps to implement your search function to my blockly application? I need to copy this code to toolbox.js and what else?

Best regards
Reply all
Reply to author
Forward
0 new messages