From f1977fd5e34c7a921ac40cafc9e28b7d9f427096 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Thu, 11 Jun 2020 09:28:40 -0700 Subject: [PATCH] No flyout mutator --- core/mutator.js | 20 ++++--- tests/blocks/test_blocks.js | 74 ++++++++++++++++++++++++ tests/playgrounds/test_blocks_toolbox.js | 6 +- 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/core/mutator.js b/core/mutator.js index 2017d9bc1..884f1ca46 100644 --- a/core/mutator.js +++ b/core/mutator.js @@ -167,8 +167,12 @@ Blockly.Mutator.prototype.createEditor_ = function() { })); workspaceOptions.toolboxPosition = this.block_.RTL ? Blockly.TOOLBOX_AT_RIGHT : Blockly.TOOLBOX_AT_LEFT; - workspaceOptions.languageTree = Blockly.utils.toolbox.convertToolboxToJSON(quarkXml); - workspaceOptions.getMetrics = this.getFlyoutMetrics_.bind(this); + var hasFlyout = !!quarkXml; + if (hasFlyout) { + workspaceOptions.languageTree = + Blockly.utils.toolbox.convertToolboxToJSON(quarkXml); + workspaceOptions.getMetrics = this.getFlyoutMetrics_.bind(this); + } this.workspace_ = new Blockly.WorkspaceSvg(workspaceOptions); this.workspace_.isMutator = true; this.workspace_.addChangeListener(Blockly.Events.disableOrphans); @@ -177,13 +181,15 @@ Blockly.Mutator.prototype.createEditor_ = function() { // a top level svg. Instead of handling scale themselves, mutators // inherit scale from the parent workspace. // To fix this, scale needs to be applied at a different level in the dom. - var flyoutSvg = this.workspace_.addFlyout('g'); + var flyoutSvg = hasFlyout ? this.workspace_.addFlyout('g') : null; var background = this.workspace_.createDom('blocklyMutatorBackground'); - // Insert the flyout after the but before the block canvas so that - // the flyout is underneath in z-order. This makes blocks layering during - // dragging work properly. - background.insertBefore(flyoutSvg, this.workspace_.svgBlockCanvas_); + if (flyoutSvg) { + // Insert the flyout after the but before the block canvas so that + // the flyout is underneath in z-order. This makes blocks layering during + // dragging work properly. + background.insertBefore(flyoutSvg, this.workspace_.svgBlockCanvas_); + } this.svgDialog_.appendChild(background); return this.svgDialog_; diff --git a/tests/blocks/test_blocks.js b/tests/blocks/test_blocks.js index a82feaf76..524a89f90 100644 --- a/tests/blocks/test_blocks.js +++ b/tests/blocks/test_blocks.js @@ -1211,6 +1211,24 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT ], "style": "text_blocks" }, + { + "type": "test_mutators_noflyout", + "message0": "noflyout mutator", + "mutator": "test_noflyout_mutator", + "colour": "#000000" + }, + { + "type": "test_mutators_noflyout_block", + "message0": "colour %1", + "args0": [ + { + "type": "field_colour", + "name": "COLOUR", + "colour": "#ff0000" + } + ], + "style": "colour_blocks" + }, { "type": "test_style_hat", "message0": "Hat block (event)", @@ -1735,6 +1753,62 @@ Blockly.Blocks['test_basic_empty_with_mutator'] = { } }; +/** + * Mutator methods added to the test_mutators_noflyout block. + * @mixin + * @augments Blockly.Block + * @package + * @readonly + */ +var NO_FLYOUT_MUTATOR = { + /** + * Create XML to represent the block mutation. + * @return {Element} XML storage element. + * @this {Blockly.Block} + */ + mutationToDom: function() { + var container = Blockly.utils.xml.createElement('mutation'); + container.setAttribute('colour', this.colour_); + this.setColour(this.colour_); + return container; + }, + /** + * Restore a block from XML. + * @param {!Element} xmlElement XML storage element. + * @this {Blockly.Block} + */ + domToMutation: function(xmlElement) { + this.colour_ = xmlElement.getAttribute('colour'); + }, + /** + * Populate the mutator's dialog with this block's components. + * @param {!Blockly.Workspace} workspace Mutator's workspace. + * @return {!Blockly.Block} Root block in mutator. + * @this {Blockly.Block} + */ + decompose: function(workspace) { + var containerBlock = workspace.newBlock('test_mutators_noflyout_block'); + containerBlock.getField('COLOUR').setValue(this.colour_); + containerBlock.initSvg(); + return containerBlock; + }, + /** + * Reconfigure this block based on the mutator dialog's components. + * @param {!Blockly.Block} containerBlock Root block in mutator. + * @this {Blockly.Block} + */ + compose: function(containerBlock) { + this.colour_ = containerBlock.getFieldValue('COLOUR'); + this.setColour(this.colour_); + }, +}; + +/** + * Register custom mutator used by the test_mutators_noflyout block. + */ +Blockly.Extensions.registerMutator('test_noflyout_mutator', + NO_FLYOUT_MUTATOR, null, []); + Blockly.Blocks['test_dropdowns_dynamic'] = { init: function() { var dropdown = new Blockly.FieldDropdown(this.dynamicOptions); diff --git a/tests/playgrounds/test_blocks_toolbox.js b/tests/playgrounds/test_blocks_toolbox.js index 1ee392ff8..ae148512d 100644 --- a/tests/playgrounds/test_blocks_toolbox.js +++ b/tests/playgrounds/test_blocks_toolbox.js @@ -875,7 +875,11 @@ var mutators = { { "kind": "BLOCK", "blockxml": "1010", - } + }, + { + "kind": "BLOCK", + "type": "test_mutators_noflyout", + }, ], };