diff --git a/core/flyout_base.js b/core/flyout_base.js index 06d51188d..f60a0108b 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -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. // - 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; }; diff --git a/core/utils/toolbox.js b/core/utils/toolbox.js index 5509cac9a..458da19e5 100644 --- a/core/utils/toolbox.js +++ b/core/utils/toolbox.js @@ -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; diff --git a/tests/playgrounds/test_blocks_toolbox.js b/tests/playgrounds/test_blocks_toolbox.js index 2a1a6a90f..1ee392ff8 100644 --- a/tests/playgrounds/test_blocks_toolbox.js +++ b/tests/playgrounds/test_blocks_toolbox.js @@ -19,15 +19,15 @@ var alignCategory = { "contents": [ { "kind": "BLOCK", - "blockxml": "", + "type": "test_align_dummy_right", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_align_all", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_align_with_external_input", } ], }; @@ -38,47 +38,47 @@ var basicCategory = { "contents": [ { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_empty", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_empty_with_mutator", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_dummy", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_multiple_dummy", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_stack", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_row", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_value_to_stack", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_value_to_statement", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_limit_instances", }, { "kind": "BLOCK", - "blockxml": "" + "type": "test_basic_tooltips" }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_basic_javascript", } ], }; @@ -110,7 +110,7 @@ var rowCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_row_input", }, { "kind": "SEP", @@ -118,7 +118,7 @@ var rowCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_row_blue", }, { "kind": "SEP", @@ -126,7 +126,7 @@ var rowCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_row_yellow", }, { "kind": "SEP", @@ -134,7 +134,7 @@ var rowCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_row_red", }, { "kind": "SEP", @@ -142,7 +142,7 @@ var rowCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_row_output", }, { "kind": "BLOCK", @@ -154,7 +154,7 @@ var rowCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_multivalue_1valid", }, { "kind": "SEP", @@ -162,7 +162,7 @@ var rowCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_multivalue_2valid", } ], }; @@ -194,7 +194,7 @@ var stackCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_stack_next", }, { "kind": "SEP", @@ -202,7 +202,7 @@ var stackCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_stack_blue", }, { "kind": "SEP", @@ -210,7 +210,7 @@ var stackCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_stack_yellow", }, { "kind": "SEP", @@ -218,7 +218,7 @@ var stackCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_stack_red", }, { "kind": "SEP", @@ -226,7 +226,7 @@ var stackCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_stack_prev", }, { "kind": "BLOCK", @@ -262,7 +262,7 @@ var statementCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_statement_blue", }, { "kind": "SEP", @@ -270,7 +270,7 @@ var statementCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_statement_yellow", }, { "kind": "SEP", @@ -278,11 +278,11 @@ var statementCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_statement_red", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_statement_nonext", }, { "kind": "SEP", @@ -290,7 +290,7 @@ var statementCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_multistatement_1valid", }, { "kind": "SEP", @@ -298,7 +298,7 @@ var statementCategory = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_connections_multistatement_2valid", } ], }; @@ -393,35 +393,35 @@ var fieldDefaults = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_angle", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_checkbox", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_colour", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_colour_options", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_text_input", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_only_text_input", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_multilinetext", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_variable", }, { "kind": "BUTTON", @@ -434,7 +434,7 @@ var fieldDefaults = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_label_serializable", }, { "kind": "BUTTON", @@ -447,7 +447,7 @@ var fieldDefaults = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_fields_image", } ], }; @@ -489,31 +489,31 @@ var fieldAngles = { "contents": [ { "kind": "BLOCK", - "blockxml": "", + "type": "test_angles_clockwise", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_angles_offset", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_angles_wrap", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_angles_round_30", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_angles_round_0", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_angles_protractor", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_angles_compass", } ], }; @@ -528,7 +528,7 @@ var fieldDropDowns = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_dropdowns_dynamic", }, { "kind": "BUTTON", @@ -542,7 +542,7 @@ var fieldDropDowns = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_dropdowns_dynamic_random", }, { "kind": "LABEL", @@ -550,15 +550,15 @@ var fieldDropDowns = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_dropdowns_long", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_dropdowns_images", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_dropdowns_images_and_text", } ], }; @@ -569,31 +569,31 @@ var fieldImages = { "contents": [ { "kind": "BLOCK", - "blockxml": "", + "type": "test_images_datauri", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_images_small", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_images_large", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_images_fliprtl", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_images_missing", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_images_many_icons", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_images_clickhandler", } ], }; @@ -608,7 +608,7 @@ var fieldEmoji = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_style_emoji", }, { "kind": "BLOCK", @@ -649,7 +649,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_dispose_block", }, { "kind": "LABEL", @@ -661,7 +661,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_angle_null", }, { "kind": "SEP", @@ -669,7 +669,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_angle_mult30_force", }, { "kind": "SEP", @@ -677,7 +677,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_angle_mult30_null", }, { "kind": "LABEL", @@ -689,7 +689,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_checkbox_null", }, { "kind": "SEP", @@ -697,7 +697,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_checkbox_match", }, { "kind": "SEP", @@ -705,7 +705,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_checkbox_not_match_null", }, { "kind": "LABEL", @@ -717,7 +717,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_colour_null", }, { "kind": "SEP", @@ -725,7 +725,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_colour_force_red", }, { "kind": "SEP", @@ -733,7 +733,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_colour_red_null", }, { "kind": "LABEL", @@ -745,7 +745,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_dropdown_null", }, { "kind": "SEP", @@ -753,7 +753,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_dropdown_force_1s", }, { "kind": "SEP", @@ -761,7 +761,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_dropdown_1s_null", }, { "kind": "LABEL", @@ -773,7 +773,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_number_null", }, { "kind": "SEP", @@ -781,7 +781,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_number_mult10_force", }, { "kind": "SEP", @@ -789,7 +789,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_number_mult10_null", }, { "kind": "LABEL", @@ -801,7 +801,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_text_null", }, { "kind": "SEP", @@ -809,7 +809,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_text_A", }, { "kind": "SEP", @@ -817,7 +817,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_text_B", }, { "kind": "LABEL", @@ -839,7 +839,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_variable_null", }, { "kind": "SEP", @@ -847,7 +847,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_variable_force_1s", }, { "kind": "SEP", @@ -855,7 +855,7 @@ var fieldValidators = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_validators_variable_1s_null", } ], }; @@ -889,7 +889,7 @@ var style = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_style_hat", }, { "kind": "LABEL", @@ -897,28 +897,34 @@ var style = { }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_style_hex1", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_style_hex2", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_style_hex3", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_style_no_colour", }, { "kind": "BLOCK", - "blockxml": "", + "type": "test_style_hex4", }, { "kind": "BLOCK", - "blockxml": "", - } + "type": "test_style_hex5", + "gap": "40", + }, + { + "kind": "BLOCK", + "type": "test_style_hex5", + "disabled": "true", + }, ], };