Commit 4ccbeea4 authored by Evan W. Patton's avatar Evan W. Patton

Implement menu option to make component blocks generic

Change-Id: I62ae3fed73342a5f6cb7652903a09e4dd9c6d620
parent 4ddd02db
...@@ -144,3 +144,35 @@ Blockly.Block.prototype.isolate = function(healStack) { ...@@ -144,3 +144,35 @@ Blockly.Block.prototype.isolate = function(healStack) {
Blockly.Block.prototype.interpolateMsg.SPLIT_REGEX_ = /(%\d+|\n)/; Blockly.Block.prototype.interpolateMsg.SPLIT_REGEX_ = /(%\d+|\n)/;
Blockly.Block.prototype.interpolateMsg.INLINE_REGEX_ = /%1\s*$/; Blockly.Block.prototype.interpolateMsg.INLINE_REGEX_ = /%1\s*$/;
/**
* Walk the tree of blocks starting with this block.
*
* @param {function(Blockly.Block, number)} callback the callback function to evaluate for each
* block. The function receives the block and the logical depth of the block in the tree.
*/
Blockly.Block.prototype.walk = function(callback) {
function doWalk(block, depth) {
callback(block, depth);
block.inputList.forEach(function(input) {
if ((input.type === Blockly.INPUT_VALUE || input.type === Blockly.NEXT_STATEMENT) &&
input.connection && input.connection.targetBlock()) {
doWalk(input.connection.targetBlock(), depth + 1);
}
if (block.nextConnection && block.nextConnection.targetBlock()) {
doWalk(block.nextConnection.targetBlock(), depth);
}
})
}
doWalk(this, 0);
};
/**
* @type {?function(this: Blockly.BlockSvg, !Element)}
*/
Blockly.Block.prototype.domToMutation = null;
/**
* @type {?function(this: Blockly.BlockSvg):!Element}
*/
Blockly.Block.prototype.mutationToDom = null;
...@@ -340,6 +340,7 @@ Blockly.Blocks['local_declaration_statement'] = { ...@@ -340,6 +340,7 @@ Blockly.Blocks['local_declaration_statement'] = {
this.rendered = savedRendered; this.rendered = savedRendered;
if (this.rendered) { if (this.rendered) {
this.initSvg();
this.render(); this.render();
} }
}, },
...@@ -487,7 +488,7 @@ Blockly.Blocks['local_declaration_statement'] = { ...@@ -487,7 +488,7 @@ Blockly.Blocks['local_declaration_statement'] = {
for (var x = 0, block; block = blocks[x]; x++) { for (var x = 0, block; block = blocks[x]; x++) {
if (block.type == 'procedures_mutatorarg') { if (block.type == 'procedures_mutatorarg') {
var oldName = block.getFieldValue('NAME'); var oldName = block.getFieldValue('NAME');
var newName = substitution.appy(oldName); var newName = substitution.apply(oldName);
if (newName !== oldName) { if (newName !== oldName) {
block.setFieldValue(newName, 'NAME'); block.setFieldValue(newName, 'NAME');
} }
...@@ -497,6 +498,7 @@ Blockly.Blocks['local_declaration_statement'] = { ...@@ -497,6 +498,7 @@ Blockly.Blocks['local_declaration_statement'] = {
} }
}, },
renameBound: function (boundSubstitution, freeSubstitution) { renameBound: function (boundSubstitution, freeSubstitution) {
var oldMutation = Blockly.Xml.domToText(this.mutationToDom());
var localNames = this.declaredNames(); var localNames = this.declaredNames();
for (var i = 0; i < localNames.length; i++) { for (var i = 0; i < localNames.length; i++) {
// This is LET semantics, not LET* semantics, and needs to change! // This is LET semantics, not LET* semantics, and needs to change!
...@@ -506,6 +508,10 @@ Blockly.Blocks['local_declaration_statement'] = { ...@@ -506,6 +508,10 @@ Blockly.Blocks['local_declaration_statement'] = {
this.renameVars(paramSubstitution); this.renameVars(paramSubstitution);
var newFreeSubstitution = freeSubstitution.remove(localNames).extend(paramSubstitution); var newFreeSubstitution = freeSubstitution.remove(localNames).extend(paramSubstitution);
Blockly.LexicalVariable.renameFree(this.getInputTargetBlock(this.bodyInputName), newFreeSubstitution); Blockly.LexicalVariable.renameFree(this.getInputTargetBlock(this.bodyInputName), newFreeSubstitution);
var newMutation = Blockly.Xml.domToText(this.mutationToDom());
if (Blockly.Events.isEnabled()) {
Blockly.Events.fire(new Blockly.Events.Change(this, 'mutation', null, oldMutation, newMutation));
}
if (this.nextConnection) { if (this.nextConnection) {
var nextBlock = this.nextConnection.targetBlock(); var nextBlock = this.nextConnection.targetBlock();
Blockly.LexicalVariable.renameFree(nextBlock, freeSubstitution); Blockly.LexicalVariable.renameFree(nextBlock, freeSubstitution);
...@@ -549,7 +555,6 @@ Blockly.Blocks['local_declaration_statement'] = { ...@@ -549,7 +555,6 @@ Blockly.Blocks['local_declaration_statement'] = {
} }
return result; return result;
}, },
//TODO (user) this has not been internationalized yet
typeblock: [{ translatedName: Blockly.Msg.LANG_VARIABLES_LOCAL_DECLARATION_TRANSLATED_NAME }] typeblock: [{ translatedName: Blockly.Msg.LANG_VARIABLES_LOCAL_DECLARATION_TRANSLATED_NAME }]
}; };
...@@ -583,12 +588,11 @@ Blockly.Blocks['local_declaration_expression'] = { ...@@ -583,12 +588,11 @@ Blockly.Blocks['local_declaration_expression'] = {
saveConnections: Blockly.Blocks.local_declaration_statement.saveConnections, saveConnections: Blockly.Blocks.local_declaration_statement.saveConnections,
getVars: Blockly.Blocks.local_declaration_statement.getVars, getVars: Blockly.Blocks.local_declaration_statement.getVars,
declaredNames: Blockly.Blocks.local_declaration_statement.declaredNames, declaredNames: Blockly.Blocks.local_declaration_statement.declaredNames,
renameVar: Blockly.Blocks.local_declaration_statement.renameVars, renameVar: Blockly.Blocks.local_declaration_statement.renameVar,
renameVars: Blockly.Blocks.local_declaration_statement.renameVar, renameVars: Blockly.Blocks.local_declaration_statement.renameVars,
renameBound: Blockly.Blocks.local_declaration_statement.renameBound, renameBound: Blockly.Blocks.local_declaration_statement.renameBound,
renameFree: Blockly.Blocks.local_declaration_statement.renameFree, renameFree: Blockly.Blocks.local_declaration_statement.renameFree,
freeVariables: Blockly.Blocks.local_declaration_statement.freeVariables, freeVariables: Blockly.Blocks.local_declaration_statement.freeVariables,
//TODO (user) this has not been internationalized yet
typeblock: [{ translatedName: Blockly.Msg.LANG_VARIABLES_LOCAL_DECLARATION_EXPRESSION_TRANSLATED_NAME }] typeblock: [{ translatedName: Blockly.Msg.LANG_VARIABLES_LOCAL_DECLARATION_EXPRESSION_TRANSLATED_NAME }]
}; };
......
...@@ -92,6 +92,8 @@ Blockly.Msg.en.switch_language_to_english = { ...@@ -92,6 +92,8 @@ Blockly.Msg.en.switch_language_to_english = {
Blockly.Msg.ENABLE_ALL_BLOCKS = 'Enable All Blocks'; Blockly.Msg.ENABLE_ALL_BLOCKS = 'Enable All Blocks';
Blockly.Msg.HIDE_ALL_COMMENTS = 'Hide All Comments'; Blockly.Msg.HIDE_ALL_COMMENTS = 'Hide All Comments';
Blockly.Msg.SHOW_ALL_COMMENTS = 'Show All Comments'; Blockly.Msg.SHOW_ALL_COMMENTS = 'Show All Comments';
Blockly.Msg.GENERICIZE_BLOCK = 'Make Generic';
Blockly.Msg.UNGENERICIZE_BLOCK = 'Make Specific';
// Variable renaming. // Variable renaming.
Blockly.Msg.CHANGE_VALUE_TITLE = 'Change value:'; Blockly.Msg.CHANGE_VALUE_TITLE = 'Change value:';
......
Subproject commit c5ead64c2b930e0794031ea999df9dca62bcf0e1 Subproject commit 174cda1495a5302037029c6ca79bf1d86b4936fe
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment