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
1 change: 1 addition & 0 deletions core/requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ goog.require('Blockly.zelos.Renderer');
goog.require('Blockly.Themes.Classic');

goog.require('Blockly.serialization.blocks');
goog.require('Blockly.serialization.variables');
62 changes: 62 additions & 0 deletions core/serialization/variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Handles serializing variables to plain JavaScript objects, only
* containing state.
*/
'use strict';

goog.module('Blockly.serialization.variables');
goog.module.declareLegacyNamespace();

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


/**
* Represents the state of a given variable.
* @typedef {{
* name: string,
* id: string,
* type: (string|undefined),
* }}
*/
var State;
exports.State = State;

/**
* Returns the state of the variable as a plain JavaScript object.
* @param {!VariableModel} variableModel The variable to serialize.
* @return {!State} The serialized state of the variable.
*/
const save = function(variableModel) {
const state = {
'name': variableModel.name,
'id': variableModel.getId()
};
if (variableModel.type) {
state['type'] = variableModel.type;
}
return state;
};
/** @package */
exports.save = save;

/**
* Loads the variable represented by the given state into the given workspace.
* Do not call this directly, use workspace.createVariable instead.
* @param {!State} state The state of a variable to deserialize into the
* workspace.
* @param {!Workspace} workspace The workspace to add the variable to.
*/
const load = function(state, workspace) {
workspace.createVariable(state['name'], state['type'], state['id']);
};
/** @package */
exports.load = load;
3 changes: 2 additions & 1 deletion tests/deps.js

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

34 changes: 27 additions & 7 deletions tests/mocha/jso_serialization_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ suite('JSO', function() {
sharedTestTeardown.call(this);
});

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

function assertNoProperty(obj, property) {
assertProperty(obj, property, undefined);
}

function assertNoProperty(obj, property) {
assertProperty(obj, property, undefined);
}

suite('Blocks', function() {
test('Null on insertionMarkers', function() {
const block = this.workspace.newBlock('row_block');
block.setInsertionMarker(true);
Expand Down Expand Up @@ -629,4 +630,23 @@ suite('JSO', function() {
});
});
});

suite('Variables', function() {
test('Without type', function() {
const variable = this.workspace.createVariable('testVar', '', 'testId');
const jso = Blockly.serialization.variables.save(variable);
assertProperty(jso, 'name', 'testVar');
assertProperty(jso, 'id', 'testId');
assertNoProperty(jso, 'type');
});

test('With type', function() {
const variable = this.workspace
.createVariable('testVar', 'testType', 'testId');
const jso = Blockly.serialization.variables.save(variable);
assertProperty(jso, 'name', 'testVar');
assertProperty(jso, 'id', 'testId');
assertProperty(jso, 'type', 'testType');
});
});
});