Skip to content

Commit 391672c

Browse files
committed
make sure the toggle updates in the dropdown and toolbox
1 parent 2b39f69 commit 391672c

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

pxtblocks/plugins/newVariableField/fieldVariable.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,23 @@ export class FieldVariable extends Blockly.FieldVariable {
108108
if (!variable) return;
109109

110110
const workspace = this.sourceBlock_.workspace;
111+
const map = workspace.getVariableMap();
111112
const varName = variable.getName();
112113
const varId = variable.getId();
113114
const isGlobal = variable.getType() === EXPORTED_VARIABLE_TYPE;
114-
console.log("this is the variable type");
115-
console.log(variable.getType());
115+
const newType = isGlobal ? '' : EXPORTED_VARIABLE_TYPE;
116116

117117
if (isGlobal) {
118118
// Check if the variable is referenced in other workspaces
119119
const program = getGlobalProgram();
120120
if (program) {
121121
const allWorkspaces = program.getAllWorkspaces();
122-
for (const ws of allWorkspaces) {
123-
if (ws === workspace) continue;
122+
for (const fileWs of allWorkspaces) {
123+
if (fileWs.workspace === workspace) continue;
124124

125-
const importedVar = ws.getVariableMap().getVariableById(varId);
125+
const importedVar = fileWs.workspace.getVariableMap().getVariableById(varId);
126126
if (importedVar) {
127-
const uses = Blockly.Variables.getVariableUsesById(ws, varId);
127+
const uses = Blockly.Variables.getVariableUsesById(fileWs.workspace, varId);
128128
if (uses.length > 0) {
129129
Blockly.dialog.alert(
130130
Blockly.Msg['CANNOT_MAKE_VARIABLE_LOCAL'].replace('%1', varName)
@@ -134,11 +134,8 @@ export class FieldVariable extends Blockly.FieldVariable {
134134
}
135135
}
136136
}
137-
138-
variable.setType('');
139-
} else {
140-
variable.setType(EXPORTED_VARIABLE_TYPE);
141137
}
138+
map.changeVariableType(variable, newType);
142139

143140
// Re-render all blocks that reference this variable so the globe icon updates
144141
const uses = Blockly.Variables.getVariableUsesById(workspace, varId);
@@ -154,6 +151,14 @@ export class FieldVariable extends Blockly.FieldVariable {
154151
if (program) {
155152
program.refreshSymbols?.();
156153
}
154+
155+
// Refresh the flyout so variable blocks there reflect the updated type
156+
if (workspace instanceof Blockly.WorkspaceSvg) {
157+
const toolbox = workspace.getToolbox();
158+
if (toolbox) {
159+
(toolbox as Blockly.Toolbox).refreshSelection();
160+
}
161+
}
157162
}
158163

159164
// Everything in this class below this line is duplicated in pxtblocks/fields/field_dropown
@@ -382,7 +387,9 @@ export class FieldVariable extends Blockly.FieldVariable {
382387
let iconValues: string[] = [];
383388

384389
if (varMap) {
385-
iconValues = varMap.getVariablesOfType(EXPORTED_VARIABLE_TYPE).concat(varMap.getVariablesOfType(IMPORTED_VARIABLE_TYPE)).map(v => v.getId());
390+
iconValues = varMap.getAllVariables()
391+
.filter(v => v.getType() === EXPORTED_VARIABLE_TYPE || v.getType() === IMPORTED_VARIABLE_TYPE)
392+
.map(v => v.getId());
386393
}
387394

388395
showEditorMixin.call(this, e, "icon globe", iconValues);

0 commit comments

Comments
 (0)