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: 18 additions & 4 deletions core/serialization/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ goog.module.declareLegacyNamespace();
* movable: (boolean|undefined),
* inline: (boolean|undefined),
* data: (string|undefined),
* extra-state: *
* }}
*/
var State;
Expand Down Expand Up @@ -60,6 +61,7 @@ const save = function(block, {addCoordinates = false} = {}) {
addCoords(block, state);
}
addAttributes(block, state);
addExtraState(block, state);

return state;
};
Expand All @@ -69,8 +71,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 {!State} state The state object to append
* to.
* @param {!State} state The state object to append to.
*/
const addAttributes = function(block, state) {
if (block.isCollapsed()) {
Expand Down Expand Up @@ -103,12 +104,25 @@ 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 {!State} state The state object to append
* to
* @param {!State} state The state object to append to
*/
const addCoords = function(block, state) {
const workspace = block.workspace;
const xy = block.getRelativeToSurfaceXY();
state['x'] = Math.round(workspace.RTL ? workspace.getWidth() - xy.x : xy.x);
state['y'] = Math.round(xy.y);
};

/**
* 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 {!State} state The state object to append to.
*/
const addExtraState = function(block, state) {
if (block.saveExtraState) {
const extraState = block.saveExtraState();
if (extraState !== null) {
state['extraState'] = extraState;
}
}
};
40 changes: 39 additions & 1 deletion tests/mocha/jso_serialization_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suite('JSO', function() {
suite('Save Single Block', function() {

function assertProperty(obj, property, value) {
chai.assert.equal(obj[property], value);
chai.assert.deepEqual(obj[property], value);
}

function assertNoProperty(obj, property) {
Expand Down Expand Up @@ -231,6 +231,44 @@ suite('JSO', function() {
assertProperty(jso, 'y', 0);
});
});

// Mutators.
suite('Extra state', function() {
test('Simple value', function() {
const block = this.workspace.newBlock('row_block');
block.saveExtraState = function() {
return 'some extra state';
};
const jso = Blockly.serialization.blocks.save(block);
assertProperty(jso, 'extraState', 'some extra state');
});

test('Object', function() {
const block = this.workspace.newBlock('row_block');
block.saveExtraState = function() {
return {
'extra1': 'state1',
'extra2': 42,
'extra3': true,
};
};
const jso = Blockly.serialization.blocks.save(block);
assertProperty(jso, 'extraState', {
'extra1': 'state1',
'extra2': 42,
'extra3': true,
});
});

test('Array', function() {
const block = this.workspace.newBlock('row_block');
block.saveExtraState = function() {
return ['state1', 42, true];
};
const jso = Blockly.serialization.blocks.save(block);
assertProperty(jso, 'extraState', ['state1', 42, true]);
});
});
});
});
});