Fix #1619. buildTooltipWithFieldValue() => buildTooltipWithFieldText() (#1638)

Fix #1619. buildTooltipWithFieldValue() => buildTooltipWithFieldText()

The "value" of FieldVariables was the variable id.
However, we usually want the field text for the human visible tooltip.
Refactoring and renaming buildTooltipWithFieldValue to use the field
text.
This commit is contained in:
Andrew n marshall
2018-02-16 16:51:18 -08:00
committed by GitHub
parent 4092a644d5
commit f45c6faf05
4 changed files with 13 additions and 33 deletions

View File

@@ -280,11 +280,11 @@ Blockly.Extensions.registerMixin('contextMenu_newGetVariableBlock',
Blockly.Constants.Loops.CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN);
Blockly.Extensions.register('controls_for_tooltip',
Blockly.Extensions.buildTooltipWithFieldValue(
Blockly.Extensions.buildTooltipWithFieldText(
'%{BKY_CONTROLS_FOR_TOOLTIP}', 'VAR'));
Blockly.Extensions.register('controls_forEach_tooltip',
Blockly.Extensions.buildTooltipWithFieldValue(
Blockly.Extensions.buildTooltipWithFieldText(
'%{BKY_CONTROLS_FOREACH_TOOLTIP}', 'VAR'));
/**

View File

@@ -492,20 +492,9 @@ Blockly.Extensions.registerMutator('math_is_divisibleby_mutator',
Blockly.Constants.Math.IS_DIVISIBLEBY_MUTATOR_MIXIN,
Blockly.Constants.Math.IS_DIVISIBLE_MUTATOR_EXTENSION);
/**
* Update the tooltip of 'math_change' block to reference the variable.
* @this Blockly.Block
* @package
*/
Blockly.Constants.Math.CHANGE_TOOLTIP_EXTENSION = function() {
this.setTooltip(function() {
return Blockly.Msg.MATH_CHANGE_TOOLTIP.replace('%1',
this.getFieldValue('VAR'));
}.bind(this));
};
// Update the tooltip of 'math_change' block to reference the variable.
Blockly.Extensions.register('math_change_tooltip',
Blockly.Extensions.buildTooltipWithFieldValue(
Blockly.Extensions.buildTooltipWithFieldText(
'%{BKY_MATH_CHANGE_TOOLTIP}', 'VAR'));
/**

View File

@@ -773,17 +773,10 @@ Blockly.Constants.Text.TEXT_JOIN_EXTENSION = function() {
this.setMutator(new Blockly.Mutator(['text_create_join_item']));
};
Blockly.Constants.Text.TEXT_APPEND_TOOLTIP_EXTENSION = function() {
// Assign 'this' to a variable for use in the tooltip closure below.
var thisBlock = this;
this.setTooltip(function() {
if (Blockly.Msg.TEXT_APPEND_TOOLTIP) {
return Blockly.Msg.TEXT_APPEND_TOOLTIP.replace('%1',
thisBlock.getFieldValue('VAR'));
}
return '';
});
};
// Update the tooltip of 'text_append' block to reference the variable.
Blockly.Extensions.register('text_append_tooltip',
Blockly.Extensions.buildTooltipWithFieldText(
'%{BKY_TEXT_APPEND_TOOLTIP}', 'VAR'));
Blockly.Constants.Text.TEXT_INDEXOF_TOOLTIP_EXTENSION = function() {
// Assign 'this' to a variable for use in the tooltip closure below.
@@ -889,9 +882,6 @@ Blockly.Extensions.register('text_indexOf_tooltip',
Blockly.Extensions.register('text_quotes',
Blockly.Constants.Text.TEXT_QUOTES_EXTENSION);
Blockly.Extensions.register('text_append_tooltip',
Blockly.Constants.Text.TEXT_APPEND_TOOLTIP_EXTENSION);
Blockly.Extensions.registerMutator('text_join_mutator',
Blockly.Constants.Text.TEXT_JOIN_MUTATOR_MIXIN,
Blockly.Constants.Text.TEXT_JOIN_EXTENSION);

View File

@@ -397,13 +397,13 @@ Blockly.Extensions.checkDropdownOptionsInTable_ = function(block, dropdownName,
/**
* Builds an extension function that will install a dynamic tooltip. The
* tooltip message should include the string '%1' and that string will be
* replaced with the value of the named field.
* replaced with the text of the named field.
* @param {string} msgTemplate The template form to of the message text, with
* %1 placeholder.
* @param {string} fieldName The field with the replacement value.
* @param {string} fieldName The field with the replacement text.
* @returns {Function} The extension function.
*/
Blockly.Extensions.buildTooltipWithFieldValue = function(msgTemplate,
Blockly.Extensions.buildTooltipWithFieldText = function(msgTemplate,
fieldName) {
// Check the tooltip string messages for invalid references.
// Wait for load, in case Blockly.Msg is not yet populated.
@@ -422,8 +422,9 @@ Blockly.Extensions.buildTooltipWithFieldValue = function(msgTemplate,
*/
var extensionFn = function() {
this.setTooltip(function() {
var field = this.getField(fieldName);
return Blockly.utils.replaceMessageReferences(msgTemplate)
.replace('%1', this.getFieldValue(fieldName));
.replace('%1', field ? field.getText() : '');
}.bind(this));
};
return extensionFn;