From 64c8e72ebe594d64c23bdbb47698a7d43c652f66 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Tue, 12 Apr 2016 12:25:59 +0200 Subject: [PATCH 1/3] convert typeName to typeId --- blockly/core/type.js | 20 ++++++++++---------- blockly/generators/arduino.js | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/blockly/core/type.js b/blockly/core/type.js index 3605fdb85f..fce657bfa1 100644 --- a/blockly/core/type.js +++ b/blockly/core/type.js @@ -16,13 +16,13 @@ goog.require('goog.asserts'); /** * Blockly Type class constructor. - * @param {Object} args Object/dictionary with typeName, and compatibleTypes. + * @param {Object} args Object/dictionary with typeId, and compatibleTypes. * @constructor */ Blockly.Type = function(args) { - if ((args.typeName === undefined) || (args.compatibleTypes === undefined)) { + if ((args.typeId === undefined) || (args.compatibleTypes === undefined)) { throw new Error('Creating a Type requires the following format:\n{\n' + - ' typeName: string,\n' + + ' typeId: string,\n' + ' compatibleTypes: [Blockly.Type,]\n}'); } if (!goog.isArray(args.compatibleTypes)) { @@ -30,7 +30,7 @@ Blockly.Type = function(args) { 'array of Blockly.Type items.'); } /** @type {string} */ - this.typeName = args.typeName; + this.typeId = args.typeId; /** * @type {Array} * @private @@ -48,7 +48,7 @@ Blockly.Type = function(args) { /** Getter for the output property, used for block output types. */ Object.defineProperty(Blockly.Type.prototype, 'output', { get: function() { - return this.typeName; + return this.typeId; }, set: function(value) { console.warn('"Blockly.Type" property "output" is not allowed to be set.'); @@ -75,12 +75,12 @@ Blockly.Type.prototype.generateCheckList_ = function(compatibleType) { for (var i = 0; i < this.compatibleTypes_.length; i++) { var unique = true; for (var j = 0; j < this.generatedCheckList_.length; j++) { - if (this.generatedCheckList_[j] === this.compatibleTypes_[i].typeName) { + if (this.generatedCheckList_[j] === this.compatibleTypes_[i].typeId) { unique = false; } } if (unique) { - this.generatedCheckList_.push(this.compatibleTypes_[i].typeName); + this.generatedCheckList_.push(this.compatibleTypes_[i].typeId); } } }; @@ -92,7 +92,7 @@ Blockly.Type.prototype.generateCheckList_ = function(compatibleType) { Blockly.Type.prototype.addCompatibleType = function(compatibleType) { if (!compatibleType || !compatibleType.constructor || !(compatibleType instanceof Blockly.Type)) { - throw new Error('To add a compatible type to ' + this.typeName + + throw new Error('To add a compatible type to ' + this.typeId + ' provide a Blockly.Type object.'); } this.compatibleTypes_.push(compatibleType); @@ -107,12 +107,12 @@ Blockly.Type.prototype.addCompatibleType = function(compatibleType) { Blockly.Type.prototype.addCompatibleTypes = function(compatibleTypeArray) { if (!goog.isArray(compatibleTypeArray)) { throw new Error('To add compatible types to the Blockly Type ' + - this.typeName +' provide an array of Blockly.Type items.'); + this.typeId +' provide an array of Blockly.Type items.'); } for (var i = 0; i < compatibleTypeArray.length; i++) { if (!compatibleTypeArray[i] || !compatibleTypeArray[i].constructor || !(compatibleTypeArray[i] instanceof Blockly.Type)) { - throw new Error('To add a compatible type to ' + this.typeName + ' you ' + + throw new Error('To add a compatible type to ' + this.typeId + ' you ' + 'must point to a Blockly.Type object.'); } this.compatibleTypes_.push(compatibleTypeArray[i]); diff --git a/blockly/generators/arduino.js b/blockly/generators/arduino.js index 0e9b713d0f..12cc5c720d 100644 --- a/blockly/generators/arduino.js +++ b/blockly/generators/arduino.js @@ -368,26 +368,26 @@ Blockly.Arduino.scrub_ = function(block, code) { * @private */ Blockly.Arduino.getArduinoType_ = function(typeBlockly) { - switch (typeBlockly.typeName) { - case Blockly.Types.SHORT_NUMBER.typeName: + switch (typeBlockly.typeId) { + case Blockly.Types.SHORT_NUMBER.typeId: return 'char'; - case Blockly.Types.NUMBER.typeName: + case Blockly.Types.NUMBER.typeId: return 'int'; - case Blockly.Types.LARGE_NUMBER.typeName: + case Blockly.Types.LARGE_NUMBER.typeId: return 'long'; - case Blockly.Types.DECIMAL.typeName: + case Blockly.Types.DECIMAL.typeId: return 'float'; - case Blockly.Types.TEXT.typeName: + case Blockly.Types.TEXT.typeId: return 'String'; - case Blockly.Types.CHARACTER.typeName: + case Blockly.Types.CHARACTER.typeId: return 'char'; - case Blockly.Types.BOOLEAN.typeName: + case Blockly.Types.BOOLEAN.typeId: return 'boolean'; - case Blockly.Types.NULL.typeName: + case Blockly.Types.NULL.typeId: return 'void'; - case Blockly.Types.UNDEF.typeName: + case Blockly.Types.UNDEF.typeId: return 'undefined'; - case Blockly.Types.CHILD_BLOCK_MISSING.typeName: + case Blockly.Types.CHILD_BLOCK_MISSING.typeId: // If no block connected default to int, change for easier debugging //return 'ChildBlockMissing'; return 'int'; From 43d11b376b0d3163d974048feb0969e2acf5f0d9 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Tue, 12 Apr 2016 12:32:04 +0200 Subject: [PATCH 2/3] Finish rename typeName to typeId --- blockly/core/types.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/blockly/core/types.js b/blockly/core/types.js index 5f37f45259..3b6dd73e62 100644 --- a/blockly/core/types.js +++ b/blockly/core/types.js @@ -15,43 +15,43 @@ goog.require('Blockly.Type'); /** Single character. */ Blockly.Types.CHARACTER = new Blockly.Type({ - typeName: 'Character', + typeId: 'Character', compatibleTypes: [] }); /** Text string. */ Blockly.Types.TEXT = new Blockly.Type({ - typeName: 'Text', + typeId: 'Text', compatibleTypes: [Blockly.Types.CHARACTER] }); /** Boolean. */ Blockly.Types.BOOLEAN = new Blockly.Type({ - typeName: 'Boolean', + typeId: 'Boolean', compatibleTypes: [] }); /** Short integer number. */ Blockly.Types.SHORT_NUMBER = new Blockly.Type({ - typeName: 'Short Positive Number', + typeId: 'Short Positive Number', compatibleTypes: [] // Circular dependencies, add after all declarations }); /** Integer number. */ Blockly.Types.NUMBER = new Blockly.Type({ - typeName: 'Number', + typeId: 'Number', compatibleTypes: [] // Circular dependencies, add after all declarations }); /** Large integer number. */ Blockly.Types.LARGE_NUMBER = new Blockly.Type({ - typeName: 'Large Number', + typeId: 'Large Number', compatibleTypes: [] // Circular dependencies, add after all declarations }); /** Decimal/floating point number. */ Blockly.Types.DECIMAL = new Blockly.Type({ - typeName: 'Decimal', + typeId: 'Decimal', compatibleTypes: [Blockly.Types.BOOLEAN, Blockly.Types.SHORT_NUMBER, Blockly.Types.NUMBER, @@ -60,25 +60,25 @@ Blockly.Types.DECIMAL = new Blockly.Type({ /** Array/List of items. */ Blockly.Types.ARRAY = new Blockly.Type({ - typeName: 'Array', + typeId: 'Array', compatibleTypes: [], }); /** Null indicate there is no type. */ Blockly.Types.NULL = new Blockly.Type({ - typeName: 'Null', + typeId: 'Null', compatibleTypes: [], }); /** Type not defined, or not yet defined. */ Blockly.Types.UNDEF = new Blockly.Type({ - typeName: 'Undefined', + typeId: 'Undefined', compatibleTypes: [], }); /** Set when no child block (meant to define the variable type) is connected. */ Blockly.Types.CHILD_BLOCK_MISSING = new Blockly.Type({ - typeName: 'ChildBlockMissing', + typeId: 'ChildBlockMissing', compatibleTypes: [], }); @@ -107,18 +107,18 @@ Blockly.Types.LARGE_NUMBER.addCompatibleTypes([ /** * Adds another type to the Blockly.Types collection. - * @param {string} typeName_ Identifiable name of the type. + * @param {string} typeId_ Identifiable name of the type. * @param {Array} compatibleTypes_ List of types this Type is * compatible with. */ -Blockly.Types.addType = function(typeName_, compatibleTypes_) { +Blockly.Types.addType = function(typeId_, compatibleTypes_) { // The name is used as the key from the value pair in the BlocklyTypes object - var key = typeName.toUpperCase().replace(/ /g, '_'); + var key = typeId.toUpperCase().replace(/ /g, '_'); if (Blockly.Types[key] !== undefined) { throw 'The Blockly type ' + key + ' already exists.'; } Blockly.Types[key] = new Blockly.Type({ - typeName: typeName_, + typeId: typeId_, compatibleTypes: compatibleTypes_, }); }; @@ -135,7 +135,7 @@ Blockly.Types.getValidTypeArray = function() { (typeKey !== 'NULL') && (typeKey !== 'ARRAY') && (typeof Blockly.Types[typeKey] !== 'function') && !(Blockly.Types[typeKey] instanceof RegExp)) { - typesArray.push([Blockly.Types[typeKey].typeName, typeKey]); + typesArray.push([Blockly.Types[typeKey].typeId, typeKey]); } } return typesArray; From 23a80604711e547ead18f129fb73e511ebb201d7 Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Tue, 12 Apr 2016 15:31:54 +0200 Subject: [PATCH 3/3] Making type translatable in the UI --- blockly/core/type.js | 9 ++++++++- blockly/core/types.js | 31 ++++++++++++++++++++++--------- blockly/msg/json/nl.json | 11 +++++++++++ blockly/msg/messages.js | 11 +++++++++++ 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/blockly/core/type.js b/blockly/core/type.js index fce657bfa1..f4c3f9e923 100644 --- a/blockly/core/type.js +++ b/blockly/core/type.js @@ -20,9 +20,11 @@ goog.require('goog.asserts'); * @constructor */ Blockly.Type = function(args) { - if ((args.typeId === undefined) || (args.compatibleTypes === undefined)) { + if ((args.typeId === undefined) || (args.typeName === undefined) || + (args.compatibleTypes === undefined)) { throw new Error('Creating a Type requires the following format:\n{\n' + ' typeId: string,\n' + + ' typeName: function returning a string,\n' + ' compatibleTypes: [Blockly.Type,]\n}'); } if (!goog.isArray(args.compatibleTypes)) { @@ -31,6 +33,11 @@ Blockly.Type = function(args) { } /** @type {string} */ this.typeId = args.typeId; + /** @type {function} + This is the translatable name. As translation is only present past build, + pass a function to return translated string. + */ + this.typeName = args.typeName; /** * @type {Array} * @private diff --git a/blockly/core/types.js b/blockly/core/types.js index 3b6dd73e62..eb52a0a568 100644 --- a/blockly/core/types.js +++ b/blockly/core/types.js @@ -16,42 +16,49 @@ goog.require('Blockly.Type'); /** Single character. */ Blockly.Types.CHARACTER = new Blockly.Type({ typeId: 'Character', + typeName: function() {return Blockly.Msg.ARD_TYPE_CHAR;}, compatibleTypes: [] }); /** Text string. */ Blockly.Types.TEXT = new Blockly.Type({ typeId: 'Text', + typeName: function() {return Blockly.Msg.ARD_TYPE_TEXT;}, compatibleTypes: [Blockly.Types.CHARACTER] }); /** Boolean. */ Blockly.Types.BOOLEAN = new Blockly.Type({ typeId: 'Boolean', + typeName: function() {return Blockly.Msg.ARD_TYPE_BOOL;}, compatibleTypes: [] }); /** Short integer number. */ Blockly.Types.SHORT_NUMBER = new Blockly.Type({ typeId: 'Short Positive Number', + typeName: function() {return Blockly.Msg.ARD_TYPE_SHORTPOS;}, compatibleTypes: [] // Circular dependencies, add after all declarations }); /** Integer number. */ Blockly.Types.NUMBER = new Blockly.Type({ typeId: 'Number', + typeName: function() {return Blockly.Msg.ARD_TYPE_NUMBER;}, compatibleTypes: [] // Circular dependencies, add after all declarations }); /** Large integer number. */ Blockly.Types.LARGE_NUMBER = new Blockly.Type({ typeId: 'Large Number', + typeName: function() {return Blockly.Msg.ARD_TYPE_LONG;}, compatibleTypes: [] // Circular dependencies, add after all declarations }); /** Decimal/floating point number. */ Blockly.Types.DECIMAL = new Blockly.Type({ typeId: 'Decimal', + typeName: function() {return Blockly.Msg.ARD_TYPE_DECIMAL;}, compatibleTypes: [Blockly.Types.BOOLEAN, Blockly.Types.SHORT_NUMBER, Blockly.Types.NUMBER, @@ -61,25 +68,29 @@ Blockly.Types.DECIMAL = new Blockly.Type({ /** Array/List of items. */ Blockly.Types.ARRAY = new Blockly.Type({ typeId: 'Array', - compatibleTypes: [], + typeName: function() {return Blockly.Msg.ARD_TYPE_ARRAY}, + compatibleTypes: [] }); /** Null indicate there is no type. */ Blockly.Types.NULL = new Blockly.Type({ typeId: 'Null', - compatibleTypes: [], + typeName: function() {return Blockly.Msg.ARD_TYPE_NULL;}, + compatibleTypes: [] }); /** Type not defined, or not yet defined. */ Blockly.Types.UNDEF = new Blockly.Type({ typeId: 'Undefined', - compatibleTypes: [], + typeName: function() {return Blockly.Msg.ARD_TYPE_UNDEF;}, + compatibleTypes: [] }); /** Set when no child block (meant to define the variable type) is connected. */ Blockly.Types.CHILD_BLOCK_MISSING = new Blockly.Type({ typeId: 'ChildBlockMissing', - compatibleTypes: [], + typeName: function() {return Blockly.Msg.ARD_TYPE_CHILDBLOCKMISSING;}, + compatibleTypes: [] }); /** @@ -108,18 +119,20 @@ Blockly.Types.LARGE_NUMBER.addCompatibleTypes([ /** * Adds another type to the Blockly.Types collection. * @param {string} typeId_ Identifiable name of the type. + * @param {string} typeName_ Descriptive name of the type for use in the UI. * @param {Array} compatibleTypes_ List of types this Type is * compatible with. */ -Blockly.Types.addType = function(typeId_, compatibleTypes_) { - // The name is used as the key from the value pair in the BlocklyTypes object - var key = typeId.toUpperCase().replace(/ /g, '_'); +Blockly.Types.addType = function(typeId_, typeName_, compatibleTypes_) { + // The Id is used as the key from the value pair in the BlocklyTypes object + var key = typeId_.toUpperCase().replace(/ /g, '_'); if (Blockly.Types[key] !== undefined) { throw 'The Blockly type ' + key + ' already exists.'; } Blockly.Types[key] = new Blockly.Type({ typeId: typeId_, - compatibleTypes: compatibleTypes_, + typeName: function() {return typeName_;}, + compatibleTypes: compatibleTypes_ }); }; @@ -135,7 +148,7 @@ Blockly.Types.getValidTypeArray = function() { (typeKey !== 'NULL') && (typeKey !== 'ARRAY') && (typeof Blockly.Types[typeKey] !== 'function') && !(Blockly.Types[typeKey] instanceof RegExp)) { - typesArray.push([Blockly.Types[typeKey].typeId, typeKey]); + typesArray.push([Blockly.Types[typeKey].typeName(), typeKey]); } } return typesArray; diff --git a/blockly/msg/json/nl.json b/blockly/msg/json/nl.json index 1b0f0f4c72..fb69924f19 100644 --- a/blockly/msg/json/nl.json +++ b/blockly/msg/json/nl.json @@ -355,6 +355,17 @@ "PROCEDURES_IFRETURN_WARNING": "Waarschuwing: dit blok mag alleen gebruikt worden binnen de definitie van een functie.", "REPLACE_EXISTING_BLOCKS": "Vervang aanwezige blokken? \"Annuleren\" zal blokken samenvoegen", "WHAT_NAME_FOR_FILE": "Welke naam wil je geven aan je bestand?", + "ARD_TYPE_CHAR": "Letter", + "ARD_TYPE_TEXT": "Tekst", + "ARD_TYPE_BOOL": "Bool (0 of 1)", + "ARD_TYPE_SHORTPOS": "Kort Positief Nummer", + "ARD_TYPE_NUMBER": "Nummer", + "ARD_TYPE_LONG": "Groot Nummer", + "ARD_TYPE_DECIMAL": "Decimaal Getal", + "ARD_TYPE_ARRAY": "Lijst", + "ARD_TYPE_NULL": "Geen Type", + "ARD_TYPE_UNDEF": "Ongedefineerd", + "ARD_TYPE_CHILDBLOCKMISSING": "ChildBlockMissing", "ARD_HIGH": "HOOG", "ARD_LOW": "LAAG", "ARD_ANALOGREAD": "lees analoge pin#", diff --git a/blockly/msg/messages.js b/blockly/msg/messages.js index 0011766381..244587d955 100644 --- a/blockly/msg/messages.js +++ b/blockly/msg/messages.js @@ -1100,6 +1100,17 @@ Blockly.Msg.PROCEDURES_IFRETURN_HELPURL = 'http://c2.com/cgi/wiki?GuardClause'; /// warning - This appears if the user tries to use this block outside of a function definition. Blockly.Msg.PROCEDURES_IFRETURN_WARNING = 'Warning: This block may be used only within a function definition.'; //Arduino blocks define as a specific type +Blockly.Msg.ARD_TYPE_CHAR = 'Character'; +Blockly.Msg.ARD_TYPE_TEXT = 'Text'; +Blockly.Msg.ARD_TYPE_BOOL = 'Boolean'; +Blockly.Msg.ARD_TYPE_SHORTPOS = 'Short Positive Number'; +Blockly.Msg.ARD_TYPE_NUMBER = 'Number'; +Blockly.Msg.ARD_TYPE_LONG = 'Large Number'; +Blockly.Msg.ARD_TYPE_DECIMAL = 'Decimal'; +Blockly.Msg.ARD_TYPE_ARRAY = 'Array'; +Blockly.Msg.ARD_TYPE_NULL = 'Null'; +Blockly.Msg.ARD_TYPE_UNDEF = 'Undefined'; +Blockly.Msg.ARD_TYPE_CHILDBLOCKMISSING = 'ChildBlockMissing'; Blockly.Msg.ARD_HIGH = 'HIGH'; Blockly.Msg.ARD_LOW = 'LOW'; Blockly.Msg.ARD_ANALOGREAD = 'read analog pin#';