From 25fbde56ef4ca8c4f0869e36e4c3e06363d4d185 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 15 Jul 2021 20:45:45 +0000 Subject: [PATCH] Upgrade field text input to use new serialization --- core/field_textinput.js | 20 ++++++++++++++++++++ tests/mocha/field_textinput_test.js | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/core/field_textinput.js b/core/field_textinput.js index b2153b65e18..51ebac2d973 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -178,6 +178,26 @@ Blockly.FieldTextInput.prototype.initView = function() { this.createTextElement_(); }; +/** + * Saves this field's value. + * @return {string} The text value held by this field. + * @override + * @package + */ +Blockly.FieldTextInput.prototype.saveState = function() { + return this.value_; +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the text input field. + * @override + * @package + */ +Blockly.FieldTextInput.prototype.loadState = function(state) { + this.setValue(state); +}; + /** * Ensure that the input value casts to a valid string. * @param {*=} opt_newValue The input value. diff --git a/tests/mocha/field_textinput_test.js b/tests/mocha/field_textinput_test.js index 1a1c1ed2acd..ec55c162240 100644 --- a/tests/mocha/field_textinput_test.js +++ b/tests/mocha/field_textinput_test.js @@ -215,4 +215,27 @@ suite('Text Input Fields', function() { }); }); }); + + suite('Serialization', function() { + setup(function() { + this.workspace = new Blockly.Workspace(); + defineRowBlock(); + + this.assertValue = (value) => { + const block = this.workspace.newBlock('row_block'); + const field = new Blockly.FieldTextInput(value); + block.getInput('INPUT').appendField(field, 'TEXT'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'TEXT': value}); + }; + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('Simple', function() { + this.assertValue('test text'); + }); + }); });