Skip to content

Commit

Permalink
Upgrade field text input to use new serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Jul 15, 2021
1 parent 0576c7a commit 25fbde5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/field_textinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions tests/mocha/field_textinput_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit 25fbde5

Please sign in to comment.