mirror of
https://github.com/google/blockly.git
synced 2026-01-08 09:30:06 +01:00
Adding extensions for JSON support of dynamic blocks. (#834)
Adding support for extensions, functions that can assist with loading blocks, much like init functions, but that can be referenced from JSON definitions. This allows JSON definitions to define dynamic blocks such as onchange handlers and mutators. Rewrote math_number as an example pure JSON block.
This commit is contained in:
committed by
GitHub
parent
bf16f11dbd
commit
a47bd93f4c
@@ -34,27 +34,31 @@ goog.require('Blockly.Blocks');
|
||||
*/
|
||||
Blockly.Blocks.math.HUE = 230;
|
||||
|
||||
Blockly.Blocks['math_number'] = {
|
||||
/**
|
||||
* Block for numeric value.
|
||||
* @this Blockly.Block
|
||||
*/
|
||||
init: function() {
|
||||
this.setHelpUrl(Blockly.Msg.MATH_NUMBER_HELPURL);
|
||||
this.setColour(Blockly.Blocks.math.HUE);
|
||||
this.appendDummyInput()
|
||||
.appendField(new Blockly.FieldNumber('0'), 'NUM');
|
||||
this.setOutput(true, 'Number');
|
||||
// Assign 'this' to a variable for use in the tooltip closure below.
|
||||
var thisBlock = this;
|
||||
// Number block is trivial. Use tooltip of parent block if it exists.
|
||||
this.setTooltip(function() {
|
||||
var parent = thisBlock.getParent();
|
||||
return (parent && parent.getInputsInline() && parent.tooltip) ||
|
||||
Blockly.Msg.MATH_NUMBER_TOOLTIP;
|
||||
});
|
||||
}
|
||||
};
|
||||
// Block for numeric value.
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "math_number",
|
||||
"message0": "%1",
|
||||
"args0": [{
|
||||
"type": "field_number",
|
||||
"name": "NUM",
|
||||
"value": 0
|
||||
}],
|
||||
"output": "Number",
|
||||
"colour": "%{BKY_MATH_HUE}",
|
||||
"helpUrl": "%{BKY_MATH_NUMBER_HELPURL}",
|
||||
"extensions": ["math_number_tooltip"]
|
||||
}]);
|
||||
|
||||
Blockly.Extensions.register('math_number_tooltip', function() {
|
||||
// Use the parent's tooltip when attached to an inline value input.
|
||||
this.setTooltip(function() {
|
||||
var parent = this.getParent();
|
||||
return (parent &&
|
||||
parent.getInputsInline() &&
|
||||
parent.tooltip) ||
|
||||
Blockly.Msg.MATH_NUMBER_TOOLTIP;
|
||||
}.bind(this));
|
||||
});
|
||||
|
||||
Blockly.Blocks['math_arithmetic'] = {
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user