Skip to content

Commit 3d1d80d

Browse files
authored
refactor!: Finish refactor of WorkspaceSvg VariableMap methods (#8946)
* docs: Make JSDoc @deprecated usage more consistent * refactor(VariableMap)!: Refresh toolbox when map modified Delete the following methods from WorkspaceSvg: - renameVariableById - deleteVariableById - createVariable Modify the following methods on VariableMap to call this.workspace.refreshToolboxSelection() if this.workspace is a WorkspaceSvg, replicating the behaviour of the aforementioned deleted methods and additionally ensuring that that method is called following any change to the variable map: - renameVariable - changeVariableType - renameVariableAndUses - createVariable - addVariable - deleteVariable BREAKING CHANGE: This change ensures that the toolbox will be refreshed regardless of what route the VaribleMap was updated, rather than only being refreshed when it is updated via calls to methods on WorkspaceSvg. Overall this is much more likely to fix a bug (where the toolbox wasn't being refreshed when it should have been) than cause one (by refreshing the toolbox when it shouldn't be), but this is still a behaviour change which could _conceivably_ result an unexpected regression. * refactor(VariableMap): Remove calls to deprecated getVariableUsesById Also refactor to use named imports core/variables.ts methods. * refactor(Workspace): Use named imports for core/variables.ts methods * refactor(FieldVariable): Remove call to deprecated getVariablesOfType * refactor(variables): Remove calls to deprecated methods * refactor(variables_dynamic): Remove call to deprecated getAllVariables * refactor(xml): Remove calls to deprecated createVariable * refactor(Events.VarCreate): Remove calls to deprecated methods * refactor(Events.VarDelete): Remove calls to deprecated methods * refactor(Events.VarRename): Remove calls to deprecated methods
1 parent 7b4f223 commit 3d1d80d

File tree

11 files changed

+74
-87
lines changed

11 files changed

+74
-87
lines changed

core/block_svg.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,13 +1083,15 @@ export class BlockSvg
10831083
}
10841084

10851085
/**
1086-
* @deprecated v11 - Set whether the block is manually enabled or disabled.
1086+
* Set whether the block is manually enabled or disabled.
1087+
*
10871088
* The user can toggle whether a block is disabled from a context menu
10881089
* option. A block may still be disabled for other reasons even if the user
10891090
* attempts to manually enable it, such as when the block is in an invalid
10901091
* location. This method is deprecated and setDisabledReason should be used
10911092
* instead.
10921093
*
1094+
* @deprecated v11: use setDisabledReason.
10931095
* @param enabled True if enabled.
10941096
*/
10951097
override setEnabled(enabled: boolean) {

core/events/events_var_create.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ export class VarCreate extends VarBase {
113113
'the constructor, or call fromJson',
114114
);
115115
}
116+
const variableMap = workspace.getVariableMap();
116117
if (forward) {
117-
workspace.createVariable(this.varName, this.varType, this.varId);
118+
variableMap.createVariable(this.varName, this.varType, this.varId);
118119
} else {
119-
workspace.deleteVariableById(this.varId);
120+
const variable = variableMap.getVariableById(this.varId);
121+
if (variable) variableMap.deleteVariable(variable);
120122
}
121123
}
122124
}

core/events/events_var_delete.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ export class VarDelete extends VarBase {
106106
'the constructor, or call fromJson',
107107
);
108108
}
109+
const variableMap = workspace.getVariableMap();
109110
if (forward) {
110-
workspace.deleteVariableById(this.varId);
111+
const variable = variableMap.getVariableById(this.varId);
112+
if (variable) variableMap.deleteVariable(variable);
111113
} else {
112-
workspace.createVariable(this.varName, this.varType, this.varId);
114+
variableMap.createVariable(this.varName, this.varType, this.varId);
113115
}
114116
}
115117
}

core/events/events_var_rename.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ export class VarRename extends VarBase {
115115
'the constructor, or call fromJson',
116116
);
117117
}
118+
const variableMap = workspace.getVariableMap();
119+
const variable = variableMap.getVariableById(this.varId);
118120
if (forward) {
119-
workspace.renameVariableById(this.varId, this.newName);
121+
if (variable) variableMap.renameVariable(variable, this.newName);
120122
} else {
121-
workspace.renameVariableById(this.varId, this.oldName);
123+
if (variable) variableMap.renameVariable(variable, this.oldName);
122124
}
123125
}
124126
}

