8000 refactor: Use IVariableModel instead of VariableModel. by gonfunko · Pull Request #8400 · google/blockly · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: Use IVariableModel instead of VariableModel. #8400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2024
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
2 changes: 1 addition & 1 deletion blocks/loops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ const CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN = {
}
const varField = this.getField('VAR') as FieldVariable;
const variable = varField.getVariable()!;
const varName = variable.name;
const varName = variable.getName();
if (!this.isCollapsed() && varName !== null) {
const getVarBlockState = {
type: 'variables_get',
Expand Down
47 changes: 28 additions & 19 deletions blocks/procedures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ import {FieldTextInput} from '../core/field_textinput.js';
import {Msg} from '../core/msg.js';
import {MutatorIcon as Mutator} from '../core/icons/mutator_icon.js';
import {Names} from '../core/names.js';
import type {VariableModel} from '../core/variable_model.js';
import type {
IVariableModel,
IVariableState,
} from '../core/interfaces/i_variable_model.js';
import type {Workspace} from '../core/workspace.js';
import type {WorkspaceSvg} from '../core/workspace_svg.js';
import {config} from '../core/config.js';
Expand All @@ -48,7 +51,7 @@ export const blocks: {[key: string]: BlockDefinition} = {};
type ProcedureBlock = Block & ProcedureMixin;
interface ProcedureMixin extends ProcedureMixinType {
arguments_: string[];
argumentVarModels_: VariableModel[];
argumentVarModels_: IVariableModel<IVariableState>[];
callType_: string;
paramIds_: string[];
hasStatements_: boolean;
Expand Down Expand Up @@ -128,7 +131,7 @@ const PROCEDURE_DEF_COMMON = {
for (let i = 0; i < this.argumentVarModels_.length; i++) {
const parameter = xmlUtils.createElement('arg');
const argModel = this.argumentVarModels_[i];
parameter.setAttribute('name', argModel.name);
parameter.setAttribute('name', argModel.getName());
parameter.setAttribute('varid', argModel.getId());
if (opt_paramIds && this.paramIds_) {
parameter.setAttribute('paramId', this.paramIds_[i]);
Expand Down Expand Up @@ -196,7 +199,7 @@ const PROCEDURE_DEF_COMMON = {
state['params'].push({
// We don't need to serialize the name, but just in case we decide
// to separate params from variables.
'name': this.argumentVarModels_[i].name,
'name': this.argumentVarModels_[i].getName(),
'id': this.argumentVarModels_[i].getId(),
});
}
Expand Down Expand Up @@ -224,7 +227,7 @@ const PROCEDURE_DEF_COMMON = {
param['name'],
'',
);
this.arguments_.push(variable.name);
this.arguments_.push(variable.getName());
this.argumentVarModels_.push(variable);
}
}
Expand Down Expand Up @@ -352,7 +355,9 @@ const PROCEDURE_DEF_COMMON = {
*
* @returns List of variable models.
*/
getVarModels: function (this: ProcedureBlock): VariableModel[] {
getVarModels: function (
this: ProcedureBlock,
): IVariableModel<IVariableState>[] {
return this.argumentVarModels_;
},
/**
Expand All @@ -370,23 +375,23 @@ const PROCEDURE_DEF_COMMON = {
newId: string,
) {
const oldVariable = this.workspace.getVariableById(oldId)!;
if (oldVariable.type !== '') {
if (oldVariable.getType() !== '') {
// Procedure arguments always have the empty type.
return;
}
const oldName = oldVariable.name;
const oldName = oldVariable.getName();
const newVar = this.workspace.getVariableById(newId)!;

let change = false;
for (let i = 0; i < this.argumentVarModels_.length; i++) {
if (this.argumentVarModels_[i].getId() === oldId) {
this.arguments_[i] = newVar.name;
this.arguments_[i] = newVar.getName();
this.argumentVarModels_[i] = newVar;
change = true;
}
}
if (change) {
this.displayRenamedVar_(oldName, newVar.name);
this.displayRenamedVar_(oldName, newVar.getName());
Procedures.mutateCallers(this);
}
},
Expand All @@ -398,9 +403,9 @@ const PROCEDURE_DEF_COMMON = {
*/
updateVarName: function (
this: ProcedureBlock & BlockSvg,
variable: VariableModel,
variable: IVariableModel<IVariableState>,
) {
const newName = variable.name;
const newName = variable.getName();
let change = false;
let oldName;
for (let i = 0; i < this.argumentVarModels_.length; i++) {
Expand Down Expand Up @@ -473,12 +478,16 @@ const PROCEDURE_DEF_COMMON = {
const getVarBlockState = {
type: 'variables_get',
fields: {
VAR: {name: argVar.name, id: argVar.getId(), type: argVar.type},
VAR: {
name: argVar.getName(),
id: argVar.getId(),
type: argVar.getType(),
},
},
};
options.push({
enabled: true,
text: Msg['VARIABLES_SET_CREATE_GET'].replace('%1', argVar.name),
text: Msg['VARIABLES_SET_CREATE_GET'].replace('%1', argVar.getName()),
callback: ContextMenu.callbackFactory(this, getVarBlockState),
});
}
Expand Down Expand Up @@ -623,7 +632,7 @@ type ArgumentMixinType = typeof PROCEDURES_MUTATORARGUMENT;
// TODO(#6920): This is kludgy.
type FieldTextInputForArgument = FieldTextInput & {
oldShowEditorFn_(_e?: Event, quietInput?: boolean): void;
createdVariables_: VariableModel[];
createdVariables_: IVariableModel<IVariableState>[];
};

const PROCEDURES_MUTATORARGUMENT = {
Expand Down Expand Up @@ -708,7 +717,7 @@ const PROCEDURES_MUTATORARGUMENT = {
}

let model = outerWs.getVariable(varName, '');
if (model && model.name !== varName) {
if (model && model.getName() !== varName) {
// Rename the variable (case change)
outerWs.renameVariableById(model.getId(), varName);
}
Expand Down Expand Up @@ -739,7 +748,7 @@ const PROCEDURES_MUTATORARGUMENT = {
}
for (let i = 0; i < this.createdVariables_.length; i++) {
const model = this.createdVariables_[i];
if (model.name !== newText) {
if (model.getName() !== newText) {
outerWs.deleteVariableById(model.getId());
}
}
Expand All @@ -750,7 +759,7 @@ blocks['procedures_mutatorarg'] = PROCEDURES_MUTATORARGUMENT;
/** Type of a block using the PROCEDURE_CALL_COMMON mixin. */
type CallBlock = Block & CallMixin;
interface CallMixin extends CallMixinType {
argumentVarModels_: VariableModel[];
argumentVarModels_: IVariableModel<IVariableState>[];
arguments_: string[];
defType_: string;
quarkIds_: string[] | null;
Expand Down Expand Up @@ -1029,7 +1038,7 @@ const PROCEDURE_CALL_COMMON = {
*
* @returns List of variable models.
*/
getVarModels: function (this: CallBlock): VariableModel[] {
getVarModels: function (this: CallBlock): IVariableModel<IVariableState>[] {
return this.argumentVarModels_;
},
/**
Expand Down
4 changes: 2 additions & 2 deletions blocks/variables_dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ const CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
const id = this.getFieldValue('VAR');
const variableModel = Variables.getVariable(this.workspace, id)!;
if (this.type === 'variables_get_dynamic') {
this.outputConnection!.setCheck(variableModel.type);
this.outputConnection!.setCheck(variableModel.getType());
} else {
this.getInput('VALUE')!.connection!.setCheck(variableModel.type);
this.getInput('VALUE')!.connection!.setCheck(variableModel.getType());
}
},
};
Expand Down
9 changes: 6 additions & 3 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ import * as idGenerator from './utils/idgenerator.js';
import * as parsing from './utils/parsing.js';
import * as registry from './registry.js';
import {Size} from './utils/size.js';
import type {VariableModel} from './variable_model.js';
import type {
IVariableModel,
IVariableState,
} from './interfaces/i_variable_model.js';
import type {Workspace} from './workspace.js';
import {DummyInput} from './inputs/dummy_input.js';
import {EndRowInput} from './inputs/end_row_input.js';
Expand Down Expand Up @@ -1133,7 +1136,7 @@ export class Block implements IASTNodeLocation {
* @returns List of variable models.
* @internal
*/
getVarModels(): VariableModel[] {
getVarModels(): IVariableModel<IVariableState>[] {
const vars = [];
for (let i = 0, input; (input = this.inputList[i]); i++) {
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
Expand All @@ -1159,7 +1162,7 @@ export class Block implements IASTNodeLocation {
* @param variable The variable being renamed.
* @internal
*/
updateVarName(variable: VariableModel) {
updateVarName(variable: IVariableModel<IVariableState>) {
for (let i = 0, input; (input = this.inputList[i]); i++) {
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
if (
Expand Down
9 changes: 6 additions & 3 deletions core/events/events_var_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
*/
// Former goog.module ID: Blockly.Events.VarBase

import type {VariableModel} from '../variable_model.js';
import type {
IVariableModel,
IVariableState,
} from '../interfaces/i_variable_model.js';

import {
Abstract as AbstractEvent,
Expand All @@ -31,13 +34,13 @@ export class VarBase extends AbstractEvent {
* @param opt_variable The variable this event corresponds to. Undefined for
* a blank event.
*/
constructor(opt_variable?: VariableModel) {
constructor(opt_variable?: IVariableModel<IVariableState>) {
super();
this.isBlank = typeof opt_variable === 'undefined';
if (!opt_variable) return;

this.varId = opt_variable.getId();
this.workspaceId = opt_variable.workspace.id;
this.workspaceId = opt_variable.getWorkspace().id;
}

/**
Expand Down
11 changes: 7 additions & 4 deletions core/events/events_var_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// Former goog.module ID: Blockly.Events.VarCreate

import * as registry from '../registry.js';
import type {VariableModel} from '../variable_model.js';
import type {
IVariableModel,
IVariableState,
} from '../interfaces/i_variable_model.js';

import {VarBase, VarBaseJson} from './events_var_base.js';
import * as eventUtils from './utils.js';
Expand All @@ -33,14 +36,14 @@ export class VarCreate extends VarBase {
/**
* @param opt_variable The created variable. Undefined for a blank event.
*/
constructor(opt_variable?: VariableModel) {
constructor(opt_variable?: IVariableModel<IVariableState>) {
super(opt_variable);

if (!opt_variable) {
return; // Blank event to be populated by fromJson.
}
this.varType = opt_variable.type;
this.varName = opt_variable.name;
this.varType = opt_variable.getType();
this.varName = opt_variable.getName();
}

/**
Expand Down
11 changes: 7 additions & 4 deletions core/events/events_var_delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
// Former goog.module ID: Blockly.Events.VarDelete

import * as registry from '../registry.js';
import type {VariableModel} from '../variable_model.js';
import type {
IVariableModel,
IVariableState,
} from '../interfaces/i_variable_model.js';

import {VarBase, VarBaseJson} from './events_var_base.js';
import * as eventUtils from './utils.js';
Expand All @@ -28,14 +31,14 @@ export class VarDelete extends VarBase {
/**
* @param opt_variable The deleted variable. Undefined for a blank event.
*/
constructor(opt_variable?: VariableModel) {
constructor(opt_variable?: IVariableModel<IVariableState>) {
super(opt_variable);

if (!opt_variable) {
return; // Blank event to be populated by fromJson.
}
this.varType = opt_variable.type;
this.varName = opt_variable.name;
this.varType = opt_variable.getType();
this.varName = opt_variable.getName();
}

/**
Expand Down
9 changes: 6 additions & 3 deletions core/events/events_var_rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
// Former goog.module ID: Blockly.Events.VarRename

import * as registry from '../registry.js';
import type {VariableModel} from '../variable_model.js';
import type {
IVariableModel,
IVariableState,
} from '../interfaces/i_variable_model.js';

import {VarBase, VarBaseJson} from './events_var_base.js';
import * as eventUtils from './utils.js';
Expand All @@ -31,13 +34,13 @@ export class VarRename extends VarBase {
* @param opt_variable The renamed variable. Undefined for a blank event.
* @param newName The new name the variable will be changed to.
*/
constructor(opt_variable?: VariableModel, newName?: string) {
constructor(opt_variable?: IVariableModel<IVariableState>, newName?: string) {
super(opt_variable);

if (!opt_variable) {
return; // Blank event to be populated by fromJson.
}
this.oldName = opt_variable.name;
this.oldName = opt_variable.getName();
this.newName = typeof newName === 'undefined' ? '' : newName;
}

Expand Down
Loading
Loading
0