Constructor

Workspace

new Workspace(opt_options)

Class for a workspace. This is a data structure that contains blocks. There is no UI, and can be created headlessly.

Parameter

opt_options

Optional

Blockly.Options

Dictionary of options.

Value must not be null.

Properties

SCAN_ANGLE

static

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

connectionDBList

Array of non-null Blockly.ConnectionDB

Set of databases for rapid lookup of connection locations.

horizontalLayout

boolean

id

string

isClearing

package

boolean

Returns true if the workspace is currently in the process of a bulk clear.

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_

protected

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

number

undoStack_

protected

non-null Array of non-null Blockly.Events.Abstract

Methods

getAll

static

getAll() returns Array of non-null Blockly.Workspace

Find all workspaces.

Returns

non-null Array of non-null Blockly.Workspace Array of workspaces.

getById

static

getById(id) returns Blockly.Workspace

Find the workspace with the specified ID.

Parameter

id

string

ID of workspace to find.

Returns

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)

Add a block to the list of top blocks.

Parameter

block

Blockly.Block

Block to add.

Value must not be null.

addTopComment

package

addTopComment(comment)

Add a comment to the list of top comments.

Parameter

comment

Blockly.WorkspaceComment

comment to add.

Value must not be null.

addTypedBlock

addTypedBlock(block)

Add a block to the list of blocks keyed by type.

Parameter

block

Blockly.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

package

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

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

Blockly.Events.Abstract

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.

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

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

package

getCommentById(id) returns Blockly.WorkspaceComment

Find the comment on this workspace with the specified ID.

Parameter

id

string

ID of comment to find.

Returns

Blockly.WorkspaceComment The sought after comment, or null if not found.

getPotentialVariableMap

package

getPotentialVariableMap() returns Blockly.VariableMap

Return the variable map that contains "potential" variables. These exist in the flyout but not in the workspace.

Returns

Blockly.VariableMap The potential variable map.

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

package

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.

getVariable

getVariable(name, opt_type) returns Blockly.VariableModel

Find the variable by the given name and return it. Return null if it is 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

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 it is not found.

Parameter

id

string

The ID to check for.

Returns

Blockly.VariableModel The variable with the given ID.

getVariableMap

getVariableMap() returns Blockly.VariableMap

Return the map of all variables on the workspace.

Returns

Blockly.VariableMap The variable map.

getVariablesOfType

getVariablesOfType(type) returns Array of 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

Array of Blockly.VariableModel The sought after variables of the passed in type. An empty array if none are found.

getVariableTypes

package

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 may 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.

removeChangeListener

removeChangeListener(func)

Stop listening for this workspace's changes.

Parameter

func

function()

Function to stop calling.

removeTopBlock

removeTopBlock(block)

Remove a block from the list of top blocks.

Parameter

block

Blockly.Block

Block to remove.

Value must not be null.

removeTopComment

package

removeTopComment(comment)

Remove a comment from the list of top comments.

Parameter

comment

Blockly.WorkspaceComment

comment to remove.

Value must not be null.

removeTypedBlock

removeTypedBlock(block)

Remove a block from the list of blocks keyed by type.

Parameter

block

Blockly.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.

undo

undo(redo)

Undo or redo the previous action.

Parameter

redo

boolean

False if undo, true if redo.

variableIndexOf

variableIndexOf(_name) returns number

Check whether a variable exists with the given name. The check is case-insensitive.

Parameter

_name

string

The name to check for.

Deprecated
April 2017
Returns

number The index of the name in the variable list, or -1 if it is not present.