Allows user to define style name using variables in json

This commit is contained in:
alschmiedt
2018-12-18 10:50:49 -08:00
parent c226654b77
commit be78838335
2 changed files with 13 additions and 15 deletions

View File

@@ -933,19 +933,20 @@ Blockly.Block.prototype.setColour = function(colour) {
* @throws {Error} if the block style does not exist.
*/
Blockly.Block.prototype.setStyle = function(blockStyleName) {
if (blockStyleName) {
var blockStyle = Blockly.getStyle().getBlockStyle(blockStyleName);
var dereferenced = Blockly.utils.replaceMessageReferences(blockStyleName);
var blockStyle = Blockly.getStyle().getBlockStyle(dereferenced);
this.styleName_ = dereferenced;
if (blockStyle) {
this.style_ = blockStyle;
this.setColour(blockStyle.primaryColour);
this.secondaryColour_ = blockStyle.secondaryColour;
this.tertiaryColour_ = blockStyle.tertiaryColour;
}
else {
var errorMsg = 'Invalid style name: ' + blockStyleName;
throw errorMsg;
}
if (blockStyle) {
this.style_ = blockStyle;
this.secondaryColour_ = blockStyle.secondaryColour;
this.tertiaryColour_ = blockStyle.tertiaryColour;
// Set colour will trigger an updateColour() on a block_svg
this.setColour(blockStyle.primaryColour);
}
else {
var errorMsg = 'Invalid style name: ' + blockStyleName;
throw errorMsg;
}
};
@@ -1449,14 +1450,12 @@ Blockly.Block.prototype.jsonInitColour_ = function(json, warningPrefix) {
Blockly.Block.prototype.jsonInitStyle_ = function(json, warningPrefix) {
var blockStyleName = json['style'];
try {
this.styleName_ = blockStyleName;
this.setStyle(blockStyleName);
} catch (colorError) {
console.warn(warningPrefix + 'Style does not exist: ', blockStyleName);
}
};
/**
* Add key/values from mixinObj to this block object. By default, this method
* will check that the keys in mixinObj will not overwrite existing values in

View File

@@ -688,7 +688,6 @@ Blockly.setStyle = function(style){
for (var i = 0; i < blocks.length; i++) {
var block = blocks[i];
block.setStyle(block.getStyleName());
block.updateColour();
}
var event = new Blockly.Events.Ui(null, 'styleChanged');