mirror of
https://github.com/google/blockly.git
synced 2026-01-06 16:40:07 +01:00
fix: Fire a VarTypeChange event when changing a variable's type. (#9236)
This commit is contained in:
@@ -109,6 +109,9 @@ export class VariableMap
|
||||
variable: IVariableModel<IVariableState>,
|
||||
newType: string,
|
||||
): IVariableModel<IVariableState> {
|
||||
const oldType = variable.getType();
|
||||
if (oldType === newType) return variable;
|
||||
|
||||
this.variableMap.get(variable.getType())?.delete(variable.getId());
|
||||
variable.setType(newType);
|
||||
const newTypeVariables =
|
||||
@@ -118,6 +121,13 @@ export class VariableMap
|
||||
if (!this.variableMap.has(newType)) {
|
||||
this.variableMap.set(newType, newTypeVariables);
|
||||
}
|
||||
eventUtils.fire(
|
||||
new (eventUtils.get(EventType.VAR_TYPE_CHANGE))(
|
||||
variable,
|
||||
oldType,
|
||||
newType,
|
||||
),
|
||||
);
|
||||
return variable;
|
||||
}
|
||||
|
||||
|
||||
@@ -505,5 +505,26 @@ suite('Variable Map', function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
suite('variable type change events', function () {
|
||||
test('are fired when a variable has its type changed', function () {
|
||||
const variable = this.variableMap.createVariable(
|
||||
'name1',
|
||||
'type1',
|
||||
'id1',
|
||||
);
|
||||
this.variableMap.changeVariableType(variable, 'type2');
|
||||
assertEventFired(
|
||||
this.eventSpy,
|
||||
Blockly.Events.VarTypeChange,
|
||||
{
|
||||
oldType: 'type1',
|
||||
newType: 'type2',
|
||||
varId: 'id1',
|
||||
},
|
||||
this.workspace.id,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user