Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 20 additions & 2 deletions core/field_angle.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ Blockly.FieldAngle.prototype.configure_ = function(config) {
}
};



/**
* Create the block UI for this field.
* @package
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions core/field_checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ Blockly.FieldCheckbox.prototype.configure_ = function(config) {
}
};

/**
* Saves this field's value.
* @return {boolean} The boolean value held by this field.
* @override
* @package
*/
Blockly.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
*/
Blockly.FieldCheckbox.prototype.loadState = function(state) {
this.setValue(state);
};

/**
* Create the block UI for this checkbox.
* @package
Expand Down
20 changes: 20 additions & 0 deletions core/field_colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ Blockly.FieldColour.prototype.initView = function() {
}
};

/**
* Saves this field's value.
* @return {string} The colour value held by this field.
* @override
* @package
*/
Blockly.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
*/
Blockly.FieldColour.prototype.loadState = function(state) {
this.setValue(state);
};

/**
* @override
*/
Expand Down
23 changes: 23 additions & 0 deletions core/field_dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,29 @@ Blockly.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
*/
Blockly.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
*/
Blockly.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.
Expand Down
20 changes: 20 additions & 0 deletions core/field_label_serializable.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,25 @@ Blockly.FieldLabelSerializable.prototype.EDITABLE = false;
*/
Blockly.FieldLabelSerializable.prototype.SERIALIZABLE = true;

/**
* Saves this field's value.
* @return {string} The text value held by this field.
* @override
* @package
*/
Blockly.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
*/
Blockly.FieldLabelSerializable.prototype.loadState = function(state) {
this.setValue(state);
};

Blockly.fieldRegistry.register(
'field_label_serializable', Blockly.FieldLabelSerializable);
20 changes: 20 additions & 0 deletions core/field_multilineinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ Blockly.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
*/
Blockly.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
*/
Blockly.FieldMultilineInput.prototype.loadState = function(state) {
this.setValue(state);
};

/**
* Create the block UI for this field.
* @package
Expand Down
20 changes: 20 additions & 0 deletions core/field_number.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ Blockly.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
*/
Blockly.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
*/
Blockly.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.
Expand Down
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 {*} The text value held by this field.
* @override
* @package
*/
Blockly.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
*/
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
22 changes: 22 additions & 0 deletions core/field_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ Blockly.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
*/
Blockly.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
*/
Blockly.FieldVariable.prototype.loadState = function(id) {
this.setValue(id);
};

/**
* Attach this field to a block.
* @param {!Blockly.Block} block The block containing this field.
Expand Down
31 changes: 31 additions & 0 deletions tests/mocha/field_angle_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
27 changes: 27 additions & 0 deletions tests/mocha/field_checkbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
27 changes: 27 additions & 0 deletions tests/mocha/field_colour_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
32 changes: 32 additions & 0 deletions tests/mocha/field_dropdown_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Loading