mirror of
https://github.com/google/blockly.git
synced 2025-12-15 13:50:08 +01:00
fix(demos): Update BlockFactory generator stub generator (#7211)
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.
This commit is contained in:
committed by
GitHub
parent
b189b1989c
commit
5579098e35
@@ -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++) {
|
||||
@@ -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}');`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user