Skip to content

Commit

Permalink
fix: not being able to specifying variable names in toolbox (google#5408
Browse files Browse the repository at this point in the history
)

* fix: not being able to specifying variable names in toolbox

* fix: id -> ID
  • Loading branch information
BeksOmega authored and alschmiedt committed Sep 20, 2021
1 parent 4f890d7 commit 6d87b85
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
4 changes: 2 additions & 2 deletions core/field_dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ FieldDropdown.prototype.fromXml = function(fieldElement) {

/**
* Saves this field's value.
* @return {string} The dropdown value held by this field.
* @return {*} The dropdown value held by this field.
* @override
* @package
*/
FieldDropdown.prototype.saveState = function() {
return /** @type {string} */ (this.getValue());
return this.getValue();
};

/**
Expand Down
18 changes: 13 additions & 5 deletions core/field_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,24 +199,32 @@ FieldVariable.prototype.toXml = function(fieldElement) {

/**
* Saves this field's value.
* @return {string} The id of the variable referenced by this field.
* @return {{id: string}} The ID of the variable referenced by this field.
* @override
* @package
*/
FieldVariable.prototype.saveState = function() {
// Make sure the variable is initialized.
this.initModel();
return this.variable_.getId();
return {
'id': 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.
* @param {*} state The state of the variable to assign to this variable field.
* @override
* @package
*/
FieldVariable.prototype.loadState = function(id) {
this.setValue(id);
FieldVariable.prototype.loadState = function(state) {
// This is necessary so that blocks in the flyout can have custom var names.
const variable = Variables.getOrCreateVariablePackage(
this.sourceBlock_.workspace,
state['id'] || null,
state['name'],
state['type'] || '');
this.setValue(variable.getId());
};

/**
Expand Down
66 changes: 64 additions & 2 deletions tests/mocha/field_variable_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ suite('Variable Fields', function() {
const field = new Blockly.FieldVariable('x');
block.getInput('INPUT').appendField(field, 'VAR');
const jso = Blockly.serialization.blocks.save(block);
chai.assert.deepEqual(jso['fields'], {'VAR': 'id2'});
chai.assert.deepEqual(jso['fields'], {'VAR': {'id': 'id2'}});
});

test('Typed', function() {
Expand All @@ -408,7 +408,69 @@ suite('Variable Fields', function() {
new Blockly.FieldVariable('x', undefined, undefined, ['String']);
block.getInput('INPUT').appendField(field, 'VAR');
const jso = Blockly.serialization.blocks.save(block);
chai.assert.deepEqual(jso['fields'], {'VAR': 'id2'});
chai.assert.deepEqual(jso['fields'], {'VAR': {'id': 'id2'}});
});
});

suite('Deserialization', function() {
setup(function() {
this.workspace = new Blockly.Workspace();
defineRowBlock();
createGenUidStubWithReturns(new Array(10).fill().map((_, i) => 'id' + i));
});

teardown(function() {
workspaceTeardown.call(this, this.workspace);
});

test('ID', function() {
this.workspace.createVariable('test', '', 'id1');
var block = Blockly.serialization.blocks.load({
'type': 'variables_get',
'fields': {
'VAR': {
'id': 'id1'
}
}
},
this.workspace);
var variable = block.getField('VAR').getVariable();
chai.assert.equal(variable.name, 'test');
chai.assert.equal(variable.type, '');
chai.assert.equal(variable.getId(), 'id1');
});

test('Name, untyped', function() {
var block = Blockly.serialization.blocks.load({
'type': 'variables_get',
'fields': {
'VAR': {
'name': 'test',
}
}
},
this.workspace);
var variable = block.getField('VAR').getVariable();
chai.assert.equal(variable.name, 'test');
chai.assert.equal(variable.type, '');
chai.assert.equal(variable.getId(), 'id2');
});

test('Name, typed', function() {
var block = Blockly.serialization.blocks.load({
'type': 'variables_get',
'fields': {
'VAR': {
'name': 'test',
'type': 'string',
}
}
},
this.workspace);
var variable = block.getField('VAR').getVariable();
chai.assert.equal(variable.name, 'test');
chai.assert.equal(variable.type, 'string');
chai.assert.equal(variable.getId(), 'id2');
});
});
});
8 changes: 6 additions & 2 deletions tests/mocha/jso_deserialization_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ suite('JSO Deserialization', function() {
'x': 42,
'y': 42,
'fields': {
'VAR': 'testId'
'VAR': {
'id': 'testId'
}
}
},
]
Expand Down Expand Up @@ -196,7 +198,9 @@ suite('JSO Deserialization', function() {
'x': 42,
'y': 42,
'fields': {
'VAR': 'testId'
'VAR': {
'id': 'testId'
}
}
},
]
Expand Down

0 comments on commit 6d87b85

Please sign in to comment.