Skip to content

Commit

Permalink
fix(demos): Update BlockFactory generator stub generator (google#7211)
Browse files Browse the repository at this point in the history
Update the BlockFactory block generator function stub generator
to apply recent changes in generator function best practices:

- Use languageGenarator instead of Blockly.Language.
- Put generator functions in .forBlock dictionary.
- Accept (and use) a second argument that is the calling
  CodeGenerator object.
- User Order.ATOMIC enum instead of ORDER_ATOMIC.

Also:

- Prefix (e.g.) javascriptGenerator and Order with "javascript.".
- Use template literals where useful.
- DRY up all the non-special field stub code generation.
  • Loading branch information
cpcallen authored Jun 27, 2023
1 parent b189b19 commit 5579098
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions demos/blockfactory/factory_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ FactoryUtils.getGeneratorStub = function(block, generatorLanguage) {
return ' var ' + root + '_' + name;
}
// The makevar function lives in the original update generator.
var language = generatorLanguage;
var language = generatorLanguage.toLowerCase();
var code = [];
code.push("Blockly." + language + "['" + block.type +
"'] = function(block) {");
code.push(`${language}.${language}Generator.forBlock['${block.type}'] = ` +
'function(block, generator) {');

// Generate getters for any fields or inputs.
for (var i = 0, input; input = block.inputList[i]; i++) {
Expand All @@ -93,42 +93,34 @@ FactoryUtils.getGeneratorStub = function(block, generatorLanguage) {
continue;
}
if (field instanceof Blockly.FieldVariable) {
// Subclass of Blockly.FieldDropdown, must test first.
code.push(makeVar('variable', name) +
" = Blockly." + language +
".nameDB_.getName(block.getFieldValue('" + name +
"'), Blockly.Variables.NAME_TYPE);");
} else if (field instanceof Blockly.FieldAngle) {
// Subclass of Blockly.FieldTextInput, must test first.
code.push(makeVar('angle', name) +
" = block.getFieldValue('" + name + "');");
} else if (field instanceof Blockly.FieldColour) {
code.push(makeVar('colour', name) +
" = block.getFieldValue('" + name + "');");
// FieldVariable is subclass of FieldDropdown; must test first.
code.push(`${makeVar('variable', name)} = ` +
`generator.nameDB_.getName(block.getFieldValue('${name}'), ` +
`Blockly.Variables.NAME_TYPE);`);
} else if (field instanceof Blockly.FieldCheckbox) {
code.push(makeVar('checkbox', name) +
" = block.getFieldValue('" + name + "') === 'TRUE';");
} else if (field instanceof Blockly.FieldDropdown) {
code.push(makeVar('dropdown', name) +
" = block.getFieldValue('" + name + "');");
} else if (field instanceof Blockly.FieldNumber) {
code.push(makeVar('number', name) +
" = block.getFieldValue('" + name + "');");
} else if (field instanceof Blockly.FieldTextInput) {
code.push(makeVar('text', name) +
" = block.getFieldValue('" + name + "');");
code.push(`${makeVar('checkbox', name)} = ` +
`block.getFieldValue('${name}') === 'TRUE';`);
} else {
let prefix =
// Angle is subclass of FieldTextInput; must test first.
field instanceof Blockly.FieldAngle ? 'angle' :
field instanceof Blockly.FieldColour ? 'colour' :
field instanceof Blockly.FieldDropdown ? 'dropdown' :
field instanceof Blockly.FieldNumber ? 'number' :
field instanceof Blockly.FieldTextInput ? 'text' :
'field'; // Default if subclass not found.
code.push(`${makeVar(prefix, name)} = block.getFieldValue('${name}');`);
}
}
var name = input.name;
if (name) {
if (input.type === Blockly.INPUT_VALUE) {
code.push(makeVar('value', name) +
" = Blockly." + language + ".valueToCode(block, '" + name +
"', Blockly." + language + ".ORDER_ATOMIC);");
code.push(`${makeVar('value', name)} = ` +
`generator.valueToCode(block, '${name}', ` +
`${language}.Order.ATOMIC);`);
} else if (input.type === Blockly.NEXT_STATEMENT) {
code.push(makeVar('statements', name) +
" = Blockly." + language + ".statementToCode(block, '" +
name + "');");
code.push(`${makeVar('statements', name)} = ` +
`generator.statementToCode(block, '${name}');`);
}
}
}
Expand Down

0 comments on commit 5579098

Please sign in to comment.