Skip to content

Commit 01477cc

Browse files
committed
global variables
1 parent 62eec4d commit 01477cc

4 files changed

Lines changed: 66 additions & 14 deletions

File tree

pxtblocks/blocksProgram.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface BlocksProgram {
6262
getKindInfo(kindName: string): string[];
6363
getVariableQualifiedName(varName: string, workspace: Blockly.Workspace): string;
6464
refreshSymbols?(): void;
65+
getVariableSymbol(varId: string): BlocksVariableSymbol;
6566
}
6667

6768

@@ -122,6 +123,19 @@ export class SingleWorkspaceBlocksProgram implements BlocksProgram {
122123
getVariableQualifiedName(varName: string, workspace: Blockly.Workspace): string {
123124
return varName;
124125
}
126+
127+
getVariableSymbol(varId: string): BlocksVariableSymbol {
128+
const variable = this.workspace.getVariableMap().getVariableById(varId);
129+
if (variable) {
130+
return {
131+
type: "variable",
132+
name: variable.getName(),
133+
id: variable.getId(),
134+
file: "main.blocks"
135+
};
136+
}
137+
return null;
138+
}
125139
}
126140

127141

@@ -307,6 +321,17 @@ export class MultiWorkspaceBlocksProgram implements BlocksProgram {
307321
throw new Error(`Variable ${varName} not found in any workspace`);
308322
}
309323

324+
getVariableSymbol(varId: string): BlocksVariableSymbol {
325+
for (const symbols of this.symbolsCache.values()) {
326+
for (const symbol of symbols) {
327+
if (symbol.type === "variable" && symbol.id === varId) {
328+
return symbol;
329+
}
330+
}
331+
}
332+
return null;
333+
}
334+
310335
protected defineSymbolInWorkspace(symbol: BlocksSymbol, workspace: Blockly.Workspace) {
311336
const map = workspace.getVariableMap();
312337

pxtblocks/builtins/variables.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { installBuiltinHelpInfo, setBuiltinHelpInfo } from "../help";
44
import { EXPORTED_VARIABLE_TYPE, IMPORTED_VARIABLE_TYPE } from "../blocksProgram";
55

66
export const CREATE_VAR_BTN_ID = 'create-variable-btn';
7+
export const CREATE_GLOBAL_VAR_BTN_ID = 'create-global-variable-btn';
78

89
export function initVariables() {
910
let varname = lf("{id:var}item");
@@ -96,7 +97,7 @@ export function initVariables() {
9697
const variablesGetDef = pxt.blocks.getBlockDefinition(variablesGetId);
9798
msg.VARIABLES_GET_CREATE_SET = variablesGetDef.block["VARIABLES_GET_CREATE_SET"];
9899
Blockly.Blocks[variablesGetId] = {
99-
init: function() {
100+
init: function () {
100101
this.jsonInit(
101102
{
102103
"type": "variables_get",
@@ -106,7 +107,7 @@ export function initVariables() {
106107
"type": "field_variable",
107108
"name": "VAR",
108109
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}",
109-
"variableTypes": [""],
110+
"variableTypes": ["", IMPORTED_VARIABLE_TYPE, EXPORTED_VARIABLE_TYPE],
110111
},
111112
],
112113
"output": null,
@@ -137,22 +138,22 @@ export function initVariables() {
137138
msg.VARIABLES_DEFAULT_NAME = varname;
138139
msg.VARIABLES_SET_CREATE_GET = lf("Create 'get %1'");
139140
Blockly.Blocks[variablesSetId] = {
140-
init: function() {
141+
init: function () {
141142
this.jsonInit(
142143
{
143144
"type": "variables_set",
144145
"message0": "%{BKY_VARIABLES_SET}",
145146
"args0": [
146-
{
147-
"type": "field_variable",
148-
"name": "VAR",
149-
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}",
150-
"variableTypes": [""],
151-
},
152-
{
153-
"type": "input_value",
154-
"name": "VALUE",
155-
},
147+
{
148+
"type": "field_variable",
149+
"name": "VAR",
150+
"variable": "%{BKY_VARIABLES_DEFAULT_NAME}",
151+
"variableTypes": ["", IMPORTED_VARIABLE_TYPE, EXPORTED_VARIABLE_TYPE],
152+
},
153+
{
154+
"type": "input_value",
155+
"name": "VALUE",
156+
},
156157
],
157158
"previousStatement": null,
158159
"nextStatement": null,
@@ -179,7 +180,7 @@ export function initVariables() {
179180
"type": "field_variable",
180181
"name": "VAR",
181182
"variable": varname,
182-
"variableTypes": [""]
183+
"variableTypes": ["", IMPORTED_VARIABLE_TYPE, EXPORTED_VARIABLE_TYPE]
183184
},
184185
{
185186
"type": "input_value",
@@ -247,11 +248,22 @@ function flyoutCategory(workspace: Blockly.WorkspaceSvg, useXml: boolean): Eleme
247248
// This id is used to re-focus the create variable button after the dialog is closed.
248249
button.setAttribute('id', CREATE_VAR_BTN_ID);
249250

251+
const globalButton = document.createElement('button') as HTMLElement;
252+
globalButton.setAttribute('text', lf("Make a Global Variable..."));
253+
globalButton.setAttribute('callbackKey', 'CREATE_GLOBAL_VARIABLE');
254+
// This id is used to re-focus the create variable button after the dialog is closed.
255+
globalButton.setAttribute('id', CREATE_GLOBAL_VAR_BTN_ID);
256+
250257
workspace.registerButtonCallback('CREATE_VARIABLE', function (button) {
251258
Blockly.Variables.createVariableButtonHandler(button.getTargetWorkspace());
252259
});
253260

261+
workspace.registerButtonCallback('CREATE_GLOBAL_VARIABLE', function (button) {
262+
Blockly.Variables.createVariableButtonHandler(button.getTargetWorkspace(), null, EXPORTED_VARIABLE_TYPE);
263+
});
264+
254265
xmlList.push(button);
266+
xmlList.push(globalButton);
255267

256268
const blockList = Blockly.Variables.flyoutCategoryBlocks(workspace) as HTMLElement[];
257269
xmlList = xmlList.concat(blockList);

pxtblocks/external.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Blockly from "blockly";
2+
import { BlocksProgram } from "./blocksProgram";
23

34
let _promptTranslateBlock: (blockId: string, blockTranslationIds: string[]) => void;
45

@@ -129,4 +130,17 @@ export function getCopyPasteHandlers() {
129130
};
130131
}
131132
return null;
133+
}
134+
135+
let _getGlobalProgram: () => BlocksProgram;
136+
137+
export function setGetGlobalProgram(impl: () => BlocksProgram) {
138+
_getGlobalProgram = impl;
139+
}
140+
141+
export function getGlobalProgram() {
142+
if (_getGlobalProgram) {
143+
return _getGlobalProgram();
144+
}
145+
return null;
132146
}

webapp/src/blocks.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ export class Editor extends toolboxeditor.ToolboxEditor {
809809
}
810810
this.blocksProgram = new MultiWorkspaceBlocksProgram(host, this.editor);
811811
pxtblockly.contextMenu.setupWorkspaceContextMenu(this.editor);
812+
pxtblockly.external.setGetGlobalProgram(() => this.blocksProgram);
812813

813814
// set Blockly Colors
814815
(async () => {

0 commit comments

Comments
 (0)