mirror of
https://github.com/google/blockly.git
synced 2026-01-04 23:50:12 +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>,
|
variable: IVariableModel<IVariableState>,
|
||||||
newType: string,
|
newType: string,
|
||||||
): IVariableModel<IVariableState> {
|
): IVariableModel<IVariableState> {
|
||||||
|
const oldType = variable.getType();
|
||||||
|
if (oldType === newType) return variable;
|
||||||
|
|
||||||
this.variableMap.get(variable.getType())?.delete(variable.getId());
|
this.variableMap.get(variable.getType())?.delete(variable.getId());
|
||||||
variable.setType(newType);
|
variable.setType(newType);
|
||||||
const newTypeVariables =
|
const newTypeVariables =
|
||||||
@@ -118,6 +121,13 @@ export class VariableMap
|
|||||||
if (!this.variableMap.has(newType)) {
|
if (!this.variableMap.has(newType)) {
|
||||||
this.variableMap.set(newType, newTypeVariables);
|
this.variableMap.set(newType, newTypeVariables);
|
||||||
}
|
}
|
||||||
|
eventUtils.fire(
|
||||||
|
new (eventUtils.get(EventType.VAR_TYPE_CHANGE))(
|
||||||
|
variable,
|
||||||
|
oldType,
|
||||||
|
newType,
|
||||||
|
),
|
||||||
|
);
|
||||||
return variable;
|
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