Skip to content

Commit 22a6b73

Browse files
authored
fix: don't show global/local options when renaming a variable (#123)
1 parent 84a9e5b commit 22a6b73

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ export { OUTPUT_SHAPE_ROUND };
5858
*/
5959
const NEW_BROADCAST_MESSAGE_ID = "NEW_BROADCAST_MESSAGE_ID";
6060
export { NEW_BROADCAST_MESSAGE_ID };
61+
62+
/**
63+
* String for use in the dropdown created in field_variable.
64+
* This string indicates that this option in the dropdown is 'Rename
65+
* variable...' and if selected, should trigger the prompt to rename a variable.
66+
*/
67+
export const RENAME_VARIABLE_ID = "RENAME_VARIABLE_ID";

src/field_variable.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import * as Blockly from "blockly/core";
2626
import * as Constants from "./constants.js";
2727
import { ScratchMsgs } from "../msg/scratch_msgs.js";
28-
import { createVariable } from "./variables.js";
28+
import { createVariable, renameVariable } from "./variables.js";
2929

3030
class FieldVariable extends Blockly.FieldVariable {
3131
constructor(varName, validator, variableTypes, defaultType, config) {
@@ -115,21 +115,23 @@ class FieldVariable extends Blockly.FieldVariable {
115115
*/
116116
onItemSelected_(menu, menuItem) {
117117
const sourceBlock = this.getSourceBlock();
118-
if (
119-
sourceBlock &&
120-
!sourceBlock.isDeadOrDying() &&
121-
menuItem.getValue() === Constants.NEW_BROADCAST_MESSAGE_ID
122-
) {
123-
createVariable(
124-
sourceBlock.workspace,
125-
(varId) => {
126-
if (varId) {
127-
this.setValue(varId);
128-
}
129-
},
130-
Constants.BROADCAST_MESSAGE_VARIABLE_TYPE
131-
);
132-
return;
118+
if (sourceBlock && !sourceBlock.isDeadOrDying()) {
119+
const selectedItem = menuItem.getValue();
120+
if (selectedItem === Constants.NEW_BROADCAST_MESSAGE_ID) {
121+
createVariable(
122+
sourceBlock.workspace,
123+
(varId) => {
124+
if (varId) {
125+
this.setValue(varId);
126+
}
127+
},
128+
Constants.BROADCAST_MESSAGE_VARIABLE_TYPE
129+
);
130+
return;
131+
} else if (selectedItem === Constants.RENAME_VARIABLE_ID) {
132+
renameVariable(sourceBlock.workspace, this.variable);
133+
return;
134+
}
133135
}
134136
super.onItemSelected_(menu, menuItem);
135137
}

0 commit comments

Comments
 (0)