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
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;
}
};
1 change: 1 addition & 0 deletions tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading