From e057da1fd43662ffe9860a94d5213ce67cd5c6e6 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Fri, 6 Aug 2021 06:54:54 -0700 Subject: [PATCH] feat: upgrade fields to use new JSO hooks (#5077) * Upgrade field angle to use new serialization * Upgrade field checkbox to use new serialization * Upgrade field colour to use new serialization * Upgrade field dropdown to use new serialization * Upgrade serializable label field to use new serialization * Upgrade field multiline input to use new serialization * Upgrade field number to use new serialization * Upgrade field text input to use new serialization * Upgrade variable field to use new serialization * Fix type casts * Feedback from PR * Switch to use getValue() --- core/field_angle.js | 22 ++++++++++- core/field_checkbox.js | 20 ++++++++++ core/field_colour.js | 20 ++++++++++ core/field_dropdown.js | 23 ++++++++++++ core/field_label_serializable.js | 20 ++++++++++ core/field_multilineinput.js | 20 ++++++++++ core/field_number.js | 20 ++++++++++ core/field_textinput.js | 20 ++++++++++ core/field_variable.js | 22 +++++++++++ tests/mocha/field_angle_test.js | 31 ++++++++++++++++ tests/mocha/field_checkbox_test.js | 27 ++++++++++++++ tests/mocha/field_colour_test.js | 27 ++++++++++++++ tests/mocha/field_dropdown_test.js | 32 ++++++++++++++++ tests/mocha/field_label_serializable_test.js | 23 ++++++++++++ tests/mocha/field_multilineinput_test.js | 27 ++++++++++++++ tests/mocha/field_number_test.js | 39 ++++++++++++++++++++ tests/mocha/field_textinput_test.js | 23 ++++++++++++ tests/mocha/field_variable_test.js | 29 +++++++++++++++ tests/mocha/serializer_test.js | 8 ++++ 19 files changed, 451 insertions(+), 2 deletions(-) diff --git a/core/field_angle.js b/core/field_angle.js index 778ebe066ba..c4c29279986 100644 --- a/core/field_angle.js +++ b/core/field_angle.js @@ -241,8 +241,6 @@ Blockly.FieldAngle.prototype.configure_ = function(config) { } }; - - /** * Create the block UI for this field. * @package @@ -256,6 +254,26 @@ Blockly.FieldAngle.prototype.initView = function() { this.textElement_.appendChild(this.symbol_); }; +/** + * Saves this field's value. + * @return {number} The angle value held by this field. + * @override + * @package + */ +Blockly.FieldAngle.prototype.saveState = function() { + return /** @type {number} */ (this.getValue()); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the angle field. + * @override + * @package + */ +Blockly.FieldAngle.prototype.loadState = function(state) { + this.setValue(state); +}; + /** * Updates the graph when the field rerenders. * @protected diff --git a/core/field_checkbox.js b/core/field_checkbox.js index 67362fd6335..274123995f6 100644 --- a/core/field_checkbox.js +++ b/core/field_checkbox.js @@ -102,6 +102,26 @@ FieldCheckbox.prototype.configure_ = function(config) { } }; +/** + * Saves this field's value. + * @return {boolean} The boolean value held by this field. + * @override + * @package + */ +FieldCheckbox.prototype.saveState = function() { + return /** @type {boolean} */ (this.getValueBoolean()); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the checkbox field. + * @override + * @package + */ +FieldCheckbox.prototype.loadState = function(state) { + this.setValue(state); +}; + /** * Create the block UI for this checkbox. * @package diff --git a/core/field_colour.js b/core/field_colour.js index b0529046047..a8ac41e8f43 100644 --- a/core/field_colour.js +++ b/core/field_colour.js @@ -188,6 +188,26 @@ FieldColour.prototype.initView = function() { } }; +/** + * Saves this field's value. + * @return {string} The colour value held by this field. + * @override + * @package + */ +FieldColour.prototype.saveState = function() { + return /** @type {string} */ (this.getValue()); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the colour field. + * @override + * @package + */ +FieldColour.prototype.loadState = function(state) { + this.setValue(state); +}; + /** * @override */ diff --git a/core/field_dropdown.js b/core/field_dropdown.js index effd6a31d8e..0a1a1013ad9 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -170,6 +170,29 @@ FieldDropdown.prototype.fromXml = function(fieldElement) { this.setValue(fieldElement.textContent); }; +/** + * Saves this field's value. + * @return {string} The dropdown value held by this field. + * @override + * @package + */ +FieldDropdown.prototype.saveState = function() { + return /** @type {string} */ (this.getValue()); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the dropdown field. + * @override + * @package + */ +FieldDropdown.prototype.loadState = function(state) { + if (this.isOptionListDynamic()) { + this.getOptions(false); + } + this.setValue(state); +}; + /** * Serializable fields are saved by the XML renderer, non-serializable fields * are not. Editable fields should also be serializable. diff --git a/core/field_label_serializable.js b/core/field_label_serializable.js index dee9da1ef60..df2119c1523 100644 --- a/core/field_label_serializable.js +++ b/core/field_label_serializable.js @@ -68,6 +68,26 @@ FieldLabelSerializable.prototype.EDITABLE = false; */ FieldLabelSerializable.prototype.SERIALIZABLE = true; +/** + * Saves this field's value. + * @return {string} The text value held by this field. + * @override + * @package + */ +FieldLabelSerializable.prototype.saveState = function() { + return /** @type {string} */ (this.getValue()); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the label field. + * @override + * @package + */ +FieldLabelSerializable.prototype.loadState = function(state) { + this.setValue(state); +}; + fieldRegistry.register('field_label_serializable', FieldLabelSerializable); exports = FieldLabelSerializable; diff --git a/core/field_multilineinput.js b/core/field_multilineinput.js index 31df9a6fc03..c72f9983d6e 100644 --- a/core/field_multilineinput.js +++ b/core/field_multilineinput.js @@ -122,6 +122,26 @@ FieldMultilineInput.prototype.fromXml = function(fieldElement) { this.setValue(fieldElement.textContent.replace(/ /g, '\n')); }; +/** + * Saves this field's value. + * @return {string} The text value held by this field. + * @override + * @package + */ +FieldMultilineInput.prototype.saveState = function() { + return /** @type {string} */ (this.getValue()); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the multiline input field. + * @override + * @package + */ +FieldMultilineInput.prototype.loadState = function(state) { + this.setValue(state); +}; + /** * Create the block UI for this field. * @package diff --git a/core/field_number.js b/core/field_number.js index 4ed5ac5aa5a..50cbc7fe775 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -118,6 +118,26 @@ FieldNumber.prototype.configure_ = function(config) { this.setPrecisionInternal_(config['precision']); }; +/** + * Saves this field's value. + * @return {number} The number value held by this field. + * @override + * @package + */ +FieldNumber.prototype.saveState = function() { + return /** @type {number} */ (this.getValue()); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the nuber field. + * @override + * @package + */ +FieldNumber.prototype.loadState = function(state) { + this.setValue(state); +}; + /** * Set the maximum, minimum and precision constraints on this field. * Any of these properties may be undefined or NaN to be disabled. diff --git a/core/field_textinput.js b/core/field_textinput.js index a682cb2ffbd..96ef15bc40d 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -182,6 +182,26 @@ FieldTextInput.prototype.initView = function() { this.createTextElement_(); }; +/** + * Saves this field's value. + * @return {*} The text value held by this field. + * @override + * @package + */ +FieldTextInput.prototype.saveState = function() { + return this.getValue(); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} state The state to apply to the text input field. + * @override + * @package + */ +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/core/field_variable.js b/core/field_variable.js index e98db6f8646..eb67b0bf9e6 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -197,6 +197,28 @@ FieldVariable.prototype.toXml = function(fieldElement) { return fieldElement; }; +/** + * Saves this field's value. + * @return {string} The id of the variable referenced by this field. + * @override + * @package + */ +FieldVariable.prototype.saveState = function() { + // Make sure the variable is initialized. + this.initModel(); + return this.variable_.getId(); +}; + +/** + * Sets the field's value based on the given state. + * @param {*} id The id of the variable to assign to this variable field. + * @override + * @package + */ +FieldVariable.prototype.loadState = function(id) { + this.setValue(id); +}; + /** * Attach this field to a block. * @param {!Block} block The block containing this field. diff --git a/tests/mocha/field_angle_test.js b/tests/mocha/field_angle_test.js index 7a1eb928c61..1b2e5926f37 100644 --- a/tests/mocha/field_angle_test.js +++ b/tests/mocha/field_angle_test.js @@ -310,4 +310,35 @@ suite('Angle 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.FieldAngle(value); + block.getInput('INPUT').appendField(field, 'ANGLE'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'ANGLE': value}); + }; + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('Simple', function() { + this.assertValue(90); + }); + + test('Max precision', function() { + this.assertValue(1.000000000000001); + }); + + test('Smallest number', function() { + this.assertValue(5e-324); + }); + }); }); diff --git a/tests/mocha/field_checkbox_test.js b/tests/mocha/field_checkbox_test.js index 5a10ef57f63..3f0fb5426b0 100644 --- a/tests/mocha/field_checkbox_test.js +++ b/tests/mocha/field_checkbox_test.js @@ -204,4 +204,31 @@ suite('Checkbox 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.FieldCheckbox(value); + block.getInput('INPUT').appendField(field, 'CHECK'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'CHECK': value}); + }; + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('True', function() { + this.assertValue(true); + }); + + test('False', function() { + this.assertValue(false); + }); + }); }); diff --git a/tests/mocha/field_colour_test.js b/tests/mocha/field_colour_test.js index e779bf831b3..b3c36f9620c 100644 --- a/tests/mocha/field_colour_test.js +++ b/tests/mocha/field_colour_test.js @@ -277,4 +277,31 @@ suite('Colour 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.FieldColour(value); + block.getInput('INPUT').appendField(field, 'COLOUR'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'COLOUR': value}); + }; + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('Three char', function() { + this.assertValue('#001122'); + }); + + test('Six char', function() { + this.assertValue('#012345'); + }); + }); }); diff --git a/tests/mocha/field_dropdown_test.js b/tests/mocha/field_dropdown_test.js index 95ad8eacf58..c166b20d8e1 100644 --- a/tests/mocha/field_dropdown_test.js +++ b/tests/mocha/field_dropdown_test.js @@ -152,4 +152,36 @@ suite('Dropdown Fields', function() { }); }); }); + + suite('Serialization', function() { + setup(function() { + this.workspace = new Blockly.Workspace(); + defineRowBlock(); + + + this.assertValue = (value, field) => { + const block = this.workspace.newBlock('row_block'); + field.setValue(value); + block.getInput('INPUT').appendField(field, 'DROPDOWN'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'DROPDOWN': value}); + }; + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('Simple', function() { + const field = new Blockly.FieldDropdown( + [['apple', 'A'], ['ball', 'B'], ['carrot', 'C']]); + this.assertValue('C', field); + }); + + test('Dynamic', function() { + const field = new Blockly.FieldDropdown( + () => [['apple', 'A'], ['ball', 'B'], ['carrot', 'C']]); + this.assertValue('C', field); + }); + }); }); diff --git a/tests/mocha/field_label_serializable_test.js b/tests/mocha/field_label_serializable_test.js index 33024150316..67eaadc1428 100644 --- a/tests/mocha/field_label_serializable_test.js +++ b/tests/mocha/field_label_serializable_test.js @@ -181,4 +181,27 @@ suite('Label Serializable 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.FieldLabelSerializable(value); + block.getInput('INPUT').appendField(field, 'LABEL'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'LABEL': value}); + }; + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('Simple', function() { + this.assertValue('test label'); + }); + }); }); diff --git a/tests/mocha/field_multilineinput_test.js b/tests/mocha/field_multilineinput_test.js index 844593f8674..cf952cab2a4 100644 --- a/tests/mocha/field_multilineinput_test.js +++ b/tests/mocha/field_multilineinput_test.js @@ -153,4 +153,31 @@ suite('Multiline Input Fields', function() { ]; testHelpers.runCodeGenerationTestSuites(testSuites); }); + + 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.FieldMultilineInput(value); + block.getInput('INPUT').appendField(field, 'MULTILINE'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'MULTILINE': value}); + }; + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('Single line', function() { + this.assertValue('this is a single line'); + }); + + test('Multiple lines', function() { + this.assertValue('this\nis\n multiple\n lines'); + }); + }); }); diff --git a/tests/mocha/field_number_test.js b/tests/mocha/field_number_test.js index 338aa4ad0e7..7c14e86378a 100644 --- a/tests/mocha/field_number_test.js +++ b/tests/mocha/field_number_test.js @@ -332,4 +332,43 @@ suite('Number 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.FieldNumber(value); + block.getInput('INPUT').appendField(field, 'NUMBER'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'NUMBER': value}); + }; + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('Simple', function() { + this.assertValue(10); + }); + + test('Max precision small', function() { + this.assertValue(1.000000000000001); + }); + + test('Max precision large', function() { + this.assertValue(1000000000000001); + }); + + test('Smallest', function() { + this.assertValue(5e-324); + }); + + test('Largest', function() { + this.assertValue(1.7976931348623157e+308); + }); + }); }); diff --git a/tests/mocha/field_textinput_test.js b/tests/mocha/field_textinput_test.js index c777134c334..2f6a437d832 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'); + }); + }); }); diff --git a/tests/mocha/field_variable_test.js b/tests/mocha/field_variable_test.js index eab65a16e9d..4aad6d0d83c 100644 --- a/tests/mocha/field_variable_test.js +++ b/tests/mocha/field_variable_test.js @@ -377,4 +377,33 @@ suite('Variable Fields', function() { chai.assert.equal(this.variableField.getValue(), 'id2'); }); }); + + suite('Serialization', function() { + setup(function() { + this.workspace = new Blockly.Workspace(); + defineRowBlock(); + createGenUidStubWithReturns(new Array(10).fill().map((_, i) => 'id' + i)); + }); + + teardown(function() { + workspaceTeardown.call(this, this.workspace); + }); + + test('Untyped', function() { + const block = this.workspace.newBlock('row_block'); + const field = new Blockly.FieldVariable('x'); + block.getInput('INPUT').appendField(field, 'VAR'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'VAR': 'id2'}); + }); + + test('Typed', function() { + const block = this.workspace.newBlock('row_block'); + const field = + new Blockly.FieldVariable('x', undefined, undefined, ['String']); + block.getInput('INPUT').appendField(field, 'VAR'); + const jso = Blockly.serialization.blocks.save(block); + chai.assert.deepEqual(jso['fields'], {'VAR': 'id2'}); + }); + }); }); diff --git a/tests/mocha/serializer_test.js b/tests/mocha/serializer_test.js index cfac118ae86..8a77e704d71 100644 --- a/tests/mocha/serializer_test.js +++ b/tests/mocha/serializer_test.js @@ -276,9 +276,17 @@ Serializer.Fields.Dropdown.NotDefault = new SerializerTestCase('NotDefault', 'ITEM32' + '' + ''); +Serializer.Fields.Dropdown.Dynamic = new SerializerTestCase( + 'Dynamic', + '' + + '' + + '0' + + '' + + ''); Serializer.Fields.Dropdown.testCases = [ Serializer.Fields.Dropdown.Default, Serializer.Fields.Dropdown.NotDefault, + Serializer.Fields.Dropdown.Dynamic, ];