how to override Blockly.Variables.flyoutCategoryBlocks

173 views
Skip to first unread message

m lz

unread,
Sep 12, 2023, 7:54:16 AM9/12/23
to Blockly
in default Variable category click create variable.. will generate 3 blocks include one 'math_change' block, In my project didn't want to generate this block.
so I want to override this function like bellows:


Blockly.Variables.flyoutCategoryBlocks = function flyoutCategoryBlocks(workspace){
 
    const variableModelList = workspace.getVariablesOfType('');

  const xmlList = [];
  if (variableModelList.length > 0) {
    // New variables are added to the end of the variableModelList.
    const mostRecentVariable = variableModelList[variableModelList.length - 1];
    if (Blockly.Blocks['variables_set']) {
      const block = Blockly.utils.xml.createElement('block');
      block.setAttribute('type', 'variables_set');
      block.setAttribute('gap', Blocks['math_change'] ? '8' : '24');
      block.appendChild(Blockly.Variables.generateVariableFieldDom(mostRecentVariable));
      xmlList.push(block);
    }
    // if (Blockly.Blocks['math_change']) {
    //   const block = Blockly.utils.xml.createElement('block');
    //   block.setAttribute('type', 'math_change');
    //   block.setAttribute('gap', Blocks['variables_get'] ? '20' : '8');
    //   block.appendChild(Blockly.Variables.generateVariableFieldDom(mostRecentVariable));
    //   const value = Blockly.Xml.textToDom(
    //       '<value name="DELTA">' +
    //       '<shadow type="math_number">' +
    //       '<field name="NUM">1</field>' +
    //       '</shadow>' +
    //       '</value>');
    //   block.appendChild(value);
    //   xmlList.push(block);
    // }

    if (Blockly.Blocks['variables_get']) {
      variableModelList.sort(Blockly.VariableModel.VariableModel.compareByName);
      for (let i = 0, variable; variable = variableModelList[i]; i++) {
        const block = Blockly.utils.xml.createElement('block');
        block.setAttribute('type', 'variables_get');
        block.setAttribute('gap', '8');
        block.appendChild(Blockly.Variables.generateVariableFieldDom(variable));
        xmlList.push(block);
      }
    }
  }
  return xmlList;
 
}

but this not work,I tried add debug code like console.error("dddddddddddddd"),it will not executed,dose anything wrong?


Beka Westberg

unread,
Sep 12, 2023, 12:16:57 PM9/12/23
to blo...@googlegroups.com
Hello!

Dynamic categories work by adding the function to a dictionary object. So what's happening under the hood when you override this is like this:

```
// Blockly defines the dynamic category.
let flyoutCategoryBlocks = function() { /* generate category */ };

// Blockly adds it to a dictionary object (this isn't actually how it works, but for demonstration purposes).
dictionaryObject['dynamic_category'] = flyoutCategoryBlocks;

// You redefine flyoutCategoryBlocks.
flyoutCategoryBlocks = function() { /* generate category */ };

// Blockly calls the function on the dictionary object, which is the original function.
dictionaryObject['dynamic_category']();
```

Instead of redefining flyoutCategoryBlocks you want to redefine what is on the dictionary object.

```
myWorkspace.registerToolboxCategoryCallback(
    'VARIABLE_DYNAMIC', myDynamicFlyoutFunction);
```

You can check out our dynamic categories docs for more info!

I hope that helps! If you have any further questions please reply =)
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/2403a6d4-f126-4c98-839a-1702b767df17n%40googlegroups.com.

Beka Westberg

unread,
Sep 12, 2023, 12:21:20 PM9/12/23
to blo...@googlegroups.com
Correction, the call should pass 'VARIABLE' not 'VARIABLE_DYNAMIC':

```
myWorkspace.registerToolboxCategoryCallback(
    'VARIABLE', myDynamicFlyoutFunction);

```

Best,
--Beka
Reply all
Reply to author
Forward
0 new messages