Skip to content

Commit 5579098

Browse files
authored
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.
1 parent b189b19 commit 5579098

1 file changed

Lines changed: 24 additions & 32 deletions

File tree

demos/blockfactory/factory_utils.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ FactoryUtils.getGeneratorStub = function(block, generatorLanguage) {
8080
return ' var ' + root + '_' + name;
8181
}
8282
// The makevar function lives in the original update generator.
83-
var language = generatorLanguage;
83+
var language = generatorLanguage.toLowerCase();
8484
var code = [];
85-
code.push("Blockly." + language + "['" + block.type +
86-
"'] = function(block) {");
85+
code.push(`${language}.${language}Generator.forBlock['${block.type}'] = ` +
86+
'function(block, generator) {');
8787

8888
// Generate getters for any fields or inputs.
8989
for (var i = 0, input; input = block.inputList[i]; i++) {
@@ -93,42 +93,34 @@ FactoryUtils.getGeneratorStub = function(block, generatorLanguage) {
9393
continue;
9494
}
9595
if (field instanceof Blockly.FieldVariable) {
96-
// Subclass of Blockly.FieldDropdown, must test first.
97-
code.push(makeVar('variable', name) +
98-
" = Blockly." + language +
99-
".nameDB_.getName(block.getFieldValue('" + name +
100-
"'), Blockly.Variables.NAME_TYPE);");
101-
} else if (field instanceof Blockly.FieldAngle) {
102-
// Subclass of Blockly.FieldTextInput, must test first.
103-
code.push(makeVar('angle', name) +
104-
" = block.getFieldValue('" + name + "');");
105-
} else if (field instanceof Blockly.FieldColour) {
106-
code.push(makeVar('colour', name) +
107-
" = block.getFieldValue('" + name + "');");
96+
// FieldVariable is subclass of FieldDropdown; must test first.
97+
code.push(`${makeVar('variable', name)} = ` +
98+
`generator.nameDB_.getName(block.getFieldValue('${name}'), ` +
99+
`Blockly.Variables.NAME_TYPE);`);
108100
} else if (field instanceof Blockly.FieldCheckbox) {
109-
code.push(makeVar('checkbox', name) +
110-
" = block.getFieldValue('" + name + "') === 'TRUE';");
111-
} else if (field instanceof Blockly.FieldDropdown) {
112-
code.push(makeVar('dropdown', name) +
113-
" = block.getFieldValue('" + name + "');");
114-
} else if (field instanceof Blockly.FieldNumber) {
115-
code.push(makeVar('number', name) +
116-
" = block.getFieldValue('" + name + "');");
117-
} else if (field instanceof Blockly.FieldTextInput) {
118-
code.push(makeVar('text', name) +
119-
" = block.getFieldValue('" + name + "');");
101+
code.push(`${makeVar('checkbox', name)} = ` +
102+
`block.getFieldValue('${name}') === 'TRUE';`);
103+
} else {
104+
let prefix =
105+
// Angle is subclass of FieldTextInput; must test first.
106+
field instanceof Blockly.FieldAngle ? 'angle' :
107+
field instanceof Blockly.FieldColour ? 'colour' :
108+
field instanceof Blockly.FieldDropdown ? 'dropdown' :
109+
field instanceof Blockly.FieldNumber ? 'number' :
110+
field instanceof Blockly.FieldTextInput ? 'text' :
111+
'field'; // Default if subclass not found.
112+
code.push(`${makeVar(prefix, name)} = block.getFieldValue('${name}');`);
120113
}
121114
}
122115
var name = input.name;
123116
if (name) {
124117
if (input.type === Blockly.INPUT_VALUE) {
125-
code.push(makeVar('value', name) +
126-
" = Blockly." + language + ".valueToCode(block, '" + name +
127-
"', Blockly." + language + ".ORDER_ATOMIC);");
118+
code.push(`${makeVar('value', name)} = ` +
119+
`generator.valueToCode(block, '${name}', ` +
120+
`${language}.Order.ATOMIC);`);
128121
} else if (input.type === Blockly.NEXT_STATEMENT) {
129-
code.push(makeVar('statements', name) +
130-
" = Blockly." + language + ".statementToCode(block, '" +
131-
name + "');");
122+
code.push(`${makeVar('statements', name)} = ` +
123+
`generator.statementToCode(block, '${name}');`);
132124
}
133125
}
134126
}

0 commit comments

Comments
 (0)