Merge pull request #1827 from rachel-fenichel/feature/references_variables

Add a referencesVariables function to field
This commit is contained in:
Rachel Fenichel
2018-04-27 15:34:53 -07:00
committed by GitHub
4 changed files with 27 additions and 6 deletions

View File

@@ -751,7 +751,7 @@ Blockly.Block.prototype.getVars = function() {
var vars = [];
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field instanceof Blockly.FieldVariable) {
if (field.referencesVariables()) {
vars.push(field.getValue());
}
}
@@ -768,7 +768,7 @@ Blockly.Block.prototype.getVarModels = function() {
var vars = [];
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field instanceof Blockly.FieldVariable) {
if (field.referencesVariables()) {
var model = this.workspace.getVariableById(field.getValue());
// Check if the variable actually exists (and isn't just a potential
// variable).
@@ -790,7 +790,7 @@ Blockly.Block.prototype.getVarModels = function() {
Blockly.Block.prototype.updateVarName = function(variable) {
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field instanceof Blockly.FieldVariable &&
if (field.referencesVariables() &&
variable.getId() == field.getValue()) {
field.setText(variable.name);
}
@@ -808,7 +808,7 @@ Blockly.Block.prototype.updateVarName = function(variable) {
Blockly.Block.prototype.renameVarById = function(oldId, newId) {
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field instanceof Blockly.FieldVariable &&
if (field.referencesVariables() &&
oldId == field.getValue()) {
field.setValue(newId);
}

View File

@@ -604,3 +604,14 @@ Blockly.Field.prototype.setTooltip = function(_newTip) {
Blockly.Field.prototype.getAbsoluteXY_ = function() {
return goog.style.getPageOffset(this.borderRect_);
};
/**
* Whether this field references any Blockly variables. If true it may need to
* be handled differently during serialization and deserialization. Subclasses
* may override this.
* @return {boolean} True if this field has any variable references.
* @package
*/
Blockly.Field.prototype.referencesVariables = function() {
return false;
};

View File

@@ -353,4 +353,14 @@ Blockly.FieldVariable.prototype.onItemSelected = function(menu, menuItem) {
this.setValue(id);
};
/**
* Overrides referencesVariables(), indicating this field refers to a variable.
* @return {boolean} True.
* @package
* @override
*/
Blockly.FieldVariable.prototype.referencesVariables = function() {
return true;
};
Blockly.Field.register('field_variable', Blockly.FieldVariable);

View File

@@ -138,7 +138,7 @@ Blockly.Xml.fieldToDomVariable_ = function(field) {
*/
Blockly.Xml.fieldToDom_ = function(field) {
if (field.name && field.EDITABLE) {
if (field instanceof Blockly.FieldVariable) {
if (field.referencesVariables()) {
return Blockly.Xml.fieldToDomVariable_(field);
} else {
var container = goog.dom.createDom('field', null, field.getValue());
@@ -806,7 +806,7 @@ Blockly.Xml.domToField_ = function(block, fieldName, xml) {
var workspace = block.workspace;
var text = xml.textContent;
if (field instanceof Blockly.FieldVariable) {
if (field.referencesVariables()) {
Blockly.Xml.domToFieldVariable_(workspace, xml, text, field);
} else {
field.setValue(text);