diff --git a/core/field_textinput.js b/core/field_textinput.js index a8014a187..fb0a7d3d2 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -55,6 +55,14 @@ goog.inherits(Blockly.FieldTextInput, Blockly.Field); */ Blockly.FieldTextInput.FONTSIZE = 11; +/** + * The HTML input element for the user to type, or null if no FieldTextInput + * editor is currently open. + * @type {HTMLInputElement} + * @private + */ +Blockly.FieldTextInput.htmlInput_ = null; + /** * Mouse cursor style when over the hotspot that initiates the editor. */ @@ -156,7 +164,7 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) { (Blockly.FieldTextInput.FONTSIZE * this.workspace_.scale) + 'pt'; div.style.fontSize = fontSize; htmlInput.style.fontSize = fontSize; - /** @type {!HTMLInputElement} */ + Blockly.FieldTextInput.htmlInput_ = htmlInput; div.appendChild(htmlInput); @@ -169,6 +177,16 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) { htmlInput.select(); } + this.bindEvents_(htmlInput); +}; + +/** + * Bind handlers for user input on this field and size changes on the workspace. + * @param {!HTMLInputElement} htmlInput The htmlInput created in showEditor, to + * which event handlers will be bound. + * @private + */ +Blockly.FieldTextInput.prototype.bindEvents_ = function(htmlInput) { // Bind to keydown -- trap Enter without IME and Esc to hide. htmlInput.onKeyDownWrapper_ = Blockly.bindEventWithChecks_(htmlInput, 'keydown', this, @@ -185,6 +203,19 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) { this.workspace_.addChangeListener(htmlInput.onWorkspaceChangeWrapper_); }; +/** + * Unbind handlers for user input and workspace size changes. + * @param {!HTMLInputElement} htmlInput The html for this text input. + * @private + */ +Blockly.FieldTextInput.prototype.unbindEvents_ = function(htmlInput) { + Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_); + Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_); + Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_); + this.workspace_.removeChangeListener( + htmlInput.onWorkspaceChangeWrapper_); +}; + /** * Handle key down to the editor. * @param {!Event} e Keyboard event. @@ -305,13 +336,11 @@ Blockly.FieldTextInput.prototype.widgetDispose_ = function() { } thisField.setText(text); thisField.sourceBlock_.rendered && thisField.sourceBlock_.render(); - Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_); - Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_); - Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_); - thisField.workspace_.removeChangeListener( - htmlInput.onWorkspaceChangeWrapper_); + + thisField.unbindEvents_(htmlInput); Blockly.FieldTextInput.htmlInput_ = null; Blockly.Events.setGroup(false); + // Delete style properties. var style = Blockly.WidgetDiv.DIV.style; style.width = 'auto';