Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit single field disabling (#4932) #4941

Merged
merged 8 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions core/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ Blockly.Field.prototype.isDirty_ = true;
*/
Blockly.Field.prototype.visible_ = true;

/**
* Whether the field value can 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}
Expand Down Expand Up @@ -392,8 +400,8 @@ Blockly.Field.prototype.bindEvents_ = function() {
};

/**
* Sets the field's value based on the given XML element. Should only be
* called by Blockly.Xml.
* Sets the field's value based on the given XML element. Should only be called
* by Blockly.Xml.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* @package
Expand Down Expand Up @@ -440,7 +448,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;
Expand All @@ -451,23 +459,45 @@ 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();
};

/**
Expand Down
8 changes: 4 additions & 4 deletions core/field_dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ Blockly.FieldDropdown.fromJson = function(options) {
};

/**
* Sets the field's value based on the given XML element. Should only be
* called by Blockly.Xml.
* @param {!Element} fieldElement The element containing info about the
* field's state.
* Sets the field's value based on the given XML element. Should only be called
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could probably remove this file from the PR now right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the file. It's not related to the pull request anymore, but I thought I would adjust the styling of the comment by (a) moving up "field's" to the previous line since the line would have 80 characters and (b) indenting the wrapped line with 4 spaces.

Regarding (b), I noticed that there is some inconsistency with comments' styling in general as some are indented by 4 spaces when they wrap to the next line and others are not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should ideally be indented, but there are many places they aren't currently. However, we are Soon (TM) going to be using clang-format for indentation so we will never have to think about it (or line wrapping) ever again :D So for now it's fine as long as eslint doesn't complain.

* by Blockly.Xml.
* @param {!Element} fieldElement The element containing info about the field's
* state.
* @package
*/
Blockly.FieldDropdown.prototype.fromXml = function(fieldElement) {
Expand Down