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: http://tvtropes.org/pmwiki/pmwiki.php/Main/DiagonalBilling.

horizontalLayout

boolean

id

string

isClearing

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

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.

Parameter

func

function()

Function to call.

Value must not be null.

Returns

non-null function() Function that can be passed to removeChangeListener.

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

addTopComment(comment)

Add a comment to the list of top comments.

Parameter

comment

Blockly.WorkspaceComment

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

Value must not be null.

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.

opt_id

Optional

string

The unique ID of the variable. This will default to a UUID.

Returns

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

Return all variables of all types.

Returns

non-null Array of 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.

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

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.

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.

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.

Value must not be null.

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

Parameter

id

string

The ID to check for.

Value must not be null.

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

nullable 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

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.

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.

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

removeTopComment(comment)

Remove a comment from the list of top comments.

Parameter

comment

Blockly.WorkspaceComment

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