From 1aa1186fc8bb25b62f131ddae3cd917d8d6ed3d4 Mon Sep 17 00:00:00 2001 From: jschanker Date: Sun, 20 Jun 2021 00:41:59 -0400 Subject: [PATCH] Permit single field disabling (https://github.com/google/blockly/issues/4932) --- core/field.js | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/core/field.js b/core/field.js index 9d52bacbb..5547d73bf 100644 --- a/core/field.js +++ b/core/field.js @@ -202,6 +202,13 @@ Blockly.Field.prototype.isDirty_ = true; */ Blockly.Field.prototype.visible_ = true; +/** + * Can the field value be changed using the editor on an editable block? + * @type {boolean} + * @protected + */ +Blockly.Field.prototype.enabled_ = true; + /** * The element the click handler is bound to. * @type {Element} @@ -440,7 +447,7 @@ Blockly.Field.prototype.updateEditable = function() { if (!this.EDITABLE || !group) { return; } - if (this.sourceBlock_.isEditable()) { + if (this.enabled_ && this.sourceBlock_.isEditable()) { Blockly.utils.dom.addClass(group, 'blocklyEditableText'); Blockly.utils.dom.removeClass(group, 'blocklyNonEditableText'); group.style.cursor = this.CURSOR; @@ -451,23 +458,44 @@ Blockly.Field.prototype.updateEditable = function() { } }; +/** + * Set whether this field's value can be changed using the editor when the + * source block is editable. + * @param {boolean} enabled True if enabled. + */ +Blockly.Field.prototype.setEnabled = function(enabled) { + this.enabled_ = enabled; + this.updateEditable(); +}; + +/** + * Check whether this field's value can be changed using the editor when the + * source block is editable. + * @return {boolean} Whether this field is enabled. + */ +Blockly.Field.prototype.isEnabled = function() { + return this.enabled_; +}; + /** * Check whether this field defines the showEditor_ function. * @return {boolean} Whether this field is clickable. */ Blockly.Field.prototype.isClickable = function() { - return !!this.sourceBlock_ && this.sourceBlock_.isEditable() && - !!this.showEditor_ && (typeof this.showEditor_ === 'function'); + return this.enabled_ && !!this.sourceBlock_ && this.sourceBlock_.isEditable() + && !!this.showEditor_ && (typeof this.showEditor_ === 'function'); }; /** * Check whether this field is currently editable. Some fields are never * EDITABLE (e.g. text labels). Other fields may be EDITABLE but may exist on - * non-editable blocks. - * @return {boolean} Whether this field is editable and on an editable block + * non-editable blocks or be currently disabled. + * @return {boolean} Whether this field is currently enabled, editable and on + * an editable block. */ Blockly.Field.prototype.isCurrentlyEditable = function() { - return this.EDITABLE && !!this.sourceBlock_ && this.sourceBlock_.isEditable(); + return this.enabled_ && this.EDITABLE && !!this.sourceBlock_ && + this.sourceBlock_.isEditable(); }; /**