Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pulls from MakeCode fork the ability to rename and delete variables i… #2144

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 66 additions & 21 deletions blocks/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,76 @@ Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
* @this Blockly.Block
*/
customContextMenu: function(options) {
if (this.isInFlyout){
return;
}
// Getter blocks have the option to create a setter block, and vice versa.
if (this.type == 'variables_get') {
var opposite_type = 'variables_set';
var contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET'];
if (!this.isInFlyout){
// Getter blocks have the option to create a setter block, and vice versa.
if (this.type == 'variables_get') {
var opposite_type = 'variables_set';
var contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET'];
} else {
var opposite_type = 'variables_get';
var contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET'];
}

var option = {enabled: this.workspace.remainingCapacity() > 0};
var name = this.getField('VAR').getText();
option.text = contextMenuMsg.replace('%1', name);
var xmlField = document.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.appendChild(document.createTextNode(name));
var xmlBlock = document.createElement('block');
xmlBlock.setAttribute('type', opposite_type);
xmlBlock.appendChild(xmlField);
option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
options.push(option);
// Getter blocks have the option to rename or delete that variable.
} else {
var opposite_type = 'variables_get';
var contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET'];
if (this.type == 'variables_get' || this.type == 'variables_get_reporter'){
var renameOption = {
text: Blockly.Msg.RENAME_VARIABLE,
enabled: true,
callback: Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY(this)
};
var name = this.getField('VAR').getText();
var deleteOption = {
text: Blockly.Msg.DELETE_VARIABLE.replace('%1', name),
enabled: true,
callback: Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY(this)
};
options.unshift(renameOption);
options.unshift(deleteOption);
}
}

var option = {enabled: this.workspace.remainingCapacity() > 0};
var name = this.getField('VAR').getText();
option.text = contextMenuMsg.replace('%1', name);
var xmlField = document.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.appendChild(document.createTextNode(name));
var xmlBlock = document.createElement('block');
xmlBlock.setAttribute('type', opposite_type);
xmlBlock.appendChild(xmlField);
option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
options.push(option);
}
};

/**
* Callback for rename variable dropdown menu option associated with a
* variable getter block.
* @param {!Blockly.Block} block The block with the variable to rename.
* @return {!function()} A function that renames the variable.
*/
Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY = function(block) {
return function() {
var workspace = block.workspace;
var variable = block.getField('VAR').getVariable();
Blockly.Variables.renameVariable(workspace, variable);
};
};

/**
* Callback for delete variable dropdown menu option associated with a
* variable getter block.
* @param {!Blockly.Block} block The block with the variable to delete.
* @return {!function()} A function that deletes the variable.
*/
Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY = function(block) {
return function() {
var workspace = block.workspace;
var variable = block.getField('VAR').getVariable();
workspace.deleteVariableById(variable.getId());
workspace.refreshToolboxSelection();
};
};

Blockly.Extensions.registerMixin('contextMenu_variableSetterGetter',
Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);
97 changes: 71 additions & 26 deletions blocks/variables_dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,50 @@ Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MI
*/
customContextMenu: function(options) {
// Getter blocks have the option to create a setter block, and vice versa.
if (this.isInFlyout) {
return;
}
var opposite_type;
var contextMenuMsg;
var id = this.getFieldValue('VAR');
var variableModel = this.workspace.getVariableById(id);
var varType = variableModel.type;
if (this.type == 'variables_get_dynamic') {
opposite_type = 'variables_set_dynamic';
contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET'];
if (!this.isInFlyout) {
var opposite_type;
var contextMenuMsg;
var id = this.getFieldValue('VAR');
var variableModel = this.workspace.getVariableById(id);
var varType = variableModel.type;
if (this.type == 'variables_get_dynamic') {
opposite_type = 'variables_set_dynamic';
contextMenuMsg = Blockly.Msg['VARIABLES_GET_CREATE_SET'];
} else {
opposite_type = 'variables_get_dynamic';
contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET'];
}

var option = {enabled: this.workspace.remainingCapacity() > 0};
var name = this.getField('VAR').getText();
option.text = contextMenuMsg.replace('%1', name);
var xmlField = document.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.setAttribute('variabletype', varType);
xmlField.appendChild(document.createTextNode(name));
var xmlBlock = document.createElement('block');
xmlBlock.setAttribute('type', opposite_type);
xmlBlock.appendChild(xmlField);
option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
options.push(option);
} else {
opposite_type = 'variables_get_dynamic';
contextMenuMsg = Blockly.Msg['VARIABLES_SET_CREATE_GET'];
if (this.type == 'variables_get_dynamic' ||
this.type == 'variables_get_reporter_dynamic') {
var renameOption = {
text: Blockly.Msg.RENAME_VARIABLE,
enabled: true,
callback: Blockly.Constants.Variables.RENAME_OPTION_CALLBACK_FACTORY(this)
};
var name = this.getField('VAR').getText();
var deleteOption = {
text: Blockly.Msg.DELETE_VARIABLE.replace('%1', name),
enabled: true,
callback: Blockly.Constants.Variables.DELETE_OPTION_CALLBACK_FACTORY(this)
};
options.unshift(renameOption);
options.unshift(deleteOption);
}
}

var option = {enabled: this.workspace.remainingCapacity() > 0};
var name = this.getField('VAR').getText();
option.text = contextMenuMsg.replace('%1', name);
var xmlField = document.createElement('field');
xmlField.setAttribute('name', 'VAR');
xmlField.setAttribute('variabletype', varType);
xmlField.appendChild(document.createTextNode(name));
var xmlBlock = document.createElement('block');
xmlBlock.setAttribute('type', opposite_type);
xmlBlock.appendChild(xmlField);
option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock);
options.push(option);
},
onchange: function() {
var id = this.getFieldValue('VAR');
Expand All @@ -137,5 +153,34 @@ Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MI
}
};

/**
* Callback for rename variable dropdown menu option associated with a
* variable getter block.
* @param {!Blockly.Block} block The block with the variable to rename.
* @return {!function()} A function that renames the variable.
*/
Blockly.Constants.VariablesDynamic.RENAME_OPTION_CALLBACK_FACTORY = function(block) {
return function() {
var workspace = block.workspace;
var variable = block.getField('VAR').getVariable();
Blockly.Variables.renameVariable(workspace, variable);
};
};

/**
* Callback for delete variable dropdown menu option associated with a
* variable getter block.
* @param {!Blockly.Block} block The block with the variable to delete.
* @return {!function()} A function that deletes the variable.
*/
Blockly.Constants.VariablesDynamic.DELETE_OPTION_CALLBACK_FACTORY = function(block) {
return function() {
var workspace = block.workspace;
var variable = block.getField('VAR').getVariable();
workspace.deleteVariableById(variable.getId());
workspace.refreshToolboxSelection();
};
};

Blockly.Extensions.registerMixin('contextMenu_variableDynamicSetterGetter',
Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN);