Handle blockly actions on the dropdown fields. (#3064)

* Handle blockly actions on the dropdown fields.
This commit is contained in:
Sam El-Husseini
2019-09-20 15:07:35 -07:00
committed by GitHub
parent 23b31c7ea8
commit 9318c370f9
2 changed files with 25 additions and 4 deletions

View File

@@ -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;

View File

@@ -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);