diff --git a/core/field_textinput.js b/core/field_textinput.js index fb0a7d3d2..b342a7e8e 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -142,18 +142,35 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) { var quietInput = opt_quietInput || false; if (!quietInput && (goog.userAgent.MOBILE || goog.userAgent.ANDROID || goog.userAgent.IPAD)) { - // Mobile browsers have issues with in-line textareas (focus & keyboards). - var fieldText = this; - Blockly.prompt(Blockly.Msg.CHANGE_VALUE_TITLE, this.text_, - function(newValue) { - if (fieldText.sourceBlock_) { - newValue = fieldText.callValidator(newValue); - } - fieldText.setValue(newValue); - }); - return; + this.showPromptEditor_(); + } else { + this.showInlineEditor_(quietInput); } +}; +/** + * Create and show a text input editor that is a prompt (usually a popup). + * Mobile browsers have issues with in-line textareas (focus and keyboards). + * @private + */ +Blockly.FieldTextInput.showPromptEditor_ = function() { + var fieldText = this; + Blockly.prompt(Blockly.Msg.CHANGE_VALUE_TITLE, this.text_, + function(newValue) { + if (fieldText.sourceBlock_) { + newValue = fieldText.callValidator(newValue); + } + fieldText.setValue(newValue); + }); +}; + +/** + * Create and show a text input editor that sits directly over the text input. + * @param {boolean} quietInput True if editor should be created without + * focus. + * @private + */ +Blockly.FieldTextInput.prototype.showInlineEditor_ = function(quietInput) { Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL, this.widgetDispose_()); var div = Blockly.WidgetDiv.DIV; // Create the input. @@ -320,22 +337,7 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() { return function() { var htmlInput = Blockly.FieldTextInput.htmlInput_; // Save the edit (if it validates). - var text = htmlInput.value; - if (thisField.sourceBlock_) { - var text1 = thisField.callValidator(text); - if (text1 === null) { - // Invalid edit. - text = htmlInput.defaultValue; - } else { - // Validation function has changed the text. - text = text1; - if (thisField.onFinishEditing_) { - thisField.onFinishEditing_(text); - } - } - } - thisField.setText(text); - thisField.sourceBlock_.rendered && thisField.sourceBlock_.render(); + thisField.maybeSaveEdit_(); thisField.unbindEvents_(htmlInput); Blockly.FieldTextInput.htmlInput_ = null; @@ -349,6 +351,27 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() { }; }; +Blockly.FieldTextInput.prototype.maybeSaveEdit_ = function() { + var htmlInput = Blockly.FieldTextInput.htmlInput_; + // Save the edit (if it validates). + var text = htmlInput.value; + if (this.sourceBlock_) { + var text1 = this.callValidator(text); + if (text1 === null) { + // Invalid edit. + text = htmlInput.defaultValue; + } else { + // Validation function has changed the text. + text = text1; + if (this.onFinishEditing_) { + this.onFinishEditing_(text); + } + } + } + this.setText(text); + this.sourceBlock_.rendered && this.sourceBlock_.render(); +}; + /** * Ensure that only a number may be entered. * @param {string} text The user's text.