Add ability to add blocks by type (#3928)

* Add ability to add blocks by type
This commit is contained in:
alschmiedt
2020-06-04 12:49:04 -07:00
committed by GitHub
parent 4b0e202761
commit 26e84fef11
3 changed files with 111 additions and 97 deletions

View File

@@ -539,7 +539,7 @@ Blockly.Flyout.prototype.createFlyoutInfo_ = function(parsedContent) {
var block = this.createBlock_(blockXml);
// This is a deprecated method for adding gap to a block.
// <block type="math_arithmetic" gap="8"></block>
var gap = parseInt(blockXml.getAttribute('gap'), 10);
var gap = parseInt(blockInfo['gap'] || blockXml.getAttribute('gap'), 10);
gaps.push(isNaN(gap) ? defaultGap : gap);
contents.push({type: 'block', block: block});
break;
@@ -612,11 +612,16 @@ Blockly.Flyout.prototype.createBlock_ = function(blockXml) {
* @private
*/
Blockly.Flyout.prototype.getBlockXml_ = function(blockInfo) {
var blockXml = blockInfo['blockxml'];
if (blockXml) {
var blockXml = null;
// All blockInfo will have type, so check for blockxml first.
if (blockInfo['blockxml']) {
blockXml = Blockly.Xml.textToDom(blockInfo['blockxml']);
} else if (blockInfo['type']) {
blockXml = Blockly.utils.xml.createElement('xml');
blockXml.setAttribute('type', blockInfo['type']);
blockXml.setAttribute('disabled', blockInfo['disabled']);
} else {
throw Error('Error: Invalid block definition. Block definition must have blockxml.');
throw Error('Error: Invalid block definition. Block definition must have blockxml or type.');
}
return blockXml;
};

View File

@@ -17,7 +17,10 @@ goog.provide('Blockly.utils.toolbox');
* The information needed to create a block in the toolbox.
* @typedef {{
* kind:string,
* blockxml:(string|Node)
* blockxml:(?string|Node),
* type: ?string,
* gap: (?string|?number),
* disabled: (?string|?boolean)
* }}
*/
Blockly.utils.toolbox.Block;