From 9318c370f9aec41a151adf9f180e44edec7b7b67 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Fri, 20 Sep 2019 15:07:35 -0700 Subject: [PATCH] Handle blockly actions on the dropdown fields. (#3064) * Handle blockly actions on the dropdown fields. --- core/components/menu/menu.js | 8 ++++---- core/field_dropdown.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/core/components/menu/menu.js b/core/components/menu/menu.js index be4905652..247a28347 100644 --- a/core/components/menu/menu.js +++ b/core/components/menu/menu.js @@ -292,9 +292,10 @@ Blockly.Menu.prototype.setHighlighted = function(item) { /** * Highlights the next highlightable item (or the first if nothing is currently * highlighted). - * @protected + * @package */ Blockly.Menu.prototype.highlightNext = function() { + this.unhighlightCurrent(); this.highlightHelper(function(index, max) { return (index + 1) % max; }, this.highlightedIndex_); @@ -303,9 +304,10 @@ Blockly.Menu.prototype.highlightNext = function() { /** * Highlights the previous highlightable item (or the last if nothing is * currently highlighted). - * @protected + * @package */ Blockly.Menu.prototype.highlightPrevious = function() { + this.unhighlightCurrent(); this.highlightHelper(function(index, max) { index--; return index < 0 ? max - 1 : index; @@ -465,12 +467,10 @@ Blockly.Menu.prototype.handleKeyEventInternal = function(e) { break; case Blockly.utils.KeyCodes.UP: - this.unhighlightCurrent(); this.highlightPrevious(); break; case Blockly.utils.KeyCodes.DOWN: - this.unhighlightCurrent(); this.highlightNext(); break; diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 48e7a531e..edcb70f4f 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -34,6 +34,7 @@ goog.require('Blockly.Field'); goog.require('Blockly.fieldRegistry'); goog.require('Blockly.Menu'); goog.require('Blockly.MenuItem'); +goog.require('Blockly.navigation'); goog.require('Blockly.utils'); goog.require('Blockly.utils.dom'); goog.require('Blockly.utils.object'); @@ -641,4 +642,24 @@ Blockly.FieldDropdown.validateOptions_ = function(options) { } }; +/** + * Handles the given action. + * This is only triggered when keyboard accessibility mode is enabled. + * @param {!Blockly.Action} action The action to be handled. + * @return {boolean} True if the field handled the action, false otherwise. + * @package + */ +Blockly.FieldDropdown.prototype.onBlocklyAction = function(action) { + if (this.menu_) { + if (action === Blockly.navigation.ACTION_PREVIOUS) { + this.menu_.highlightPrevious(); + return true; + } else if (action === Blockly.navigation.ACTION_NEXT) { + this.menu_.highlightNext(); + return true; + } + } + return Blockly.FieldDropdown.superClass_.onBlocklyAction.call(this, action); +}; + Blockly.fieldRegistry.register('field_dropdown', Blockly.FieldDropdown);