From 1d4cfb8dfafec3bafe83cff24e841e4f8286a6dd Mon Sep 17 00:00:00 2001 From: Sam El-Husseini <16690124+samelhusseini@users.noreply.github.com> Date: Tue, 20 Aug 2019 18:25:15 -0700 Subject: [PATCH] Field angle keyboard navigation (#2853) Field angle keyboard accessibility --- core/field_angle.js | 41 +++++++++++++++++++++++++++++++++++++++++ core/field_textinput.js | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/core/field_angle.js b/core/field_angle.js index f1d54af76..d3028a01a 100644 --- a/core/field_angle.js +++ b/core/field_angle.js @@ -272,6 +272,14 @@ Blockly.FieldAngle.prototype.onMouseMove = function(e) { angle -= 360; } + this.setAngle(angle); +}; + +/** + * Set the angle value and update the graph. + * @param {number} angle New angle + */ +Blockly.FieldAngle.prototype.setAngle = function(angle) { // Do rounding. if (Blockly.FieldAngle.ROUND) { angle = Math.round(angle / Blockly.FieldAngle.ROUND) * @@ -281,6 +289,8 @@ Blockly.FieldAngle.prototype.onMouseMove = function(e) { // Do wrapping. if (angle > Blockly.FieldAngle.WRAP) { angle -= 360; + } else if (angle < 0) { + angle += 360; } // Update value. @@ -333,6 +343,37 @@ Blockly.FieldAngle.prototype.updateGraph_ = function() { this.line_.setAttribute('y2', y2); }; +/** + * Handle key down to the editor. + * @param {!Event} e Keyboard event. + * @protected + * @override + */ +Blockly.FieldAngle.prototype.onHtmlInputKeyDown_ = function(e) { + Blockly.FieldAngle.superClass_.onHtmlInputKeyDown_.call(this, e); + + var multiplier; + if (e.keyCode === Blockly.utils.KeyCodes.LEFT) { + // decrement (increment in RTL) + multiplier = this.sourceBlock_.RTL ? 1 : -1; + } else if (e.keyCode === Blockly.utils.KeyCodes.RIGHT) { + // increment (decrement in RTL) + multiplier = this.sourceBlock_.RTL ? -1 : 1; + } else if (e.keyCode === Blockly.utils.KeyCodes.DOWN) { + // decrement + multiplier = -1; + } else if (e.keyCode === Blockly.utils.KeyCodes.UP) { + // increment + multiplier = 1; + } + if (multiplier) { + this.setAngle(Number(this.getValue()) + + (multiplier * Blockly.FieldAngle.ROUND)); + e.preventDefault(); + e.stopPropagation(); + } +}; + /** * Ensure that the input value is a valid angle. * @param {string|number=} opt_newValue The input value. diff --git a/core/field_textinput.js b/core/field_textinput.js index 5adc88734..9cc6be85f 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -339,7 +339,7 @@ Blockly.FieldTextInput.prototype.unbindInputEvents_ = function() { /** * Handle key down to the editor. * @param {!Event} e Keyboard event. - * @private + * @protected */ Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_ = function(e) { var tabKey = 9, enterKey = 13, escKey = 27;