Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions block-lexical-variables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,25 @@ for your variables, as this would interfere with the way that variables are decl
used with this plugin. Just create an ordinary Variables category, if you want, and
place the lexical-variable-get and lexical-variable-set blocks in there.

### Exported Blocks

The plugin exports the `lexical_variable_get` and `lexical_variable_set` block definitions, which are required by the core fields. These blocks are automatically registered with Blockly when you import the plugin:

```js
// Blocks are automatically registered when importing
import {LexicalVariablesPlugin} from '@mit-app-inventor/blockly-block-lexical-variables';

// You can also access the block definitions directly if needed
import {lexical_variable_get, lexical_variable_set} from '@mit-app-inventor/blockly-block-lexical-variables';

// Or from the core module
import {LexicalVariablesPlugin} from '@mit-app-inventor/blockly-block-lexical-variables/core';
const getterBlock = LexicalVariablesPlugin.lexical_variable_get;
const setterBlock = LexicalVariablesPlugin.lexical_variable_set;
```

The blocks are registered idempotently, so importing the plugin multiple times is safe.

## BYOB (Build Your Own Blocks), aka, how to build your own blocks using the lexical variable fields
The lexical variable fields are designed to be used in blocks that contain a set of methods that the lexical variable
implementation will call. In the general case, you would need to define all of these methods (see
Expand Down
16 changes: 12 additions & 4 deletions block-lexical-variables/src/blocks/variable-get-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {NameSet} from "../nameSet.js";
/**
* Prototype bindings for a variable getter block.
*/
Blockly.Blocks['lexical_variable_get'] = {
export const lexical_variable_get = {
// Variable getter.
category: 'Variables',
helpUrl: Blockly.Msg.LANG_VARIABLES_GET_HELPURL,
Expand Down Expand Up @@ -134,7 +134,7 @@ Blockly.Blocks['lexical_variable_get'] = {
/**
* Prototype bindings for a variable setter block.
*/
Blockly.Blocks['lexical_variable_set'] = {
export const lexical_variable_set = {
// Variable setter.
category: 'Variables',
helpUrl: Blockly.Msg.LANG_VARIABLES_SET_HELPURL, // *** [lyn, 11/10/12] Fix
Expand All @@ -160,11 +160,11 @@ Blockly.Blocks['lexical_variable_set'] = {
this.workspace.getWarningHandler().checkErrors(this);
});
},
referenceResults: Blockly.Blocks.lexical_variable_get.referenceResults,
referenceResults: lexical_variable_get.referenceResults,
getDeclaredVars: function() {
return [this.getFieldValue('VAR')];
},
renameLexicalVar: Blockly.Blocks.lexical_variable_get.renameLexicalVar,
renameLexicalVar: lexical_variable_get.renameLexicalVar,
renameFree: function(freeSubstitution) {
// potentially rename the set variable
const prefixPair = Shared.unprefixName(this.getFieldValue('VAR'));
Expand Down Expand Up @@ -199,3 +199,11 @@ Blockly.Blocks['lexical_variable_set'] = {
return result;
},
};

// Register the blocks with Blockly (idempotent - safe to call multiple times)
if (!Blockly.Blocks['lexical_variable_get']) {
Blockly.Blocks['lexical_variable_get'] = lexical_variable_get;
}
if (!Blockly.Blocks['lexical_variable_set']) {
Blockly.Blocks['lexical_variable_set'] = lexical_variable_set;
}
5 changes: 4 additions & 1 deletion block-lexical-variables/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {Substitution} from './substitution.js';
import './procedure_database.js';
import * as Blockly from 'blockly/core';
import {GerasRenderer} from './renderers/geras.js';
import {lexicalVariableScopeMixin} from './mixins.js'
import {lexicalVariableScopeMixin} from './mixins.js';
import {lexical_variable_get, lexical_variable_set} from './blocks/variable-get-set.js';

export class LexicalVariablesPlugin {

Expand Down Expand Up @@ -71,6 +72,8 @@ export class LexicalVariablesPlugin {
static NameSet = NameSet;
static Shared = Shared;
static Substitution = Substitution;
static lexical_variable_get = lexical_variable_get;
static lexical_variable_set = lexical_variable_set;
}

Blockly.blockRendering.register('geras2_renderer', GerasRenderer);
Loading