mirror of
https://github.com/google/blockly.git
synced 2026-01-06 00:20:37 +01:00
refactor: Use IVariableModel instead of VariableModel. (#8400)
* refactor: Use IVariableModel methods instead of directly accessing properties. * refactor: replace references to VariableModel with IVariableModel.
This commit is contained in:
@@ -30,7 +30,7 @@ import type {MenuItem} from './menuitem.js';
|
||||
import {Msg} from './msg.js';
|
||||
import * as parsing from './utils/parsing.js';
|
||||
import {Size} from './utils/size.js';
|
||||
import {VariableModel} from './variable_model.js';
|
||||
import {IVariableModel, IVariableState} from './interfaces/i_variable_model.js';
|
||||
import * as Variables from './variables.js';
|
||||
import * as Xml from './xml.js';
|
||||
|
||||
@@ -52,7 +52,7 @@ export class FieldVariable extends FieldDropdown {
|
||||
protected override size_: Size;
|
||||
|
||||
/** The variable model associated with this field. */
|
||||
private variable: VariableModel | null = null;
|
||||
private variable: IVariableModel<IVariableState> | null = null;
|
||||
|
||||
/**
|
||||
* Serializable fields are saved by the serializer, non-serializable fields
|
||||
@@ -196,12 +196,12 @@ export class FieldVariable extends FieldDropdown {
|
||||
);
|
||||
|
||||
// This should never happen :)
|
||||
if (variableType !== null && variableType !== variable.type) {
|
||||
if (variableType !== null && variableType !== variable.getType()) {
|
||||
throw Error(
|
||||
"Serialized variable type with id '" +
|
||||
variable.getId() +
|
||||
"' had type " +
|
||||
variable.type +
|
||||
variable.getType() +
|
||||
', and ' +
|
||||
'does not match variable field that references it: ' +
|
||||
Xml.domToText(fieldElement) +
|
||||
@@ -224,9 +224,9 @@ export class FieldVariable extends FieldDropdown {
|
||||
this.initModel();
|
||||
|
||||
fieldElement.id = this.variable!.getId();
|
||||
fieldElement.textContent = this.variable!.name;
|
||||
if (this.variable!.type) {
|
||||
fieldElement.setAttribute('variabletype', this.variable!.type);
|
||||
fieldElement.textContent = this.variable!.getName();
|
||||
if (this.variable!.getType()) {
|
||||
fieldElement.setAttribute('variabletype', this.variable!.getType());
|
||||
}
|
||||
return fieldElement;
|
||||
}
|
||||
@@ -249,8 +249,8 @@ export class FieldVariable extends FieldDropdown {
|
||||
this.initModel();
|
||||
const state = {'id': this.variable!.getId()};
|
||||
if (doFullSerialization) {
|
||||
(state as AnyDuringMigration)['name'] = this.variable!.name;
|
||||
(state as AnyDuringMigration)['type'] = this.variable!.type;
|
||||
(state as AnyDuringMigration)['name'] = this.variable!.getName();
|
||||
(state as AnyDuringMigration)['type'] = this.variable!.getType();
|
||||
}
|
||||
return state;
|
||||
}
|
||||
@@ -307,7 +307,7 @@ export class FieldVariable extends FieldDropdown {
|
||||
* is selected.
|
||||
*/
|
||||
override getText(): string {
|
||||
return this.variable ? this.variable.name : '';
|
||||
return this.variable ? this.variable.getName() : '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -318,7 +318,7 @@ export class FieldVariable extends FieldDropdown {
|
||||
* @returns The selected variable, or null if none was selected.
|
||||
* @internal
|
||||
*/
|
||||
getVariable(): VariableModel | null {
|
||||
getVariable(): IVariableModel<IVariableState> | null {
|
||||
return this.variable;
|
||||
}
|
||||
|
||||
@@ -365,7 +365,7 @@ export class FieldVariable extends FieldDropdown {
|
||||
return null;
|
||||
}
|
||||
// Type Checks.
|
||||
const type = variable.type;
|
||||
const type = variable.getType();
|
||||
if (!this.typeIsAllowed(type)) {
|
||||
console.warn("Variable type doesn't match this field! Type was " + type);
|
||||
return null;
|
||||
@@ -499,16 +499,13 @@ export class FieldVariable extends FieldDropdown {
|
||||
const id = menuItem.getValue();
|
||||
// Handle special cases.
|
||||
if (this.sourceBlock_ && !this.sourceBlock_.isDeadOrDying()) {
|
||||
if (id === internalConstants.RENAME_VARIABLE_ID) {
|
||||
if (id === internalConstants.RENAME_VARIABLE_ID && this.variable) {
|
||||
// Rename variable.
|
||||
Variables.renameVariable(
|
||||
this.sourceBlock_.workspace,
|
||||
this.variable as VariableModel,
|
||||
);
|
||||
Variables.renameVariable(this.sourceBlock_.workspace, this.variable);
|
||||
return;
|
||||
} else if (id === internalConstants.DELETE_VARIABLE_ID) {
|
||||
} else if (id === internalConstants.DELETE_VARIABLE_ID && this.variable) {
|
||||
// Delete variable.
|
||||
this.sourceBlock_.workspace.deleteVariableById(this.variable!.getId());
|
||||
this.sourceBlock_.workspace.deleteVariableById(this.variable.getId());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -560,7 +557,7 @@ export class FieldVariable extends FieldDropdown {
|
||||
);
|
||||
}
|
||||
const name = this.getText();
|
||||
let variableModelList: VariableModel[] = [];
|
||||
let variableModelList: IVariableModel<IVariableState>[] = [];
|
||||
if (this.sourceBlock_ && !this.sourceBlock_.isDeadOrDying()) {
|
||||
const variableTypes = this.getVariableTypes();
|
||||
// Get a copy of the list, so that adding rename and new variable options
|
||||
@@ -572,12 +569,15 @@ export class FieldVariable extends FieldDropdown {
|
||||
variableModelList = variableModelList.concat(variables);
|
||||
}
|
||||
}
|
||||
variableModelList.sort(VariableModel.compareByName);
|
||||
variableModelList.sort(Variables.compareByName);
|
||||
|
||||
const options: [string, string][] = [];
|
||||
for (let i = 0; i < variableModelList.length; i++) {
|
||||
// Set the UUID as the internal representation of the variable.
|
||||
options[i] = [variableModelList[i].name, variableModelList[i].getId()];
|
||||
options[i] = [
|
||||
variableModelList[i].getName(),
|
||||
variableModelList[i].getId(),
|
||||
];
|
||||
}
|
||||
options.push([
|
||||
Msg['RENAME_VARIABLE'],
|
||||
|
||||
Reference in New Issue
Block a user