core/field_variable.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,9 @@ export class FieldVariable extends FieldDropdown {
586586
// doesn't modify the workspace's list.
587587
for (let i = 0; i < variableTypes.length; i++) {
588588
const variableType = variableTypes[i];
589-
const variables = workspace.getVariablesOfType(variableType);
589+
const variables = workspace
590+
.getVariableMap()
591+
.getVariablesOfType(variableType);
590592
variableModelList = variableModelList.concat(variables);
591593
if (workspace.isFlyout) {
592594
variableModelList = variableModelList.concat(

core/variable_map.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import {Names} from './names.js';
2525
import * as registry from './registry.js';
2626
import * as deprecation from './utils/deprecation.js';
2727
import * as idGenerator from './utils/idgenerator.js';
28-
import * as Variables from './variables.js';
28+
import {deleteVariable, getVariableUsesById} from './variables.js';
2929
import type {Workspace} from './workspace.js';
30+
import {WorkspaceSvg} from './workspace_svg.js';
3031

3132
/**
3233
* Class for a variable map. This contains a dictionary data structure with
@@ -92,6 +93,9 @@ export class VariableMap
9293
} finally {
9394
eventUtils.setGroup(existingGroup);
9495
}
96+
if (this.workspace instanceof WorkspaceSvg) {
97+
this.workspace.refreshToolboxSelection();
98+
}
9599
return variable;
96100
}
97101

@@ -108,15 +112,17 @@ export class VariableMap
108112
if (!this.variableMap.has(newType)) {
109113
this.variableMap.set(newType, newTypeVariables);
110114
}
111-
115+
if (this.workspace instanceof WorkspaceSvg) {
116+
this.workspace.refreshToolboxSelection();
117+
}
112118
return variable;
113119
}
114120

115121
/**
116122
* Rename a variable by updating its name in the variable map. Identify the
117123
* variable to rename with the given ID.
118124
*
119-
* @deprecated v12, use VariableMap.renameVariable.
125+
* @deprecated v12: use VariableMap.renameVariable.
120126
* @param id ID of the variable to rename.
121127
* @param newName New variable name.
122128
*/
@@ -155,6 +161,9 @@ export class VariableMap
155161
for (let i = 0; i < blocks.length; i++) {
156162
blocks[i].updateVarName(variable);
157163
}
164+
if (this.workspace instanceof WorkspaceSvg) {
165+
this.workspace.refreshToolboxSelection();
166+
}
158167
}
159168

160169
/**
@@ -250,6 +259,9 @@ export class VariableMap
250259
this.variableMap.set(type, variables);
251260
}
252261
eventUtils.fire(new (eventUtils.get(EventType.VAR_CREATE))(variable));
262+
if (this.workspace instanceof WorkspaceSvg) {
263+
this.workspace.refreshToolboxSelection();
264+
}
253265
return variable;
254266
}
255267

@@ -267,6 +279,9 @@ export class VariableMap
267279
);
268280
}
269281
this.variableMap.get(type)?.set(variable.getId(), variable);
282+
if (this.workspace instanceof WorkspaceSvg) {
283+
this.workspace.refreshToolboxSelection();
284+
}
270285
}
271286

272287
/* Begin functions for variable deletion. */
@@ -276,7 +291,7 @@ export class VariableMap
276291
* @param variable Variable to delete.
277292
*/
278293
deleteVariable(variable: IVariableModel<IVariableState>) {
279-
const uses = this.getVariableUsesById(variable.getId());
294+
const uses = getVariableUsesById(this.workspace, variable.getId());
280295
const existingGroup = eventUtils.getGroup();
281296
if (!existingGroup) {
282297
eventUtils.setGroup(true);
@@ -295,13 +310,16 @@ export class VariableMap
295310
} finally {
296311
eventUtils.setGroup(existingGroup);
297312
}
313+
if (this.workspace instanceof WorkspaceSvg) {
314+
this.workspace.refreshToolboxSelection();
315+
}
298316
}
299317

300318
/**
301319
* Delete a variables by the passed in ID and all of its uses from this
302320
* workspace. May prompt the user for confirmation.
303321
*
304-
* @deprecated v12, use Blockly.Variables.deleteVariable.
322+
* @deprecated v12: use Blockly.Variables.deleteVariable.
305323
* @param id ID of variable to delete.
306324
*/
307325
deleteVariableById(id: string) {
@@ -313,7 +331,7 @@ export class VariableMap
313331
);
314332
const variable = this.getVariableById(id);
315333
if (variable) {
316-
Variables.deleteVariable(this.workspace, variable);
334+
deleteVariable(this.workspace, variable);
317335
}
318336
}
319337

@@ -398,7 +416,7 @@ export class VariableMap
398416
/**
399417
* Returns all of the variable names of all types.
400418
*
401-
* @deprecated v12, use Blockly.Variables.getAllVariables.
419+
* @deprecated v12: use Blockly.Variables.getAllVariables.
402420
* @returns All of the variable names of all types.
403421
*/
404422
getAllVariableNames(): string[] {
@@ -420,7 +438,7 @@ export class VariableMap
420438
/**
421439
* Find all the uses of a named variable.
422440
*
423-
* @deprecated v12, use Blockly.Variables.getVariableUsesById.
441+
* @deprecated v12: use Blockly.Variables.getVariableUsesById.
424442
* @param id ID of the variable to find.
425443
* @returns Array of block usages.
426444
*/
@@ -431,7 +449,7 @@ export class VariableMap
431449
'v13',
432450
'Blockly.Variables.getVariableUsesById',
433451
);
434-
return Variables.getVariableUsesById(this.workspace, id);
452+
return getVariableUsesById(this.workspace, id);
435453
}
436454
}
437455

