Blockly. Workspace
Class for a workspace. This is a data structure that contains blocks. There is no UI, and can be created headlessly.
Constructor
Workspace
new Workspace(opt_options)
Parameter |
|
---|---|
opt_options |
Optional Dictionary of options. Value must not be null. |
- Implements
- Blockly.IASTNodeLocation
Properties
SCAN_ANGLE
Angle away from the horizontal to sweep for blocks. Order of execution is generally top to bottom, but a small angle changes the scan to give a bit of a left to right bias (reversed in RTL). Units are in degrees. See: https://tvtropes.org/pmwiki/pmwiki.php/Main/DiagonalBilling
connectionChecker
non-null Blockly.IConnectionChecker
An object that encapsulates logic for safety, type, and dragging checks.
connectionDBList
Array of non-null Blockly.ConnectionDB
Set of databases for rapid lookup of connection locations.
horizontalLayout
boolean
id
string
isClearing
boolean
Returns true
if the workspace is currently in the process of a bulk
clear.
isFlyout
boolean
Is this workspace the surface for a flyout?
isMutator
boolean
Is this workspace the surface for a mutator?
MAX_UNDO
number
Maximum number of undo events in stack. 0
turns off undo, Infinity
sets it to unlimited.
options
non-null Blockly.Options
redoStack_
non-null Array of non-null Blockly.Events.Abstract
rendered
boolean
Returns true
if the workspace is visible and false
if it's headless.
RTL
boolean
toolboxPosition
Blockly.utils.toolbox.Position
undoStack_
non-null Array of non-null Blockly.Events.Abstract
Methods
getAll
getAll() returns Array of non-null Blockly.Workspace
Find all workspaces.
- Returns
-
non-null Array of non-null Blockly.Workspace
Array of workspaces.
getById
getById(id) returns Blockly.Workspace
Find the workspace with the specified ID.
Parameter |
|
---|---|
id |
string ID of workspace to find. |
- Returns
-
nullable Blockly.Workspace
The sought after workspace or null if not found.
addChangeListener
addChangeListener(func) returns function()
When something in this workspace changes, call a function. Note that there may be a few recent events already on the stack. Thus the new change listener might be called with events that occurred a few milliseconds before the change listener was added.
Parameter |
|
---|---|
func |
function() Function to call. Value must not be null. |
- Returns
-
non-null function()
Obsolete return value, ignore.
addTopBlock
addTopBlock(block)
Adds a block to the list of top blocks.
Parameter |
|
---|---|
block |
Block to add. Value must not be null. |
addTopComment
addTopComment(comment)
Adds a comment to the list of top comments.
Parameter |
|
---|---|
comment |
comment to add. Value must not be null. |
addTypedBlock
addTypedBlock(block)
Add a block to the list of blocks keyed by type.
Parameter |
|
---|---|
block |
Block to add. Value must not be null. |
allInputsFilled
allInputsFilled(opt_shadowBlocksAreFilled) returns boolean
Checks whether all value and statement inputs in the workspace are filled with blocks.
Parameter |
|
---|---|
opt_shadowBlocksAreFilled |
Optional boolean An optional argument controlling whether shadow blocks are counted as filled. Defaults to true. |
- Returns
-
boolean
True if all inputs are filled, false otherwise.
clear
clear()
Dispose of all blocks and comments in workspace.
clearUndo
clearUndo()
Clear the undo/redo stacks.
createPotentialVariableMap
createPotentialVariableMap()
Create and store the potential variable map for this workspace.
createVariable
createVariable(name, opt_type, opt_id) returns Blockly.VariableModel
Create a variable with a given name, optional type, and optional ID.
Parameter |
|
---|---|
name |
string The name of the variable. This must be unique across variables and procedures. |
opt_type |
Optional string The type of the variable like 'int' or 'string'. Does not need to be unique. Field_variable can filter variables based on their type. This will default to '' which is a specific type. Value may be null. |
opt_id |
Optional string The unique ID of the variable. This will default to a UUID. Value may be null. |
- Returns
-
non-null Blockly.VariableModel
The newly created variable.
deleteVariableById
deleteVariableById(id)
Delete a variables by the passed in ID and all of its uses from this workspace. May prompt the user for confirmation.
Parameter |
|
---|---|
id |
string ID of variable to delete. |
dispose
dispose()
Dispose of this workspace. Unlink from all DOM elements to prevent memory leaks.
fireChangeListener
fireChangeListener(event)
Fire a change event.
Parameter |
|
---|---|
event |
Event to fire. Value must not be null. |
getAllBlocks
getAllBlocks(ordered) returns Array of non-null Blockly.Block
Find all blocks in workspace. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
Parameter |
|
---|---|
ordered |
boolean Sort the list if true. |
- Returns
-
non-null Array of non-null Blockly.Block
Array of blocks.
getAllVariableNames
getAllVariableNames() returns Array of string
Returns all variable names of all types.
- Returns
-
non-null Array of string
List of all variable names of all types.
getAllVariables
getAllVariables() returns Array of non-null Blockly.VariableModel
Return all variables of all types.
- Returns
-
non-null Array of non-null Blockly.VariableModel
List of variable models.
getBlockById
getBlockById(id) returns Blockly.Block
Find the block on this workspace with the specified ID.
Parameter |
|
---|---|
id |
string ID of block to find. |
- Returns
-
nullable Blockly.Block
The sought after block, or null if not found.
getBlocksByType
getBlocksByType(type, ordered) returns Array of non-null Blockly.Block
Finds the blocks with the associated type and returns them. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
Parameter |
|
---|---|
type |
string The type of block to search for. |
ordered |
boolean Sort the list if true. |
- Returns
-
non-null Array of non-null Blockly.Block
The blocks of the given type.
getCommentById
getCommentById(id) returns Blockly.WorkspaceComment
Find the comment on this workspace with the specified ID.
Parameter |
|
---|---|
id |
string ID of comment to find. |
- Returns
-
nullable Blockly.WorkspaceComment
The sought after comment, or null if not found.
getPotentialVariableMap
getPotentialVariableMap() returns Blockly.VariableMap
Return the variable map that contains "potential" variables. These exist in the flyout but not in the workspace.
- Returns
-
nullable Blockly.VariableMap
The potential variable map.
getRedoStack
getRedoStack() returns Array of non-null Blockly.Events.Abstract
Gets the redo stack for workplace.
- Returns
-
non-null Array of non-null Blockly.Events.Abstract
redo stack
getTopBlocks
getTopBlocks(ordered) returns Array of non-null Blockly.Block
Finds the top-level blocks and returns them. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
Parameter |
|
---|---|
ordered |
boolean Sort the list if true. |
- Returns
-
non-null Array of non-null Blockly.Block
The top-level block objects.
getTopComments
getTopComments(ordered) returns Array of non-null Blockly.WorkspaceComment
Finds the top-level comments and returns them. Comments are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
Parameter |
|
---|---|
ordered |
boolean Sort the list if true. |
- Returns
-
non-null Array of non-null Blockly.WorkspaceComment
The top-level comment objects.
getUndoStack
getUndoStack() returns Array of non-null Blockly.Events.Abstract
Gets the undo stack for workplace.
- Returns
-
non-null Array of non-null Blockly.Events.Abstract
undo stack
getVariable
getVariable(name, opt_type) returns Blockly.VariableModel
Find the variable by the given name and return it. Return null if not found.
Parameter |
|
---|---|
name |
string The name to check for. |
opt_type |
Optional string The type of the variable. If not provided it defaults to the empty string, which is a specific type. |
- Returns
-
nullable Blockly.VariableModel
The variable with the given name.
getVariableById
getVariableById(id) returns Blockly.VariableModel
Find the variable by the given ID and return it. Return null if not found.
Parameter |
|
---|---|
id |
string The ID to check for. |
- Returns
-
nullable Blockly.VariableModel
The variable with the given ID.
getVariableMap
getVariableMap() returns Blockly.VariableMap
Return the map of all variables on the workspace.
- Returns
-
non-null Blockly.VariableMap
The variable map.
getVariablesOfType
getVariablesOfType(type) returns Array of non-null Blockly.VariableModel
Find the variable with the specified type. If type is null, return list of variables with empty string type.
Parameter |
|
---|---|
type |
string Type of the variables to find. Value may be null. |
- Returns
-
non-null Array of non-null Blockly.VariableModel
The sought after variables of the passed in type. An empty array if none are found.
getVariableTypes
getVariableTypes() returns Array of string
Return all variable types.
- Returns
-
non-null Array of string
List of variable types.
getVariableUsesById
getVariableUsesById(id) returns Array of non-null Blockly.Block
Find all the uses of the given variable, which is identified by ID.
Parameter |
|
---|---|
id |
string ID of the variable to find. |
- Returns
-
non-null Array of non-null Blockly.Block
Array of block usages.
getWidth
getWidth() returns number
Returns the horizontal offset of the workspace. Intended for LTR/RTL compatibility in XML. Not relevant for a headless workspace.
- Returns
-
number
Width.
hasBlockLimits
hasBlockLimits() returns boolean
Checks if the workspace has any limits on the maximum number of blocks, or the maximum number of blocks of specific types.
- Returns
-
boolean
True if it has block limits, false otherwise.
isCapacityAvailable
isCapacityAvailable(typeCountsMap) returns boolean
Check if there is remaining capacity for blocks of the given counts to be created. If the total number of blocks represented by the map is more than the total remaining capacity, it returns false. If a type count is more than the remaining capacity for that type, it returns false.
Parameter |
|
---|---|
typeCountsMap |
Object A map of types to counts (usually representing blocks to be created). Value must not be null. |
- Returns
-
boolean
True if there is capacity for the given map, false otherwise.
newBlock
newBlock(prototypeName, opt_id) returns Blockly.Block
Obtain a newly created block.
Parameter |
|
---|---|
prototypeName |
string Name of the language object containing type-specific functions for this block. Value must not be null. |
opt_id |
Optional string Optional ID. Use this ID if provided, otherwise create a new ID. |
- Returns
-
non-null Blockly.Block
The created block.
remainingCapacity
remainingCapacity() returns number
The number of blocks that may be added to the workspace before reaching the maxBlocks.
- Returns
-
number
Number of blocks left.
remainingCapacityOfType
remainingCapacityOfType(type) returns number
The number of blocks of the given type that may be added to the workspace before reaching the maxInstances allowed for that type.
Parameter |
|
---|---|
type |
string Type of block to return capacity for. |
- Returns
-
number
Number of blocks of type left.
removeBlockById
removeBlockById(id)
Delete a block off this workspace with the specified ID.
Parameter |
|
---|---|
id |
string ID of block to delete. |
removeChangeListener
removeChangeListener(func)
Stop listening for this workspace's changes.
Parameter |
|
---|---|
func |
function() Function to stop calling. Value must not be null. |
removeTopBlock
removeTopBlock(block)
Removes a block from the list of top blocks.
Parameter |
|
---|---|
block |
Block to remove. Value must not be null. |
removeTopComment
removeTopComment(comment)
Removes a comment from the list of top comments.
Parameter |
|
---|---|
comment |
comment to remove. Value must not be null. |
removeTypedBlock
removeTypedBlock(block)
Remove a block from the list of blocks keyed by type.
Parameter |
|
---|---|
block |
Block to remove. Value must not be null. |
renameVariableById
renameVariableById(id, newName)
Rename a variable by updating its name in the variable map. Identify the variable to rename with the given ID.
Parameter |
|
---|---|
id |
string ID of the variable to rename. |
newName |
string New variable name. |
setBlockById
setBlockById(id, block)
Set a block on this workspace with the specified ID.
Parameter |
|
---|---|
id |
string ID of block to set. |
block |
The block to set. |
setVariableMap
setVariableMap(variableMap)
Set the map of all variables on the workspace.
Parameter |
|
---|---|
variableMap |
The variable map. Value must not be null. |
undo
undo(redo)
Undo or redo the previous action.
Parameter |
|
---|---|
redo |
boolean False if undo, true if redo. |