From 2a9744ad1cb3ecf3fd0661b4a49bf7f3e1023eb5 Mon Sep 17 00:00:00 2001 From: Harish Thakur Date: Wed, 29 Apr 2026 16:25:46 +0530 Subject: [PATCH] Fixed field_toggle state persistence on reload --- .../components/blockly/blocks/field_toggle.js | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/open-code/src/components/blockly/blocks/field_toggle.js b/open-code/src/components/blockly/blocks/field_toggle.js index 5c355807b..57e10492a 100644 --- a/open-code/src/components/blockly/blocks/field_toggle.js +++ b/open-code/src/components/blockly/blocks/field_toggle.js @@ -7,15 +7,14 @@ export class FieldToggle extends Blockly.Field { constructor(state, onText, offText, opt_validator) { super('', opt_validator); this.state_ = state; - this.onText_ = onText; - this.offText_ = offText; + this.onText_ = onText || "ON"; + this.offText_ = offText || "OFF"; this.SERIALIZABLE = true; } init() { super.init(); Blockly.Tooltip.bindMouseEvents(this.fieldGroup_); this.setValue(this.state_); - this.showEditor_(); this.fieldGroup_.classList.add('blocklyFieldToggle'); } @@ -29,8 +28,16 @@ export class FieldToggle extends Blockly.Field { //set the text value to the selected state setValue(newValue) { - this.state_ = !!newValue; - const text = this.state_ ? this.onText_ : this.offText_; + const onText = this.onText_ || "ON"; + const offText = this.offText_ || "OFF"; + + if (typeof newValue === "string") { + const normalized = newValue.trim().toUpperCase(); + this.state_ = normalized === onText.toUpperCase(); + } else { + this.state_ = !!newValue; + } + const text = this.state_ ? onText : offText; super.setValue(text); this.updateDisplay_(); } @@ -38,10 +45,14 @@ export class FieldToggle extends Blockly.Field { //update the value of state after clicking on element updateDisplay_() { if (this.textElement_) { - this.textElement_.firstChild.nodeValue = this.state_ ? this.onText_ : this.offText_; - const rectElement = this.fieldGroup_.querySelector('rect'); - rectElement.classList.toggle('field-toggle-on', this.state_); - rectElement.classList.toggle('field-toggle-off', !this.state_); + const onText = this.onText_ || "ON"; + const offText = this.offText_ || "OFF"; + this.textElement_.firstChild.nodeValue = this.state_ ? onText : offText; + const rectElement = this.fieldGroup_?.querySelector('rect'); + if (rectElement) { + rectElement.classList.toggle('field-toggle-on', this.state_); + rectElement.classList.toggle('field-toggle-off', !this.state_); + } } } @@ -57,3 +68,4 @@ Blockly.fieldRegistry.register('field_toggle', FieldToggle); +