core/variables.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export const CATEGORY_NAME = 'VARIABLE';
3131
/**
3232
* Find all user-created variables that are in use in the workspace.
3333
* For use by generators.
34+
*
3435
* To get a list of all variables on a workspace, including unused variables,
35-
* call Workspace.getAllVariables.
36+
* call getAllVariables.
3637
*
3738
* @param ws The workspace to search for variables.
3839
* @returns Array of variable models.
@@ -61,6 +62,7 @@ export function allUsedVarModels(
6162

6263
/**
6364
* Find all developer variables used by blocks in the workspace.
65+
*
6466
* Developer variables are never shown to the user, but are declared as global
6567
* variables in the generated code.
6668
* To declare developer variables, define the getDeveloperVariables function on
@@ -146,7 +148,7 @@ export function flyoutCategory(
146148
},
147149
...jsonFlyoutCategoryBlocks(
148150
workspace,
149-
workspace.getVariablesOfType(''),
151+
workspace.getVariableMap().getVariablesOfType(''),
150152
true,
151153
),
152154
];
@@ -268,7 +270,7 @@ function xmlFlyoutCategory(workspace: WorkspaceSvg): Element[] {
268270
* @returns Array of XML block elements.
269271
*/
270272
export function flyoutCategoryBlocks(workspace: Workspace): Element[] {
271-
const variableModelList = workspace.getVariablesOfType('');
273+
const variableModelList = workspace.getVariableMap().getVariablesOfType('');
272274

273275
const xmlList = [];
274276
if (variableModelList.length > 0) {
@@ -332,7 +334,10 @@ export function generateUniqueName(workspace: Workspace): string {
332334
function generateUniqueNameInternal(workspace: Workspace): string {
333335
return generateUniqueNameFromOptions(
334336
VAR_LETTER_OPTIONS.charAt(0),
335-
workspace.getAllVariableNames(),
337+
workspace
338+
.getVariableMap()
339+
.getAllVariables()
340+
.map((v) => v.getName()),
336341
);
337342
}
338343

@@ -415,7 +420,7 @@ export function createVariableButtonHandler(
415420
const existing = nameUsedWithAnyType(text, workspace);
416421
if (!existing) {
417422
// No conflict
418-
workspace.createVariable(text, type);
423+
workspace.getVariableMap().createVariable(text, type);
419424
if (opt_callback) opt_callback(text);
420425
return;
421426
}
@@ -478,7 +483,7 @@ export function renameVariable(
478483
);
479484
if (!existing && !procedure) {
480485
// No conflict.
481-
workspace.renameVariableById(variable.getId(), newName);
486+
workspace.getVariableMap().renameVariable(variable, newName);
482487
if (opt_callback) opt_callback(newName);
483488
return;
484489
}
@@ -762,6 +767,7 @@ function createVariable(
762767
opt_name?: string,
763768
opt_type?: string,
764769
): IVariableModel<IVariableState> {
770+
const variableMap = workspace.getVariableMap();
765771
const potentialVariableMap = workspace.getPotentialVariableMap();
766772
// Variables without names get uniquely named for this workspace.
767773
if (!opt_name) {
@@ -781,7 +787,7 @@ function createVariable(
781787
);
782788
} else {
783789
// In the main workspace, create a real variable.
784-
variable = workspace.createVariable(opt_name, opt_type, id);
790+
variable = variableMap.createVariable(opt_name, opt_type, id);
785791
}
786792
return variable;
787793
}

core/variables_dynamic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function flyoutCategory(
148148
},
149149
...Variables.jsonFlyoutCategoryBlocks(
150150
workspace,
151-
workspace.getAllVariables(),
151+
workspace.getVariableMap().getAllVariables(),
152152
false,
153153
'variables_get_dynamic',
154154
'variables_set_dynamic',
@@ -203,7 +203,7 @@ function xmlFlyoutCategory(workspace: WorkspaceSvg): Element[] {
203203
* @returns Array of XML block elements.
204204
*/
205205
export function flyoutCategoryBlocks(workspace: Workspace): Element[] {
206-
const variableModelList = workspace.getAllVariables();
206+
const variableModelList = workspace.getVariableMap().getAllVariables();
207207

208208
const xmlList = [];
209209
if (variableModelList.length > 0) {

0 commit comments

Comments
 (0)