Constructor
new Blocks(runtime, optNoGlow)
Parameters:
Name | Type | Description |
---|---|---|
runtime |
Runtime | The runtime this block container operates within |
optNoGlow |
boolean | Optional flag to indicate that blocks in this container should not request glows. This does not affect glows when clicking on a block to execute it. |
Members
(static) BRANCH_INPUT_PREFIX
Blockly inputs that represent statements/branch. are prefixed with this string.
_blocks :Object.<string, Object>
All blocks in the workspace. Keys are block IDs, values are metadata about the block.
Type:
- Object.<string, Object>
_scripts :Array.<String>
All top-level scripts in the workspace. A list of block IDs that represent scripts (i.e., first block in script).
Type:
- Array.<String>
forceNoGlow :boolean
Flag which indicates that blocks in this container should not glow. Blocks will still glow when clicked on, but this flag is used to control whether the blocks in this container can request a glow as part of a running stack. E.g. the flyout block container and the monitor block container should not be able to request a glow, but blocks containers belonging to sprites should.
Type:
- boolean
Methods
_addScript(topBlockIdnullable)
Helper to add a stack to this._scripts
.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
topBlockId |
string |
<nullable> |
ID of block that starts the script. |
_deleteScript(topBlockIdnullable)
Helper to remove a script from this._scripts
.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
topBlockId |
string |
<nullable> |
ID of block that starts the script. |
_getBackdropField(blockId) → (nullable) {object}
Helper function to retrieve a backdrop menu field from a block given its id.
Parameters:
Name | Type | Description |
---|---|---|
blockId |
string | A unique identifier for a block |
Returns:
The backdrop menu field of the block with the given block id. Null, if either a block with the given id doesn't exist or if a backdrop menu field does not exist on the block with the given id.
- Type
- object
_getBlockParams(blocknon-null) → (non-null) {object}
Helper to serialize block fields and input fields for reporting new monitors
Parameters:
Name | Type | Description |
---|---|---|
block |
object | Block to be paramified. |
Returns:
object of param key/values.
- Type
- object
_getCostumeField(blockId) → (nullable) {object}
Helper function to retrieve a costume menu field from a block given its id.
Parameters:
Name | Type | Description |
---|---|---|
blockId |
string | A unique identifier for a block |
Returns:
The costume menu field of the block with the given block id. Null if either a block with the given id doesn't exist or if a costume menu field does not exist on the block with the given id.
- Type
- object
_getCustomBlockInternal(defineBlocknon-null) → (non-null) {object}
Helper to get the corresponding internal procedure definition block
Parameters:
Name | Type | Description |
---|---|---|
defineBlock |
object | Outer define block. |
Returns:
internal definition block which has the mutation.
- Type
- object
_getSoundField(blockId) → (nullable) {object}
Helper function to retrieve a sound menu field from a block given its id.
Parameters:
Name | Type | Description |
---|---|---|
blockId |
string | A unique identifier for a block |
Returns:
The sound menu field of the block with the given block id. Null, if either a block with the given id doesn't exist or if a sound menu field does not exist on the block with the given id.
- Type
- object
_getSpriteField(blockId) → (nullable) {object}
Helper function to retrieve a sprite menu field from a block given its id.
Parameters:
Name | Type | Description |
---|---|---|
blockId |
string | A unique identifier for a block |
Returns:
The sprite menu field of the block with the given block id. Null, if either a block with the given id doesn't exist or if a sprite menu field does not exist on the block with the given id.
- Type
- object
blocklyListen(e)
Create event listener for blocks, variables, and comments. Handles validation and serves as a generic adapter between the blocks, variables, and the runtime interface.
Parameters:
Name | Type | Description |
---|---|---|
e |
object | Blockly "block" or "variable" event |
blockToXML(blockIdnon-null, comments) → {string}
Recursively encode an individual block and its children into a Blockly/scratch-blocks XML string.
Parameters:
Name | Type | Description |
---|---|---|
blockId |
string | ID of block to encode. |
comments |
object.<string, Comment> | Map of comments referenced by id |
Returns:
String of XML representing this block and any children.
- Type
- string
changeBlock(argsnon-null)
Block management: change block field values
Parameters:
Name | Type | Description |
---|---|---|
args |
object | Blockly change event to be processed |
createBlock(blocknon-null)
Block management: create blocks and scripts from a create
event
Parameters:
Name | Type | Description |
---|---|---|
block |
object | Blockly create event to be processed |
deleteBlock(blockIdnon-null)
Block management: delete blocks and their associated scripts. Does nothing if a block with the given ID does not exist.
Parameters:
Name | Type | Description |
---|---|---|
blockId |
string | Id of block to delete |
emitProjectChanged()
Emit a project changed event if this is a block container that can affect the project state.
getAllVariableAndListReferences(optBlocks, optIncludeBroadcastnullable) → {object}
Returns a map of all references to variables or lists from blocks in this block container.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
optBlocks |
Array.<object> | Optional list of blocks to constrain the search to. This is useful for getting variable/list references for a stack of blocks instead of all blocks on the workspace |
|
optIncludeBroadcast |
boolean |
<nullable> |
Optional whether to include broadcast fields. |
Returns:
A map of variable ID to a list of all variable references for that ID. A variable reference contains the field referencing that variable and also the type of the variable being referenced.
- Type
- object
getBlock(blockIdnon-null) → (nullable) {object}
Provide an object with metadata for the requested block ID.
Parameters:
Name | Type | Description |
---|---|---|
blockId |
string | ID of block we have stored. |
Returns:
Metadata about the block, if it exists.
- Type
- object
getBranch(idnullable, branchNumnullable) → (nullable) {string}
Get the branch for a particular C-shaped block.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
id |
string |
<nullable> |
ID for block to get the branch for. |
branchNum |
number |
<nullable> |
Which branch to select (e.g. for if-else). |
Returns:
ID of block in the branch.
- Type
- string
getFields(blocknullable) → (nullable) {object}
Get all fields and their values for a block.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
block |
object |
<nullable> |
The block to query. |
Returns:
All fields and their values.
- Type
- object
getInputs(blocknullable) → (nullable) {Array.<object>}
Get all non-branch inputs for a block.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
block |
object |
<nullable> |
the block to query. |
Returns:
All non-branch inputs and their associated blocks.
- Type
- Array.<object>
getMutation(blocknullable) → (nullable) {object}
Get mutation data for a block.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
block |
object |
<nullable> |
The block to query. |
Returns:
Mutation for the block.
- Type
- object
getNextBlock(idnullable) → (nullable) {string}
Get the next block for a particular block
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
id |
string |
<nullable> |
ID of block to get the next block for |
Returns:
ID of next block in the sequence
- Type
- string
getOpcode(blocknullable) → (nullable) {string}
Get the opcode for a particular block
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
block |
object |
<nullable> |
The block to query |
Returns:
the opcode corresponding to that block
- Type
- string
getProcedureDefinition(namenullable) → (nullable) {string}
Get the procedure definition for a given name.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string |
<nullable> |
Name of procedure to query. |
Returns:
ID of procedure definition.
- Type
- string
getProcedureParamNamesAndIds(namenullable) → (nullable) {Array.<string>}
Get names and ids of parameters for the given procedure.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string |
<nullable> |
Name of procedure to query. |
Returns:
List of param names for a procedure.
- Type
- Array.<string>
getProcedureParamNamesIdsAndDefaults(namenullable) → (nullable) {Array.<string>}
Get names, ids, and defaults of parameters for the given procedure.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string |
<nullable> |
Name of procedure to query. |
Returns:
List of param names for a procedure.
- Type
- Array.<string>
getScripts() → {Array.<string>}
Get all known top-level blocks that start scripts.
Returns:
List of block IDs.
- Type
- Array.<string>
getTopLevelScript(idnullable) → (nullable) {string}
Get the top-level script for a given block.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
id |
string |
<nullable> |
ID of block to query. |
Returns:
ID of top-level script block.
- Type
- string
moveBlock(enon-null)
Block management: move blocks from parent to parent
Parameters:
Name | Type | Description |
---|---|---|
e |
object | Blockly move event to be processed |
mutationToXML(mutationnon-null) → {string}
Recursively encode a mutation object to XML.
Parameters:
Name | Type | Description |
---|---|---|
mutation |
object | Object representing a mutation. |
Returns:
XML string representing a mutation.
- Type
- string
resetCache()
Reset all runtime caches.
runAllMonitored(runtimenon-null)
Block management: run all blocks.
Parameters:
Name | Type | Description |
---|---|---|
runtime |
object | Runtime to run all blocks in. |
toXML(comments) → {string}
Encode all of this._blocks
as an XML string usable
by a Blockly/scratch-blocks workspace.
Parameters:
Name | Type | Description |
---|---|---|
comments |
object.<string, Comment> | Map of comments referenced by id |
Returns:
String of XML representing this object's blocks.
- Type
- string
updateAssetName(oldName, newName, assetType)
Update blocks after a sound, costume, or backdrop gets renamed. Any block referring to the old name of the asset should get updated to refer to the new name.
Parameters:
Name | Type | Description |
---|---|---|
oldName |
string | The old name of the asset that was renamed. |
newName |
string | The new name of the asset that was renamed. |
assetType |
string | String representation of the kind of asset that was renamed. This can be one of 'sprite','costume', 'sound', or 'backdrop'. |
updateBlocksAfterVarRename(varId, newName)
Keep blocks up to date after a variable gets renamed.
Parameters:
Name | Type | Description |
---|---|---|
varId |
string | The id of the variable that was renamed |
newName |
string | The new name of the variable that was renamed |
updateTargetSpecificBlocks(isStage)
Keep blocks up to date after they are shared between targets.
Parameters:
Name | Type | Description |
---|---|---|
isStage |
boolean | If the new target is a stage. |