Skip to content

Commit

Permalink
Add serialization of field values (#5072)
Browse files Browse the repository at this point in the history
* Add tests for field serialization

* Add field serialization

* Fixup types and tests
  • Loading branch information
BeksOmega authored and alschmiedt committed Sep 20, 2021
1 parent 780b616 commit cc559cc
Show file tree
Hide file tree
Showing 2 changed files with 297 additions and 201 deletions.
38 changes: 33 additions & 5 deletions core/serialization/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
goog.module('Blockly.serialization.blocks');
goog.module.declareLegacyNamespace();

// eslint-disable-next-line no-unused-vars
const Block = goog.requireType('Blockly.Block');


// TODO: Remove this once lint is fixed.
/* eslint-disable no-use-before-define */
Expand All @@ -31,15 +34,16 @@ goog.module.declareLegacyNamespace();
* movable: (boolean|undefined),
* inline: (boolean|undefined),
* data: (string|undefined),
* extra-state: *
* extra-state: *,
* fields: (Object<string, *>|undefined),
* }}
*/
var State;
exports.State = State;

/**
* Returns the state of the given block as a plain JavaScript object.
* @param {!Blockly.Block} block The block to serialize.
* @param {!Block} block The block to serialize.
* @param {{addCoordinates: (boolean|undefined)}=} param1
* addCoordinates: If true the coordinates of the block are added to the
* serialized state. False by default.
Expand All @@ -62,6 +66,7 @@ const save = function(block, {addCoordinates = false} = {}) {
}
addAttributes(block, state);
addExtraState(block, state);
addFields(block, state);

return state;
};
Expand All @@ -70,7 +75,7 @@ exports.save = save;
/**
* Adds attributes to the given state object based on the state of the block.
* Eg collapsed, disabled, editable, etc.
* @param {!Blockly.Block} block The block to base the attributes on.
* @param {!Block} block The block to base the attributes on.
* @param {!State} state The state object to append to.
*/
const addAttributes = function(block, state) {
Expand Down Expand Up @@ -103,7 +108,7 @@ const addAttributes = function(block, state) {

/**
* Adds the coordinates of the given block to the given state object.
* @param {!Blockly.Block} block The block to base the coordinates on
* @param {!Block} block The block to base the coordinates on
* @param {!State} state The state object to append to
*/
const addCoords = function(block, state) {
Expand All @@ -115,7 +120,7 @@ const addCoords = function(block, state) {

/**
* Adds any extra state the block may provide to the given state object.
* @param {!Blockly.Block} block The block to serialize the extra state of.
* @param {!Block} block The block to serialize the extra state of.
* @param {!State} state The state object to append to.
*/
const addExtraState = function(block, state) {
Expand All @@ -126,3 +131,26 @@ const addExtraState = function(block, state) {
}
}
};

/**
* Adds the state of all of the fields on the block to the given state object.
* @param {!Block} block The block to serialize the field state of.
* @param {!State} state The state object to append to.
*/
const addFields = function(block, state) {
let hasFieldState = false;
let fields = Object.create(null);
for (let i = 0; i < block.inputList.length; i++) {
const input = block.inputList[i];
for (let j = 0; j < input.fieldRow.length; j++) {
const field = input.fieldRow[j];
if (field.isSerializable()) {
hasFieldState = true;
fields[field.name] = field.saveState();
}
}
}
if (hasFieldState) {
state['fields'] = fields;
}
};
Loading

0 comments on commit cc559cc

Please sign in to comment.